diff --git a/__tests__/helpers/parsing.spec.js b/__tests__/helpers/parsing.spec.js index 54d484b0859e6de2861edf33872083ab0721c8dc..b2e6d0c8d1e081a33fd1f7a18b7885d274d25413 100644 --- a/__tests__/helpers/parsing.spec.js +++ b/__tests__/helpers/parsing.spec.js @@ -11,6 +11,7 @@ const { parseValue, removeMultipleSpaces, removeAddressNumber, + removeDots, parsePointId, } = require('../../src/helpers/parsing') @@ -198,6 +199,19 @@ describe('parsing', () => { expect(reply).toBe('20 rue du lac') }) + it('should remove dots ✅', () => { + const reply = removeDots('.....03G2') + expect(reply).toBe('03G2') + }) + it('should remove dots and replace them with 1 space char if they are middle dots ✅', () => { + const reply = removeDots('.....03..G.2') + expect(reply).toBe('03 G 2') + }) + it('should not change anything when there are no dots ✅', () => { + const reply = removeDots('ETG 4 D') + expect(reply).toBe('ETG 4 D') + }) + describe('removeAddressNumber', () => { it('should remove address number ✅', () => { const reply = removeAddressNumber('20 rue du lac') diff --git a/src/core/verifyUserIdentity.js b/src/core/verifyUserIdentity.js index d1729be88d14cfe3d0a0676e841e152f1fc0587e..c66697f20d3b47acd0f5322503b0aed897216ac2 100644 --- a/src/core/verifyUserIdentity.js +++ b/src/core/verifyUserIdentity.js @@ -5,6 +5,7 @@ const { getInseeCode } = require('../requests/insee') const { findUserAddress } = require('./findUserAddress') const { removeMultipleSpaces, + removeDots, removeAddressNumber, } = require('../helpers/parsing') const Sentry = require('@sentry/node') @@ -78,7 +79,7 @@ async function verifyUserIdentity( escalierEtEtageEtAppartement ) - // Third try, remove address number because it's buggy on SGE side + // Third try, remove address number if (!pdl) { log('warn', 'Third try onboarding for sge') pdl = await findUserPdl( @@ -92,9 +93,24 @@ async function verifyUserIdentity( ) } - // Fourth try, remove address number and add escalierEtEtageEtAppartement because it's buggy on SGE side + // Fourth try, add escalierEtEtageEtAppartement if (!pdl) { log('warn', 'Fourth try onboarding for sge') + pdl = await findUserPdl( + `${baseUrl}/enedis_SDE_recherche-point/1.0`, + apiAuthKey, + loginUtilisateur, + lastname, + removeMultipleSpaces(userAddress.numeroEtNomVoie), + userAddress.codePostal, + userAddress.commune.$.code, + removeDots(escalierEtEtageEtAppartement) + ) + } + + // Fifth try, remove address number and add escalierEtEtageEtAppartement + if (!pdl) { + log('warn', 'Fifth try onboarding for sge') pdl = await findUserPdl( `${baseUrl}/enedis_SDE_recherche-point/1.0`, apiAuthKey, @@ -103,7 +119,7 @@ async function verifyUserIdentity( removeMultipleSpaces(removeAddressNumber(userAddress.numeroEtNomVoie)), userAddress.codePostal, userAddress.commune.$.code, - escalierEtEtageEtAppartement + removeDots(escalierEtEtageEtAppartement) ) } diff --git a/src/helpers/parsing.js b/src/helpers/parsing.js index 420c4447ae91ba08b487b6e9c00d88e7796eee44..4258c65343fd31a8d5be89a52a910676d7502a10 100644 --- a/src/helpers/parsing.js +++ b/src/helpers/parsing.js @@ -162,6 +162,32 @@ function removeMultipleSpaces(str) { return str.replace(/ +/g, ' ') } +/** + * Remove SGE useless dots + * This regular expression matches one or more consecutive dots + * and then in the replacement function, it checks if the dots are surrounded by non-dot characters. + * If so, it replaces them with a space; otherwise, it removes them + * @example + * console.log(removeDots(".....03G2")); // Outputs: "03G2" + * console.log(removeDots("....ETG..4...D")); // Outputs: "ETG 4 D" + * console.log(removeDots("ETG 4 D")); // Outputs: "ETG 4 D" + * @param {string} input - The input string containing dots to be removed. + * @returns {string} The input string without dots. + */ +function removeDots(input) { + return input.replace(/\.+/g, (match, offset, string) => { + if ( + offset > 0 && + offset < string.length - match.length && + string[offset - 1] !== '.' && + string[offset + match.length] !== '.' + ) { + return ' ' + } + return '' + }) +} + /** * Remove SGE address number * @param {string} str @@ -203,4 +229,5 @@ module.exports = { parseValueHalfHour, removeAddressNumber, removeMultipleSpaces, + removeDots, }