From 0c764b38c30ba95afc31bda5133b36105aa20ce0 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Wed, 10 Aug 2022 10:43:33 +0200
Subject: [PATCH] publish: restore husky

generated from commit d709de0e0961044a71e4376aab783a5b1e5477cd
---
 index.js     | 167 +++++++++++++++++++++++++++------------------------
 package.json |   5 ++
 2 files changed, 93 insertions(+), 79 deletions(-)

diff --git a/index.js b/index.js
index 2e0549c..3e08820 100644
--- a/index.js
+++ b/index.js
@@ -21,10 +21,8 @@ const {
   formateDataForDoctype,
   parseTags,
   parseValue,
-  parseContractStartDate,
 } = __webpack_require__(1555)
 const {
-  consulterDonneesTechniquesContractuelles,
   consultationMesuresDetailleesMaxPower,
   consultationMesuresDetaillees,
 } = __webpack_require__(1556)
@@ -38,8 +36,9 @@ const { verifyUserIdentity } = __webpack_require__(1558)
 const { activateContract } = __webpack_require__(1598)
 const { verifyContract } = __webpack_require__(1599)
 const { terminateContract } = __webpack_require__(1601)
-const { getAccount, saveAccountData } = __webpack_require__(1602)
-const { iSLocal } = __webpack_require__(1603)
+const { getContractStartDate } = __webpack_require__(1602)
+const { getAccount, saveAccountData } = __webpack_require__(1603)
+const { iSLocal } = __webpack_require__(1604)
 
 moment.locale('fr') // set the language
 moment.tz.setDefault('Europe/Paris') // set the timezone
@@ -57,7 +56,6 @@ const endDateString = endDate.format('YYYY-MM-DD')
 const ACCOUNT_ID = iSLocal() ? 'default_account_id' : 'enedis-sge-grandlyon'
 
 module.exports = new BaseKonnector(start)
-module.exports = { getContractStartDate }
 
 /**
  * The start function is run by the BaseKonnector instance only when it got all the account
@@ -102,16 +100,25 @@ async function start(fields, cozyParameters) {
     let consent = await createBoConsent(
       pointId,
       user.lastname,
+      user.firstname,
       user.address,
       user.postalCode,
       user.inseeCode
     )
-    await getContractStartDate(baseUrl, apiAuthKey, sgeLogin, pointId)
+
+    // handle user contract start date in order to preperly request data
+    const userContractstartDate = await getContractStartDate(
+      baseUrl,
+      apiAuthKey,
+      sgeLogin,
+      pointId
+    )
+    startDailyDate = moment(userContractstartDate, 'YYYY-MM-DD')
+    startDailyDateString = startDailyDate.format('YYYY-MM-DD')
 
     const contractStartDate = moment().format('YYYY-MM-DD')
-    //TODO: consent time ? 5 years?
     const contractEndDate = moment()
-      .add(1, 'year')
+      .add(1, 'year') // SGE force 1 year duration
       .format('YYYY-MM-DD')
 
     let serviceId = await verifyContract(
@@ -136,10 +143,6 @@ async function start(fields, cozyParameters) {
     consent = await updateBoConsent(consent, serviceId)
     // Save bo id into account
     const accountData = await getAccount(ACCOUNT_ID)
-    console.log(
-      '🚀 ~ file: index.js ~ line 132 ~ start ~ accountData',
-      accountData
-    )
 
     await saveAccountData(this.accountId, {
       ...accountData.data,
@@ -150,7 +153,7 @@ async function start(fields, cozyParameters) {
     const accountData = await getAccount(ACCOUNT_ID)
     const userConsent = await getBoConsent(accountData.data.consentId)
     const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin)
-    console.log(user)
+
     if (user.lastname !== userConsent.lastname || !user) {
       log('error', `Invalid or not found consent for user`)
       await deleteBoConsent()
@@ -206,42 +209,6 @@ async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) {
   log('info', 'Querying data: done')
 }
 
-/**
- * //TODO: Move
- * @param {string} url
- * @param {string} apiAuthKey
- * @param {string} userLogin
- * @param {number} pointId
- * @returns {Promise<void>}
- */
-async function getContractStartDate(url, apiAuthKey, userLogin, pointId) {
-  log('info', 'Fetching data start date')
-  const sgeHeaders = {
-    'Content-Type': 'text/xml;charset=UTF-8',
-    apikey: apiAuthKey,
-  }
-
-  const { response } = await soapRequest({
-    url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`,
-    headers: sgeHeaders,
-    xml: consulterDonneesTechniquesContractuelles(pointId, userLogin),
-  }).catch(err => {
-    log('error', 'Error while fetching contract start date : ' + err)
-    throw errors.VENDOR_DOWN
-  })
-
-  log('info', consulterDonneesTechniquesContractuelles(pointId, userLogin))
-  xml2js.parseString(
-    response.body,
-    {
-      tagNameProcessors: [parseTags],
-      valueProcessors: [parseValue],
-      explicitArray: false,
-    },
-    processStartDate()
-  )
-}
-
 /**
  * Get hour data
  * @param {string} url
@@ -426,26 +393,6 @@ function processData(doctype = 'com.grandlyon.enedis.day') {
   }
 }
 
-/**
- * Store an accurate start date based on contrat start
- */
-function processStartDate() {
-  return async (err, result) => {
-    if (err) {
-      log('error', err)
-      throw err
-    }
-    // update start Date with contract openning date
-    try {
-      startDailyDate = moment(parseContractStartDate(result), 'YYYY-MM-DD')
-      startDailyDateString = startDailyDate.format('YYYY-MM-DD')
-    } catch (error) {
-      log('error', 'Error while processing contract start date: ' + error)
-      throw errors.NOT_EXISTING_DIRECTORY
-    }
-  }
-}
-
 /**
  * Save data in the right doctype db and prevent duplicated keys
  * @param {EnedisKonnectorData[]} data
@@ -500,7 +447,6 @@ async function agregateMonthAndYearData(data) {
  * @returns {boolean}
  */
 function isFirstStart(account) {
-  console.log(account)
   if (account && account.data && account.data.consentId) {
     log('info', 'Konnector not first start')
     return false
@@ -223880,23 +223826,32 @@ module.exports = {
 /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
 
 // @ts-check
-const { log, errors } = __webpack_require__(1)
+const { log } = __webpack_require__(1)
 
 /**
  * @param {number} pointId
- * @param {string} name
+ * @param {string} lastname
+ * @param {string} firstname
  * @param {string} address
  * @param {string} postalCode
  * @param {string} inseeCode
  * @returns {Consent}
  */
-function createBoConsent(pointId, name, address, postalCode, inseeCode) {
+function createBoConsent(
+  pointId,
+  lastname,
+  firstname,
+  address,
+  postalCode,
+  inseeCode
+) {
   //TODO: Implement
   log('info', `Query createBoConsent`)
   return {
     id: 1,
     pointId,
-    name,
+    lastname,
+    firstname,
     address,
     postalCode,
     inseeCode,
@@ -223923,7 +223878,7 @@ function updateBoConsent(consent, serviceId) {
  */
 function getBoConsent(boId) {
   //TODO: Implement
-  log('info', `Query getBoConsent`)
+  log('info', `Query getBoConsent ${boId}`)
   return {
     pointId: 1234,
     lastname: 'SUBTIL',
@@ -224058,6 +224013,7 @@ async function findUserPdl(
   })
 
   try {
+    log('info', parsedReply)
     return parseUserPdl(parsedReply)
   } catch (error) {
     log('error', 'Error while parsing user PDL: ' + error)
@@ -228138,8 +228094,6 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) {
     explicitArray: false,
   })
 
-  log('info', parsedReply)
-
   try {
     const currentContract = parseContracts(parsedReply)[0]
     if (currentContract.etatCode === contractState.ACTIF)
@@ -228241,8 +228195,63 @@ module.exports = { terminateContract }
 /* 1602 */
 /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
 
+// @ts-check
+const { log, errors } = __webpack_require__(1)
+const soapRequest = __webpack_require__(1331)
+const {
+  parseTags,
+  parseValue,
+  parseContractStartDate,
+} = __webpack_require__(1555)
+const xml2js = __webpack_require__(1513)
+const { consulterDonneesTechniquesContractuelles } = __webpack_require__(1556)
+
+/**
+ * Get user contract start date
+ * @param {string} url
+ * @param {string} apiAuthKey
+ * @param {string} userLogin
+ * @param {number} pointId
+ * @returns {Promise<string>}
+ */
+async function getContractStartDate(url, apiAuthKey, userLogin, pointId) {
+  log('info', 'Fetching data start date')
+  const sgeHeaders = {
+    'Content-Type': 'text/xml;charset=UTF-8',
+    apikey: apiAuthKey,
+  }
+
+  const { response } = await soapRequest({
+    url: `${url}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`,
+    headers: sgeHeaders,
+    xml: consulterDonneesTechniquesContractuelles(pointId, userLogin),
+  }).catch(err => {
+    log('error', 'Error while fetching contract start date : ' + err)
+    throw errors.VENDOR_DOWN
+  })
+
+  const result = await xml2js.parseStringPromise(response.body, {
+    tagNameProcessors: [parseTags],
+    valueProcessors: [parseValue],
+    explicitArray: false,
+  })
+  try {
+    return parseContractStartDate(result)
+  } catch (error) {
+    log('error', 'Error while processing contract start date: ' + error)
+    throw errors.NOT_EXISTING_DIRECTORY
+  }
+}
+
+module.exports = { getContractStartDate }
+
+
+/***/ }),
+/* 1603 */
+/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
+
 const { log, updateOrCreate } = __webpack_require__(1)
-const { iSLocal } = __webpack_require__(1603)
+const { iSLocal } = __webpack_require__(1604)
 const cozyClient = __webpack_require__(485)
 
 async function saveAccountData(accountId, accountData) {
@@ -228270,7 +228279,7 @@ module.exports = { getAccount, saveAccountData }
 
 
 /***/ }),
-/* 1603 */
+/* 1604 */
 /***/ ((module) => {
 
 function iSLocal() {
diff --git a/package.json b/package.json
index 87eab8c..eb3c5f8 100644
--- a/package.json
+++ b/package.json
@@ -19,6 +19,11 @@
     "build",
     "data"
   ],
+  "husky": {
+    "hooks": {
+      "pre-commit": "yarn lint"
+    }
+  },
   "scripts": {
     "start": "node ./src/index.js",
     "dev": "cozy-konnector-dev",
-- 
GitLab