Skip to content
Snippets Groups Projects
index.js 7.56 MiB
Newer Older
Hugo NOUTS's avatar
Hugo NOUTS committed
/******/ (() => { // webpackBootstrap
/******/ 	var __webpack_modules__ = ([
/* 0 */
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

// @ts-check
const {
  BaseKonnector,
  log,
  hydrateAndFilter,
  addData,
Hugo NOUTS's avatar
Hugo NOUTS committed
} = __webpack_require__(1)
const soapRequest = __webpack_require__(1331)
const moment = __webpack_require__(1373)
__webpack_require__(1510)
const xml2js = __webpack_require__(1513)
const { buildAgregatedData } = __webpack_require__(1554)
Hugo NOUTS's avatar
Hugo NOUTS committed
const {
  parseSgeXmlData,
  formateDataForDoctype,
  parseTags,
  parseValue,
} = __webpack_require__(1555)
build-token's avatar
build-token committed
const {
  consultationMesuresDetailleesMaxPower,
  consultationMesuresDetaillees,
} = __webpack_require__(1556)
  updateBoConsent,
  createBoConsent,
  getBoConsent,
  deleteBoConsent,
} = __webpack_require__(1557)
const { verifyUserIdentity } = __webpack_require__(1595)
Hugo SUBTIL's avatar
Hugo SUBTIL committed
const { activateContract } = __webpack_require__(1598)
const { verifyContract } = __webpack_require__(1599)
const { terminateContract } = __webpack_require__(1601)
Hugo SUBTIL's avatar
Hugo SUBTIL committed
const { getContractStartDate } = __webpack_require__(1602)
const { getAccount, saveAccountData } = __webpack_require__(1603)
const { iSLocal } = __webpack_require__(1604)
Hugo NOUTS's avatar
Hugo NOUTS committed
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone

/*** Connector Constants ***/
const manualExecution =
  process.env.COZY_JOB_MANUAL_EXECUTION === 'true' ? true : false
let startDailyDate = manualExecution
  ? moment().subtract(12, 'month')
  : moment().subtract(6, 'month')
let startDailyDateString = startDailyDate.format('YYYY-MM-DD')
const startLoadDate = moment().subtract(7, 'day')
const endDate = moment()
const endDateString = endDate.format('YYYY-MM-DD')
const ACCOUNT_ID = iSLocal() ? 'default_account_id' : 'enedis-sge-grandlyon'
Hugo NOUTS's avatar
Hugo NOUTS committed

module.exports = new BaseKonnector(start)

Hugo SUBTIL's avatar
Hugo SUBTIL committed
/**
 * The start function is run by the BaseKonnector instance only when it got all the account
 * information (fields). When you run this connector yourself in "standalone" mode or "dev" mode,
 * the account information come from ./konnector-dev-config.json file
 * cozyParameters are static parameters, independents from the account. Most often, it can be a
 * secret api key.
 * @param {fields} fields
 * @param {{secret: fields}} cozyParameters
 */
Hugo NOUTS's avatar
Hugo NOUTS committed
async function start(fields, cozyParameters) {
  log('info', 'Konnector configuration ...')
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  const pointId = fields.pointId
Hugo NOUTS's avatar
Hugo NOUTS committed
  let baseUrl = fields.wso2BaseUrl
  let apiAuthKey = fields.apiToken
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  let contractId = fields.contractId
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  //TODO switch variable to english
  let sgeLogin = fields.sgeLogin
  //TODO: Verify if condition is working in local and on build version
Hugo NOUTS's avatar
Hugo NOUTS committed
  if (cozyParameters && Object.keys(cozyParameters).length !== 0) {
    log('debug', 'Found COZY_PARAMETERS')
    baseUrl = cozyParameters.secret.wso2BaseUrl
    apiAuthKey = cozyParameters.secret.apiToken
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    contractId = cozyParameters.secret.contractId
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    sgeLogin = cozyParameters.secret.sgeLogin
Hugo NOUTS's avatar
Hugo NOUTS committed
  }
  // Prevent missing configuration
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  if (
    !baseUrl ||
    !apiAuthKey ||
    !contractId ||
    !sgeLogin ||
    !fields.apiToken ||
    !fields.boBaseUrl
  ) {
    log('error', `Missing configuration secrets`)
    throw errors.VENDOR_DOWN
  }

  /**
   * If it's first start we have to do the following operations:
   * - verify pdl are matching
   * - BO: create backoffice consent
   * - get contract start date and store it
   * - activate half-hour
   * - BO: update consent with service ID
   */
  log('info', 'User Logging...')

  if (await isFirstStart(await getAccount(ACCOUNT_ID))) {
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin)

    let consent = await createBoConsent(
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      fields.boBaseUrl,
      fields.boToken,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      pointId,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      user.lastname,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      user.firstname,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      user.address,
      user.postalCode,
      user.inseeCode
    )
Hugo SUBTIL's avatar
Hugo SUBTIL committed

    // 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')
Hugo SUBTIL's avatar
Hugo SUBTIL committed

    const contractStartDate = moment().format('YYYY-MM-DD')
    const contractEndDate = moment()
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      .add(1, 'year') // SGE force 1 year duration
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      .format('YYYY-MM-DD')

    let serviceId = await verifyContract(
      baseUrl,
      apiAuthKey,
      sgeLogin,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      contractId,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      user.pointId
    )
    if (!serviceId) {
      serviceId = await activateContract(
        baseUrl,
        apiAuthKey,
        sgeLogin,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
        contractId,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
        user.lastname,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
        user.pointId,
        contractStartDate,
        contractEndDate
      )
    }
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    consent = await updateBoConsent(
      fields.boBaseUrl,
      fields.boToken,
      consent,
      serviceId.toString()
    )
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    // Save bo id into account
    const accountData = await getAccount(ACCOUNT_ID)

    await saveAccountData(this.accountId, {
      ...accountData.data,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      consentId: consent.ID,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    // AlternateStart
    const accountData = await getAccount(ACCOUNT_ID)
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    const userConsent = await getBoConsent(
      fields.boBaseUrl,
      fields.boToken,
      accountData.data.consentId
    )
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    const user = await verifyUserIdentity(fields, baseUrl, apiAuthKey, sgeLogin)
    if (
      user.lastname.toLocaleUpperCase() !==
        userConsent.lastname.toLocaleUpperCase() ||
      !user
    ) {
Hugo SUBTIL's avatar
Hugo SUBTIL committed
      log('error', `Invalid or not found consent for user`)
      if (userConsent.serviceId) {
        await terminateContract(
          baseUrl,
          apiAuthKey,
          sgeLogin,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
          contractId,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
          fields.pointId,
          userConsent.serviceId
        )
Hugo SUBTIL's avatar
Hugo SUBTIL committed
        await deleteBoConsent(
          fields.boBaseUrl,
          fields.boToken,
          userConsent.ID ? userConsent.ID : 0
        )
Loading
Loading full blame...