diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 3f322d808c26aae425beb6e33f27cfb6cfd396ea..06856d7d0f4792192b070e54fa3fad565cc1637b 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -187,39 +187,28 @@ export class StructuresService { * @param key structure key * @return [{id: 'key', count: 'value'}] */ - public async countByStructureKey(key: string, selected: { cat: string; key: string }[]): Promise<any> { - console.log('key is:', key); + public async countByStructureKey(key: string, selected: { id: string; text: string }[]): Promise<any> { 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 }, - }); + if (selected && selected.length > 0) { + for (let val of selected) { + keyList.push({ + [val.text]: { $elemMatch: { $eq: val.id } }, + deletedAt: { $exists: false }, + }); + } } return { id: value, count: await this.structureModel .countDocuments({ $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 e8303550b4ddbdcd4ce53f2dcdc20bbfb0bd2ed1..00bee85f3ecd6493c7c0d5350d487c58137531e8 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -74,11 +74,11 @@ export class StructuresController { return this.userService.updateStructureLinkedClaim(user.email, idStructure); } - @Get('count') + @Post('count') public async countCategories( - selectedFilter: { cat: string; key: string }[] + @Body() + selectedFilter: { id: string; text: string }[] ): Promise<Array<{ id: string; count: number }>> { - //const testFilter = [{ cat: 'poleEmploi', key: 'proceduresAccompaniment' }]; const data = await Promise.all([ this.structureService.countByStructureKey('proceduresAccompaniment', selectedFilter),