Skip to content

Commit

Permalink
fix(stats): update stats for loginPortail
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Nov 20, 2023
1 parent bfa5dff commit 8c71caa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ async function totalInteractionAllUsagersStructure({
colisIn: number;
colisOut: number;
npai: number;
loginPortail: number;
}[]
> {
// NOTE: cette requête ne renvoit pas de résultats pour les usagers de cette structure qui n'ont pas d'interaction
Expand All @@ -279,22 +278,29 @@ async function totalInteractionAllUsagersStructure({

const results = await interactionRepository.query(query, [structureId]);

const loginPortailStats = await userUsagerLoginRepository.query(
`SELECT u."usagerRef", coalesce (COUNT(uuid THEN 1 END), 0) AS "loginPortail" FROM user_usager_login u WHERE u."structureId" = $1 GROUP BY u."usagerRef"`,
[structureId]
return results.map(
(x: {
usagerRef: string;
appel: string;
visite: string;
courrierIn: string;
courrierOut: string;
recommandeIn: string;
recommandeOut: string;
colisIn: string;
colisOut: string;
npai: string;
}) => ({
usagerRef: parseInt(x.usagerRef, 10),
courrierIn: parseInt(x.courrierIn, 10),
courrierOut: parseInt(x.courrierOut, 10),
recommandeIn: parseInt(x.recommandeIn, 10),
recommandeOut: parseInt(x.recommandeOut, 10),
colisIn: parseInt(x.colisIn, 10),
colisOut: parseInt(x.colisOut, 10),
appel: parseInt(x.appel, 10),
visite: parseInt(x.visite, 10),
npai: parseInt(x.npai, 10),
})
);

return results.map((x: any) => ({
usagerRef: x.usagerRef,
courrierIn: parseInt(x.courrierIn, 10),
courrierOut: parseInt(x.courrierOut, 10),
recommandeIn: parseInt(x.recommandeIn, 10),
recommandeOut: parseInt(x.recommandeOut, 10),
colisIn: parseInt(x.colisIn, 10),
colisOut: parseInt(x.colisOut, 10),
appel: parseInt(x.appel, 10),
visite: parseInt(x.visite, 10),
loginPortail: loginPortailStats,
npai: parseInt(x.npai, 10),
}));
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Between } from "typeorm";
import { UserUsagerLoginTable } from "../../entities";
import { myDataSource } from "../_postgres";

export const userUsagerLoginRepository = myDataSource
.getRepository(UserUsagerLoginTable)
.extend({
countBetween(
dateInteractionBefore: Date,
dateInteractionAfter: Date,
structureId: number
) {
return userUsagerLoginRepository.countBy({
structureId,
createdAt: Between(dateInteractionAfter, dateInteractionBefore),
});
async totalLoginAllUsagersStructure(structureId: number): Promise<
| {
usagerUUID: string;
total: string;
}[]
| null
> {
return await userUsagerLoginRepository.query(
`SELECT u."usagerUUID", COUNT(uuid) AS "total" FROM user_usager_login u WHERE u."structureId" = $1 GROUP BY u."usagerUUID"`,
[structureId]
);
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Between } from "typeorm";
import { interactionRepository } from "../../database";
import {
interactionRepository,
userUsagerLoginRepository,
} from "../../database";

import { StructureStatsQuestionsInPeriodInteractions } from "../../_common/model/structure-stats";
import { InteractionType } from "@domifa/common";
Expand Down Expand Up @@ -84,8 +87,17 @@ async function getStats({
structureId,
interactionType: "npai",
}),
loginPortail: 0,
loginPortail: await userUsagerLoginRepository.count({
where: {
structureId,
createdAt: Between(
startDateUTC,
endDateUTCExclusive
) as unknown as Date,
},
}),
};

return stats;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { UsagersService } from "../services/usagers.service";

import { format } from "date-fns";
import { InteractionType } from "@domifa/common";
import { userUsagerLoginRepository } from "../../database";

@UseGuards(AuthGuard("jwt"), AppUserGuard)
@ApiTags("export")
Expand Down Expand Up @@ -69,6 +70,11 @@ export class ExportStructureUsagersController {
structureId: user.structureId,
});

const userUsagerLoginByUsagerMap =
await userUsagerLoginRepository.totalLoginAllUsagersStructure(
user.structureId
);

for (const element of usagers) {
const usager: Usager = element;
const data = interactionsByUsagerMap.find(
Expand All @@ -83,37 +89,30 @@ export class ExportStructureUsagersController {
colisIn: number;
colisOut: number;
npai: number;
loginPortail: number;
}) => x.usagerRef === usager.ref
);

if (data) {
usagersInteractionsCountByType[usager.ref] = {
courrierIn: data.courrierIn,
courrierOut: data.courrierOut,
recommandeIn: data.recommandeIn,
recommandeOut: data.recommandeOut,
colisIn: data.colisIn,
colisOut: data.colisOut,
appel: data.appel,
visite: data.visite,
loginPortail: data.loginPortail,
npai: data.npai,
};
} else {
usagersInteractionsCountByType[usager.ref] = {
courrierIn: 0,
courrierOut: 0,
recommandeIn: 0,
recommandeOut: 0,
colisIn: 0,
colisOut: 0,
appel: 0,
loginPortail: 0,
visite: 0,
npai: 0,
};
}
const userUsagerLogin = userUsagerLoginByUsagerMap.find(
(x: { usagerUUID: string; total: string }) =>
x.usagerUUID === usager.uuid
);

const loginPortail = userUsagerLogin
? parseInt(userUsagerLogin.total, 10)
: 0;

usagersInteractionsCountByType[usager.ref] = {
courrierIn: data?.courrierIn ?? 0,
courrierOut: data?.courrierOut ?? 0,
recommandeIn: data?.recommandeIn ?? 0,
recommandeOut: data?.recommandeOut ?? 0,
colisIn: data?.colisIn ?? 0,
colisOut: data?.colisOut ?? 0,
appel: data?.appel ?? 0,
loginPortail,
visite: 0,
npai: data?.npai ?? 0,
};
}

const model: StructureUsagersExportModel = {
Expand Down

0 comments on commit 8c71caa

Please sign in to comment.