From d918fb7e8a15061f482359b6e9b4cf8b1e424cea Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Mon, 23 Nov 2020 16:05:37 +0100
Subject: [PATCH] feat: add count handling

---
 src/structures/structures.controller.ts | 22 ++++++++++++++++++++++
 src/structures/structures.service.ts    | 21 +++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index e4bcde4b3..462e4adf2 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -16,4 +16,26 @@ export class StructuresController {
   async findAll(): Promise<Structure[]> {
     return this.structureService.findAll();
   }
+
+  @Get('count')
+  async countCategories(): Promise<Array<{ id: string; count: number }>> {
+    const data = await Promise.all([
+      this.structureService.countByStructureKey('accesAuxDroits'),
+      this.structureService.countByStructureKey('aideALaParentalite'),
+      this.structureService.countByStructureKey('cultureEtSecuriteNumerique'),
+      this.structureService.countByStructureKey('insertionSocialeEtProfessionnelle'),
+      this.structureService.countByStructureKey('accompagnementDesDemarches'),
+      this.structureService.countByStructureKey('labelsEtQualifications'),
+      this.structureService.countByStructureKey('publicsAcceptes'),
+      this.structureService.countByStructureKey('modalitesDacces'),
+      this.structureService.countByStructureKey('lesCompetencesDeBase'),
+      this.structureService.countByEquipmentsKey('wifiEnAccesLibre'),
+      this.structureService.countByEquipmentsKey('ordinateurs'),
+      this.structureService.countByEquipmentsKey('tablettes'),
+      this.structureService.countByEquipmentsKey('bornesNumeriques'),
+      this.structureService.countByEquipmentsKey('imprimantes'),
+    ]);
+    // Return a concat of all arrays
+    return data.reduce((a, b) => [...a, ...b]);
+  }
 }
diff --git a/src/structures/structures.service.ts b/src/structures/structures.service.ts
index d6b6750f0..5526a29fb 100644
--- a/src/structures/structures.service.ts
+++ b/src/structures/structures.service.ts
@@ -16,4 +16,25 @@ export class StructuresService {
   async findAll(): Promise<Structure[]> {
     return this.structureModel.find().exec();
   }
+
+  /**
+   * Count every value occurence of a given key
+   * @param key structure key
+   * @return [{id: 'key', count: 'value'}]
+   */
+  async countByStructureKey(key: string): Promise<any> {
+    const uniqueElements = await this.structureModel.distinct(key).exec();
+    return await Promise.all(
+      uniqueElements.map(async (value) => {
+        return {
+          id: value,
+          count: await this.structureModel.countDocuments({ [key]: { $elemMatch: { $eq: value } } }).exec(),
+        };
+      })
+    );
+  }
+
+  async countByEquipmentsKey(key: string): Promise<any> {
+    return [{ id: key, count: await this.structureModel.countDocuments({ [key]: true }).exec() }];
+  }
 }
-- 
GitLab