diff --git a/api/src/nlp/repositories/nlp-value.repository.ts b/api/src/nlp/repositories/nlp-value.repository.ts index 4ac2efe1..ab276904 100644 --- a/api/src/nlp/repositories/nlp-value.repository.ts +++ b/api/src/nlp/repositories/nlp-value.repository.ts @@ -8,7 +8,7 @@ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; -import { ClassTransformOptions, plainToClass } from 'class-transformer'; +import { plainToInstance } from 'class-transformer'; import { Document, Model, @@ -24,7 +24,6 @@ import { TFilterQuery } from '@/utils/types/filter.types'; import { Format } from '@/utils/types/format.types'; import { NlpValueDto } from '../dto/nlp-value.dto'; -import { NlpEntity } from '../schemas/nlp-entity.schema'; import { NLP_VALUE_POPULATE, NlpValue, @@ -121,6 +120,14 @@ export class NlpValueRepository extends BaseRepository< } } + private getSortDirection(sortOrder: SortOrder) { + return typeof sortOrder === 'number' + ? sortOrder + : sortOrder.toString().toLowerCase() === 'desc' + ? -1 + : 1; + } + /** * Performs an aggregation to retrieve NLP values with their sample counts. * @@ -231,33 +238,6 @@ export class NlpValueRepository extends BaseRepository< return await this.model.aggregate>(pipeline).exec(); } - private async plainToClass( - format: F, - aggregatedResults: TNlpValueCount[], - options: ClassTransformOptions = { excludePrefixes: ['_'] }, - ): Promise[]> { - return aggregatedResults.map(({ entity, ...rest }) => { - if (format === Format.FULL) { - return plainToClass( - NlpValueFullWithCount, - { - ...rest, - entity: plainToClass(NlpEntity, entity, options), - }, - { - excludePrefixes: ['_'], - }, - ) as TNlpValueCount; - } else { - return plainToClass( - NlpValueWithCount, - { entity, ...rest }, - options, - ) as TNlpValueCount; - } - }); - } - async findWithCount( format: F, pageQuery: PageQueryDto, @@ -270,18 +250,18 @@ export class NlpValueRepository extends BaseRepository< filterQuery, ); - return await this.plainToClass(format, aggregatedResults); + if (format === Format.FULL) { + return plainToInstance(NlpValueFullWithCount, aggregatedResults, { + excludePrefixes: ['_'], + }) as TNlpValueCount[]; + } + + return plainToInstance(NlpValueWithCount, aggregatedResults, { + excludePrefixes: ['_'], + }) as TNlpValueCount[]; } catch (error) { this.logger.error(`Error in findWithCount: ${error.message}`, error); throw error; } } - - private getSortDirection(sortOrder: SortOrder) { - return typeof sortOrder === 'number' - ? sortOrder - : sortOrder.toString().toLowerCase() === 'desc' - ? -1 - : 1; - } }