From c674844665819be1bc59b0c5cdc2c568e6e430cd Mon Sep 17 00:00:00 2001
From: Etienne LOUPIAS <eloupias@grandlyon.com>
Date: Wed, 5 Jun 2024 09:26:30 +0200
Subject: [PATCH] fix(structure): wait for updating of all ctm (issue #585)

---
 src/structures/services/structures.service.ts | 11 +++++------
 src/structures/structures.controller.ts       |  2 +-
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 64c304a7f..bf325484c 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -630,7 +630,7 @@ export class StructuresService {
         structure.address.inseeCode = inseeCode;
         const ctm: Module = await this.categoriesService.findCtmByInseeCode(inseeCode);
         if (ctm) {
-          this.logger.debug(`${structure.structureName} : CTM ${ctm.id}`);
+          this.logger.log(`${structure.structureName} : CTM ${ctm.id}`);
           structure.categories['ctm'] = [ctm.id];
         }
       }
@@ -1333,11 +1333,10 @@ export class StructuresService {
     this.logger.debug('setCTMs');
     const structures: StructureDocument[] = await this.findAll();
     for (const structure of structures) {
-      this.setCtmTerritory(structure).then(async (updatedStructure) => {
-        await this.structureModel
-          .findByIdAndUpdate(new Types.ObjectId(structure._id), updatedStructure, { timestamps: false })
-          .exec();
-      });
+      const updatedStructure = await this.setCtmTerritory(structure);
+      await this.structureModel
+        .findByIdAndUpdate(new Types.ObjectId(structure._id), updatedStructure, { timestamps: false })
+        .exec();
     }
     return null;
   }
diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index fe62766f7..01a595881 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -204,7 +204,7 @@ export class StructuresController {
   @UseGuards(JwtAuthGuard, RolesGuard)
   @Roles('admin')
   public async updateCTM(): Promise<void> {
-    this.logger.debug('updateCTM');
+    this.logger.log('updateCTM');
     await this.categoriesService.updateCTM();
     return this.structureService.setCTMs();
   }
-- 
GitLab