diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 441d7022d82e56f8175c954d621cfb3ced95c130..c9f08354faedda8a3a574946057ff2dfd7dee2c4 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'; @@ -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(), }; }) diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 438107c4edc061bafec9143b69ab2e0bc1b5b421..96ed9e332a5dbed7b9190cbc5dc88802b678c94d 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -89,22 +89,25 @@ export class StructuresController { return this.userService.updateStructureLinkedClaim(user.email, idStructure); } - @Get('count') - public async countCategories(): Promise<Array<{ id: string; count: number }>> { + @Post('count') + public async countCategories( + @Body() + selectedFilter: { id: string; text: string }[] + ): Promise<Array<{ id: string; count: number }>> { 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]);