diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index e4bcde4b349b2d938440217338fb84ee68ca030b..462e4adf25fc5bf2b3923184da27f8757705a485 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 d6b6750f0b1c2f85cae1527749f6cdddb9f1cc29..5526a29fbbf2c2220725a76994fad9d1b8773c4c 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() }]; + } }