refactor(api): Refactor updateOne logic

This commit is contained in:
yassinedorbozgithub
2025-01-16 18:47:25 +01:00
parent 47e8056a15
commit 6c75e6df4a
35 changed files with 92 additions and 164 deletions

View File

@@ -17,6 +17,7 @@ import { AttachmentService } from '@/attachment/services/attachment.service';
import { BlockService } from '@/chat/services/block.service';
import { LoggerService } from '@/logger/logger.service';
import { NOT_FOUND_ID } from '@/utils/constants/mock';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import { installContentFixtures } from '@/utils/test/fixtures/content';
import { contentTypeFixtures } from '@/utils/test/fixtures/contenttype';
import { getPageQuery } from '@/utils/test/pagination';
@@ -174,7 +175,7 @@ describe('ContentTypeController', () => {
jest.spyOn(contentTypeService, 'updateOne');
await expect(
contentTypeController.updateOne(updatedContent, NOT_FOUND_ID),
).rejects.toThrow(NotFoundException);
).rejects.toThrow(getUpdateOneError(ContentType.name, NOT_FOUND_ID));
expect(contentTypeService.updateOne).toHaveBeenCalledWith(
NOT_FOUND_ID,
updatedContent,

View File

@@ -148,17 +148,6 @@ export class ContentTypeController extends BaseController<ContentType> {
@Body() contentTypeDto: ContentTypeUpdateDto,
@Param('id') id: string,
) {
const updatedContentType = await this.contentTypeService.updateOne(
id,
contentTypeDto,
);
if (!updatedContentType) {
this.logger.warn(
`Failed to update content type with id ${id}. Content type not found.`,
);
throw new NotFoundException(`Content type with id ${id} not found`);
}
return updatedContentType;
return await this.contentTypeService.updateOne(id, contentTypeDto);
}
}

View File

@@ -23,6 +23,7 @@ import { LoggerService } from '@/logger/logger.service';
import { NOT_FOUND_ID } from '@/utils/constants/mock';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { IGNORED_TEST_FIELDS } from '@/utils/test/constants';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import {
contentFixtures,
installContentFixtures,
@@ -194,7 +195,7 @@ describe('ContentController', () => {
it('should throw NotFoundException if the content is not found', async () => {
await expect(
contentController.updateOne(updatedContent, NOT_FOUND_ID),
).rejects.toThrow(NotFoundException);
).rejects.toThrow(getUpdateOneError(Content.name, NOT_FOUND_ID));
});
});

View File

@@ -300,13 +300,6 @@ export class ContentController extends BaseController<
@Body() contentDto: ContentUpdateDto,
@Param('id') id: string,
): Promise<Content> {
const updatedContent = await this.contentService.updateOne(id, contentDto);
if (!updatedContent) {
this.logger.warn(
`Failed to update content with id ${id}. Content not found.`,
);
throw new NotFoundException(`Content of id ${id} not found`);
}
return updatedContent;
return await this.contentService.updateOne(id, contentDto);
}
}

View File

@@ -165,7 +165,10 @@ export class MenuController extends BaseController<
*/
@CsrfCheck(true)
@Patch(':id')
async updateOne(@Body() body: MenuCreateDto, @Param('id') id: string) {
async updateOne(
@Body() body: MenuCreateDto,
@Param('id') id: string,
): Promise<Menu> {
if (!id) return await this.create(body);
return await this.menuService.updateOne(id, body);
}