diff --git a/lib/api/consumers/format-to-legacy-helpers.js b/lib/api/consumers/format-to-legacy-helpers.js index 473b8811..b88712ca 100644 --- a/lib/api/consumers/format-to-legacy-helpers.js +++ b/lib/api/consumers/format-to-legacy-helpers.js @@ -107,15 +107,7 @@ export const formatCommonToponymDataForLegacy = (commonToponym, district, pseudo const codeAncienneCommune = meta?.bal?.codeAncienneCommune const legacyCommonToponymFantoirId = meta?.dgfip?.fantoir ? `${cog}_${meta?.dgfip?.fantoir}` : null - let legacyCommonToponymId = legacyCommonToponymFantoirId - // If the legacy common toponym id is already used or not defined, we calculate a pseudo code - if (!legacyCommonToponymId || commonToponymLegacyIDSet.has(legacyCommonToponymId)) { - legacyCommonToponymId = `${cog}_${pseudoCodeVoieGenerator.getCode(legacyLabelValue, codeAncienneCommune)}`.toLowerCase() - // If the pseudo code is already used, we generate a new one with a hash from the common toponym id - if (commonToponymLegacyIDSet.has(legacyCommonToponymId)) { - legacyCommonToponymId = `${cog}_${createHmac('sha256', 'ban').update(id).digest('hex').slice(0, 6)}` - } - } + const legacyCommonToponymId = getLegacyCommonToponymId(commonToponymLegacyIDSet, legacyCommonToponymFantoirId, meta, {cog, id, pseudoCodeVoieGenerator, legacyLabelValue, codeAncienneCommune}) // Store the legacy common toponym id for each common toponym to then be able to set it on legacy addresses commonToponymLegacyIDCommonToponymIDMap.set(id, legacyCommonToponymId) @@ -212,7 +204,7 @@ export const formatAddressDataForLegacy = (address, district, commonToponymLegac // Ids const legacyCommonToponymId = commonToponymLegacyIDCommonToponymIDMap.get(mainCommonToponymID) - const legacyInteropKey = `${legacyCommonToponymId}_${String(number).padStart(5, '0')}${suffix ? `_${suffix}` : ''}`.toLowerCase() + const legacyInteropKey = getAddressLegacyInteropKey(meta, {legacyCommonToponymId, number, suffix}) const legacyID = getAddressLegacyId(addressLegacyIDSet, legacyInteropKey) addressLegacyIDSet.add(legacyID) const banIdSecondaryCommonToponyms = secondaryCommonToponymIDs && secondaryCommonToponymIDs.length > 0 ? secondaryCommonToponymIDs : null @@ -332,3 +324,70 @@ const getAddressLegacyId = (addressLegacyIDSet, legacyInteropKey, suffix = 0) => return `${legacyInteropKey}` } + +const getAddressLegacyInteropKey = (meta, {legacyCommonToponymId, number, suffix}) => { + const cleInterop = meta.bal?.cleInterop + const isValidInteropKey = checkIfCleInteropIsValid(cleInterop) + const addressLegacyInteropKey = isValidInteropKey + ? cleInterop + : `${legacyCommonToponymId}_${String(number).padStart(5, '0')}${suffix ? `_${suffix}` : ''}`.toLowerCase() + + return addressLegacyInteropKey +} + +const getLegacyCommonToponymId = ( + commonToponymLegacyIDSet, + legacyCommonToponymFantoirId, + meta, + {cog, id, pseudoCodeVoieGenerator, legacyLabelValue, codeAncienneCommune} +) => { + let legacyCommonToponymId + const cleInterop = meta.bal?.cleInterop + const isCleInteropValid = checkIfCleInteropIsValid(cleInterop) + if (isCleInteropValid) { + const lastUnderscoreIndex = cleInterop.lastIndexOf('_') + legacyCommonToponymId = cleInterop.slice(0, lastUnderscoreIndex) + } else { + legacyCommonToponymId = legacyCommonToponymFantoirId + } + + // If the legacy common toponym id is already used or not defined, we calculate a pseudo code + if (!legacyCommonToponymId || commonToponymLegacyIDSet.has(legacyCommonToponymId)) { + legacyCommonToponymId = `${cog}_${pseudoCodeVoieGenerator.getCode(legacyLabelValue, codeAncienneCommune)}`.toLowerCase() + // If the pseudo code is already used, we generate a new one with a hash from the common toponym id + if (commonToponymLegacyIDSet.has(legacyCommonToponymId)) { + legacyCommonToponymId = `${cog}_${createHmac('sha256', 'ban').update(id).digest('hex').slice(0, 6)}` + } + } + + return legacyCommonToponymId +} + +const checkIfCleInteropIsValid = cleInterop => { + if (!cleInterop) { + return false + } + + // https://aitf-sig-topo.github.io/voies-adresses/files/AITF_SIG_Topo_Format_Base_Adresse_Locale_v1.3.pdf + // ● INSEE code with 5 characters + // ● Street code: the unique street identifier provided by the national unique service, + // or the FANTOIR DGFiP code, consisting of 4 to 6 alphanumeric characters + // ● Address number consisting of 5 characters, prefixed with zeros if necessary + // ● Suffix (bis / ter / qua / qui / a / b / c...). The repetition indices “bis, ter…” will + // be coded with 3 characters, and others (a, b, c, a1, b2...) will be in lowercase + // without a fixed number of characters. + // ● Each item is separated by an underscore “_” + // ● Everything is in lowercase + + const cleInteropFormatRegex = /^\d{5}_[a-z\d]{4,6}_\d{5}(_[a-z]{3}|_[a-z])?(_[a-z])*$/ + if (!cleInteropFormatRegex.test(cleInterop)) { + return false + } + + // Check if the street code is 'xxxx' to detect the non-valid pseudo code created by mes-adresses + if (cleInterop.split('_')[1] === 'xxxx') { + return false + } + + return true +}