diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 2ae465e1554349495ec2cb7ae3ebea274c3ca04a..3f322d808c26aae425beb6e33f27cfb6cfd396ea 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -1,6 +1,6 @@ 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(), }; diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index b4aeea9bc83c7d3a6198af2d44909e4300b60f89..e8303550b4ddbdcd4ce53f2dcdc20bbfb0bd2ed1 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -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]);