diff --git a/api/src/nlp/controllers/nlp-value.controller.ts b/api/src/nlp/controllers/nlp-value.controller.ts index 42cb334f..f1e09dad 100644 --- a/api/src/nlp/controllers/nlp-value.controller.ts +++ b/api/src/nlp/controllers/nlp-value.controller.ts @@ -132,12 +132,12 @@ export class NlpValueController extends BaseController< @Query( new SearchFilterPipe({ allowedFields: ['entity', 'value'] }), ) - filters?: TFilterQuery, + filters: TFilterQuery, ) { return await this.nlpValueService.findAndPopulateNlpValuesWithCount( + pageQuery, populate, filters, - pageQuery, ); } diff --git a/api/src/nlp/repositories/nlp-value.repository.ts b/api/src/nlp/repositories/nlp-value.repository.ts index 99cfdd11..5c5af5bd 100644 --- a/api/src/nlp/repositories/nlp-value.repository.ts +++ b/api/src/nlp/repositories/nlp-value.repository.ts @@ -109,44 +109,39 @@ export class NlpValueRepository extends BaseRepository< } async findAndPopulateNlpValuesWithCount( + { limit = 10, skip = 0, sort = ['createdAt', -1] }: PageQueryDto, populate: string[], - filters?: TFilterQuery, - pageQuery?: PageQueryDto, + { $and = [], ...rest }: TFilterQuery, ) { - const { $and = [], ...rest } = filters || ({} as TFilterQuery); - return this.model .aggregate([ { // support filters $match: { ...rest, - ...($and?.length && { + ...($and.length && { $and: - $and?.map((v) => { - if (v.entity) { - return { - ...v, - entity: new Types.ObjectId(String(v.entity)), - }; - } - - return v; - }) || [], + $and.map(({ entity, ...rest }) => + entity + ? { + ...rest, + entity: new Types.ObjectId(String(entity)), + } + : rest, + ) || [], }), }, }, // support pageQuery { - $skip: pageQuery?.skip || 0, + $limit: limit, }, { - $limit: pageQuery?.limit || 10, + $skip: skip, }, { $sort: { - [pageQuery?.sort?.[0] || 'createdAt']: - pageQuery?.sort?.[1] === 'desc' ? -1 : 1, + [sort[0]]: sort[1] === 'desc' ? -1 : 1, }, }, { @@ -163,22 +158,6 @@ export class NlpValueRepository extends BaseRepository< preserveNullAndEmptyArrays: true, }, }, - { - $lookup: { - from: 'nlpsamples', - localField: 'sampleEntities.sample', - foreignField: '_id', - as: 'samples', - }, - }, - { - $lookup: { - from: 'nlpentities', - localField: 'entity', - foreignField: '_id', - as: 'entities', - }, - }, { $group: { _id: '$_id', @@ -190,17 +169,10 @@ export class NlpValueRepository extends BaseRepository< updatedAt: { $first: '$updatedAt' }, entity: { // support populate - $first: populate.some((p) => - this.getPopulate() - .map((p) => p.toString()) - .includes(p), - ) - ? '$entities' - : '$entity', + $first: this.canPopulate(populate) ? '$entities' : '$entity', }, - //TODO when samples is empty array we need to return 0 not 1 nlpSamplesCount: { - $sum: { $cond: [{ $ifNull: ['samples', false] }, 1, 0] }, + $sum: { $cond: [{ $ifNull: ['$sampleEntities', false] }, 1, 0] }, }, }, }, diff --git a/api/src/nlp/services/nlp-value.service.ts b/api/src/nlp/services/nlp-value.service.ts index f4147333..71ccd031 100644 --- a/api/src/nlp/services/nlp-value.service.ts +++ b/api/src/nlp/services/nlp-value.service.ts @@ -14,7 +14,6 @@ import { PageQueryDto } from '@/utils/pagination/pagination-query.dto'; import { TFilterQuery } from '@/utils/types/filter.types'; import { NlpValueCreateDto, NlpValueDto } from '../dto/nlp-value.dto'; -import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.repository'; import { NlpValueRepository } from '../repositories/nlp-value.repository'; import { NlpEntity } from '../schemas/nlp-entity.schema'; import { @@ -37,7 +36,6 @@ export class NlpValueService extends BaseService< readonly repository: NlpValueRepository, @Inject(forwardRef(() => NlpEntityService)) private readonly nlpEntityService: NlpEntityService, - private readonly nlpSampleEntityRepository: NlpSampleEntityRepository, ) { super(repository); } @@ -224,14 +222,14 @@ export class NlpValueService extends BaseService< } async findAndPopulateNlpValuesWithCount( + pageQuery: PageQueryDto, populate: string[], - filters?: TFilterQuery, - pageQuery?: PageQueryDto, + filters: TFilterQuery, ) { return await this.repository.findAndPopulateNlpValuesWithCount( + pageQuery, populate, filters, - pageQuery, ); } }