From 05aa070a4abf91b1074483e2cf04cf48fb5d2bc5 Mon Sep 17 00:00:00 2001
From: Etienne LOUPIAS <eloupias@grandlyon.com>
Date: Thu, 26 Jan 2023 10:28:33 +0000
Subject: [PATCH] fix(CNFS): idCNFS import updates structure only if needed

---
 src/structures/services/structures.service.ts | 39 +++++++++++--------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 9d5647dec..db0e75f3c 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -69,9 +69,9 @@ export class StructuresService {
     const results = await this.structuresSearchService.search(text, fields);
     const ids = results.map((result) => result.structureId);
     let structures;
-    let multipleFilters = filters ? filters.filter((elem) => Object.keys(elem)[0].length == 0) : null;
+    let multipleFilters = filters ? filters.filter((elem) => Object.keys(elem)[0]?.length == 0) : null;
     filters = filters?.filter((elem) => {
-      return Object.keys(elem)[0].length != 0;
+      return Object.keys(elem)[0]?.length != 0;
     });
 
     //TODO: remove useless code ?
@@ -768,7 +768,7 @@ export class StructuresService {
 
   @Cron(CronExpression.EVERY_DAY_AT_4AM)
   public async structuresTasksProcess(): Promise<void> {
-    this.logger.debug('structuresTasksProcess');
+    this.logger.log('structuresTasksProcess');
 
     await this.processToBeDeletedStructures().catch((error) => {
       this.logger.error(error);
@@ -1039,7 +1039,7 @@ export class StructuresService {
         })
       ),
       tap((response) => {
-        this.logger.debug(`${response.length} CNFS rhone structures containing found`);
+        this.logger.debug(`${response.length} CNFS rhone structures found`);
       })
     );
 
@@ -1073,10 +1073,10 @@ export class StructuresService {
     const CNFSData = await this.getCNFSStructures();
     if (!resinStructure) return 'Could not find structure';
 
-    const mathingStructure = this.findMatchingStructure(resinStructure, CNFSData);
-    if (mathingStructure) {
+    const matchingStructure = this.findMatchingStructure(resinStructure, CNFSData);
+    if (matchingStructure) {
       // if a corresponing structure has been found but its email is different, we probably will need to update it here.
-      await this.setCNFSid(resinStructure.id, mathingStructure.id);
+      await this.setCNFSid(resinStructure.id, matchingStructure.id);
       return 'A match has been found, updating CNFSid';
     }
     this.logger.debug(`No match found`);
@@ -1089,7 +1089,7 @@ export class StructuresService {
    */
   @Cron(CronExpression.EVERY_WEEK)
   public async bindCNFSids(): Promise<string> {
-    this.logger.debug(`bind CNFS ids to resin structures`);
+    this.logger.log('bind CNFS ids to resin structures process');
     const CNFSData = await this.getCNFSStructures();
     const resinStructures = await this.findAll();
     this.logger.debug(`${resinStructures.length} resin structures found`);
@@ -1098,17 +1098,21 @@ export class StructuresService {
     let dismatchCount = 0;
 
     for (const resinStructure of resinStructures) {
-      const mathingStructure = this.findMatchingStructure(resinStructure, CNFSData);
-      if (mathingStructure) {
-        await this.setCNFSid(resinStructure.id, mathingStructure.id);
-        matchCount++;
+      const matchingStructure = this.findMatchingStructure(resinStructure, CNFSData);
+      if (matchingStructure) {
+        if (await this.setCNFSid(resinStructure.id, matchingStructure.id)) {
+          matchCount++;
+        }
         // if a corresponing structure has been found but its email is different, we probably will need to update it here.
       } else {
-        await this.setCNFSid(resinStructure.id, '');
-        dismatchCount++;
+        if (await this.setCNFSid(resinStructure.id, '')) {
+          dismatchCount++;
+        }
       }
     }
-    this.logger.debug(`${matchCount} out of ${dismatchCount} structures affected with a idCNFS`);
+    this.logger.log(
+      `${matchCount} structures affected with a idCNFS (${dismatchCount} structures with idCNFS emptied)`
+    );
     return `${matchCount} structures affected`;
   }
 
@@ -1116,8 +1120,11 @@ export class StructuresService {
    * Update the idCNFS of a structure with its value of ""
    */
   public async setCNFSid(idStructure: string, idCNFS: string): Promise<Structure> {
-    this.logger.debug(`A resin structure '${idStructure}' matched with a CNFS one, updating its idCNFS '${idCNFS}'`);
     const oldStructure = await this.findOne(idStructure);
+    if (oldStructure.idCNFS || '' == idCNFS) {
+      return null;
+    }
+    this.logger.log(`A resin structure '${idStructure}' matched with a CNFS one, updating its idCNFS '${idCNFS}'`);
     const deepClone = Object.assign(oldStructure, { idCNFS });
     return this.structureModel.findByIdAndUpdate(new Types.ObjectId(idStructure), deepClone).exec();
   }
-- 
GitLab