From 25480d585119ca6bef98dc4567cd63c8b273e980 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Mon, 22 Feb 2021 17:38:36 +0100
Subject: [PATCH] fix: user cration bug + structure find refacto

---
 src/structures/services/structures.service.ts | 25 +++++++------------
 src/users/users.service.ts                    |  8 +++---
 2 files changed, 14 insertions(+), 19 deletions(-)

diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 4dd3dfd27..fe78e14d6 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -82,25 +82,13 @@ export class StructuresService {
     await Promise.all(
       structures.map((structure: StructureDocument) => {
         // If structre has no address, add it
-        if (!structure.address) {
-          return this.getStructurePosition(structure).then((postition) => {
+        if (!structure.address || structure.coord.length <= 0) {
+          return this.getStructurePosition(structure).then((postition: StructureDocument) => {
             this.structureModel
               .findByIdAndUpdate(Types.ObjectId(structure._id), { address: postition.address, coord: postition.coord })
               .exec();
           });
         }
-        if (structure.coord.length <= 0) {
-          return new Promise((resolve) => {
-            this.getStructurePosition(structure).then((postition: StructureDocument) => {
-              this.structureModel
-                .findByIdAndUpdate(Types.ObjectId(postition._id), { coord: postition.coord })
-                .exec()
-                .then(() => {
-                  resolve('');
-                });
-            });
-          });
-        }
       })
     );
     return this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec();
@@ -127,11 +115,16 @@ export class StructuresService {
    * Get structures positions and add marker corresponding to those positons on the map
    */
   private getStructurePosition(structure: Structure): Promise<Structure> {
-    return new Promise((resolve) => {
+    return new Promise((resolve, reject) => {
       this.getCoord(structure.address.numero, structure.address.street, structure.address.commune).subscribe(
         (res) => {
           const address = res.data.features[0];
-          structure.coord = address.geometry.coordinates;
+          if (address && address.geometry) {
+            structure.coord = address.geometry.coordinates;
+          } else {
+            Logger.error(`No coord found: ${structure.address.street}`, 'StructureService');
+            structure.coord = [];
+          }
           resolve(structure);
         },
         (err) => {
diff --git a/src/users/users.service.ts b/src/users/users.service.ts
index c695d8185..cf71c5b3f 100644
--- a/src/users/users.service.ts
+++ b/src/users/users.service.ts
@@ -34,9 +34,11 @@ export class UsersService {
     }
     let createUser = new this.userModel(createUserDto);
     createUser.structuresLink = [];
-    createUserDto.structuresLink.forEach((structureId) => {
-      createUser.structuresLink.push(Types.ObjectId(structureId));
-    });
+    if (createUserDto.structuresLink) {
+      createUserDto.structuresLink.forEach((structureId) => {
+        createUser.structuresLink.push(Types.ObjectId(structureId));
+      });
+    }
     // createUser.email = createUserDto.email;
     createUser.password = await this.hashPassword(createUser.password);
     // Send verification email
-- 
GitLab