Skip to content
Snippets Groups Projects
Commit f7102873 authored by Antonin COQUET's avatar Antonin COQUET
Browse files

implement multiple filters count

parent a1568197
No related branches found
No related tags found
3 merge requests!96release V1.10.0,!62Dev,!58Feat/structure filter
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';
......@@ -187,26 +187,39 @@ 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: { cat: string; key: string }[]): Promise<any> {
console.log('key is:', key);
const uniqueElements = await this.structureModel.distinct(key).exec();
console.log('unique elements:', uniqueElements);
return await Promise.all(
uniqueElements.map(async (value) => {
console.log(value);
let keyList: FilterQuery<DocumentDefinition<StructureDocument>>[] = [];
keyList.push({
[key]: { $elemMatch: { $eq: value } },
deletedAt: { $exists: false },
});
for (let val of selected) {
keyList.push({
//[val.key]: { $match: { $eq: val.cat } },
$match: { $eq: val.cat },
deletedAt: { $exists: false },
});
}
return {
id: value,
count: await this.structureModel
.countDocuments({
$and: [
{
[key]: { $elemMatch: { $eq: value } },
//['proceduresAccompaniment']: { $elemMatch: { $eq: 'accompagnantCaf' } },
deletedAt: { $exists: false },
},
{
// ['proceduresAccompaniment']: { $elemMatch: { $eq: 'accompagnantCaf' } },
},
],
$and: keyList,
// $and: [
// {
// [key]: { $elemMatch: { $eq: value } },
// deletedAt: { $exists: false },
// },
// {
// // ['proceduresAccompaniment']: { $elemMatch: { $eq: 'accompagnantCaf' } },
// },
// ],
})
.exec(),
};
......
......@@ -75,21 +75,24 @@ export class StructuresController {
}
@Get('count')
public async countCategories(): Promise<Array<{ id: string; count: number }>> {
public async countCategories(
selectedFilter: { cat: string; key: string }[]
): Promise<Array<{ id: string; count: number }>> {
//const testFilter = [{ cat: 'poleEmploi', key: 'proceduresAccompaniment' }];
const data = await Promise.all([
this.structureService.countByStructureKey('proceduresAccompaniment'),
this.structureService.countByStructureKey('accessRight'),
this.structureService.countByStructureKey('baseSkills'),
this.structureService.countByStructureKey('parentingHelp'),
this.structureService.countByStructureKey('digitalCultureSecurity'),
this.structureService.countByStructureKey('socialAndProfessional'),
this.structureService.countByStructureKey('publicsAccompaniment'),
this.structureService.countByStructureKey('labelsQualifications'),
this.structureService.countByStructureKey('publics'),
this.structureService.countByStructureKey('accessModality'),
this.structureService.countByStructureKey('equipmentsAndServices'),
this.structureService.countByStructureKey('proceduresAccompaniment', selectedFilter),
this.structureService.countByStructureKey('accessRight', selectedFilter),
this.structureService.countByStructureKey('baseSkills', selectedFilter),
this.structureService.countByStructureKey('parentingHelp', selectedFilter),
this.structureService.countByStructureKey('digitalCultureSecurity', selectedFilter),
this.structureService.countByStructureKey('socialAndProfessional', selectedFilter),
this.structureService.countByStructureKey('publicsAccompaniment', selectedFilter),
this.structureService.countByStructureKey('labelsQualifications', selectedFilter),
this.structureService.countByStructureKey('publics', selectedFilter),
this.structureService.countByStructureKey('accessModality', selectedFilter),
this.structureService.countByStructureKey('equipmentsAndServices', selectedFilter),
]);
// Return a concat of all arrays
return data.reduce((a, b) => [...a, ...b]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment