diff --git a/__tests__/helpers/parsing.spec.js b/__tests__/helpers/parsing.spec.js index 73b2cd9435abbb5898f4693b3a99774cddefb541..54d484b0859e6de2861edf33872083ab0721c8dc 100644 --- a/__tests__/helpers/parsing.spec.js +++ b/__tests__/helpers/parsing.spec.js @@ -11,7 +11,9 @@ const { parseValue, removeMultipleSpaces, removeAddressNumber, + parsePointId, } = require('../../src/helpers/parsing') + describe('parsing', () => { it('should parse userPdl', () => { const result = { @@ -222,4 +224,19 @@ describe('parsing', () => { expect(reply).toBe('rue du lac') }) }) + + describe('parsePointId', () => { + it('should test a regular point id', () => { + const point = parsePointId('12345678901234') + expect(point).toBe('12345678901234') + expect(point.length).toBe(14) + }) + it('should test a point id starting with 0', () => { + const input = '7123456789012' + expect(input.length).toBe(13) + const point = parsePointId(input) + expect(point).toBe('07123456789012') + expect(point.length).toBe(14) + }) + }) }) diff --git a/src/core/contractActivation.js b/src/core/contractActivation.js index 614810359503182c514586a01faee8108dd378d8..d3352721bb388e50f0d2101017406e9d4a3c561e 100644 --- a/src/core/contractActivation.js +++ b/src/core/contractActivation.js @@ -11,7 +11,7 @@ const Sentry = require('@sentry/node') * @param {string} apiAuthKey * @param {string} appLogin * @param {string} lastname - * @param {number} pointId + * @param {string} pointId * @param {string} startDate * @param {string} endDate * @return {Promise<number>} User contractId diff --git a/src/core/contractStartDate.js b/src/core/contractStartDate.js index ff77cc1a1b08e33860e0071db3a249997d79916d..8a2eb209466cf2da19702d2fbc6ef95f5bae8971 100644 --- a/src/core/contractStartDate.js +++ b/src/core/contractStartDate.js @@ -15,7 +15,7 @@ const Sentry = require('@sentry/node') * @param {string} url * @param {string} apiAuthKey * @param {string} userLogin - * @param {number} pointId + * @param {string} pointId * @returns {Promise<string>} */ async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { diff --git a/src/core/contractTermination.js b/src/core/contractTermination.js index 692bc2affac9eac706c1de89b3d0477f557a7ded..e2a7eb934038368f30966cc441c0ab23729da874 100644 --- a/src/core/contractTermination.js +++ b/src/core/contractTermination.js @@ -10,7 +10,7 @@ const Sentry = require('@sentry/node') * @param {string} url * @param {string} apiAuthKey * @param {string} appLogin - * @param {number} pointId + * @param {string} pointId * @param {number} serviceId * @return {Promise<string>} User contractId */ diff --git a/src/core/contractVerification.js b/src/core/contractVerification.js index 6757f1b43ea6b688a52e881ba37df37052e54b70..fddd45fcb5fc0bb18719b1095d576a1df62c0aec 100644 --- a/src/core/contractVerification.js +++ b/src/core/contractVerification.js @@ -16,7 +16,7 @@ const Sentry = require('@sentry/node') * @param {string} url * @param {string} apiAuthKey * @param {string} appLogin - * @param {number} pointId + * @param {string} pointId * @return {Promise<number | null>} User contractId */ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) { diff --git a/src/core/findUserAddress.js b/src/core/findUserAddress.js index 9732915820ac9fe8572f2fd99d005886fa643732..fa2ef7138daa3a0c6c0fb2de79089f8fa45a662e 100644 --- a/src/core/findUserAddress.js +++ b/src/core/findUserAddress.js @@ -15,7 +15,7 @@ const Sentry = require('@sentry/node') * @param {string} url * @param {string} apiAuthKey * @param {string} userLogin - * @param {number} pointId + * @param {string} pointId * @returns {Promise<Address>} */ async function findUserAddress(url, apiAuthKey, userLogin, pointId) { diff --git a/src/core/types/types.js b/src/core/types/types.js index df5323ee754adf436d21664755895248d5e6eb99..5a344f9061d4f856717f0df7e037aabfbdb3d401 100644 --- a/src/core/types/types.js +++ b/src/core/types/types.js @@ -31,7 +31,7 @@ /** * Consent definition * @typedef {object} Consent - * @property {number} pointID + * @property {string} pointID * @property {string} lastname * @property {string} firstname * @property {string} address @@ -46,7 +46,7 @@ /** * User definition * @typedef {object} User - * @property {number} pointId + * @property {string} pointId * @property {string} lastname * @property {string} firstname * @property {string} postalCode diff --git a/src/helpers/parsing.js b/src/helpers/parsing.js index d1c46388ad2f1488ff32b019c1ccb5926aa38940..420c4447ae91ba08b487b6e9c00d88e7796eee44 100644 --- a/src/helpers/parsing.js +++ b/src/helpers/parsing.js @@ -171,11 +171,29 @@ function removeAddressNumber(str) { return str.replace(/\d+ |b |B |T |t |\d+/g, '') } +/** + * Parse PDL and to validate correct length of 14 digits + * @param {number} pointId - some pdl start with 0 + * @returns {string} pointId with 14 digits + * @example "07123456789012" + */ +function parsePointId(pointId) { + const strPointId = pointId.toString() + if (strPointId.length === 14) { + return strPointId + } else if (strPointId.length === 13) { + return `0${strPointId}` + } else { + throw new Error(`PointId ${pointId} is malformed`) + } +} + module.exports = { checkContractExists, formateDataForDoctype, parseContracts, parseContractStartDate, + parsePointId, parseServiceId, parseSgeXmlData, parseTags, diff --git a/src/index.js b/src/index.js index fe7afd2f9be8f8219566411a36beed3370ccf620..85a43c8ddb0b71f3162d3d7b3b7431cc4e9a3485 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ const { parseTags, parseValue, parseValueHalfHour, + parsePointId, } = require('./helpers/parsing') const { consultationMesuresDetailleesMaxPower, @@ -104,7 +105,7 @@ async function start(fields, cozyParameters) { name: 'SGE Konnector', }) - const pointId = parseInt(fields.pointId) + const pointId = parsePointId(parseInt(fields.pointId)) let baseUrl = fields.wso2BaseUrl let apiAuthKey = fields.apiToken let contractId = fields.contractId @@ -288,7 +289,7 @@ async function start(fields, cozyParameters) { * @param {string} apiAuthKey * @param {string} sgeLogin * @param {string} contractId - * @param {number} pointId + * @param {string} pointId * @param {string} boBaseUrl * @param {string} boToken * @param {boolean} isConsentExpired @@ -334,7 +335,7 @@ async function deleteConsent( * @param {string} baseUrl * @param {string} apiAuthKey * @param {string} sgeLogin - * @param {number} pointId + * @param {string} pointId */ async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) { log('info', 'Querying data...') @@ -374,7 +375,7 @@ async function gatherData(baseUrl, apiAuthKey, sgeLogin, pointId) { * @param {string} url * @param {string} apiAuthKey * @param {string} userLogin - * @param {number} pointId + * @param {string} pointId */ async function getData(url, apiAuthKey, userLogin, pointId) { log('info', 'Fetching data') @@ -419,7 +420,7 @@ async function getData(url, apiAuthKey, userLogin, pointId) { * @param {string} url * @param {string} apiAuthKey * @param {string} userLogin - * @param {number} pointId + * @param {string} pointId */ async function getMaxPowerData(url, apiAuthKey, userLogin, pointId) { log('info', 'Fetching Max Power data') @@ -483,7 +484,7 @@ function limitStartDate() { * @param {string} url * @param {string} apiAuthKey * @param {string} userLogin - * @param {number} pointId + * @param {string} pointId */ async function getDataHalfHour(url, apiAuthKey, userLogin, pointId) { log('info', 'Fetching data') diff --git a/src/requests/bo.js b/src/requests/bo.js index e3979c308247bead37cb6f60c2cd3405fdb504fa..7b938edf4b895e5d1bd0cbe1b53cde5ea5b86f26 100644 --- a/src/requests/bo.js +++ b/src/requests/bo.js @@ -4,7 +4,7 @@ const { default: axios } = require('axios') const Sentry = require('@sentry/node') /** - * @param {number} pointID + * @param {string} pointID * @param {string} lastname * @param {string} firstname * @param {string} address diff --git a/src/requests/sge.js b/src/requests/sge.js index 41599dc13ff43f4fa2f2763524603806bd4fb97d..9bc1562cc905b9189594960bc7ab2fab960778b3 100644 --- a/src/requests/sge.js +++ b/src/requests/sge.js @@ -3,7 +3,7 @@ const { log } = require('cozy-konnector-libs') /** * Get daily data up to 36 months & P max - * @param {number} pointId + * @param {string} pointId * @param {string} appLogin * @param {string} startDate * @param {string} endDate @@ -50,7 +50,7 @@ function consultationMesuresDetaillees( /** * Get user max power - * @param {number} pointId + * @param {string} pointId * @param {string} appLogin * @param {string} startDate * @param {string} endDate @@ -98,7 +98,7 @@ function consultationMesuresDetailleesMaxPower( /** * Get user technical data (contract start date) - * @param {number} pointId + * @param {string} pointId * @param {string} appLogin * @returns {string} */ @@ -194,7 +194,7 @@ function rechercherPoint( * Search if user as a service * @param {string} appLogin * @param {string} contractId - * @param {number} pointId + * @param {string} pointId * @returns {*} */ function rechercherServicesSouscritsMesures(appLogin, contractId, pointId) { @@ -220,7 +220,7 @@ function rechercherServicesSouscritsMesures(appLogin, contractId, pointId) { * Activate half hour data collect for user * @param {string} appLogin * @param {string} contractId - * @param {number} pointId + * @param {string} pointId * @param {string} lastname * @param {string} startDate * @param {string} endDate @@ -279,7 +279,7 @@ function commanderCollectePublicationMesures( * Stop the user consent * @param {string} appLogin * @param {string} contractId - * @param {number} pointId + * @param {string} pointId * @param {number} serviceSouscritId * @returns {*} */