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(
new SearchFilterPipe<NlpValue>({ allowedFields: ['entity', 'value'] }),
)
filters?: TFilterQuery<NlpValue>,
filters: TFilterQuery<NlpValue>,
) {
return await this.nlpValueService.findAndPopulateNlpValuesWithCount(
pageQuery,
populate,
filters,
pageQuery,
);
}

View File

@ -109,44 +109,39 @@ export class NlpValueRepository extends BaseRepository<
}
async findAndPopulateNlpValuesWithCount(
{ limit = 10, skip = 0, sort = ['createdAt', -1] }: PageQueryDto<NlpValue>,
populate: string[],
filters?: TFilterQuery<NlpValue>,
pageQuery?: PageQueryDto<NlpValue>,
{ $and = [], ...rest }: TFilterQuery<NlpValue>,
) {
const { $and = [], ...rest } = filters || ({} as TFilterQuery<NlpValue>);
return this.model
.aggregate<NlpValue>([
{
// 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] },
},
},
},

View File

@ -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<NlpValue>,
populate: string[],
filters?: TFilterQuery<NlpValue>,
pageQuery?: PageQueryDto<NlpValue>,
filters: TFilterQuery<NlpValue>,
) {
return await this.repository.findAndPopulateNlpValuesWithCount(
pageQuery,
populate,
filters,
pageQuery,
);
}
}