From 9358e4436cdbb845d0f8b85b966c69c396240eae Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Mon, 1 Aug 2022 17:15:42 +0200 Subject: [PATCH] feat: add rechercher point --- __tests__/requests/insee.spec.js | 2 +- src/index.js | 55 ++++++++++++++++++++------------ src/parsing.js | 20 ++++++++++-- src/requests/sge.js | 8 +++-- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/__tests__/requests/insee.spec.js b/__tests__/requests/insee.spec.js index bdcf8e3..c8ae344 100644 --- a/__tests__/requests/insee.spec.js +++ b/__tests__/requests/insee.spec.js @@ -5,7 +5,7 @@ describe('getInseeCode', () => { }) it('should return null for a unexisting post code', async () => { - expect(await getInseeCode(69013)).toEqual(null) + expect(await getInseeCode(69069)).toEqual(null) }) it('should return Craponne insee code for post code 69290', async () => { diff --git a/src/index.js b/src/index.js index 9a6fc25..45aa453 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ const { formateDataForDoctype, parseTags, parseValue, + parseUserPdl, } = require('./parsing') const { consulterDonneesTechniquesContractuelles, @@ -80,7 +81,9 @@ async function start(fields, cozyParameters) { log('info', 'User Logging...') if (await isFirstStart()) { - if (!(await verifyUserIdentity(fields))) { + if ( + !(await verifyUserIdentity(fields, baseUrl, apiAuthKey, loginUtilisateur)) + ) { throw errors.LOGIN_FAILED } await createBoConsent() @@ -109,16 +112,29 @@ async function start(fields, cozyParameters) { /** * Verify user identity * @param {object} fields + * @param {string} baseUrl + * @param {string} apiAuthKey + * @param {string} loginUtilisateur */ -async function verifyUserIdentity(fields) { - const inseeCode = getInseeCode(fields.postalCode) - const user = await findUser( +async function verifyUserIdentity( + fields, + baseUrl, + apiAuthKey, + loginUtilisateur +) { + const inseeCode = await getInseeCode(fields.postalCode) + + const pdl = await findUserPdl( + `${baseUrl}/enedis_SDE_recherche-point/1.0`, + apiAuthKey, + loginUtilisateur, fields.name, fields.addresse, fields.postalCode, inseeCode ) - if (fields.pointId !== user.pointId) { + + if (fields.pointId != pdl) { log('error', 'PointId does not match') return false } @@ -455,14 +471,15 @@ async function agregateMonthAndYearData(data) { * @returns {boolean} */ function isFirstStart() { + console.log('isFirstStart') //TODO: Implement - return false + return true } /** - * @return {User} + * @return {Promise<string>} User Pdl */ -async function findUser( +async function findUserPdl( url, apiAuthKey, appLogin, @@ -476,11 +493,10 @@ async function findUser( 'Content-Type': 'text/xml;charset=UTF-8', apikey: apiAuthKey, } - const { response } = await soapRequest({ url: url, headers: sampleHeaders, - xml: rechercherPoint(appLogin, name, addresse, postalCode, inseeCode), + xml: rechercherPoint(appLogin, name, postalCode, inseeCode, addresse), }).catch(err => { log('error', 'rechercherPointResponse') log('error', err) @@ -488,14 +504,13 @@ async function findUser( //TODO: handling code error SGT4F6 and SGT432 into USER_ACTIon_NEEDED }) - //TODO: handle reply - xml2js.parseString( - response.body, - { - tagNameProcessors: [parseTags], - valueProcessors: [parseValue], - explicitArray: false, - }, - processStartDate() - ) + const parsedReply = await xml2js.parseStringPromise(response.body, { + tagNameProcessors: [parseTags], + valueProcessors: [parseValue], + explicitArray: false, + }) + + //TODO: handle errors + console.log(parsedReply) + return parseUserPdl(parsedReply) } diff --git a/src/parsing.js b/src/parsing.js index 64e6e7e..05439e1 100644 --- a/src/parsing.js +++ b/src/parsing.js @@ -2,6 +2,19 @@ const { log } = require('cozy-konnector-libs') const moment = require('moment') +/** + * Return User PDL + * @param {string} result + * @returns {string} + */ +function parseUserPdl(result) { + log('info', 'Parsing User Pdl') + const json = JSON.stringify(result) + return JSON.parse(json)['Envelope']['Body']['rechercherPointResponse'][ + 'points' + ]['point']['$'].id +} + /** * Return start date * @param {string} result @@ -9,7 +22,7 @@ const moment = require('moment') */ function parseSgeXmlTechnicalData(result) { log('info', 'Parsing technical data') - let json = JSON.stringify(result) + const json = JSON.stringify(result) return JSON.parse(json)['Envelope']['Body'][ 'consulterDonneesTechniquesContractuellesResponse' ]['point']['donneesGenerales'][ @@ -24,7 +37,7 @@ function parseSgeXmlTechnicalData(result) { */ function parseSgeXmlData(result) { log('info', 'Parsing list of documents') - let json = JSON.stringify(result) + const json = JSON.stringify(result) return JSON.parse(json)['Envelope']['Body'][ 'consulterMesuresDetailleesResponse' ]['grandeur']['mesure'] @@ -38,7 +51,7 @@ function parseSgeXmlData(result) { async function formateDataForDoctype(data) { log('info', 'Formating data') return data.map(record => { - let date = moment(record.d, 'YYYY/MM/DD h:mm:ss') + const date = moment(record.d, 'YYYY/MM/DD h:mm:ss') return { load: record.v, year: parseInt(date.format('YYYY')), @@ -82,4 +95,5 @@ module.exports = { formateDataForDoctype, parseTags, parseValue, + parseUserPdl, } diff --git a/src/requests/sge.js b/src/requests/sge.js index 0dd4b07..22669bd 100644 --- a/src/requests/sge.js +++ b/src/requests/sge.js @@ -125,11 +125,15 @@ function consulterDonneesTechniquesContractuelles(pointId, appLogin) { * @param {string} name * @param {string} postalCode * @param {string} inseeCode - * @param {string} address + * @param {string} [address] * @returns {string} PDL */ function rechercherPoint(appLogin, name, postalCode, inseeCode, address) { - log('info', `Query rechercherPoint - postal code: ${postalCode}`) + log( + 'info', + `Query rechercherPoint - postal code / insee code: ${postalCode} / ${inseeCode}` + ) + //TODO: handle address return `<?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://www.enedis.fr/sge/b2b/services/rechercherpoint/v2.0" -- GitLab