Skip to content
Snippets Groups Projects
Commit 4e241459 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

Merge branch 'feat/structure-filter' into 'dev'

Feat/structure filter

See merge request web-et-numerique/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server!58
parents d0483017 b094c494
No related branches found
No related tags found
3 merge requests!96release V1.10.0,!62Dev,!58Feat/structure filter
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(),
};
})
......
......@@ -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]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment