diff --git a/packages/backend/src/database/services/interaction/interactionRepository.service.ts b/packages/backend/src/database/services/interaction/interactionRepository.service.ts index c328cbb790..547831daff 100644 --- a/packages/backend/src/database/services/interaction/interactionRepository.service.ts +++ b/packages/backend/src/database/services/interaction/interactionRepository.service.ts @@ -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 @@ -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), - })); } diff --git a/packages/backend/src/database/services/user-usager/userUsagerLoginRepository.service.ts b/packages/backend/src/database/services/user-usager/userUsagerLoginRepository.service.ts index a89bd32aef..d162281ee7 100644 --- a/packages/backend/src/database/services/user-usager/userUsagerLoginRepository.service.ts +++ b/packages/backend/src/database/services/user-usager/userUsagerLoginRepository.service.ts @@ -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] + ); }, }); diff --git a/packages/backend/src/stats/services/structureStatsQuestionsInPeriodInteractionsRepository.service.ts b/packages/backend/src/stats/services/structureStatsQuestionsInPeriodInteractionsRepository.service.ts index 7d5dd32f48..97e4dea45d 100644 --- a/packages/backend/src/stats/services/structureStatsQuestionsInPeriodInteractionsRepository.service.ts +++ b/packages/backend/src/stats/services/structureStatsQuestionsInPeriodInteractionsRepository.service.ts @@ -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"; @@ -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; } diff --git a/packages/backend/src/usagers/controllers/export-structure-usagers.controller.ts b/packages/backend/src/usagers/controllers/export-structure-usagers.controller.ts index 118ff8c7bc..c13524d091 100644 --- a/packages/backend/src/usagers/controllers/export-structure-usagers.controller.ts +++ b/packages/backend/src/usagers/controllers/export-structure-usagers.controller.ts @@ -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") @@ -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( @@ -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 = {