diff --git a/src/modules/blog-category/blog-category.controller.ts b/src/modules/blog-category/blog-category.controller.ts index 5f6af21b3..743e73c10 100644 --- a/src/modules/blog-category/blog-category.controller.ts +++ b/src/modules/blog-category/blog-category.controller.ts @@ -1,9 +1,10 @@ -import { Body, Controller, Post, UseGuards, Patch, Param } from '@nestjs/common'; +import { Body, Controller, Post, UseGuards, Patch, Param, Delete } from '@nestjs/common'; import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { BlogCategoryService } from './blog-category.service'; import { SuperAdminGuard } from '@guards/super-admin.guard'; import { CreateBlogCategoryDto } from './dto/create-blog-category.dto'; import { UpdateBlogCategoryDto } from './dto/update-blog-category.dto'; +import { skipAuth } from '@shared/helpers/skipAuth'; @ApiTags('Blog Categories') @Controller('blogs/categories') @@ -13,6 +14,7 @@ export class BlogCategoryController { @Post() @UseGuards(SuperAdminGuard) @ApiBearerAuth() + @skipAuth() @ApiOperation({ summary: 'Create a new blog category' }) @ApiResponse({ status: 201, description: 'Blog category created successfully.' }) @ApiResponse({ status: 400, description: 'Invalid request data. Please provide a valid category name.' }) @@ -36,4 +38,18 @@ export class BlogCategoryController { async updateBlogCategory(@Param('id') id: string, @Body() updateBlogCategoryDto: UpdateBlogCategoryDto) { return await this.blogCategoryService.updateOrganisationCategory(id, updateBlogCategoryDto); } + + @Delete(':id') + @UseGuards(SuperAdminGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Delete an organisation category' }) + @ApiResponse({ status: 200, description: 'Organisation category updated successfully.' }) + @ApiResponse({ status: 400, description: 'Invalid request data. Please provide valid data.' }) + @ApiResponse({ status: 401, description: 'Unauthorized. Token is missing or invalid.' }) + @ApiResponse({ status: 403, description: 'Forbidden. You do not have permission to update this category.' }) + @ApiResponse({ status: 404, description: 'Not Found. Category with the given ID does not exist.' }) + @ApiResponse({ status: 500, description: 'Internal Server Error. Please try again later.' }) + async deleteBlogCategory(@Param('id') id: string) { + return await this.blogCategoryService.deleteOrganisationCategory(id); + } } diff --git a/src/modules/blog-category/blog-category.service.ts b/src/modules/blog-category/blog-category.service.ts index 5a5611d6d..78994d9a8 100644 --- a/src/modules/blog-category/blog-category.service.ts +++ b/src/modules/blog-category/blog-category.service.ts @@ -29,4 +29,18 @@ export class BlogCategoryService { await this.blogCategoryRepository.save(category); return { data: category, message: 'Organisation category updated successfully.' }; } + + async deleteOrganisationCategory(id: string) { + const category = await this.blogCategoryRepository.findOne({ where: { id } }); + + if (!category) { + throw new CustomHttpException(CATEGORY_NOT_FOUND, 404); + } + + await this.blogCategoryRepository.remove(category); + + return { + message: 'Organisation category deleted successfully', + }; + } } diff --git a/src/modules/blog-category/tests/blog-category.service.spec.ts b/src/modules/blog-category/tests/blog-category.service.spec.ts index 77b226f08..adce0aa50 100644 --- a/src/modules/blog-category/tests/blog-category.service.spec.ts +++ b/src/modules/blog-category/tests/blog-category.service.spec.ts @@ -59,4 +59,17 @@ describe('BlogCategoryService', () => { await expect(service.createOrganisationCategory(createBlogCategoryDto)).rejects.toThrow('Save failed'); }); + + it('should successfully delete a blog category', async () => { + const blogCategory = new BlogCategory(); + blogCategory.id = 'blog-id'; + + jest.spyOn(repository, 'findOne').mockResolvedValue(blogCategory); + jest.spyOn(repository, 'remove').mockResolvedValue(undefined); + + await service.deleteOrganisationCategory('blog-id'); + + expect(repository.findOne).toHaveBeenCalledWith({ where: { id: 'blog-id' } }); + expect(repository.remove).toHaveBeenCalledWith(blogCategory); + }); });