From 9f83c1f163c6ea07dbc355f291dda3916cfea65d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marl=C3=A8ne=20SIMONDANT?= <msimondant@grandlyon.com>
Date: Wed, 15 Mar 2023 16:11:43 +0000
Subject: [PATCH] fix(structures):remove unknow@unknown.com as contact emails

---
 .../1678284373613-remove-unknown-emails.ts    | 26 +++++++++++++++++++
 .../services/aptic-structures.service.ts      |  2 +-
 src/structures/services/structures.service.ts |  8 ------
 3 files changed, 27 insertions(+), 9 deletions(-)
 create mode 100644 src/migrations/scripts/1678284373613-remove-unknown-emails.ts

diff --git a/src/migrations/scripts/1678284373613-remove-unknown-emails.ts b/src/migrations/scripts/1678284373613-remove-unknown-emails.ts
new file mode 100644
index 000000000..017884832
--- /dev/null
+++ b/src/migrations/scripts/1678284373613-remove-unknown-emails.ts
@@ -0,0 +1,26 @@
+import { Db } from 'mongodb';
+import { StructureDocument } from '../../structures/schemas/structure.schema';
+import { getDb } from '../migrations-utils/db';
+
+export const up = async () => {
+  const db: Db = await getDb();
+  const cursor = db.collection('structures').find({});
+  let document;
+  while ((document = await cursor.next())) {
+    const newDoc: StructureDocument = removeUnknownContactMail(document);
+    await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]);
+  }
+  console.log('Update done: Contact emails unknown@unknown.com emptied');
+};
+
+export const down = async () => {
+  // Nothing can be done since we can't know which null contactMail fields were previously filled with unknow@unknown.com
+  console.log('Downgrade done');
+};
+
+function removeUnknownContactMail(doc: StructureDocument): StructureDocument {
+  if (doc.contactMail && doc.contactMail === 'unknown@unknown.com') {
+    doc.contactMail = null;
+  }
+  return doc;
+}
diff --git a/src/structures/services/aptic-structures.service.ts b/src/structures/services/aptic-structures.service.ts
index 217407631..d98ab942a 100644
--- a/src/structures/services/aptic-structures.service.ts
+++ b/src/structures/services/aptic-structures.service.ts
@@ -80,7 +80,7 @@ export class ApticStructuresService {
         createdStructure.structureName = structure.name;
         createdStructure.contactPhone = structure.phone;
         // Unkown fields (but mandatory)
-        createdStructure.contactMail = 'unknown@unknown.com';
+        createdStructure.contactMail = null;
         createdStructure.categories.labelsQualifications = ['passNumerique'];
         createdStructure.structureType = await this.structureTypeService.findByValue('autre');
         createdStructure.pmrAccess = false;
diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 2b8e21dd8..74a26dcbf 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -275,10 +275,6 @@ export class StructuresService {
         .select('-_id -accountVerified -otherDescription -dataShareConsentDate')
         .exec()
     ).map((structure) => {
-      // If structure has temp email, hide it
-      if (this.hasTempMail(structure)) {
-        structure.contactMail = null;
-      }
       const repositoryKeys = categories.map((category) => category.id);
       repositoryKeys.forEach((el) => {
         // Add referentiel
@@ -937,10 +933,6 @@ export class StructuresService {
     this.mailerService.send(emailsObject, jsonConfig.subject, html);
   }
 
-  private hasTempMail(structure: Structure): boolean {
-    return structure.contactMail === 'unknown@unknown.com';
-  }
-
   public async getAllUserCompletedStructures(users: IUser[]) {
     return Promise.all(
       users.map(async (user) => {
-- 
GitLab