From ec6d2acfd96a69ffd38e08310ec6f1863332a306 Mon Sep 17 00:00:00 2001
From: Antonin Coquet <ext.sopra.acoquet@grandlyon.com>
Date: Wed, 5 May 2021 18:13:32 +0200
Subject: [PATCH] fix: hide some fiel on endpoint and create migration for
 opening hours

---
 .../scripts/1620229047628-opening-hours.ts    |  67 ++++++++++++
 src/structures/schemas/structure.schema.ts    |   2 +
 src/structures/schemas/time.schema.ts         |   2 +-
 src/structures/services/structures.service.ts | 102 +++++++++---------
 src/structures/structures.controller.ts       |   3 +
 5 files changed, 125 insertions(+), 51 deletions(-)
 create mode 100644 src/migrations/scripts/1620229047628-opening-hours.ts

diff --git a/src/migrations/scripts/1620229047628-opening-hours.ts b/src/migrations/scripts/1620229047628-opening-hours.ts
new file mode 100644
index 000000000..16d11df31
--- /dev/null
+++ b/src/migrations/scripts/1620229047628-opening-hours.ts
@@ -0,0 +1,67 @@
+import { Db } from 'mongodb';
+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 = updateStructure(document);
+    await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]);
+  }
+  console.log(`Update done`);
+};
+
+export const down = async () => {
+  const db: Db = await getDb();
+
+  const cursor = db.collection('structures').find({});
+  let document;
+  while ((document = await cursor.next())) {
+    const newDoc = downgradeStructure(document);
+    await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]);
+  }
+  console.log(`Update done`);
+};
+
+function updateStructure(doc) {
+  return updateHours(doc);
+}
+
+function downgradeStructure(doc) {
+  doc = restoreHours(doc);
+  return doc;
+}
+
+function updateHours(doc) {
+  if (doc.hours) {
+    Object.keys(doc.hours).forEach((key) => {
+      doc.hours[key].time.forEach((timeRange) => {
+        timeRange.opening = timeRange.openning;
+        delete timeRange.openning;
+      });
+    });
+    return doc;
+  } else {
+    console.warn(`No hours on doc ${doc._id}`);
+    return doc;
+  }
+}
+
+function restoreHours(doc) {
+  if (doc.hours) {
+    Object.keys(doc.hours).forEach((key) => {
+      if (doc.hours[key].time.length > 0) {
+        doc.hours[key].time.forEach((timeRange) => {
+          timeRange.openning = timeRange.opening;
+          delete timeRange.opening;
+        });
+      }
+    });
+    return doc;
+  } else {
+    console.warn(`No hours on doc ${doc._id}`);
+    return doc;
+  }
+}
diff --git a/src/structures/schemas/structure.schema.ts b/src/structures/schemas/structure.schema.ts
index fdd977bfc..718d4bb03 100644
--- a/src/structures/schemas/structure.schema.ts
+++ b/src/structures/schemas/structure.schema.ts
@@ -1,4 +1,5 @@
 import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
+import { Exclude } from 'class-transformer';
 import { Document } from 'mongoose';
 import { Address } from './address.schema';
 import { Week } from './week.schema';
@@ -29,6 +30,7 @@ export class Structure {
   lockdownActivity: string;
 
   @Prop()
+  @Exclude()
   address: Address;
 
   @Prop()
diff --git a/src/structures/schemas/time.schema.ts b/src/structures/schemas/time.schema.ts
index 65109f63a..8a2261331 100644
--- a/src/structures/schemas/time.schema.ts
+++ b/src/structures/schemas/time.schema.ts
@@ -4,7 +4,7 @@ import { Document } from 'mongoose';
 export type TimeDocument = Time & Document;
 
 export class Time {
-  openning: string;
+  opening: string;
   closing: string;
 }
 
diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index ae5c8dd48..aec2207c6 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -185,57 +185,59 @@ export class StructuresService {
         }
       })
     );
-    return (await this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec()).map(
-      (structure) => {
-        structure.proceduresAccompaniment = this.mapModules(
-          structure.proceduresAccompaniment,
-          accompagnementCategories.find((category) => category.name === 'Accompagnement des démarches').modules
-        );
-        structure.labelsQualifications = this.mapModules(
-          structure.labelsQualifications,
-          otherCategories.find((category) => category.name === 'Labels et qualifications').modules
-        );
-        structure.publics = this.mapModules(
-          structure.publics,
-          otherCategories.find((category) => category.name === 'Publics acceptés').modules
-        );
-        structure.accessModality = this.mapModules(
-          structure.accessModality,
-          otherCategories.find((category) => category.name === "Modalités d'accès").modules
-        );
-        structure.publicsAccompaniment = this.mapModules(
-          structure.publicsAccompaniment,
-          otherCategories.find((category) => category.name === 'Accompagnement des publics spécifique').modules
-        );
-        structure.equipmentsAndServices = this.mapModules(
-          structure.equipmentsAndServices,
-          otherCategories.find((category) => category.name === 'Équipements et services proposés').modules
-        );
-        structure.baseSkills = this.mapFormationModules(
-          structure.baseSkills,
-          formationCategories.find((category) => category.name === 'Les compétences de base').modules
-        );
-        structure.accessRight = this.mapFormationModules(
-          structure.accessRight,
-          formationCategories.find((category) => category.name === 'Accès aux droits').modules
-        );
-        structure.socialAndProfessional = this.mapFormationModules(
-          structure.socialAndProfessional,
-          formationCategories.find((category) => category.name === 'Insertion sociale et professionnelle').modules
-        );
-        structure.parentingHelp = this.mapFormationModules(
-          structure.parentingHelp,
-          formationCategories.find((category) => category.name === 'Aide à la parentalité').modules
-        );
-        structure.digitalCultureSecurity = this.mapFormationModules(
-          structure.digitalCultureSecurity,
-          formationCategories.find((category) => category.name === 'Culture et sécurité numérique').modules
-        );
-        return structure;
-      }
-    );
+    return (
+      await this.structureModel
+        .find({ deletedAt: { $exists: false }, accountVerified: true })
+        .select('-_id -accountVerified -otherDescription')
+        .exec()
+    ).map((structure) => {
+      structure.proceduresAccompaniment = this.mapModules(
+        structure.proceduresAccompaniment,
+        accompagnementCategories.find((category) => category.name === 'Accompagnement des démarches').modules
+      );
+      structure.labelsQualifications = this.mapModules(
+        structure.labelsQualifications,
+        otherCategories.find((category) => category.name === 'Labels et qualifications').modules
+      );
+      structure.publics = this.mapModules(
+        structure.publics,
+        otherCategories.find((category) => category.name === 'Publics acceptés').modules
+      );
+      structure.accessModality = this.mapModules(
+        structure.accessModality,
+        otherCategories.find((category) => category.name === "Modalités d'accès").modules
+      );
+      structure.publicsAccompaniment = this.mapModules(
+        structure.publicsAccompaniment,
+        otherCategories.find((category) => category.name === 'Accompagnement des publics spécifique').modules
+      );
+      structure.equipmentsAndServices = this.mapModules(
+        structure.equipmentsAndServices,
+        otherCategories.find((category) => category.name === 'Équipements et services proposés').modules
+      );
+      structure.baseSkills = this.mapFormationModules(
+        structure.baseSkills,
+        formationCategories.find((category) => category.name === 'Les compétences de base').modules
+      );
+      structure.accessRight = this.mapFormationModules(
+        structure.accessRight,
+        formationCategories.find((category) => category.name === 'Accès aux droits').modules
+      );
+      structure.socialAndProfessional = this.mapFormationModules(
+        structure.socialAndProfessional,
+        formationCategories.find((category) => category.name === 'Insertion sociale et professionnelle').modules
+      );
+      structure.parentingHelp = this.mapFormationModules(
+        structure.parentingHelp,
+        formationCategories.find((category) => category.name === 'Aide à la parentalité').modules
+      );
+      structure.digitalCultureSecurity = this.mapFormationModules(
+        structure.digitalCultureSecurity,
+        formationCategories.find((category) => category.name === 'Culture et sécurité numérique').modules
+      );
+      return structure;
+    });
   }
-
   public mapFormationModules(structureModule: string[], baseModule: CategoriesFormationsModule[]): string[] {
     if (structureModule == []) {
       return [];
diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index 65dcfd00b..67012bced 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -1,5 +1,6 @@
 import {
   Body,
+  ClassSerializerInterceptor,
   Controller,
   Delete,
   Get,
@@ -11,6 +12,7 @@ import {
   Put,
   Query,
   UseGuards,
+  UseInterceptors,
 } from '@nestjs/common';
 import { ApiParam } from '@nestjs/swagger';
 import { Types } from 'mongoose';
@@ -94,6 +96,7 @@ export class StructuresController {
   }
 
   @Get('formated')
+  //@UseInterceptors(ClassSerializerInterceptor)
   public async findAllFormated(): Promise<Structure[]> {
     const formationCategories = await this.categoriesFormationsService.findAll();
     const accompagnementCategories = await this.categoriesAccompagnementService.findAll();
-- 
GitLab