Skip to content
Snippets Groups Projects

Feat/structure filter

Merged Antonin COQUET requested to merge feat/structure-filter into dev
Files
2
import { HttpException, HttpService, Injectable, HttpStatus, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Types, Model } from 'mongoose';
import { Types, Model, FilterQuery, DocumentDefinition } from 'mongoose';
import { Observable } from 'rxjs';
import { AxiosResponse } from 'axios';
import { Structure, StructureDocument } from '../schemas/structure.schema';
@@ -58,7 +58,7 @@ export class StructuresService {
.exec();
} else if (filters) {
return this.structureModel
.find({ $and: [{ $or: this.parseFilter(filters), deletedAt: { $exists: false }, accountVerified: true }] })
.find({ $and: [{ $and: this.parseFilter(filters), deletedAt: { $exists: false }, accountVerified: true }] })
.exec();
} else {
return this.structureModel
@@ -187,14 +187,29 @@ export class StructuresService {
* @param key structure key
* @return [{id: 'key', count: 'value'}]
*/
public async countByStructureKey(key: string): Promise<any> {
public async countByStructureKey(key: string, selected: { id: string; text: string }[]): Promise<any> {
const uniqueElements = await this.structureModel.distinct(key).exec();
return await Promise.all(
uniqueElements.map(async (value) => {
const keyList: FilterQuery<DocumentDefinition<StructureDocument>>[] = [];
keyList.push({
[key]: { $elemMatch: { $eq: value } },
deletedAt: { $exists: false },
});
if (selected && selected.length > 0) {
for (const val of selected) {
keyList.push({
[val.text]: { $elemMatch: { $eq: val.id } },
deletedAt: { $exists: false },
});
}
}
return {
id: value,
count: await this.structureModel
.countDocuments({ $and: [{ [key]: { $elemMatch: { $eq: value } }, deletedAt: { $exists: false } }] })
.countDocuments({
$and: keyList,
})
.exec(),
};
})
Loading