From 471fc79ff2ee0688d7e6da9c2cd724a9e2682339 Mon Sep 17 00:00:00 2001 From: "Yassine R." Date: Tue, 2 Jan 2024 15:57:33 +0100 Subject: [PATCH] fix(interactions): update lastinteraction date --- ...1703676787762-update-migrated-migration.ts | 18 +++ ...76787763-fix-last-interaction-migration.ts | 123 ++++++++++++++++++ .../getLastInteractionDate.service.spec.ts | 25 ++++ .../getLastInteractionDate.service.ts | 36 +++-- 4 files changed, 183 insertions(+), 19 deletions(-) create mode 100644 packages/backend/src/_migrations/1703676787762-update-migrated-migration.ts create mode 100644 packages/backend/src/_migrations/1703676787763-fix-last-interaction-migration.ts create mode 100644 packages/backend/src/interactions/services/getLastInteractionDate.service.spec.ts diff --git a/packages/backend/src/_migrations/1703676787762-update-migrated-migration.ts b/packages/backend/src/_migrations/1703676787762-update-migrated-migration.ts new file mode 100644 index 0000000000..9b8d8772bc --- /dev/null +++ b/packages/backend/src/_migrations/1703676787762-update-migrated-migration.ts @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { MigrationInterface, QueryRunner } from "typeorm"; +import { usagerRepository } from "../database"; +import { appLogger } from "../util"; + +export class FixLastInteractionMigration1703676787762 + implements MigrationInterface +{ + public async up(_queryRunner: QueryRunner): Promise { + appLogger.info(`Update Migration variable`); + + await usagerRepository.update({ migrated: true }, { migrated: false }); + } + + public async down(_queryRunner: QueryRunner): Promise { + //} + } +} diff --git a/packages/backend/src/_migrations/1703676787763-fix-last-interaction-migration.ts b/packages/backend/src/_migrations/1703676787763-fix-last-interaction-migration.ts new file mode 100644 index 0000000000..7576f2ba90 --- /dev/null +++ b/packages/backend/src/_migrations/1703676787763-fix-last-interaction-migration.ts @@ -0,0 +1,123 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { In, MigrationInterface, QueryRunner } from "typeorm"; +import { structureRepository, usagerRepository } from "../database"; +import { appLogger } from "../util"; +import { Usager } from "../_common/model"; +import { + getDateFromInteraction, + getDateFromUserLogin, +} from "../interactions/services/getLastInteractionDate.service"; +import { differenceInCalendarDays, max } from "date-fns"; + +const QUERY = `decision->>'statut' = 'VALIDE' AND migrated is false`; +const usagersUpdated = []; + +export class FixLastInteractionMigration1703676787763 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + appLogger.info(`[MIGRATION] nettoyage des dates de dernier passage`); + + let cpt = 0; + const total = await this.countMigration(); + + const structures = await structureRepository.find({ + select: ["uuid", "id", "nom", "portailUsager"], + }); + + const structuresMap = {}; + structures.forEach((structure) => { + structuresMap[structure.id.toString()] = structure; + }); + + while ((await this.countMigration()) > 0) { + appLogger.info(`${cpt}/${total} usagers`); + + const usagers: Usager[] = await usagerRepository + .createQueryBuilder("usager") + .where(QUERY) + .limit(1000) + .getMany(); + + await queryRunner.startTransaction(); + const uuidsToEdit = usagers.map((usager) => usager.uuid); + + console.log(cpt + "/" + total + " dossiers"); + + for (const usager of usagers) { + usager.lastInteraction.dateInteraction = new Date( + usager.lastInteraction.dateInteraction + ); + + let dateInteractionOut = await getDateFromInteraction(usager, null); + const dateLogin = await getDateFromUserLogin( + usager, + structuresMap[usager.structureId.toString()], + null + ); + + if (dateInteractionOut && dateLogin) { + dateInteractionOut = max([dateInteractionOut, dateLogin]); + } + + if ( + dateInteractionOut && + differenceInCalendarDays( + new Date(usager.lastInteraction.dateInteraction), + dateInteractionOut + ) !== 0 && + differenceInCalendarDays( + dateInteractionOut, + new Date(usager.decision.dateDebut) + ) > 0 + ) { + const log = { + structureId: usager.structureId, + before: usager.lastInteraction.dateInteraction, + after: dateInteractionOut, + dateLogin, + dateDebut: usager.decision.dateDebut, + dateDiff: differenceInCalendarDays( + new Date(usager.lastInteraction.dateInteraction), + dateInteractionOut + ), + }; + + usagersUpdated.push(log); + + await usagerRepository.update( + { uuid: usager.uuid }, + { + lastInteraction: { + ...usager.lastInteraction, + dateInteraction: dateInteractionOut, + }, + + migrated: true, + } + ); + + console.log(); + } + } + cpt = cpt + 1000; + + await usagerRepository.update( + { uuid: In(uuidsToEdit) }, + { migrated: true } + ); + + await queryRunner.commitTransaction(); + } + console.log(usagersUpdated); + console.log(usagersUpdated.length); + } + + public async down(_queryRunner: QueryRunner): Promise { + //} + } + + private async countMigration(): Promise { + return await usagerRepository.createQueryBuilder().where(QUERY).getCount(); + } +} diff --git a/packages/backend/src/interactions/services/getLastInteractionDate.service.spec.ts b/packages/backend/src/interactions/services/getLastInteractionDate.service.spec.ts new file mode 100644 index 0000000000..68d34e0aa4 --- /dev/null +++ b/packages/backend/src/interactions/services/getLastInteractionDate.service.spec.ts @@ -0,0 +1,25 @@ +import { getMostRecentInteractionDate } from "./getLastInteractionDate.service"; + +describe("getMostRecentInteractionDate", () => { + it("'dateDebut' > dateInteraction, on renvoie date de début de décision", () => { + const usager: any = { + decision: { + dateDebut: new Date("2026-08-29T13:24:56.563Z"), + }, + }; + expect( + getMostRecentInteractionDate(usager, new Date("2021-08-29T13:24:56.563Z")) + ).toEqual(new Date("2026-08-29T13:24:56.563Z")); + }); + + it("'dateDebut' < dateInteraction, on renvoie date d'interaction", () => { + const usager: any = { + decision: { + dateDebut: new Date("2026-08-29T13:24:56.563Z"), + }, + }; + expect( + getMostRecentInteractionDate(usager, new Date("2029-08-29T13:24:56.563Z")) + ).toEqual(new Date("2029-08-29T13:24:56.563Z")); + }); +}); diff --git a/packages/backend/src/interactions/services/getLastInteractionDate.service.ts b/packages/backend/src/interactions/services/getLastInteractionDate.service.ts index f3acb35a88..058abda812 100644 --- a/packages/backend/src/interactions/services/getLastInteractionDate.service.ts +++ b/packages/backend/src/interactions/services/getLastInteractionDate.service.ts @@ -20,14 +20,12 @@ export const getLastInteractionOut = async ( dateInteraction ); - return shouldReturnDateInteraction(usager, dateInteraction) - ? dateInteraction - : usager.decision.dateDebut; + return getMostRecentInteractionDate(usager, dateInteraction); }; -const getDateFromInteraction = async ( +export const getDateFromInteraction = async ( usager: Usager, - interaction: CommonInteraction + interaction?: CommonInteraction ): Promise => { if ( (interaction && INTERACTION_OK_LIST.includes(interaction.type)) || @@ -41,15 +39,12 @@ const getDateFromInteraction = async ( return null; }; -const getDateFromUserLogin = async ( +export const getDateFromUserLogin = async ( usager: Usager, - structure: Pick, + structure: Pick, dateInteraction: Date | null ): Promise => { - if ( - structure.portailUsager.usagerLoginUpdateLastInteraction && - usager.options.portailUsagerEnabled - ) { + if (structure.portailUsager.usagerLoginUpdateLastInteraction) { const lastUserUsagerLogin = await userUsagerLoginRepository.findOne({ where: { usagerUUID: usager.uuid }, select: { createdAt: true }, @@ -71,15 +66,18 @@ const getDateFromUserLogin = async ( return dateInteraction; }; -const shouldReturnDateInteraction = ( +export const getMostRecentInteractionDate = ( usager: Usager, dateInteraction: Date | null -): boolean => { - return ( +): Date => { + const dateDebut = new Date(usager.decision.dateDebut); + + // Si la date d'interaction précède la dernière décision, on affecte la date de début de la décision + if ( dateInteraction && - differenceInCalendarDays( - dateInteraction, - new Date(usager.decision.dateDebut) - ) > 0 - ); + differenceInCalendarDays(dateInteraction, dateDebut) > 0 + ) { + return dateInteraction; + } + return dateDebut; };