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

@@ -167,17 +167,7 @@ export class NlpEntityController extends BaseController<
);
}
const result = await this.nlpEntityService.updateOne(
id,
updateNlpEntityDto,
);
if (!result) {
this.logger.warn(`Failed to update NLP Entity by id ${id}`);
throw new InternalServerErrorException(
`Failed to update NLP Entity with ID ${id}`,
);
}
return result;
return await this.nlpEntityService.updateOne(id, updateNlpEntityDto);
}
/**

View File

@@ -22,6 +22,7 @@ import { SettingRepository } from '@/setting/repositories/setting.repository';
import { SettingModel } from '@/setting/schemas/setting.schema';
import { SettingSeeder } from '@/setting/seeds/setting.seed';
import { SettingService } from '@/setting/services/setting.service';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import { installAttachmentFixtures } from '@/utils/test/fixtures/attachment';
import { nlpSampleFixtures } from '@/utils/test/fixtures/nlpsample';
import { installNlpSampleEntityFixtures } from '@/utils/test/fixtures/nlpsampleentity';
@@ -310,7 +311,7 @@ describe('NlpSampleController', () => {
type: NlpSampleState.test,
language: 'fr',
}),
).rejects.toThrow(NotFoundException);
).rejects.toThrow(getUpdateOneError(NlpSample.name, byeJhonSampleId!));
});
});

View File

@@ -305,11 +305,6 @@ export class NlpSampleController extends BaseController<
trained: false,
});
if (!sample) {
this.logger.warn(`Unable to update NLP Sample by id ${id}`);
throw new NotFoundException(`NLP Sample with ID ${id} not found`);
}
await this.nlpSampleEntityService.deleteMany({ sample: id });
const updatedSampleEntities =

View File

@@ -12,6 +12,7 @@ import { MongooseModule } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { LoggerService } from '@/logger/logger.service';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity';
import {
installNlpValueFixtures,
@@ -241,7 +242,7 @@ describe('NlpValueController', () => {
expressions: [],
builtin: true,
}),
).rejects.toThrow(NotFoundException);
).rejects.toThrow(getUpdateOneError(NlpValue.name, jhonNlpValue!.id));
});
});
describe('deleteMany', () => {

View File

@@ -168,12 +168,7 @@ export class NlpValueController extends BaseController<
@Param('id') id: string,
@Body() updateNlpValueDto: NlpValueUpdateDto,
): Promise<NlpValue> {
const result = await this.nlpValueService.updateOne(id, updateNlpValueDto);
if (!result) {
this.logger.warn(`Unable to update NLP Value by id ${id}`);
throw new NotFoundException(`NLP Value with ID ${id} not found`);
}
return result;
return await this.nlpValueService.updateOne(id, updateNlpValueDto);
}
/**