fix: enhance logic

This commit is contained in:
yassinedorbozgithub 2025-03-23 13:03:56 +01:00
parent 7ecf821c48
commit 6a25d47349
3 changed files with 21 additions and 51 deletions

View File

@ -132,12 +132,12 @@ export class NlpValueController extends BaseController<
@Query( @Query(
new SearchFilterPipe<NlpValue>({ allowedFields: ['entity', 'value'] }), new SearchFilterPipe<NlpValue>({ allowedFields: ['entity', 'value'] }),
) )
filters?: TFilterQuery<NlpValue>, filters: TFilterQuery<NlpValue>,
) { ) {
return await this.nlpValueService.findAndPopulateNlpValuesWithCount( return await this.nlpValueService.findAndPopulateNlpValuesWithCount(
pageQuery,
populate, populate,
filters, filters,
pageQuery,
); );
} }

View File

@ -109,44 +109,39 @@ export class NlpValueRepository extends BaseRepository<
} }
async findAndPopulateNlpValuesWithCount( async findAndPopulateNlpValuesWithCount(
{ limit = 10, skip = 0, sort = ['createdAt', -1] }: PageQueryDto<NlpValue>,
populate: string[], populate: string[],
filters?: TFilterQuery<NlpValue>, { $and = [], ...rest }: TFilterQuery<NlpValue>,
pageQuery?: PageQueryDto<NlpValue>,
) { ) {
const { $and = [], ...rest } = filters || ({} as TFilterQuery<NlpValue>);
return this.model return this.model
.aggregate<NlpValue>([ .aggregate<NlpValue>([
{ {
// support filters // support filters
$match: { $match: {
...rest, ...rest,
...($and?.length && { ...($and.length && {
$and: $and:
$and?.map((v) => { $and.map(({ entity, ...rest }) =>
if (v.entity) { entity
return { ? {
...v, ...rest,
entity: new Types.ObjectId(String(v.entity)), entity: new Types.ObjectId(String(entity)),
}; }
} : rest,
) || [],
return v;
}) || [],
}), }),
}, },
}, },
// support pageQuery // support pageQuery
{ {
$skip: pageQuery?.skip || 0, $limit: limit,
}, },
{ {
$limit: pageQuery?.limit || 10, $skip: skip,
}, },
{ {
$sort: { $sort: {
[pageQuery?.sort?.[0] || 'createdAt']: [sort[0]]: sort[1] === 'desc' ? -1 : 1,
pageQuery?.sort?.[1] === 'desc' ? -1 : 1,
}, },
}, },
{ {
@ -163,22 +158,6 @@ export class NlpValueRepository extends BaseRepository<
preserveNullAndEmptyArrays: true, preserveNullAndEmptyArrays: true,
}, },
}, },
{
$lookup: {
from: 'nlpsamples',
localField: 'sampleEntities.sample',
foreignField: '_id',
as: 'samples',
},
},
{
$lookup: {
from: 'nlpentities',
localField: 'entity',
foreignField: '_id',
as: 'entities',
},
},
{ {
$group: { $group: {
_id: '$_id', _id: '$_id',
@ -190,17 +169,10 @@ export class NlpValueRepository extends BaseRepository<
updatedAt: { $first: '$updatedAt' }, updatedAt: { $first: '$updatedAt' },
entity: { entity: {
// support populate // support populate
$first: populate.some((p) => $first: this.canPopulate(populate) ? '$entities' : '$entity',
this.getPopulate()
.map((p) => p.toString())
.includes(p),
)
? '$entities'
: '$entity',
}, },
//TODO when samples is empty array we need to return 0 not 1
nlpSamplesCount: { nlpSamplesCount: {
$sum: { $cond: [{ $ifNull: ['samples', false] }, 1, 0] }, $sum: { $cond: [{ $ifNull: ['$sampleEntities', false] }, 1, 0] },
}, },
}, },
}, },

View File

@ -14,7 +14,6 @@ import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { TFilterQuery } from '@/utils/types/filter.types'; import { TFilterQuery } from '@/utils/types/filter.types';
import { NlpValueCreateDto, NlpValueDto } from '../dto/nlp-value.dto'; import { NlpValueCreateDto, NlpValueDto } from '../dto/nlp-value.dto';
import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.repository';
import { NlpValueRepository } from '../repositories/nlp-value.repository'; import { NlpValueRepository } from '../repositories/nlp-value.repository';
import { NlpEntity } from '../schemas/nlp-entity.schema'; import { NlpEntity } from '../schemas/nlp-entity.schema';
import { import {
@ -37,7 +36,6 @@ export class NlpValueService extends BaseService<
readonly repository: NlpValueRepository, readonly repository: NlpValueRepository,
@Inject(forwardRef(() => NlpEntityService)) @Inject(forwardRef(() => NlpEntityService))
private readonly nlpEntityService: NlpEntityService, private readonly nlpEntityService: NlpEntityService,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) { ) {
super(repository); super(repository);
} }
@ -224,14 +222,14 @@ export class NlpValueService extends BaseService<
} }
async findAndPopulateNlpValuesWithCount( async findAndPopulateNlpValuesWithCount(
pageQuery: PageQueryDto<NlpValue>,
populate: string[], populate: string[],
filters?: TFilterQuery<NlpValue>, filters: TFilterQuery<NlpValue>,
pageQuery?: PageQueryDto<NlpValue>,
) { ) {
return await this.repository.findAndPopulateNlpValuesWithCount( return await this.repository.findAndPopulateNlpValuesWithCount(
pageQuery,
populate, populate,
filters, filters,
pageQuery,
); );
} }
} }