From 759fcb53a0f78c03c594c524572d55f547d6f7ec Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Mon, 31 May 2021 11:15:58 +0200
Subject: [PATCH] fix: opendata endpoint data and email format

---
 src/structures/schemas/structure.schema.ts    |  2 +-
 src/structures/services/structures.service.ts | 15 +++++++++++++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/structures/schemas/structure.schema.ts b/src/structures/schemas/structure.schema.ts
index 8de601425..a4745ffb3 100644
--- a/src/structures/schemas/structure.schema.ts
+++ b/src/structures/schemas/structure.schema.ts
@@ -5,7 +5,7 @@ import { Week } from './week.schema';
 
 export type StructureDocument = Structure & Document;
 
-@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } })
+@Schema({ timestamps: true })
 export class Structure {
   @Prop()
   numero: string;
diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 6b70fb759..e5e357365 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -182,11 +182,10 @@ export class StructuresService {
     otherCategories: CategoriesOthers[]
   ): Promise<StructureDocument[]> {
     const structures = await this.structureModel.find({ deletedAt: { $exists: false } }).exec();
-
     // Update structures coord and address before sending them
     await Promise.all(
       structures.map((structure: StructureDocument) => {
-        // If structre has no address, add it
+        // If structure has no address, add it
         if (!structure.address || structure.coord.length <= 0) {
           return this.getStructurePosition(structure).then((postition: StructureDocument) => {
             this.structureModel
@@ -205,6 +204,14 @@ export class StructuresService {
         .select('-_id -accountVerified -otherDescription')
         .exec()
     ).map((structure) => {
+      // If structure has temp email, hide it
+      if (this.hasTempMail(structure)) {
+        structure.contactMail = null;
+      }
+      // Format date
+      structure.createdAt = new Date(structure.createdAt).toISOString();
+      structure.updatedAt = new Date(structure.updatedAt).toISOString();
+      // Add referentiel
       structure.proceduresAccompaniment = this.mapModules(
         structure.proceduresAccompaniment,
         accompagnementCategories.find((category) => category.id === 'proceduresAccompaniment').modules
@@ -586,4 +593,8 @@ export class StructuresService {
     });
     this.mailerService.send(emailsObject, jsonConfig.subject, html);
   }
+
+  private hasTempMail(structure: Structure): boolean {
+    return structure.contactMail === 'unknown@unknown.com';
+  }
 }
-- 
GitLab