Skip to content
Snippets Groups Projects
index.js 8.18 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 SUBTIL's avatar
Hugo SUBTIL committed
  errors,
Hugo NOUTS's avatar
Hugo NOUTS committed
} = __webpack_require__(1)
const soapRequest = __webpack_require__(1337)
const moment = __webpack_require__(1379)
__webpack_require__(1516)
const xml2js = __webpack_require__(1519)
const { buildAggregatedData } = __webpack_require__(1560)
Hugo NOUTS's avatar
Hugo NOUTS committed
const {
  parseSgeXmlData,
  formateDataForDoctype,
  parseTags,
  parseValue,
} = __webpack_require__(1561)
build-token's avatar
build-token committed
const {
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  consultationMesuresDetailleesMaxPower,
  consultationMesuresDetaillees,
} = __webpack_require__(1562)
Hugo SUBTIL's avatar
Hugo SUBTIL committed
const {
  updateBoConsent,
  createBoConsent,
  getBoConsent,
  deleteBoConsent,
} = __webpack_require__(1563)
const {
  verifyUserIdentity,
  activateContract,
  verifyContract,
  terminateContract,
  getContractStartDate,
} = __webpack_require__(1681)
const { getAccount, saveAccountData } = __webpack_require__(1691)
const { isLocal, isDev } = __webpack_require__(1692)
const Sentry = __webpack_require__(1601)
// eslint-disable-next-line
const Tracing = __webpack_require__(1693) // Needed for tracking performance in Sentry
const { version } = __webpack_require__(1730)
Hugo SUBTIL's avatar
Hugo SUBTIL committed

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' : 'enedissgegrandlyon'
Hugo NOUTS's avatar
Hugo NOUTS committed

module.exports = new BaseKonnector(start)

/**
 * Sentry
 */
Sentry.init({
  dsn:
    'https://18747a93401447f2a81b83cd8c4bbbdf@grandlyon.errors.cozycloud.cc/5',

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: isLocal() ? 0 : 1.0,
  release: version,
  environment: isDev() ? 'development' : 'production',
  debug: isDev(),
  integrations: [
    // enable HTTP calls tracing
    new Sentry.Integrations.Http({ tracing: true }),
  ],
})

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) {
  try {
    log('info', 'Konnector configuration ...')
    log('info', `isManual execution: ${manualExecution}`)
    const transaction = Sentry.startTransaction({
      op: 'konnector',
      name: 'SGE Konnector',
    })
Hugo SUBTIL's avatar
Hugo SUBTIL committed

    const pointId = parseInt(fields.pointId)
    let baseUrl = fields.wso2BaseUrl
    let apiAuthKey = fields.apiToken
    let contractId = fields.contractId
    let sgeLogin = fields.sgeLogin
    let boToken = fields.boToken
    let boBaseUrl = fields.boBaseUrl
    if (cozyParameters && Object.keys(cozyParameters).length !== 0) {
      log('debug', 'Found COZY_PARAMETERS')
      baseUrl = cozyParameters.secret.wso2BaseUrl
      apiAuthKey = cozyParameters.secret.apiToken
      contractId = cozyParameters.secret.contractId
      sgeLogin = cozyParameters.secret.sgeLogin
      boBaseUrl = cozyParameters.secret.boBaseUrl
      boToken = cozyParameters.secret.boToken
    }

    // Prevent missing configuration
    if (
      !baseUrl ||
      !apiAuthKey ||
      !contractId ||
      !sgeLogin ||
      !boToken ||
      !boBaseUrl
    ) {
      const errorMessage = 'Missing configuration secrets'
      log('error', errorMessage)
      Sentry.captureException(errorMessage)
      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 (isFirstStart(await getAccount(ACCOUNT_ID))) {
      log('info', 'First start...')
      transaction.startChild({ op: 'First start' })
      const user = await verifyUserIdentity(
        fields,
        baseUrl,
        apiAuthKey,
        sgeLogin
      )
Hugo SUBTIL's avatar
Hugo SUBTIL committed

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

      // handle user contract start date in order to properly request data
      const userContractStartDate = await getContractStartDate(
Hugo SUBTIL's avatar
Hugo SUBTIL committed
        baseUrl,
        apiAuthKey,
        sgeLogin,
        pointId
      startDailyDate = moment(userContractStartDate, 'YYYY-MM-DD')
      startDailyDateString = startDailyDate.format('YYYY-MM-DD')
      const contractStartDate = moment().format('YYYY-MM-DD')
      const contractEndDate = moment()
        .add(1, 'year') // SGE force 1 year duration
        .format('YYYY-MM-DD')

      let serviceId = await verifyContract(
Hugo SUBTIL's avatar
Hugo SUBTIL committed
        baseUrl,
        apiAuthKey,
        sgeLogin,
        contractId,
        user.pointId
      )
      if (!serviceId) {
        serviceId = await activateContract(
          baseUrl,
          apiAuthKey,
          sgeLogin,
          contractId,
          user.lastname,
          user.pointId,
Loading
Loading full blame...