UNPKG

@claudebernard/fhir-mapper

Version:

A simple FHIR / BCB resource mapper to help stay interoperable while still using the Claude Bernard intelligence

809 lines (797 loc) 52.7 kB
var unitsOfTime = { 's': { value: 0, label: 'seconde' }, 'min': { value: 10, label: 'minute' }, 'h': { value: 20, label: 'heure' }, 'd': { value: 40, label: 'jour' }, 'wk': { value: 50, label: 'semaine' }, 'mo': { value: 70, label: 'mois' }, 'a': { value: 100, label: 'an' } }; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; function buildDoseAndRate(dosage) { var _a; var coding = { system: dosage.codeUnitePrise ? 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/intake-units' : undefined, code: (_a = dosage.codeUnitePrise) !== null && _a !== undefined ? _a : undefined, unit: dosage.codeUnitePrise ? dosage.libUnitePrise : undefined }; var doseAndRate = dosage.quantite2 ? { doseRange: { low: __assign({ value: dosage.quantite1 }, coding), high: __assign({ value: dosage.quantite2 }, coding) } } : { doseQuantity: __assign({ value: dosage.quantite1 }, coding) }; return [doseAndRate]; } // The formula used here is count = dose * frequency * (duration / period) function calculateCount(dose, frequency, period, periodUnit, duration, durationUnit) { // Provide default values for null or undefined parameters var actualDose = dose !== null && dose !== undefined ? dose : 0; var actualFrequency = frequency !== null && frequency !== undefined ? frequency : 0; var actualPeriod = period !== null && period !== undefined ? period : 1; var actualDuration = duration !== null && duration !== undefined ? duration : 0; var actualPeriodUnit = periodUnit !== null && periodUnit !== undefined ? periodUnit : ''; var actualDurationUnit = durationUnit !== null && durationUnit !== undefined ? durationUnit : ''; // Convert duration to the same unit as period var convertedDuration = actualDurationUnit === actualPeriodUnit ? actualDuration : convertTime(actualDuration, actualDurationUnit, actualPeriodUnit); // Check if dose, frequency, convertedDuration, or period is null or 0 and compute count accordingly if (!actualDose) { return actualFrequency * (convertedDuration / actualPeriod); } else if (!actualFrequency) { return actualDose * (convertedDuration / actualPeriod); } else if (!convertedDuration) { return actualDose * actualFrequency * actualPeriod; } else if (!actualPeriod) { return actualDose * actualFrequency * convertedDuration; } else { return actualDose * actualFrequency * (convertedDuration / actualPeriod); } } function convertTime(value, fromUnit, toUnit) { var conversionFactors = { 's': 1, 'min': 60, 'h': 3600, 'd': 86400, 'wk': 604800, 'mo': 2628000, 'a': 31536000 }; if (!conversionFactors[fromUnit]) { throw new Error("Invalid fromUnit: ".concat(fromUnit)); } if (!conversionFactors[toUnit]) { throw new Error("Invalid toUnit: ".concat(toUnit)); } // Convert the input value to seconds var valueInSeconds = value * conversionFactors[fromUnit]; // Convert from seconds to the desired unit var convertedValue = valueInSeconds / conversionFactors[toUnit]; return convertedValue; } function handleCodifications(dosage, indicationMapper, routeMapper, intakeMapper) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11; var conditionCode; var conditionLabel; var intakeCode; var intakeLabel; var routeCode; var routeLabel; var errors = []; if (dosage.asNeededFor && dosage.asNeededFor[0].coding && dosage.asNeededFor[0].coding.length > 0) { if (((_c = (_b = (_a = dosage.asNeededFor[0]) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.system) === 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/indications') { conditionCode = (_g = (_f = (_e = (_d = dosage.asNeededFor) === null || _d === undefined ? undefined : _d[0]) === null || _e === undefined ? undefined : _e.coding) === null || _f === undefined ? undefined : _f[0]) === null || _g === undefined ? undefined : _g.code; conditionLabel = (_l = (_k = (_j = (_h = dosage.asNeededFor) === null || _h === undefined ? undefined : _h[0]) === null || _j === undefined ? undefined : _j.coding) === null || _k === undefined ? undefined : _k[0]) === null || _l === undefined ? undefined : _l.display; } else if (indicationMapper) { var indication = indicationMapper((_p = (_o = (_m = dosage === null || dosage === undefined ? undefined : dosage.asNeededFor) === null || _m === undefined ? undefined : _m[0]) === null || _o === undefined ? undefined : _o.coding) === null || _p === undefined ? undefined : _p[0]); conditionCode = indication === null || indication === undefined ? undefined : indication.code; conditionLabel = indication === null || indication === undefined ? undefined : indication.label; } else { errors.push({ field: 'codeIndication', message: 'The system used in the asNeededFor coding field is not supported and no custom mapper was provided' }); } } if (dosage.route && dosage.route.coding && dosage.route.coding.length > 0) { if (((_s = (_r = (_q = dosage.route) === null || _q === undefined ? undefined : _q.coding) === null || _r === undefined ? undefined : _r[0]) === null || _s === undefined ? undefined : _s.system) === 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/routes') { routeCode = parseInt((_w = (_v = (_u = (_t = dosage === null || dosage === undefined ? undefined : dosage.route) === null || _t === undefined ? undefined : _t.coding) === null || _u === undefined ? undefined : _u[0]) === null || _v === undefined ? undefined : _v.code) !== null && _w !== undefined ? _w : ''); routeLabel = (_z = (_y = (_x = dosage.route) === null || _x === undefined ? undefined : _x.coding) === null || _y === undefined ? undefined : _y[0]) === null || _z === undefined ? undefined : _z.display; } else if (dosage.route && routeMapper) { var route = routeMapper((_1 = (_0 = dosage.route) === null || _0 === undefined ? undefined : _0.coding) === null || _1 === undefined ? undefined : _1[0]); routeCode = parseInt(route === null || route === undefined ? undefined : route.code); routeLabel = route === null || route === undefined ? undefined : route.label; } else { errors.push({ field: 'codeVoie', message: 'The system used in the route coding field is not supported and no custom mapper was provided' }); } } if (dosage.doseAndRate && dosage.doseAndRate.length > 0) { if (((_4 = (_3 = (_2 = dosage.doseAndRate) === null || _2 === undefined ? undefined : _2[0]) === null || _3 === undefined ? undefined : _3.doseQuantity) === null || _4 === undefined ? undefined : _4.system) === 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/intake-units') { intakeCode = parseInt((_7 = (_6 = (_5 = dosage.doseAndRate) === null || _5 === undefined ? undefined : _5[0]) === null || _6 === undefined ? undefined : _6.doseQuantity.code) !== null && _7 !== undefined ? _7 : ''); intakeLabel = (_9 = (_8 = dosage.doseAndRate) === null || _8 === undefined ? undefined : _8[0]) === null || _9 === undefined ? undefined : _9.doseQuantity.unit; } else if (dosage.doseAndRate && intakeMapper) { var intake = intakeMapper((_11 = (_10 = dosage.doseAndRate) === null || _10 === undefined ? undefined : _10[0]) === null || _11 === undefined ? undefined : _11.doseQuantity); intakeCode = parseInt(intake === null || intake === undefined ? undefined : intake.code); intakeLabel = intake === null || intake === undefined ? undefined : intake.label; } else { errors.push({ field: 'codeUnitePrise', message: 'The system used in the doseQuantity coding field is not supported and no custom mapper was provided' }); } } return { conditionCode: conditionCode, conditionLabel: conditionLabel, intakeCode: intakeCode, intakeLabel: intakeLabel, routeCode: routeCode, routeLabel: routeLabel, codificationErrors: errors }; } // adding this function to support new naming convention without breaking changes function fhirToBcb$1(dosageInstructions, indicationMapper, routeMapper, intakeMapper) { return mapToBcb(dosageInstructions, indicationMapper, routeMapper, intakeMapper); } function mapToBcb(dosageInstructions, indicationMapper, routeMapper, intakeMapper) { if (!dosageInstructions || dosageInstructions.length === 0) { return [ { result: undefined, errors: [{ field: 'root', message: 'Invalid Dosage array' }] } ]; } return dosageInstructions.map(function (dosage) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18; var errors = []; var periodUnit = (_b = (_a = dosage.timing) === null || _a === undefined ? undefined : _a.repeat) === null || _b === undefined ? undefined : _b.periodUnit; var quantite1 = (_f = (_e = (_d = (_c = dosage === null || dosage === undefined ? undefined : dosage.doseAndRate) === null || _c === undefined ? undefined : _c[0]) === null || _d === undefined ? undefined : _d.doseQuantity) === null || _e === undefined ? undefined : _e.value) !== null && _f !== undefined ? _f : (_k = (_j = (_h = (_g = dosage === null || dosage === undefined ? undefined : dosage.doseAndRate) === null || _g === undefined ? undefined : _g[0]) === null || _h === undefined ? undefined : _h.doseRange) === null || _j === undefined ? undefined : _j.low) === null || _k === undefined ? undefined : _k.value; var quantite2 = (_q = (_p = (_o = (_m = (_l = dosage === null || dosage === undefined ? undefined : dosage.doseAndRate) === null || _l === undefined ? undefined : _l[0]) === null || _m === undefined ? undefined : _m.doseRange) === null || _o === undefined ? undefined : _o.high) === null || _p === undefined ? undefined : _p.value) !== null && _q !== undefined ? _q : null; var maximum = null; var codeDuree3 = null; var maxDosePerDay = null; if (dosage.maxDosePerPeriod) { maxDosePerDay = dosage.maxDosePerPeriod.find(function (period) { var _a, _b; return ((_a = period === null || period === undefined ? undefined : period.denominator) === null || _a === undefined ? undefined : _a.unit) === 'd' && ((_b = period === null || period === undefined ? undefined : period.denominator) === null || _b === undefined ? undefined : _b.value) === 1; }); if (maxDosePerDay) { maximum = (_r = maxDosePerDay === null || maxDosePerDay === undefined ? undefined : maxDosePerDay.numerator) === null || _r === undefined ? undefined : _r.value; codeDuree3 = (_s = maxDosePerDay === null || maxDosePerDay === undefined ? undefined : maxDosePerDay.denominator) === null || _s === undefined ? undefined : _s.unit; } } var libSpecifPrise1 = dosage.asNeeded ? 'selon besoin' : null; var doseSpacing = (_t = dosage.maxDosePerPeriod) === null || _t === undefined ? undefined : _t.find(function (period) { var _a; return ((_a = period === null || period === undefined ? undefined : period.denominator) === null || _a === undefined ? undefined : _a.unit) === 'h'; }); if (doseSpacing) { var spacingText = "en espa\u00E7ant les prises de ".concat((_u = doseSpacing === null || doseSpacing === undefined ? undefined : doseSpacing.denominator) === null || _u === undefined ? undefined : _u.value, "h minimum"); libSpecifPrise1 = libSpecifPrise1 ? "".concat(libSpecifPrise1, ", ").concat(spacingText) : spacingText; } var _19 = handleCodifications(dosage, indicationMapper, routeMapper, intakeMapper), conditionCode = _19.conditionCode, conditionLabel = _19.conditionLabel, intakeCode = _19.intakeCode, intakeLabel = _19.intakeLabel, routeCode = _19.routeCode, routeLabel = _19.routeLabel, codificationErrors = _19.codificationErrors; var codeDuree2 = (_x = (_w = (_v = dosage === null || dosage === undefined ? undefined : dosage.timing) === null || _v === undefined ? undefined : _v.repeat) === null || _w === undefined ? undefined : _w.boundsDuration) === null || _x === undefined ? undefined : _x.code; var bcbDosage = { noPosologie: (_y = dosage.sequence) !== null && _y !== undefined ? _y : null, libellePosologie: (_z = dosage.text) !== null && _z !== undefined ? _z : null, codeIndication: conditionCode !== null && conditionCode !== undefined ? conditionCode : null, libIndication: conditionLabel !== null && conditionLabel !== undefined ? conditionLabel : null, codeDuree1: periodUnit ? (_0 = unitsOfTime[periodUnit]) === null || _0 === undefined ? undefined : _0.value : null, libDuree1: periodUnit ? (_1 = unitsOfTime[periodUnit]) === null || _1 === undefined ? undefined : _1.label : null, quantite1: quantite1 !== null && quantite1 !== undefined ? quantite1 : null, quantite2: quantite2 !== null && quantite2 !== undefined ? quantite2 : null, tousLes: (_4 = (_3 = (_2 = dosage.timing) === null || _2 === undefined ? undefined : _2.repeat) === null || _3 === undefined ? undefined : _3.period) !== null && _4 !== undefined ? _4 : null, combien1: (_7 = (_6 = (_5 = dosage.timing) === null || _5 === undefined ? undefined : _5.repeat) === null || _6 === undefined ? undefined : _6.frequency) !== null && _7 !== undefined ? _7 : null, combien2: (_10 = (_9 = (_8 = dosage.timing) === null || _8 === undefined ? undefined : _8.repeat) === null || _9 === undefined ? undefined : _9.frequencyMax) !== null && _10 !== undefined ? _10 : null, pendant1: (_14 = (_13 = (_12 = (_11 = dosage.timing) === null || _11 === undefined ? undefined : _11.repeat) === null || _12 === undefined ? undefined : _12.boundsDuration) === null || _13 === undefined ? undefined : _13.value) !== null && _14 !== undefined ? _14 : null, codeUnitePrise: intakeCode !== null && intakeCode !== undefined ? intakeCode : null, libUnitePrise: intakeLabel !== null && intakeLabel !== undefined ? intakeLabel : null, codeVoie: routeCode !== null && routeCode !== undefined ? routeCode : null, libVoie: routeLabel !== null && routeLabel !== undefined ? routeLabel : null, codeDuree2: codeDuree2 ? (_15 = unitsOfTime[codeDuree2]) === null || _15 === undefined ? undefined : _15.value : null, libDuree2: codeDuree2 ? (_16 = unitsOfTime[codeDuree2]) === null || _16 === undefined ? undefined : _16.label : null, maximum: maximum !== null && maximum !== undefined ? maximum : null, codeDuree3: codeDuree3 ? (_17 = unitsOfTime[codeDuree3]) === null || _17 === undefined ? undefined : _17.value : null, libDuree3: codeDuree3 ? (_18 = unitsOfTime[codeDuree3]) === null || _18 === undefined ? undefined : _18.label : null, libSpecifPrise1: libSpecifPrise1 !== null && libSpecifPrise1 !== undefined ? libSpecifPrise1 : null }; return { result: bcbDosage, errors: codificationErrors.concat(errors) }; }); } // adding this function to support new naming convention without breaking changes function bcbToFhir$1(bcbDosages) { return mapToFhir(bcbDosages); } function mapToFhir(bcbDosages) { if (!bcbDosages || bcbDosages.length === 0) { throw new Error('Invalid BCBPosologieStructuree2 array'); } return bcbDosages.map(function (dosage) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; var keys = Object.keys(unitsOfTime); var periodUnit = (_a = keys.find(function (key) { return unitsOfTime[key].value === dosage.codeDuree1; })) !== null && _a !== undefined ? _a : undefined; var durationUnit = (_b = keys.find(function (key) { return unitsOfTime[key].value === dosage.codeDuree2; })) !== null && _b !== undefined ? _b : undefined; var maximumUnit = (_c = keys.find(function (key) { return unitsOfTime[key].value === dosage.codeDuree3; })) !== null && _c !== undefined ? _c : undefined; var route = dosage.codeVoie ? { coding: [{ system: 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/routes', code: dosage.codeVoie.toString(), display: dosage.libVoie }] } : undefined; var intake = dosage.codeUnitePrise ? { coding: [{ system: 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/intake-units', code: dosage.codeUnitePrise.toString(), display: dosage.libUnitePrise }] } : undefined; var indication = dosage.codeIndication ? [{ coding: [{ system: 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/indications', code: dosage.codeIndication, display: dosage.libIndication }] }] : undefined; var maxDosePerPeriod = dosage.maximum ? [{ numerator: { value: dosage.maximum, system: 'https://platform.claudebernard.fr/fhir/ImplementationGuides/ValueSet/intake-units', code: (_e = (_d = intake === null || intake === undefined ? undefined : intake.coding) === null || _d === undefined ? undefined : _d[0]) === null || _e === undefined ? undefined : _e.code, unit: (_g = (_f = intake === null || intake === undefined ? undefined : intake.coding) === null || _f === undefined ? undefined : _f[0]) === null || _g === undefined ? undefined : _g.display }, denominator: { value: 1, unit: maximumUnit } }] : undefined; var doseAndRate = buildDoseAndRate(dosage); var boundsDuration = dosage.pendant1 ? { value: dosage.pendant1, unit: durationUnit } : undefined; var asNeeded = (_h = dosage.libSpecifPrise1) === null || _h === undefined ? undefined : _h.includes('besoin'); return { result: { text: dosage.libellePosologie, sequence: dosage.noPosologie, doseAndRate: doseAndRate, maxDosePerPeriod: dosage.maximum ? maxDosePerPeriod : undefined, route: route, asNeeded: asNeeded, asNeededFor: indication, timing: { repeat: { frequency: dosage.combien1, frequencyMax: dosage.combien2, period: dosage.tousLes, periodUnit: periodUnit, boundsDuration: boundsDuration, count: calculateCount(dosage.quantite1, dosage.combien1, dosage.tousLes, periodUnit, dosage.pendant1, durationUnit), countMax: calculateCount((_j = dosage.quantite2) !== null && _j !== undefined ? _j : dosage.quantite1, (_k = dosage.combien2) !== null && _k !== undefined ? _k : dosage.combien1, dosage.tousLes, periodUnit, (_l = dosage.pendant2) !== null && _l !== undefined ? _l : dosage.pendant1, durationUnit) } } }, errors: [] }; }); } var dosageMapper = /*#__PURE__*/Object.freeze({ __proto__: null, bcbToFhir: bcbToFhir$1, fhirToBcb: fhirToBcb$1, mapToBcb: mapToBcb, mapToFhir: mapToFhir }); var genderMap = { male: 'M', female: 'F' }; var fhirGenderMap = { M: 'male', F: 'female' }; var hepaticInsufficiencyMap = { 'A': { code: 'K72.90', display: 'Mild hepatic insufficiency' }, 'B': { code: 'K72.91', display: 'Moderate hepatic insufficiency' }, 'C': { code: 'K72.92', display: 'Severe hepatic insufficiency' } }; var hepaticInsufficiencyReverseMap = { 'K72.90': 'A', 'K72.91': 'B', 'K72.92': 'C' }; var calculateAgeInMonths = function (birthDate) { var birth = new Date(birthDate); var today = new Date(); var yearsDifference = today.getFullYear() - birth.getFullYear(); var monthsDifference = today.getMonth() - birth.getMonth(); var daysDifference = today.getDate() - birth.getDate(); var ageInMonths = yearsDifference * 12 + monthsDifference; if (daysDifference < 0) { ageInMonths--; } return ageInMonths; }; var calculateBirthDateFromAgeInMonths = function (ageInMonths) { var today = new Date(); var birthDate = new Date(today.setMonth(today.getMonth() - ageInMonths)); var year = birthDate.getFullYear(); var month = (birthDate.getMonth() + 1).toString().padStart(2, '0'); var day = birthDate.getDate().toString().padStart(2, '0'); return "".concat(year, "-").concat(month, "-").concat(day); }; var convertPatient = function (bcbPatient) { return { age: bcbPatient.age, gender: bcbPatient.sexe, weight: bcbPatient.poids, pregnancy: bcbPatient.grossesse === 1, breastfeeding: bcbPatient.allaitement === 1, ammPathologies: bcbPatient.lstPathologiesAMM.map(function (code) { return ({ type: 'AMM', code: code, label: '' }); }), allergies: bcbPatient.lstIdComposantAllergie.map(function (code) { return ({ code: code, label: '' }); }), hepaticStage: bcbPatient.insuffisanceHepatique, height: bcbPatient.taille, weeksOfPregnancy: '0', creatinineClearance: bcbPatient.clairanceCreatinine, molCreatinine: bcbPatient.creatininemieMol, mglCreatinine: bcbPatient.creatininemieMg, gfr: 0, medicalTeam: {} }; }; var sortEntries = function (target, entries, allergiesMapper, snomedPathologiesMapper, errors) { var _a; if (errors === undefined) { errors = []; } var patientEntry = (_a = entries.find(function (entry) { var _a; return ((_a = entry === null || entry === undefined ? undefined : entry.resource) === null || _a === undefined ? undefined : _a.resourceType) === "Patient"; })) === null || _a === undefined ? undefined : _a.resource; var observationEntries = entries.filter(function (entry) { var _a; return ((_a = entry === null || entry === undefined ? undefined : entry.resource) === null || _a === undefined ? undefined : _a.resourceType) === "Observation"; }).map(function (entry) { return entry === null || entry === undefined ? undefined : entry.resource; }); var conditionEntries = entries.filter(function (entry) { var _a; return ((_a = entry === null || entry === undefined ? undefined : entry.resource) === null || _a === undefined ? undefined : _a.resourceType) === "Condition"; }).map(function (entry) { return entry === null || entry === undefined ? undefined : entry.resource; }); var allergyIntoleranceEntries = entries.filter(function (entry) { var _a; return ((_a = entry === null || entry === undefined ? undefined : entry.resource) === null || _a === undefined ? undefined : _a.resourceType) === "AllergyIntolerance"; }).map(function (entry) { return entry === null || entry === undefined ? undefined : entry.resource; }); var weightObservation = observationEntries.find(function (entry) { var _a, _b, _c; return ((_c = (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.code) === '29463-7'; }); var heightObservation = observationEntries.find(function (entry) { var _a, _b, _c; return ((_c = (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.code) === '8302-2'; }); var pregnancyObservation = observationEntries.find(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.code === '82810-3'; }); }); var breastfeedingObservation = observationEntries.find(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.code === '63895-7'; }); }); var hepaticCondition = conditionEntries.find(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.code === '2164-2' || coding.code === '14682-9' || coding.code === '2160-0'; }); }); var lstIdComposantAllergie = []; var errorField = target === 'bcb' ? 'lstIdComposantAllergie' : 'allergies'; allergyIntoleranceEntries.forEach(function (allergyEntry) { var _a, _b, _c; if (allergiesMapper) { var mappingResult = allergiesMapper(allergyEntry === null || allergyEntry === undefined ? undefined : allergyEntry.code); if (mappingResult) { lstIdComposantAllergie.push(mappingResult); } else { errors.push({ field: errorField, message: "No mapping found for allergy code ".concat((_c = (_b = (_a = allergyEntry === null || allergyEntry === undefined ? undefined : allergyEntry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.code) }); } } else { errors.push({ field: errorField, message: 'Allergy Intolerance values were found but no allergies mapper was provided, the output field is thus left empty.' }); } }); var lstCim10Pathologies = conditionEntries.filter(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.system === 'http://hl7.org/fhir/sid/icd-10'; }); }).map(function (pathology) { var _a, _b, _c; return ({ code: ((_c = (_b = (_a = pathology === null || pathology === undefined ? undefined : pathology.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.code) || '', label: '' }); }); var lstSnomedPathologies = conditionEntries.filter(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.system === 'http://hl7.org/fhir/snomed-ct'; }); }); var lstAmmPathologies = conditionEntries.filter(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.system === 'https://platform.claudebernard.fr/fhir/ImplementationGuides/CodeSystem/amm'; }); }); var lstPathologies = []; lstAmmPathologies ? lstPathologies = lstAmmPathologies.map(function (pathology) { var _a, _b, _c; return ({ code: ((_c = (_b = (_a = pathology === null || pathology === undefined ? undefined : pathology.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.code) || '', label: '' }); }) : []; errorField = target === 'bcb' ? 'lstPathologiesAMM' : 'ammPathologies'; lstSnomedPathologies.forEach(function (pathology) { var _a, _b, _c; if (snomedPathologiesMapper) { var mappingResult = snomedPathologiesMapper(pathology === null || pathology === undefined ? undefined : pathology.code); if (mappingResult) { lstPathologies.push(mappingResult); } else { errors.push({ field: errorField, message: "No mapping found for snomed code ".concat((_c = (_b = (_a = pathology === null || pathology === undefined ? undefined : pathology.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b[0]) === null || _c === undefined ? undefined : _c.code) }); } } else { errors.push({ field: errorField, message: "Snomed pathologies were found but no mapper was provided, the output field is thus left empty." }); } }); var clairanceCreatinineObservation = observationEntries.find(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.code === '2164-2'; }); }); var creatininemieMolObservation = observationEntries.find(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.code === '14682-9'; }); }); var creatininemieMgObservation = observationEntries.find(function (entry) { var _a, _b; return (_b = (_a = entry === null || entry === undefined ? undefined : entry.code) === null || _a === undefined ? undefined : _a.coding) === null || _b === undefined ? undefined : _b.some(function (coding) { return coding.code === '2160-0'; }); }); return { patientEntry: patientEntry, weightObservation: weightObservation, heightObservation: heightObservation, pregnancyObservation: pregnancyObservation, breastfeedingObservation: breastfeedingObservation, hepaticCondition: hepaticCondition, lstIdComposantAllergie: lstIdComposantAllergie, lstCim10Pathologies: lstCim10Pathologies, lstPathologies: lstPathologies, clairanceCreatinineObservation: clairanceCreatinineObservation, creatininemieMolObservation: creatininemieMolObservation, creatininemieMgObservation: creatininemieMgObservation }; }; var patientToFhir = function (patientData) { if (!patientData) { return { result: undefined, errors: [{ field: 'root', message: 'Invalid patient object' }] }; } var errors = []; var bundleEntries = []; var patient = { resourceType: 'Patient', id: '1', birthDate: calculateBirthDateFromAgeInMonths(patientData.age), gender: fhirGenderMap[patientData.gender] || 'unknown' }; var weightObservation = { resourceType: 'Observation', id: '2', code: { coding: [{ code: '29463-7', display: 'Body Weight' }] }, status: 'final', valueQuantity: { value: patientData.weight, unit: 'kg' }, subject: { reference: 'Patient/1' } }; var heightObservation = { resourceType: 'Observation', id: '3', code: { coding: [{ code: '8302-2', display: 'Body Height' }] }, status: 'final', valueQuantity: { value: patientData.height, unit: 'cm' }, subject: { reference: 'Patient/1' } }; var pregnancyObservation = undefined; if (patientData.pregnancy) { pregnancyObservation = { resourceType: 'Observation', id: '4', code: { coding: [{ code: '82810-3', display: 'Pregnancy Status' }] }, status: 'final', subject: { reference: 'Patient/1' } }; } if (pregnancyObservation && parseInt(patientData.weeksOfPregnancy) > 0) { pregnancyObservation.valueQuantity = { value: Number(patientData.weeksOfPregnancy), unit: 'weeks', system: 'http://unitsofmeasure.org', code: 'wk' }; } var breastfeedingObservation = undefined; if (patientData.breastfeeding) { breastfeedingObservation = { resourceType: 'Observation', id: '5', code: { coding: [{ code: '63895-7', display: 'Breastfeeding Status' }] }, status: 'final', subject: { reference: 'Patient/1' } }; } var creatinineClearanceObservation; if (patientData.creatinineClearance > 0) { creatinineClearanceObservation = { resourceType: 'Observation', id: '5', code: { coding: [{ system: 'http://loinc.org', code: '2164-2', display: 'Creatinine clearance' }] }, status: 'final', valueQuantity: { value: patientData.creatinineClearance, unit: 'mL/min' }, subject: { reference: 'Patient/1' } }; } var creatinineMolObservation; if (patientData.molCreatinine > 0) { creatinineMolObservation = { resourceType: 'Observation', id: '6', code: { coding: [{ system: 'http://loinc.org', code: '14682-9', display: 'Creatinine [Moles/volume] in Serum or Plasma' }] }, status: 'final', valueQuantity: { value: patientData.molCreatinine, unit: 'mmol/L' }, subject: { reference: 'Patient/1' } }; } var creatinineMgObservation; if (patientData.mglCreatinine > 0) { creatinineMgObservation = { resourceType: 'Observation', id: '7', code: { coding: [{ system: 'http://loinc.org', code: '2160-0', display: 'Creatinine [Mass/volume] in Serum or Plasma' }] }, status: 'final', valueQuantity: { value: patientData.mglCreatinine, unit: 'mg/dL' }, subject: { reference: 'Patient/1' } }; } var conditions = []; if (patientData.ammPathologies) { conditions = patientData.ammPathologies.map(function (pathology) { return { resourceType: 'Condition', id: pathology.code, code: { coding: [{ system: 'https://platform.claudebernard.fr/fhir/ImplementationGuides/CodeSystem/amm', code: pathology.code, display: pathology.label // TODO : find a way to add the label }] }, clinicalStatus: { coding: [{ system: 'http://terminology.hl7.org/CodeSystem/condition-clinical', code: 'active', display: 'Active' }] }, subject: { reference: 'Patient/1' } }; }); } var hepaticInsufficiencyCondition; if (patientData.hepaticStage && hepaticInsufficiencyMap[patientData.hepaticStage]) { var stage = hepaticInsufficiencyMap[patientData.hepaticStage]; hepaticInsufficiencyCondition = { resourceType: 'Condition', id: 'hepatic-insufficiency', code: { coding: [{ system: 'http://hl7.org/fhir/sid/icd-10', code: stage.code, display: stage.display }] }, clinicalStatus: { coding: [{ system: 'http://terminology.hl7.org/CodeSystem/condition-clinical', code: 'active', display: 'Active' }] }, verificationStatus: { coding: [{ system: 'http://terminology.hl7.org/CodeSystem/condition-ver-status', code: 'confirmed', display: 'Confirmed' }] }, subject: { reference: 'Patient/1' } }; } var allergies = []; if (patientData.allergies) { allergies = patientData.allergies.map(function (allergy) { return { resourceType: 'AllergyIntolerance', id: allergy.code.toString(), patient: { reference: 'Patient/1' }, code: { coding: [{ system: 'https://platform.claudebernard.fr/fhir/ImplementationGuides/CodeSystem/allergies', code: allergy.code.toString(), display: allergy.label.toString() // TODO : find a way to add the label }] }, clinicalStatus: { coding: [{ system: 'http://terminology.hl7.org/CodeSystem/condition-clinical', code: 'active', display: 'Active' }] }, }; }); } bundleEntries.push.apply(bundleEntries, __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([ { resource: patient } ], (weightObservation ? [{ resource: weightObservation }] : []), true), (heightObservation ? [{ resource: heightObservation }] : []), true), (pregnancyObservation ? [{ resource: pregnancyObservation }] : []), true), (breastfeedingObservation ? [{ resource: breastfeedingObservation }] : []), true), (creatinineClearanceObservation ? [{ resource: creatinineClearanceObservation }] : []), true), (creatinineMolObservation ? [{ resource: creatinineMolObservation }] : []), true), (creatinineMgObservation ? [{ resource: creatinineMgObservation }] : []), true), conditions.map(function (condition) { return ({ resource: condition }); }), true), allergies.map(function (allergy) { return ({ resource: allergy }); }), true), (hepaticInsufficiencyCondition ? [{ resource: hepaticInsufficiencyCondition }] : []), true)); return { result: bundleEntries, errors: errors }; }; /* ================== FHIR <=> BCB ================== */ function bcbToFhir(bcbPatient) { return patientToFhir(convertPatient(bcbPatient)); } function fhirToBcb(fhirPatient, allergiesMapper, snomedPathologiesMapper) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; if (!fhirPatient || fhirPatient.length === 0) { return { result: undefined, errors: [{ field: 'root', message: 'Invalid Patient array' }] }; } var errors = []; var _p = sortEntries('bcb', fhirPatient, allergiesMapper, snomedPathologiesMapper, errors), patientEntry = _p.patientEntry, weightObservation = _p.weightObservation, heightObservation = _p.heightObservation, pregnancyObservation = _p.pregnancyObservation, breastfeedingObservation = _p.breastfeedingObservation, hepaticCondition = _p.hepaticCondition, lstIdComposantAllergie = _p.lstIdComposantAllergie, lstCim10Pathologies = _p.lstCim10Pathologies, lstPathologies = _p.lstPathologies, clairanceCreatinineObservation = _p.clairanceCreatinineObservation, creatininemieMolObservation = _p.creatininemieMolObservation, creatininemieMgObservation = _p.creatininemieMgObservation; var bcbPatient = { lstIdComposantAllergie: lstIdComposantAllergie.map(function (codification) { return Number(codification.code); }), lstPathologiesCIM10: lstCim10Pathologies.map(function (codification) { return codification.code; }), lstPathologiesAMM: lstPathologies.map(function (codification) { return codification.code; }), age: patientEntry && patientEntry.birthDate ? calculateAgeInMonths(patientEntry.birthDate) : 0, poids: weightObservation && ((_a = weightObservation === null || weightObservation === undefined ? undefined : weightObservation.valueQuantity) === null || _a === undefined ? undefined : _a.value) ? (_b = weightObservation === null || weightObservation === undefined ? undefined : weightObservation.valueQuantity) === null || _b === undefined ? undefined : _b.value : 0, taille: heightObservation && ((_c = heightObservation === null || heightObservation === undefined ? undefined : heightObservation.valueQuantity) === null || _c === undefined ? undefined : _c.value) ? (_d = heightObservation === null || heightObservation === undefined ? undefined : heightObservation.valueQuantity) === null || _d === undefined ? undefined : _d.value : 0, grossesse: pregnancyObservation ? 1 : 0, allaitement: breastfeedingObservation ? 1 : 0, sexe: (patientEntry === null || patientEntry === undefined ? undefined : patientEntry.gender) ? genderMap[patientEntry.gender] || '' : '', clairanceCreatinine: clairanceCreatinineObservation && ((_e = clairanceCreatinineObservation === null || clairanceCreatinineObservation === undefined ? undefined : clairanceCreatinineObservation.valueQuantity) === null || _e === undefined ? undefined : _e.value) ? (_f = clairanceCreatinineObservation === null || clairanceCreatinineObservation === undefined ? undefined : clairanceCreatinineObservation.valueQuantity) === null || _f === undefined ? undefined : _f.value : 0, creatininemieMol: creatininemieMolObservation && ((_g = creatininemieMolObservation === null || creatininemieMolObservation === undefined ? undefined : creatininemieMolObservation.valueQuantity) === null || _g === undefined ? undefined : _g.value) ? (_h = creatininemieMolObservation === null || creatininemieMolObservation === undefined ? undefined : creatininemieMolObservation.valueQuantity) === null || _h === undefined ? undefined : _h.value : 0, creatininemieMg: creatininemieMgObservation && ((_j = creatininemieMgObservation === null || creatininemieMgObservation === undefined ? undefined : creatininemieMgObservation.valueQuantity) === null || _j === undefined ? undefined : _j.value) ? (_k = creatininemieMgObservation === null || creatininemieMgObservation === undefined ? undefined : creatininemieMgObservation.valueQuantity) === null || _k === undefined ? undefined : _k.value : 0, insuffisanceHepatique: ((_o = (_m = (_l = hepaticCondition === null || hepaticCondition === undefined ? undefined : hepaticCondition.code) === null || _l === undefined ? undefined : _l.coding) === null || _m === undefined ? undefined : _m[0]) === null || _o === undefined ? undefined : _o.code) ? hepaticInsufficiencyReverseMap[hepaticCondition.code.coding[0].code] : '', }; return { result: bcbPatient, errors: errors }; } /* ================== FHIR <=> CB ================== */ function cbToFhir(cbPatient) { return patientToFhir(cbPatient); } function fhirToCb(fhirPatient, allergiesMapper, snomedPathologiesMapper) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q; if (!fhirPatient || fhirPatient.length === 0) { return { result: undefined, errors: [{ field: 'root', message: 'Invalid Patient array' }] }; } var errors = []; var _r = sortEntries('cb', fhirPatient, allergiesMapper, snomedPathologiesMapper, errors), patientEntry = _r.patientEntry, weightObservation = _r.weightObservation, heightObservation = _r.heightObservation, pregnancyObservation = _r.pregnancyObservation, breastfeedingObservation = _r.breastfeedingObservation, hepaticCondition = _r.hepaticCondition, lstIdComposantAllergie = _r.lstIdComposantAllergie, lstCim10Pathologies = _r.lstCim10Pathologies, lstPathologies = _r.lstPathologies, clairanceCreatinineObservation = _r.clairanceCreatinineObservation, creatininemieMolObservation = _r.creatininemieMolObservation, creatininemieMgObservation = _r.creatininemieMgObservation; var pathologies = __spreadArray(__spreadArray([], lstPathologies.map(function (codification) { return ({ type: 'AMM', code: codification.code, label: codification.label }); }), true), lstCim10Pathologies.map(function (codification) { return ({ type: 'CIM10', code: codification.code, label: codification.label }); }), true); return { result: { age: patientEntry && patientEntry.birthDate ? calculateAgeInMonths(patientEntry.birthDate) : 0, gender: (patientEntry === null || patientEntry === undefined ? undefined : patientEntry.gender) ? genderMap[patientEntry.gender] || '' : '', weight: weightObservation && ((_a = weightObservation === null || weightObservation === undefined ? undefined : weightObservation.valueQuantity) === null || _a === undefined ? undefined : _a.value) ? (_b = weightObservation === null || weightObservation === undefined ? undefined : weightObservation.valueQuantity) === null || _b === undefined ? undefined : _b.value : 0, pregnancy: pregnancyObservation ? true : false, breastfeeding: breastfeedingObservation ? true : false, ammPathologies: pathologies, allergies: lstIdComposantAllergie.map(function (codification) { return ({ code: Number(codification.code), label: codification.label }); }), hepaticStage: ((_e = (_d = (_c = hepaticCondition === null || hepaticCondition === undefined ? undefined : hepaticCondition.code) === null || _c === undefined ? undefined : _c.coding) ==