From f11e1f2a5bed0e027a76f256f117934abcfa4d2c Mon Sep 17 00:00:00 2001 From: Yassine R Date: Wed, 6 Sep 2023 21:56:11 +0200 Subject: [PATCH] feat(import): migration pour activer les SMS de Bordeaux --- .../1684358283099-manual-migration.ts | 120 ++++++++++++++++++ ...222559-delete-corrupted-files-migration.ts | 19 --- 2 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 packages/backend/src/_migrations/1684358283099-manual-migration.ts delete mode 100644 packages/backend/src/_migrations/1693919222559-delete-corrupted-files-migration.ts diff --git a/packages/backend/src/_migrations/1684358283099-manual-migration.ts b/packages/backend/src/_migrations/1684358283099-manual-migration.ts new file mode 100644 index 0000000000..e32bc49211 --- /dev/null +++ b/packages/backend/src/_migrations/1684358283099-manual-migration.ts @@ -0,0 +1,120 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; +import { Usager, UsagerOptions, UserStructureProfile } from "../_common/model"; +import { + usagerRepository, + userStructureRepository, + userUsagerRepository, +} from "../database"; +import { userUsagerCreator } from "../users/services"; +import { format } from "date-fns"; +import { getPhoneString } from "../util/phone"; +import { join } from "path"; +import { domifaConfig } from "../config"; +import { writeFile } from "fs-extra"; + +export class ManualMigration1684358283099 implements MigrationInterface { + public async up(): Promise { + if ( + domifaConfig().envId === "preprod" || + domifaConfig().envId === "prod" || + domifaConfig().envId === "local" + ) { + const message = "[MIGRATION] [BORDEAUX] "; + console.log(message + "Suppression des comptes usagers de Bordeaux"); + + await userUsagerRepository.delete({ + structureId: 201, + }); + + console.log(message + "Activation des sms pour les usagers de bordeaux"); + + await usagerRepository + .createQueryBuilder("usager") + .update({ + contactByPhone: true, + }) + .where( + `telephone->>'numero' != '' and decision->>'statut' = :statut and "structureId" = :structureId `, + { structureId: 201, statut: "VALIDE" } + ) + .execute(); + + console.log(message + "Activation des comptes usagers de bordeaux"); + + const jsonToExport: { + ref: number; + telephone: string; + nom: string; + prenom: string; + login: string; + temporaryPassword: string; + customRef: string; + dateNaissance: string; + }[] = []; + + const user: UserStructureProfile = + await userStructureRepository.findOneBy({ + role: "admin", + structureId: 201, + }); + + const usagers: Partial[] = await usagerRepository + .createQueryBuilder("usager") + .where( + `decision->>'statut' = :statut and "structureId" = :structureId `, + { structureId: 201, statut: "VALIDE" } + ) + .select( + `"uuid", "structureId", "dateNaissance", "telephone", "nom", "prenom", "customRef", "ref", "options"` + ) + .execute(); + + console.log(message + usagers.length + " comptes usagers à créer"); + + let cpt = 0; + for (const usager of usagers) { + const { login, temporaryPassword } = + await userUsagerCreator.createUserWithTmpPassword( + { + usagerUUID: usager.uuid, + structureId: usager.structureId, + }, + { creator: user } + ); + + const options: UsagerOptions = { + ...usager.options, + portailUsagerEnabled: true, + }; + + await usagerRepository.update({ uuid: usager.uuid }, { options }); + + const newUserUsager = { + ref: usager.ref, + customRef: usager.customRef, + nom: usager.nom, + prenom: usager.prenom, + dateNaissance: format(new Date(usager.dateNaissance), "dd/MM/yyyy"), + telephone: getPhoneString(usager.telephone), + login, + temporaryPassword, + }; + jsonToExport.push(newUserUsager); + + cpt++; + if (cpt % 200 === 0) { + console.log(message + cpt + "/" + usagers.length + " comptes créés"); + } + } + console.log(message + " écriture du fichier"); + + await writeFile( + join(domifaConfig().upload.basePath, "export_bordeaux.json"), + JSON.stringify(jsonToExport) + ); + } + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function + public async down(_queryRunner: QueryRunner): Promise {} +} diff --git a/packages/backend/src/_migrations/1693919222559-delete-corrupted-files-migration.ts b/packages/backend/src/_migrations/1693919222559-delete-corrupted-files-migration.ts deleted file mode 100644 index f925b5d076..0000000000 --- a/packages/backend/src/_migrations/1693919222559-delete-corrupted-files-migration.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; - -export class ManualMigration1693919222559 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - const docsToDelete = await queryRunner.query( - `SELECT COUNT(uuid) FROM "usager_docs" where "encryptionContext" IS NULL` - ); - - console.log(docsToDelete[0]?.count + " docs to delete"); - await queryRunner.query( - `DELETE FROM "usager_docs" where "encryptionContext" IS NULL` - ); - } - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - public async down(_queryRunner: QueryRunner): Promise { - // Nothing - } -}