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

fix(interactions): update lastinteraction date #3137

Merged
merged 1 commit into from
Jan 2, 2024
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
Original file line number Diff line number Diff line change
@@ -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<void> {
appLogger.info(`Update Migration variable`);

await usagerRepository.update({ migrated: true }, { migrated: false });
}

public async down(_queryRunner: QueryRunner): Promise<void> {
//}
}
}
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
//}
}

private async countMigration(): Promise<number> {
return await usagerRepository.createQueryBuilder().where(QUERY).getCount();
}
}
Original file line number Diff line number Diff line change
@@ -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"));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<Date | null> => {
if (
(interaction && INTERACTION_OK_LIST.includes(interaction.type)) ||
Expand All @@ -41,15 +39,12 @@ const getDateFromInteraction = async (
return null;
};

const getDateFromUserLogin = async (
export const getDateFromUserLogin = async (
usager: Usager,
structure: Pick<Structure, "id" | "sms" | "telephone" | "portailUsager">,
structure: Pick<Structure, "id" | "portailUsager">,
dateInteraction: Date | null
): Promise<Date | null> => {
if (
structure.portailUsager.usagerLoginUpdateLastInteraction &&
usager.options.portailUsagerEnabled
) {
if (structure.portailUsager.usagerLoginUpdateLastInteraction) {
const lastUserUsagerLogin = await userUsagerLoginRepository.findOne({
where: { usagerUUID: usager.uuid },
select: { createdAt: true },
Expand All @@ -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;
};
Loading