diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 4dd3dfd2746cfbdb6c0fa1af4962d5a110603db6..fe78e14d6c16f45862a6e66a452a117c93204e63 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 c695d81850048c79ba82dce8e60676309a5dd4d1..cf71c5b3f0f89cb29c61f33730dfe384a20ddb1d 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