From a21971b3d60acf02427a08e0c03062466c76e85e Mon Sep 17 00:00:00 2001 From: Benjamin Ramet Date: Wed, 15 Jan 2025 16:45:30 +0100 Subject: [PATCH] Refactor family account count query --- .../helpers/addPhaseFilteringToQuery.helper.ts | 4 ++-- .../helpers/createFamilyAccountQuery.helper.ts | 16 ++++++++++++---- server/stats/queryStatsByFilter.ts | 18 +++++++++++------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/server/stats/helpers/addPhaseFilteringToQuery.helper.ts b/server/stats/helpers/addPhaseFilteringToQuery.helper.ts index b679df95c..0b06f5fed 100644 --- a/server/stats/helpers/addPhaseFilteringToQuery.helper.ts +++ b/server/stats/helpers/addPhaseFilteringToQuery.helper.ts @@ -9,8 +9,8 @@ export const addPhaseFilteringToQuery = async (query: if (phaseWasSelected(phase)) { const phaseValue = phase as number; const { debut, end } = await getPhasePeriod(village.id, phaseValue); - query.andWhere('student.createdAt >= :debut', { debut }); - if (phaseValue != village?.activePhase) query.andWhere('student.createdAt <= :end', { end }); + query.andWhere('user.createdAt >= :debut', { debut }); + if (phaseValue != village?.activePhase) query.andWhere('user.createdAt <= :end', { end }); } return query; }; diff --git a/server/stats/helpers/createFamilyAccountQuery.helper.ts b/server/stats/helpers/createFamilyAccountQuery.helper.ts index dc2ea51e4..75a1482a2 100644 --- a/server/stats/helpers/createFamilyAccountQuery.helper.ts +++ b/server/stats/helpers/createFamilyAccountQuery.helper.ts @@ -3,15 +3,23 @@ import { AppDataSource } from '../../utils/data-source'; const userRepository = AppDataSource.getRepository(User); -export const createFamilyAccountQuery = async (villageId: number) => { - const query = userRepository +export const createFamilyAccountQuery = (villageId: number) => { + /* const query = userRepository .createQueryBuilder('user') .innerJoin('user.village', 'village') .innerJoin('classroom', 'classroom', 'classroom.villageId = village.id') .innerJoin('student', 'student', 'student.classroomId = classroom.id') + .addSelect('village') + .addSelect('classroom') + .addSelect('student') .where('user.type = 3') - .andWhere('classroom.villageId = :villageId', { villageId }); + .andWhere('classroom.villageId = :villageId', { villageId }); */ + + const query = userRepository + .createQueryBuilder('user') + .leftJoinAndSelect('user.village', 'village') + .where('user.type = :userType', { userType: 3 }) + .andWhere('village.id = :villageId', { villageId }); - query.groupBy('user.id'); return query; }; diff --git a/server/stats/queryStatsByFilter.ts b/server/stats/queryStatsByFilter.ts index 19754092a..1ac2ea305 100644 --- a/server/stats/queryStatsByFilter.ts +++ b/server/stats/queryStatsByFilter.ts @@ -97,27 +97,31 @@ export const getFamilyAccountsCount = async (filterParams: StatsFilterParams) => const { villageId, countryId, phase } = filterParams; let familyAccountsCount = 0; - if (villageId) { - const query = await createFamilyAccountQuery(villageId); - familyAccountsCount = await query.getCount(); + if (villageId && phase) { + const village = await villageRepository.findOne({ where: { id: villageId } }); + if (village) familyAccountsCount = await countFamilyAccounts(village, phase); } else if (countryId) { const villages = await villageRepository .createQueryBuilder('village') .where('village.countryCodes LIKE :countryId', { countryId: `%${countryId}%` }) .getMany(); const countPromises = villages.map(async (vil) => { - let query = await createFamilyAccountQuery(vil.id); - if (phase) query = await addPhaseFilteringToQuery(query, phase, vil); - return query.getCount(); + return countFamilyAccounts(vil, phase); }); - const results = await Promise.all(countPromises); + const results = (await Promise.all(countPromises)) as number[]; familyAccountsCount = results.reduce((total, count) => total + count, 0); } return familyAccountsCount; }; +const countFamilyAccounts = async (village: Village, phase: number | undefined) => { + const query = createFamilyAccountQuery(village.id); + if (village && phase) await addPhaseFilteringToQuery(query, phase, village); + return query.getCount(); +}; + export const getChildrenCodesCount = async (filterParams: StatsFilterParams, whereClause?: WhereClause) => { const { villageId, countryId, phase } = filterParams; let childrenCodesCount = 0;