mirror of
https://github.com/hexastack/hexabot
synced 2025-06-04 03:26:22 +00:00
fix: move findAndPopulateNlpValuesWithCount to the repository
This commit is contained in:
parent
b79fcac080
commit
7ecf821c48
@ -8,9 +8,10 @@
|
|||||||
|
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectModel } from '@nestjs/mongoose';
|
import { InjectModel } from '@nestjs/mongoose';
|
||||||
import { Document, Model, Query } from 'mongoose';
|
import { Document, Model, Query, Types } from 'mongoose';
|
||||||
|
|
||||||
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
|
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
|
||||||
|
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
||||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { NlpValueDto } from '../dto/nlp-value.dto';
|
import { NlpValueDto } from '../dto/nlp-value.dto';
|
||||||
@ -106,4 +107,118 @@ export class NlpValueRepository extends BaseRepository<
|
|||||||
throw new Error('Attempted to delete a NLP value using unknown criteria');
|
throw new Error('Attempted to delete a NLP value using unknown criteria');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findAndPopulateNlpValuesWithCount(
|
||||||
|
populate: string[],
|
||||||
|
filters?: TFilterQuery<NlpValue>,
|
||||||
|
pageQuery?: PageQueryDto<NlpValue>,
|
||||||
|
) {
|
||||||
|
const { $and = [], ...rest } = filters || ({} as TFilterQuery<NlpValue>);
|
||||||
|
|
||||||
|
return this.model
|
||||||
|
.aggregate<NlpValue>([
|
||||||
|
{
|
||||||
|
// support filters
|
||||||
|
$match: {
|
||||||
|
...rest,
|
||||||
|
...($and?.length && {
|
||||||
|
$and:
|
||||||
|
$and?.map((v) => {
|
||||||
|
if (v.entity) {
|
||||||
|
return {
|
||||||
|
...v,
|
||||||
|
entity: new Types.ObjectId(String(v.entity)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}) || [],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// support pageQuery
|
||||||
|
{
|
||||||
|
$skip: pageQuery?.skip || 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$limit: pageQuery?.limit || 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$sort: {
|
||||||
|
[pageQuery?.sort?.[0] || 'createdAt']:
|
||||||
|
pageQuery?.sort?.[1] === 'desc' ? -1 : 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'nlpsampleentities',
|
||||||
|
localField: '_id',
|
||||||
|
foreignField: 'value',
|
||||||
|
as: 'sampleEntities',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$unwind: {
|
||||||
|
path: '$sampleEntities',
|
||||||
|
preserveNullAndEmptyArrays: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'nlpsamples',
|
||||||
|
localField: 'sampleEntities.sample',
|
||||||
|
foreignField: '_id',
|
||||||
|
as: 'samples',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'nlpentities',
|
||||||
|
localField: 'entity',
|
||||||
|
foreignField: '_id',
|
||||||
|
as: 'entities',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: '$_id',
|
||||||
|
value: { $first: '$value' },
|
||||||
|
expressions: { $first: '$expressions' },
|
||||||
|
builtin: { $first: '$builtin' },
|
||||||
|
metadata: { $first: '$metadata' },
|
||||||
|
createdAt: { $first: '$createdAt' },
|
||||||
|
updatedAt: { $first: '$updatedAt' },
|
||||||
|
entity: {
|
||||||
|
// support populate
|
||||||
|
$first: populate.some((p) =>
|
||||||
|
this.getPopulate()
|
||||||
|
.map((p) => p.toString())
|
||||||
|
.includes(p),
|
||||||
|
)
|
||||||
|
? '$entities'
|
||||||
|
: '$entity',
|
||||||
|
},
|
||||||
|
//TODO when samples is empty array we need to return 0 not 1
|
||||||
|
nlpSamplesCount: {
|
||||||
|
$sum: { $cond: [{ $ifNull: ['samples', false] }, 1, 0] },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
id: '$_id',
|
||||||
|
_id: 0,
|
||||||
|
value: 1,
|
||||||
|
expressions: 1,
|
||||||
|
builtin: 1,
|
||||||
|
entity: 1,
|
||||||
|
metadata: 1,
|
||||||
|
createdAt: 1,
|
||||||
|
updatedAt: 1,
|
||||||
|
nlpSamplesCount: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.exec();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
||||||
import { Types } from 'mongoose';
|
|
||||||
|
|
||||||
import { DeleteResult } from '@/utils/generics/base-repository';
|
import { DeleteResult } from '@/utils/generics/base-repository';
|
||||||
import { BaseService } from '@/utils/generics/base-service';
|
import { BaseService } from '@/utils/generics/base-service';
|
||||||
@ -229,115 +228,10 @@ export class NlpValueService extends BaseService<
|
|||||||
filters?: TFilterQuery<NlpValue>,
|
filters?: TFilterQuery<NlpValue>,
|
||||||
pageQuery?: PageQueryDto<NlpValue>,
|
pageQuery?: PageQueryDto<NlpValue>,
|
||||||
) {
|
) {
|
||||||
const { $and = [], ...rest } = filters || ({} as TFilterQuery<NlpValue>);
|
return await this.repository.findAndPopulateNlpValuesWithCount(
|
||||||
|
populate,
|
||||||
const entityValueModel = this.getRepository().model;
|
filters,
|
||||||
|
pageQuery,
|
||||||
return entityValueModel
|
);
|
||||||
.aggregate<NlpValue>([
|
|
||||||
{
|
|
||||||
// support filters
|
|
||||||
$match: {
|
|
||||||
...rest,
|
|
||||||
...($and?.length && {
|
|
||||||
$and:
|
|
||||||
$and?.map((v) => {
|
|
||||||
if (v.entity) {
|
|
||||||
return {
|
|
||||||
...v,
|
|
||||||
entity: new Types.ObjectId(String(v.entity)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return v;
|
|
||||||
}) || [],
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
// support pageQuery
|
|
||||||
{
|
|
||||||
$skip: pageQuery?.skip || 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$limit: pageQuery?.limit || 10,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$sort: {
|
|
||||||
[pageQuery?.sort?.[0] || 'createdAt']:
|
|
||||||
pageQuery?.sort?.[1] === 'desc' ? -1 : 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: 'nlpsampleentities',
|
|
||||||
localField: '_id',
|
|
||||||
foreignField: 'value',
|
|
||||||
as: 'sampleEntities',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$unwind: {
|
|
||||||
path: '$sampleEntities',
|
|
||||||
preserveNullAndEmptyArrays: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: 'nlpsamples',
|
|
||||||
localField: 'sampleEntities.sample',
|
|
||||||
foreignField: '_id',
|
|
||||||
as: 'samples',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$lookup: {
|
|
||||||
from: 'nlpentities',
|
|
||||||
localField: 'entity',
|
|
||||||
foreignField: '_id',
|
|
||||||
as: 'entities',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: '$_id',
|
|
||||||
value: { $first: '$value' },
|
|
||||||
expressions: { $first: '$expressions' },
|
|
||||||
builtin: { $first: '$builtin' },
|
|
||||||
metadata: { $first: '$metadata' },
|
|
||||||
createdAt: { $first: '$createdAt' },
|
|
||||||
updatedAt: { $first: '$updatedAt' },
|
|
||||||
entity: {
|
|
||||||
// support populate
|
|
||||||
$first: populate.some((p) =>
|
|
||||||
this.getRepository()
|
|
||||||
.getPopulate()
|
|
||||||
.map((p) => p.toString())
|
|
||||||
.includes(p),
|
|
||||||
)
|
|
||||||
? '$entities'
|
|
||||||
: '$entity',
|
|
||||||
},
|
|
||||||
//TODO when samples is empty array we need to return 0 not 1
|
|
||||||
nlpSamplesCount: {
|
|
||||||
$sum: { $cond: [{ $ifNull: ['samples', false] }, 1, 0] },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
id: '$_id',
|
|
||||||
_id: 0,
|
|
||||||
value: 1,
|
|
||||||
expressions: 1,
|
|
||||||
builtin: 1,
|
|
||||||
entity: 1,
|
|
||||||
metadata: 1,
|
|
||||||
createdAt: 1,
|
|
||||||
updatedAt: 1,
|
|
||||||
nlpSamplesCount: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
])
|
|
||||||
.exec();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user