Skip to content

Commit

Permalink
Refactor family account count query
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjyhy committed Jan 15, 2025
1 parent 60a2229 commit a21971b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions server/stats/helpers/addPhaseFilteringToQuery.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const addPhaseFilteringToQuery = async <T extends User | Student>(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;
};
16 changes: 12 additions & 4 deletions server/stats/helpers/createFamilyAccountQuery.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
18 changes: 11 additions & 7 deletions server/stats/queryStatsByFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a21971b

Please sign in to comment.