From c0cc84040cea7c8db335c0c8e80e7062565c75df Mon Sep 17 00:00:00 2001 From: "Yassine R." Date: Tue, 7 Nov 2023 01:22:28 +0100 Subject: [PATCH] fix: add notes --- .../src/_common/model/usager/Usager.type.ts | 5 +- .../_common/tmp-toulouse/getDateFromXml.ts | 13 ++++ ...90702-import-toulouse-usagers-migration.ts | 60 +++++++++++++++++-- ...1697659880952-import-passages-migration.ts | 3 +- .../entities/usager/UsagerTable.typeorm.ts | 5 +- .../services/usagersCreator.service.ts | 2 +- 6 files changed, 79 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/_common/model/usager/Usager.type.ts b/packages/backend/src/_common/model/usager/Usager.type.ts index 6ae87afeaa..91adb6a262 100644 --- a/packages/backend/src/_common/model/usager/Usager.type.ts +++ b/packages/backend/src/_common/model/usager/Usager.type.ts @@ -40,7 +40,10 @@ export type Usager = AppEntity & { etapeDemande: number; rdv: UsagerRdv | null; entretien: UsagerEntretien; - pinnedNote: Partial | null; + pinnedNote: Pick< + UsagerNote, + "message" | "usagerRef" | "createdAt" | "createdBy" + > | null; // INFOS DOMICILIATION typeDom: UsagerTypeDom; diff --git a/packages/backend/src/_common/tmp-toulouse/getDateFromXml.ts b/packages/backend/src/_common/tmp-toulouse/getDateFromXml.ts index de35678513..f6641d2130 100644 --- a/packages/backend/src/_common/tmp-toulouse/getDateFromXml.ts +++ b/packages/backend/src/_common/tmp-toulouse/getDateFromXml.ts @@ -1,4 +1,6 @@ +import { isNil } from "lodash"; import { isValid, parse, startOfDay } from "date-fns"; +import striptags from "striptags"; export const getDateFromXml = (dateString: string | number): Date => { const parsedDate = startOfDay( @@ -9,3 +11,14 @@ export const getDateFromXml = (dateString: string | number): Date => { } return parsedDate; }; + +export const getText = (str?: string): string => { + if (isNil(str)) { + return ""; + } + + return striptags(str.toString()) + .replace(/[\\$~*<>{}]/gi, "") + .replace(/\s+/g, " ") + .trim(); +}; diff --git a/packages/backend/src/_migrations/1692191990702-import-toulouse-usagers-migration.ts b/packages/backend/src/_migrations/1692191990702-import-toulouse-usagers-migration.ts index be6151333b..6c4b7ce07d 100644 --- a/packages/backend/src/_migrations/1692191990702-import-toulouse-usagers-migration.ts +++ b/packages/backend/src/_migrations/1692191990702-import-toulouse-usagers-migration.ts @@ -8,7 +8,13 @@ import { StructureLight, UsagerTypeDom, } from "../_common/model"; -import { myDataSource, usagerRepository, UsagerTable } from "../database"; +import { + myDataSource, + usagerNotesRepository, + UsagerNotesTable, + usagerRepository, + UsagerTable, +} from "../database"; import { usagersCreator } from "../usagers/services"; import { domifaConfig } from "../config"; @@ -20,6 +26,7 @@ import { TUsager, PAYS, getDateFromXml, + getText, } from "../_common/tmp-toulouse"; import { tmpHistoriqueRepository } from "../database/services/interaction/historiqueRepository.service"; @@ -139,7 +146,7 @@ export class ManualMigration1692191990702 implements MigrationInterface { : [], dateNaissance: getDateFromXml(usagerToulouse.date_naissance), customRef: - usagerToulouse?.Num_domici.toString() ?? + usagerToulouse?.Num_domici?.toString() ?? usagerToulouse.IDDomicilie.toString(), lastInteraction: { dateInteraction: lastInteractionDate, @@ -155,7 +162,7 @@ export class ManualMigration1692191990702 implements MigrationInterface { dateDecision, statut, userName: "DomiFa", - userId: 1200, + userId: 1, dateFin, dateDebut, typeDom, @@ -164,9 +171,54 @@ export class ManualMigration1692191990702 implements MigrationInterface { }, }; + usagersCreator.setUsagerDefaultAttributes(partialUsager); const usager = new UsagerTable(partialUsager); - usagersCreator.setUsagerDefaultAttributes(usager); await usagerRepository.save(usager); + + if (usagerToulouse?.procuration === 1) { + await usagerNotesRepository.save( + new UsagerNotesTable({ + message: getText(usagerToulouse?.nom_procuration), + usagerRef: usager.ref, + usagerUUID: usager.uuid, + pinned: false, + structureId: TOULOUSE_STRUCTURE_ID, + createdBy: { + userId: 1, + userName: "DomiFa", + }, + archived: false, + }) + ); + } + + if (getText(usagerToulouse?.Remarques) !== "") { + const newNote = new UsagerNotesTable({ + message: getText(usagerToulouse?.Remarques), + usagerRef: usager.ref, + usagerUUID: usager.uuid, + pinned: true, + structureId: TOULOUSE_STRUCTURE_ID, + createdBy: { + userId: 1, + userName: "DomiFa", + }, + archived: false, + }); + await usagerNotesRepository.save(newNote); + + await usagerRepository.update( + { uuid: usager.uuid }, + { + pinnedNote: { + message: newNote.message, + usagerRef: newNote.usagerRef, + createdAt: newNote.createdAt, + createdBy: newNote.createdBy, + }, + } + ); + } } await queryRunner.commitTransaction(); diff --git a/packages/backend/src/_migrations/1697659880952-import-passages-migration.ts b/packages/backend/src/_migrations/1697659880952-import-passages-migration.ts index 2ff5e15f9e..29ee67d217 100644 --- a/packages/backend/src/_migrations/1697659880952-import-passages-migration.ts +++ b/packages/backend/src/_migrations/1697659880952-import-passages-migration.ts @@ -58,10 +58,9 @@ export class ManualMigration1697659880952 implements MigrationInterface { usagerRef: usager.ref, dateInteraction: getDateFromXml(interaction.date), type: "visite", - userId: 1200, + userId: 1, userName: "DomiFa", structureId: TOULOUSE_STRUCTURE_ID, - event: "create", interactionOutUUID: null, }) ); diff --git a/packages/backend/src/database/entities/usager/UsagerTable.typeorm.ts b/packages/backend/src/database/entities/usager/UsagerTable.typeorm.ts index a9b48d7e8f..0f8080e0f9 100644 --- a/packages/backend/src/database/entities/usager/UsagerTable.typeorm.ts +++ b/packages/backend/src/database/entities/usager/UsagerTable.typeorm.ts @@ -143,7 +143,10 @@ export class UsagerTable public numeroDistribution!: string | null; @Column({ type: "jsonb", default: null, nullable: true }) - public pinnedNote!: Partial | null; + public pinnedNote!: Pick< + UsagerNote, + "usagerRef" | "createdAt" | "createdBy" | "message" + > | null; public constructor(entity?: Partial) { super(entity); diff --git a/packages/backend/src/usagers/services/usagersCreator.service.ts b/packages/backend/src/usagers/services/usagersCreator.service.ts index f2767a055b..31c8e1ecb2 100644 --- a/packages/backend/src/usagers/services/usagersCreator.service.ts +++ b/packages/backend/src/usagers/services/usagersCreator.service.ts @@ -13,7 +13,7 @@ async function findNextUsagerRef(structureId: number): Promise { return usager?.ref ? usager?.ref + 1 : 1; } -function setUsagerDefaultAttributes(usager: Usager): void { +function setUsagerDefaultAttributes(usager: Partial): void { usager.options = { transfert: { actif: false,