From defecee393bc25543edcb9c9eac70385b75755a1 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Fri, 19 Aug 2022 16:17:30 +0200
Subject: [PATCH] fix: no contract found throw errors

---
 src/core/contractVerification.js | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/core/contractVerification.js b/src/core/contractVerification.js
index abc62f0..6df8b9c 100644
--- a/src/core/contractVerification.js
+++ b/src/core/contractVerification.js
@@ -37,6 +37,11 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) {
   })
 
   try {
+    if (!checkContractExists(parsedReply)) {
+      log('error', 'no contract found')
+      return null
+    }
+
     const currentContracts = parseContracts(parsedReply)
     let currentContract = null
     if (Array.isArray(currentContracts)) {
@@ -61,4 +66,15 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) {
   }
 }
 
+/**
+ * @param {string} parsedReply
+ * @return {boolean}
+ */
+function checkContractExists(parsedReply) {
+  const json = JSON.stringify(parsedReply)
+  return JSON.parse(json)['Envelope']['Body'][
+    'rechercherServicesSouscritsMesuresResponse'
+  ]['servicesSouscritsMesures']
+}
+
 module.exports = { verifyContract }
-- 
GitLab