From 449eef942cec883757c1e173e88c15bb7f698d7c Mon Sep 17 00:00:00 2001
From: Antonin Coquet <ext.sopra.acoquet@grandlyon.com>
Date: Thu, 8 Apr 2021 11:34:54 +0200
Subject: [PATCH] count filter depends on actual filters

---
 src/structures/services/structures.service.ts | 27 ++++++-------------
 src/structures/structures.controller.ts       |  6 ++---
 2 files changed, 11 insertions(+), 22 deletions(-)

diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 3f322d808..06856d7d0 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 e8303550b..00bee85f3 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),
 
-- 
GitLab