Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat migrate login portail #3056

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
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: packages/backend/src/_migrations/1699910877010-auto-migration.ts
checksum: bd05dd6b68f7fe2ca36ab1180a0449878cf78f4e048d2e5979b3dbe5fba1db53
- filename: packages/backend/src/_migrations/_init-db/1603812391580-pr-env-create-database.ts
checksum: 586e3b26481d71256a1b7626956aaf9e68211dfbda527dd862a2aad075b5cc04
- filename: yarn.lock
checksum: 4ac842f95592d194e65b208fdc4b9a78a0bb6470ad2d4950626c2ffcec831a33
version: "1.0"
Binary file modified _scripts/db/dumps/domifa_test.postgres.custom.gz
Binary file not shown.
18 changes: 14 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,18 @@ 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
\.


--
-- Data for Name: user_usager_login; Type: TABLE DATA; Schema: public; Owner: -
--

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 @@ -266,8 +276,8 @@ a657f4bd-e4d1-4c38-bdd0-ffd268b356df 2021-10-05 11:34:41.369505+02 2021-11-30 15
--

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,18 @@ 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
\.


--
-- Data for Name: user_usager_login; Type: TABLE DATA; Schema: public; Owner: -
--

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 @@ -292,8 +302,8 @@ a657f4bd-e4d1-4c38-bdd0-ffd268b356df 2021-10-05 11:34:41.369505+02 2021-11-30 15
--

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 @@ -10,7 +10,6 @@ export const ALL_INTERACTION_TYPES: InteractionType[] = [
"appel",
"visite",
"npai",
"loginPortail",
];

export const INTERACTION_OK_LIST: InteractionType[] = [
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MessageSmsId } from "@domifa/common";
import {
MessageSmsInteractionMetas,
MessageSmsId,
MessageSmsReminderMetas,
MessageSmsStatus,
} from ".";
MessageSmsInteractionMetas,
} from "@domifa/common";
import { AppEntity } from "../../../_common/model";

export type MessageSms = AppEntity & {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions packages/backend/src/_common/model/message-sms/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
//@index('./*', f => `export * from '${f.path}'`)
export * from "./MESSAGE_SMS_RESPONSE_ERRORS.const";
export * from "./MESSAGE_SMS_STATUS.const";
export * from "./MESSAGE_SMS_SUIVI_INDEX.const";
export * from "./MessageSms.type";
export * from "./MessageSmsInteractionMetas.type";
export * from "./MessageSmsReminderMetas.type";
export * from "./MessageSmsSendResponse.type";
export * from "./MessageSmsStatus.type";
export * from "./SuiviSmsErrors.type";
38 changes: 38 additions & 0 deletions packages/backend/src/_migrations/1699910877010-auto-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { domifaConfig } from "../config";

export class AutoMigration1699910877010 implements MigrationInterface {
name = "AutoMigration1699910877010";
public async up(queryRunner: QueryRunner): Promise<void> {
if (
domifaConfig().envId !== "preprod" &&
domifaConfig().envId === "prod" &&
domifaConfig().envId === "local"
) {
await queryRunner.query(
`CREATE TABLE "user_usager_login" ("uuid" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "version" integer NOT NULL, "usagerUUID" uuid NOT NULL, "structureId" integer NOT NULL, CONSTRAINT "PK_cfb7dc4a81d1db054ab5b4d50bf" PRIMARY KEY ("uuid"))`
);
await queryRunner.query(
`CREATE INDEX "idx_user_usager_login" ON "user_usager_login" ("structureId", "usagerUUID") `
);
await queryRunner.query(
`ALTER TABLE "user_usager_login" ADD CONSTRAINT "FK_4bf76763fec5203f945338a0377" FOREIGN KEY ("usagerUUID") REFERENCES "usager"("uuid") ON DELETE CASCADE ON UPDATE NO ACTION`
);
await queryRunner.query(
`ALTER TABLE "user_usager_login" ADD CONSTRAINT "FK_8722e56ff917692645abcd29e7c" FOREIGN KEY ("structureId") REFERENCES "structure"("id") ON DELETE CASCADE ON UPDATE NO ACTION`
);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "user_usager_login" DROP CONSTRAINT "FK_8722e56ff917692645abcd29e7c"`
);
await queryRunner.query(
`ALTER TABLE "user_usager_login" DROP CONSTRAINT "FK_4bf76763fec5203f945338a0377"`
);

await queryRunner.query(`DROP INDEX "public"."idx_user_usager_login"`);
await queryRunner.query(`DROP TABLE "user_usager_login"`);
}
}
59 changes: 59 additions & 0 deletions packages/backend/src/_migrations/1699913113875-manual-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { In, MigrationInterface, QueryRunner } from "typeorm";
import {
UserUsagerLoginTable,
interactionRepository,
userUsagerLoginRepository,
} from "../database";

export class ManualMigration1699913113875 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
let i = 0;
const total = await interactionRepository.countBy({
type: "loginPortail" as any,
});

const batch = 25000;
while (
(await interactionRepository.countBy({ type: "loginPortail" as any })) > 0
) {
const uuidsToDelete = [];
const interactions = await interactionRepository.find({
where: { type: "loginPortail" as any },
take: batch,
select: {
dateInteraction: true,
uuid: true,
usagerUUID: true,
structureId: true,
updatedAt: true,
},
});

await queryRunner.startTransaction();
try {
for (const interaction of interactions) {
const userUsagerLogin = new UserUsagerLoginTable();
userUsagerLogin.usagerUUID = interaction.usagerUUID;
userUsagerLogin.structureId = interaction.structureId;
userUsagerLogin.createdAt = interaction.dateInteraction;
userUsagerLogin.updatedAt = interaction.updatedAt;
await userUsagerLoginRepository.save(userUsagerLogin);
uuidsToDelete.push(interaction.uuid);
}

await interactionRepository.delete({ uuid: In(uuidsToDelete) });
await queryRunner.commitTransaction();
i = i + batch;
console.log(`${i}/${total} interactions migrated`);
} catch (err) {
await queryRunner.rollbackTransaction();
throw err;
}
}
}

public async down(_queryRunner: QueryRunner): Promise<void> {
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ async function createTables(queryRunner: QueryRunner) {
CREATE INDEX "IDX_fa4dea9a1ff8deb8fcf47c451e" ON public.structure USING btree (departement);


-- public.user_usager_login definition

-- Drop table

-- DROP TABLE public.user_usager_login;

CREATE UNLOGGED TABLE public.user_usager_login (
uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
"createdAt" timestamptz NOT NULL DEFAULT now(),
"updatedAt" timestamptz NOT NULL DEFAULT now(),
"version" int4 NOT NULL,
"usagerUUID" uuid NOT NULL,
"structureId" int4 NOT NULL,
CONSTRAINT "PK_cfb7dc4a81d1db054ab5b4d50bf" PRIMARY KEY (uuid)
);
CREATE INDEX idx_user_usager_login ON public.user_usager_login USING btree ("structureId", "usagerUUID");


-- public.message_sms definition

-- Drop table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
typeOrmSearch,
usagerRepository,
UsagerTable,
userUsagerLoginRepository,
} from "../../../database";
import { usagerDocsRepository } from "../../../database/services/usager/usagerDocsRepository.service";
import { StatsDeploiementExportModel } from "../../../excel/export-stats-deploiement";
Expand Down Expand Up @@ -140,7 +141,7 @@ export class AdminStructuresService {
}

public async getInteractionsCountByTypeMap(): Promise<{
[statut in InteractionType]: number;
[statut in InteractionType | "loginPortail"]: number;
}> {
return {
courrierIn: await this.totalInteractions("courrierIn"),
Expand All @@ -152,7 +153,7 @@ export class AdminStructuresService {
appel: await this.totalInteractions("appel"),
visite: await this.totalInteractions("visite"),
npai: await this.totalInteractions("npai"),
loginPortail: await this.totalInteractions("loginPortail"),
loginPortail: await userUsagerLoginRepository.count(),
};
}

Expand Down Expand Up @@ -370,7 +371,6 @@ export class AdminStructuresService {
if (
interactionType === "appel" ||
interactionType === "visite" ||
interactionType === "loginPortail" ||
interactionType === "npai"
) {
return interactionRepository.count({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import {
} from "@nestjs/common";
import { ApiTags } from "@nestjs/swagger";
import {
UserUsagerLoginTable,
usagerRepository,
userUsagerLoginRepository,
userUsagerSecurityPasswordChecker,
} from "../../database";
import { UsagerLoginDto } from "../../users/dto";
import { ExpressResponse } from "../../util/express";
import {
PortailUsagerAuthApiResponse,
PortailUsagerProfile,
UserStructure,
} from "../../_common/model";
import { portailUsagerProfilBuilder } from "../portail-usager-profil/services/portail-usager-profil-builder.service";
import { UsagersAuthService } from "./services/usagers-auth.service";
import { interactionsCreator } from "../../interactions/services";

@Controller("portail-usagers/auth")
@ApiTags("auth")
Expand Down Expand Up @@ -60,26 +60,20 @@ export class PortailUsagersLoginController {
id: user.structureId,
});

const userStructure: Pick<
UserStructure,
"id" | "structureId" | "nom" | "prenom" | "structure"
> = {
id: 0,
nom: usager.nom,
prenom: usager.prenom,
structureId: user.structureId,
structure,
};
const lastLogin = await userUsagerLoginRepository.save(
new UserUsagerLoginTable({
usagerUUID: usager.uuid,
structureId: usager.structureId,
})
);

// Création d'une interaction avec la date de connexion
await interactionsCreator.createInteraction({
interaction: {
type: "loginPortail",
nbCourrier: 0,
},
usager,
user: userStructure,
});
if (structure.portailUsager.usagerLoginUpdateLastInteraction) {
usager.lastInteraction.dateInteraction = lastLogin.createdAt;
await usagerRepository.update(
{ uuid: usager.uuid },
{ lastInteraction: usager.lastInteraction }
);
}

const response: PortailUsagerAuthApiResponse = {
token: access_token,
Expand Down
Loading
Loading