From 008e6e43db0a1b7b0d8b193b0f6705fa5df531d4 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Fri, 10 Jan 2025 15:16:27 +0100 Subject: [PATCH] fix: apply feedbacks --- api/src/helper/lib/base-nlp-helper.ts | 2 +- api/src/i18n/services/language.service.ts | 14 +++++++++++++- api/src/nlp/controllers/nlp-sample.controller.ts | 14 +------------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/api/src/helper/lib/base-nlp-helper.ts b/api/src/helper/lib/base-nlp-helper.ts index 7c52e11b..02c30863 100644 --- a/api/src/helper/lib/base-nlp-helper.ts +++ b/api/src/helper/lib/base-nlp-helper.ts @@ -151,7 +151,7 @@ export default abstract class BaseNlpHelper< }) .concat({ entity: 'language', - value: s.language.code, + value: s.language!.code, }); return { diff --git a/api/src/i18n/services/language.service.ts b/api/src/i18n/services/language.service.ts index 7fbeeb70..774dbeee 100644 --- a/api/src/i18n/services/language.service.ts +++ b/api/src/i18n/services/language.service.ts @@ -11,9 +11,11 @@ import { Inject, Injectable, InternalServerErrorException, + NotFoundException, } from '@nestjs/common'; import { Cache } from 'cache-manager'; +import { LoggerService } from '@/logger/logger.service'; import { DEFAULT_LANGUAGE_CACHE_KEY, LANGUAGES_CACHE_KEY, @@ -29,6 +31,7 @@ export class LanguageService extends BaseService { constructor( readonly repository: LanguageRepository, @Inject(CACHE_MANAGER) private readonly cacheManager: Cache, + private readonly logger: LoggerService, ) { super(repository); } @@ -72,6 +75,15 @@ export class LanguageService extends BaseService { * @returns A promise that resolves to the `Language` object. */ async getLanguageByCode(code: string) { - return await this.findOne({ code }); + const language = await this.findOne({ code }); + + if (!language) { + this.logger.warn(`Unable to Language by languageCode ${code}`); + throw new NotFoundException( + `Language with languageCode ${code} not found`, + ); + } + + return language; } } diff --git a/api/src/nlp/controllers/nlp-sample.controller.ts b/api/src/nlp/controllers/nlp-sample.controller.ts index cecc7082..27ed1f60 100644 --- a/api/src/nlp/controllers/nlp-sample.controller.ts +++ b/api/src/nlp/controllers/nlp-sample.controller.ts @@ -91,7 +91,7 @@ export class NlpSampleController extends BaseController< ); const entities = await this.nlpEntityService.findAllAndPopulate(); const helper = await this.helperService.getDefaultNluHelper(); - const result = await helper.format?.(samples, entities); + const result = await helper.format(samples, entities); // Sending the JSON data as a file const buffer = Buffer.from(JSON.stringify(result)); @@ -129,11 +129,6 @@ export class NlpSampleController extends BaseController< ): Promise { const language = await this.languageService.getLanguageByCode(languageCode); - if (!language) - throw new NotFoundException( - `Language with code ${languageCode} not found`, - ); - const nlpSample = await this.nlpSampleService.create({ ...createNlpSampleDto, language: language.id, @@ -303,13 +298,6 @@ export class NlpSampleController extends BaseController< ): Promise { const language = await this.languageService.getLanguageByCode(languageCode); - if (!language) { - this.logger.warn(`Unable to Language by languageCode ${languageCode}`); - throw new NotFoundException( - `Language with languageCode ${languageCode} not found`, - ); - } - const sample = await this.nlpSampleService.updateOne(id, { ...sampleAttrs, language: language.id,