Skip to content

Commit

Permalink
fix(mon-domifa): add user usager login in stats
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Nov 20, 2023
1 parent 0f03f5c commit bfa5dff
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
fileignoreconfig:
- filename: _scripts/db/dumps/domifa_test.postgres.restore-data-only.sql
checksum: 104c8036ca644bc51bb4c797e56b3a5b178a55b1c293a77308844e54ca7c404d
- filename: _scripts/db/dumps/domifa_test.postgres.truncate-restore-data-only.sql
checksum: 9976de417c719ab5c3bec9d4a1bafc40bfa8d4388d2dbd5da7d1ea4a11d2db75
- filename: yarn.lock
checksum: 4ac842f95592d194e65b208fdc4b9a78a0bb6470ad2d4950626c2ffcec831a33
version: "1.0"
Binary file modified _scripts/db/dumps/domifa_test.postgres.custom.gz
Binary file not shown.
10 changes: 6 additions & 4 deletions _scripts/db/dumps/domifa_test.postgres.restore-data-only.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 14.9 (Debian 14.9-1.pgdg110+1)
-- Dumped by pg_dump version 14.9 (Debian 14.9-1.pgdg110+1)
-- Dumped from database version 14.9
-- Dumped by pg_dump version 14.9

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down Expand Up @@ -256,8 +256,8 @@ e0bcefc6-f1be-4c83-ac9d-6ea47335a9c3 2021-03-15 16:53:55.740856+01 2023-09-05 23
--
COPY public.user_usager (uuid, "createdAt", "updatedAt", version, id, "usagerUUID", "structureId", login, password, salt, "isTemporaryPassword", "lastLogin", "passwordLastUpdate", "lastPasswordResetDate", "lastPasswordResetStructureUser", enabled) FROM stdin;
a03a9a49-ae31-4160-9879-bab02dc46361 2021-11-30 14:50:26.278073+01 2021-11-30 15:01:39.89434+01 10 2 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1 LNQIFFBK $2a$10$u..ofvcb5t5a44N0Oriwbew6oz600aoOFzll9yl92Lx1VOb0E8o0a $2a$10$zmuPXxUOuQJ7nE6ag4.x6e f 2021-11-30 15:01:39.893+01 2021-11-30 15:01:39.675+01 2021-11-30 14:50:26.275+01 {"userId": 1, "userName": "Patrick Roméro"} t
a657f4bd-e4d1-4c38-bdd0-ffd268b356df 2021-10-05 11:34:41.369505+02 2021-11-30 15:02:07.705535+01 18 1 b2c26e55-ab37-457d-b307-6fe161050a9b 1 WKYJBDXS $2a$10$cmwXTghEgrGRYTkGIJlj3Owjo8YVVDXOgceAGtkgzcD2M/Tf3Dhb. $2a$10$/uFyiR8hCe8XkRe7x2fYm. t 2021-11-30 15:02:07.69+01 \N 2021-10-05 11:34:41.365+02 {"userId": 1, "userName": "Patrick Roméro"} t
a03a9a49-ae31-4160-9879-bab02dc46361 2021-11-30 14:50:26.278073+01 2023-11-20 16:40:58.362384+01 17 2 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1 LNQIFFBK $2a$10$u..ofvcb5t5a44N0Oriwbew6oz600aoOFzll9yl92Lx1VOb0E8o0a $2a$10$zmuPXxUOuQJ7nE6ag4.x6e f 2023-11-20 16:40:58.36+01 2021-11-30 15:01:39.675+01 2021-11-30 14:50:26.275+01 {"userId": 1, "userName": "Patrick Roméro"} t
\.
Expand All @@ -266,6 +266,8 @@ a657f4bd-e4d1-4c38-bdd0-ffd268b356df 2021-10-05 11:34:41.369505+02 2021-11-30 15
--
COPY public.user_usager_login (uuid, "createdAt", "updatedAt", version, "usagerUUID", "structureId") FROM stdin;
3927beaf-04d8-4cbb-9be7-c7c9086434b3 2023-11-20 16:40:44.4121+01 2023-11-20 16:40:44.4121+01 1 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1
e80dafea-8fe4-4792-92b9-3608c723aa1b 2023-11-20 16:40:58.289642+01 2023-11-20 16:40:58.289642+01 1 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1
\.
Expand All @@ -274,8 +276,8 @@ COPY public.user_usager_login (uuid, "createdAt", "updatedAt", version, "usagerU
--
COPY public.user_usager_security (uuid, "createdAt", "updatedAt", version, "userId", "structureId", "eventsHistory") FROM stdin;
fadd55b6-ca41-48d4-bd56-238b1a3c6f7b 2021-11-30 14:50:26.290488+01 2021-11-30 15:01:39.66811+01 8 2 1 [{"date": "2021-11-30T13:50:38.357Z", "type": "login-success"}, {"date": "2021-11-30T13:51:20.351Z", "type": "login-success"}, {"date": "2021-11-30T13:51:20.608Z", "type": "change-password-success"}, {"date": "2021-11-30T14:00:55.533Z", "type": "login-error"}, {"date": "2021-11-30T14:01:21.310Z", "type": "login-success"}, {"date": "2021-11-30T14:01:39.398Z", "type": "login-success"}, {"date": "2021-11-30T14:01:39.667Z", "type": "change-password-success"}]
9bc8decb-5f78-48de-8c1b-9f61ea5acfba 2021-10-05 11:34:41.388922+02 2021-11-30 15:02:07.682311+01 3 1 1 [{"date": "2021-11-30T14:02:07.680Z", "type": "login-success"}]
fadd55b6-ca41-48d4-bd56-238b1a3c6f7b 2021-11-30 14:50:26.290488+01 2023-11-20 16:40:58.2674+01 10 2 1 [{"date": "2023-11-20T15:40:44.378Z", "type": "login-success"}, {"date": "2023-11-20T15:40:58.265Z", "type": "login-success"}]
\.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ TRUNCATE TABLE public.usager_options_history RESTART IDENTITY CASCADE;
-- PostgreSQL database dump
--

-- Dumped from database version 14.9 (Debian 14.9-1.pgdg110+1)
-- Dumped by pg_dump version 14.9 (Debian 14.9-1.pgdg110+1)
-- Dumped from database version 14.9
-- Dumped by pg_dump version 14.9

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down Expand Up @@ -282,8 +282,8 @@ e0bcefc6-f1be-4c83-ac9d-6ea47335a9c3 2021-03-15 16:53:55.740856+01 2023-09-05 23
--
COPY public.user_usager (uuid, "createdAt", "updatedAt", version, id, "usagerUUID", "structureId", login, password, salt, "isTemporaryPassword", "lastLogin", "passwordLastUpdate", "lastPasswordResetDate", "lastPasswordResetStructureUser", enabled) FROM stdin;
a03a9a49-ae31-4160-9879-bab02dc46361 2021-11-30 14:50:26.278073+01 2021-11-30 15:01:39.89434+01 10 2 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1 LNQIFFBK $2a$10$u..ofvcb5t5a44N0Oriwbew6oz600aoOFzll9yl92Lx1VOb0E8o0a $2a$10$zmuPXxUOuQJ7nE6ag4.x6e f 2021-11-30 15:01:39.893+01 2021-11-30 15:01:39.675+01 2021-11-30 14:50:26.275+01 {"userId": 1, "userName": "Patrick Roméro"} t
a657f4bd-e4d1-4c38-bdd0-ffd268b356df 2021-10-05 11:34:41.369505+02 2021-11-30 15:02:07.705535+01 18 1 b2c26e55-ab37-457d-b307-6fe161050a9b 1 WKYJBDXS $2a$10$cmwXTghEgrGRYTkGIJlj3Owjo8YVVDXOgceAGtkgzcD2M/Tf3Dhb. $2a$10$/uFyiR8hCe8XkRe7x2fYm. t 2021-11-30 15:02:07.69+01 \N 2021-10-05 11:34:41.365+02 {"userId": 1, "userName": "Patrick Roméro"} t
a03a9a49-ae31-4160-9879-bab02dc46361 2021-11-30 14:50:26.278073+01 2023-11-20 16:40:58.362384+01 17 2 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1 LNQIFFBK $2a$10$u..ofvcb5t5a44N0Oriwbew6oz600aoOFzll9yl92Lx1VOb0E8o0a $2a$10$zmuPXxUOuQJ7nE6ag4.x6e f 2023-11-20 16:40:58.36+01 2021-11-30 15:01:39.675+01 2021-11-30 14:50:26.275+01 {"userId": 1, "userName": "Patrick Roméro"} t
\.
Expand All @@ -292,6 +292,8 @@ a657f4bd-e4d1-4c38-bdd0-ffd268b356df 2021-10-05 11:34:41.369505+02 2021-11-30 15
--
COPY public.user_usager_login (uuid, "createdAt", "updatedAt", version, "usagerUUID", "structureId") FROM stdin;
3927beaf-04d8-4cbb-9be7-c7c9086434b3 2023-11-20 16:40:44.4121+01 2023-11-20 16:40:44.4121+01 1 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1
e80dafea-8fe4-4792-92b9-3608c723aa1b 2023-11-20 16:40:58.289642+01 2023-11-20 16:40:58.289642+01 1 97b7e840-0e93-4bf4-ba7d-0a406aa898f2 1
\.
Expand All @@ -300,8 +302,8 @@ COPY public.user_usager_login (uuid, "createdAt", "updatedAt", version, "usagerU
--
COPY public.user_usager_security (uuid, "createdAt", "updatedAt", version, "userId", "structureId", "eventsHistory") FROM stdin;
fadd55b6-ca41-48d4-bd56-238b1a3c6f7b 2021-11-30 14:50:26.290488+01 2021-11-30 15:01:39.66811+01 8 2 1 [{"date": "2021-11-30T13:50:38.357Z", "type": "login-success"}, {"date": "2021-11-30T13:51:20.351Z", "type": "login-success"}, {"date": "2021-11-30T13:51:20.608Z", "type": "change-password-success"}, {"date": "2021-11-30T14:00:55.533Z", "type": "login-error"}, {"date": "2021-11-30T14:01:21.310Z", "type": "login-success"}, {"date": "2021-11-30T14:01:39.398Z", "type": "login-success"}, {"date": "2021-11-30T14:01:39.667Z", "type": "change-password-success"}]
9bc8decb-5f78-48de-8c1b-9f61ea5acfba 2021-10-05 11:34:41.388922+02 2021-11-30 15:02:07.682311+01 3 1 1 [{"date": "2021-11-30T14:02:07.680Z", "type": "login-success"}]
fadd55b6-ca41-48d4-bd56-238b1a3c6f7b 2021-11-30 14:50:26.290488+01 2023-11-20 16:40:58.2674+01 10 2 1 [{"date": "2023-11-20T15:40:44.378Z", "type": "login-success"}, {"date": "2023-11-20T15:40:58.265Z", "type": "login-success"}]
\.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ export class PortailUsagersLoginController {
id: user.structureId,
});

await userUsagerLoginRepository.save(
const lastLogin = await userUsagerLoginRepository.save(
new UserUsagerLoginTable({
usagerUUID: usager.uuid,
structureId: usager.structureId,
})
);

if (structure.portailUsager.usagerLoginUpdateLastInteraction) {
usager.lastInteraction.dateInteraction = new Date();
usager.lastInteraction.dateInteraction = lastLogin.createdAt;
await usagerRepository.update(
{ uuid: usager.uuid },
{ lastInteraction: usager.lastInteraction }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,10 @@ describe("interactionRepository", () => {
});

it("get all interactions", async () => {
{
const interactions = await interactionRepository.findBy({
structureId: 1,
usagerRef: 7,
});

expect(interactions.length).toEqual(9);
}
{
const interactions = await interactionRepository.findBy({
structureId: 1,
usagerRef: 7,
});

expect(interactions.length).toBeGreaterThan(2);
}
const interactions = await interactionRepository.findBy({
structureId: 1,
usagerRef: 7,
});
expect(interactions.length).toEqual(8);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
import { InteractionsTable } from "../../entities";
import { myDataSource } from "../_postgres";
import { InteractionType } from "@domifa/common";
import { userUsagerLoginRepository } from "../user-usager";
import { differenceInMinutes, parseISO } from "date-fns";

export const interactionRepository = myDataSource
.getRepository<Interactions>(InteractionsTable)
Expand Down Expand Up @@ -122,13 +124,6 @@ async function countPendingInteractionsIn({
lastInteractionOut: Date | null;
}> {
const INTERACTIONS_TO_CHECK = Object.assign([], INTERACTION_OK_LIST);

if (structure.portailUsager.usagerLoginUpdateLastInteraction) {
INTERACTIONS_TO_CHECK.push("loginPortail");
}

const inArray = INTERACTIONS_TO_CHECK.join(", ");

// NOTE: cette requête ne renvoit pas de résultats pour les usagers de cette structure qui n'ont pas d'interaction
const query = `SELECT
coalesce (SUM(CASE WHEN i.type = 'courrierIn' THEN "nbCourrier" END), 0) AS "courrierIn",
Expand All @@ -138,31 +133,52 @@ async function countPendingInteractionsIn({
FROM interactions i
WHERE i."usagerUUID" = $2 AND i."interactionOutUUID" is null
GROUP BY i."usagerRef"`;

const results = await interactionRepository.query(query, [
inArray,
INTERACTIONS_TO_CHECK.join(", "),
usagerUUID,
]);

const defaultResult = {
courrierIn: 0,
recommandeIn: 0,
colisIn: 0,
lastInteractionOut: null,
};

if (typeof results[0] === "undefined") {
return defaultResult;
if (
typeof results[0] === "undefined" ||
results[0] === null ||
results[0]?.length === 0
) {
return {
courrierIn: 0,
recommandeIn: 0,
colisIn: 0,
lastInteractionOut: null,
};
}

if (results[0] === null || results[0].length === 0) {
return defaultResult;
let lastInteractionOut: Date | null =
results[0].lastInteractionOut !== null
? results[0].lastInteractionOut === "string"
? parseISO(results[0].lastInteractionOut)
: results[0].lastInteractionOut
: null;

if (structure.portailUsager.usagerLoginUpdateLastInteraction) {
const lastUserUsagerLogin = await userUsagerLoginRepository.findOne({
where: {
usagerUUID,
},
order: { createdAt: "DESC" },
});

if (
differenceInMinutes(lastInteractionOut, lastUserUsagerLogin.createdAt) > 0
) {
lastInteractionOut = lastUserUsagerLogin.createdAt;
}
}

return {
courrierIn: parseInt(results[0].courrierIn, 10),
recommandeIn: parseInt(results[0].recommandeIn, 10),
colisIn: parseInt(results[0].colisIn, 10),
lastInteractionOut: results[0].lastInteractionOut,
lastInteractionOut,
};
}

Expand Down Expand Up @@ -263,7 +279,11 @@ async function totalInteractionAllUsagersStructure({

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

// TODO: add loginPortail
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: any) => ({
usagerRef: x.usagerRef,
courrierIn: parseInt(x.courrierIn, 10),
Expand All @@ -274,7 +294,7 @@ async function totalInteractionAllUsagersStructure({
colisOut: parseInt(x.colisOut, 10),
appel: parseInt(x.appel, 10),
visite: parseInt(x.visite, 10),
loginPortail: 0,
loginPortail: loginPortailStats,
npai: parseInt(x.npai, 10),
}));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AppTestHelper } from "../../../util/test";
import { userUsagerLoginRepository } from "./userUsagerLoginRepository.service";

describe("userUsagerLoginRepository", () => {
beforeAll(async () => {
await AppTestHelper.bootstrapTestConnection();
});

afterAll(async () => {
await AppTestHelper.tearDownTestConnection();
});

it("Count all logins made", async () => {
const users = await userUsagerLoginRepository.count();
expect(users).toBeGreaterThan(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("Usagers Controller", () => {
expect(usager.ayantsDroits[0].nom).toEqual(
exceptedResponse.ayantsDroits[0].nom
);
console.log("XXX");

expect(usager.ayantsDroits[0].prenom).toEqual(
exceptedResponse.ayantsDroits[0].prenom
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ComponentFixture, TestBed } from "@angular/core/testing";

import { ProfilHistoriqueLoginPortailComponent } from "./profil-historique-login-portail.component";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { NoopAnimationsModule } from "@angular/platform-browser/animations";
import { StoreModule } from "@ngrx/store";
import { _usagerReducer } from "../../../../../shared";
import { UsagerFormModel } from "../../../../usager-shared/interfaces";
import { SharedModule } from "../../../../shared/shared.module";

describe("ProfilHistoriqueLoginPortailComponent", () => {
let component: ProfilHistoriqueLoginPortailComponent;
Expand All @@ -9,10 +15,17 @@ describe("ProfilHistoriqueLoginPortailComponent", () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ProfilHistoriqueLoginPortailComponent],
imports: [
HttpClientTestingModule,
StoreModule.forRoot({ app: _usagerReducer }),
NoopAnimationsModule,
SharedModule,
],
}).compileComponents();

fixture = TestBed.createComponent(ProfilHistoriqueLoginPortailComponent);
component = fixture.componentInstance;
component.usager = new UsagerFormModel();
fixture.detectChanges();
});

Expand Down

0 comments on commit bfa5dff

Please sign in to comment.