-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(interactions): update lastinteraction date
- Loading branch information
Showing
4 changed files
with
183 additions
and
19 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/backend/src/_migrations/1703676787762-update-migrated-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
//} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
packages/backend/src/_migrations/1703676787763-fix-last-interaction-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/backend/src/interactions/services/getLastInteractionDate.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters