@gouvfr-anct/mednum
Version:
✨ Permet de transformer une source de données vers le schéma des lieux de mediation numerique
69 lines (68 loc) • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processContact = void 0;
const lieux_de_mediation_numerique_1 = require("@gouvfr-anct/lieux-de-mediation-numerique");
const clean_operations_1 = require("./clean-operations");
const toInternationalFormat = (phone) => (/^0\d{9}$/.test(phone) ? `+33${phone.slice(1)}` : phone);
const telephoneField = (telephone) => telephone == null
? {}
: {
telephone: toInternationalFormat(telephone
.toString()
.replace(/[\s,.-]/g, '')
.replace('(0)', '')
.trim())
};
const siteWebField = (siteWeb) => siteWeb == null ? {} : { site_web: siteWeb.split('|').map(lieux_de_mediation_numerique_1.Url) };
const courrielField = (courriels) => courriels == null || courriels === '' ? {} : { courriels: courriels.split('|').map(lieux_de_mediation_numerique_1.Courriel) };
const toLieuxMediationNumeriqueContact = (source, matching) => (0, lieux_de_mediation_numerique_1.Contact)({
...(matching.telephone?.colonne == null ? {} : telephoneField(source[matching.telephone.colonne]?.toString())),
...(matching.site_web?.colonne == null ? {} : siteWebField(source[matching.site_web.colonne]?.toString()?.toLowerCase())),
...(matching.courriels?.colonne == null ? {} : courrielField(source[matching.courriels.colonne]?.toString()?.toLowerCase()))
});
const testCleanSelector = (cleanOperation, property) => property != null && new RegExp(cleanOperation.selector, 'u').test(property);
const shouldApplyFix = (cleanOperation, property) => cleanOperation.negate === true ? !testCleanSelector(cleanOperation, property) : testCleanSelector(cleanOperation, property);
const applyRemoveFix = (recorder) => (cleanOperation, valueToFix, source) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [cleanOperation.field]: removedProperty, ...filteredProperties } = source;
recorder.fix({
apply: cleanOperation.name,
before: valueToFix
});
return filteredProperties;
};
const applyUpdateFix = (recorder) => (cleanOperation, valueToFix, source) => {
const cleanValue = cleanOperation.fix(valueToFix);
recorder.fix({
apply: cleanOperation.name,
before: valueToFix,
after: cleanValue
});
return {
...source,
...{
[cleanOperation.field]: cleanValue
}
};
};
const canFix = (cleanOperation) => cleanOperation.fix != null;
const applyCleanOperation = (recorder) => (cleanOperation, valueToFix, source) => canFix(cleanOperation)
? applyUpdateFix(recorder)(cleanOperation, valueToFix, source)
: applyRemoveFix(recorder)(cleanOperation, valueToFix, source);
const toFixedContact = (recorder) => (source) => (contact, cleanOperation) => contact == null && shouldApplyFix(cleanOperation, source[cleanOperation.field]?.toString())
? applyCleanOperation(recorder)(cleanOperation, source[cleanOperation.field]?.toString() ?? '', source)
: contact;
const cannotFixContact = (error) => {
throw error;
};
const retryOrThrow = (recorder) => (fixedContact, matching, error) => fixedContact == null ? cannotFixContact(error) : (0, exports.processContact)(recorder)(fixedContact, matching);
const fixAndRetry = (recorder) => (source, matching, error) => retryOrThrow(recorder)((0, clean_operations_1.cleanOperations)(matching, source[matching.code_postal.colonne]?.toString()).reduce(toFixedContact(recorder)(source), undefined), matching, error);
const processContact = (recorder) => (source, matching) => {
try {
return toLieuxMediationNumeriqueContact(source, matching);
}
catch (error) {
return fixAndRetry(recorder)(source, matching, error);
}
};
exports.processContact = processContact;