UNPKG

@vidal-community/vidal-web-components

Version:

Vidal Web Components

287 lines 14.2 kB
import { Group } from './group'; import { SubGroup } from './subgroup'; import { Point } from './point'; import { hashCode } from '../hash'; import { Link } from './link'; import { addDays, format, isBefore, isEqual } from 'date-fns'; const OTHER_GROUP_ID = -1; function reduceLinksBasedOnTheirGroup(prescriptionItems) { const linksGroupedByGroup = []; prescriptionItems .filter((prescriptionItem) => prescriptionItem instanceof Link) .forEach((link) => { const indexOfGroupOfLinks = linksGroupedByGroup .map((groupOfLinks) => groupOfLinks.id) .indexOf(link.group); if (indexOfGroupOfLinks !== -1) { linksGroupedByGroup[indexOfGroupOfLinks].links.push(link); } else { linksGroupedByGroup.push({ id: link.group, links: [link] }); } }); linksGroupedByGroup .filter((x) => x.links.length > 1) .map((x) => x.links.sort((a, b) => { return isBefore(new Date(a.start), new Date(b.start)) ? -1 : isEqual(new Date(a.start), new Date(b.start)) ? 0 : 1; })) .forEach((links) => { let accumulatorLink = links[0]; links.forEach((currentLink, index) => { const currentLinkStartDate = new Date(currentLink.start); if (!accumulatorLink) { return; } if (!currentLink) { return; } if (accumulatorLink === currentLink) { return; } const accumulatorLinkEndDate = new Date(accumulatorLink.end); if (isBefore(currentLinkStartDate, accumulatorLinkEndDate) || isEqual(currentLinkStartDate, accumulatorLinkEndDate)) { accumulatorLink.className = accumulatorLink.className.replace('fading', ''); accumulatorLink.className = accumulatorLink.className.replace('last-link', ''); currentLink.className = currentLink.className.replace('first-link', ''); const nextLink = links[index + 1]; if (nextLink) { const nextLinkStartDate = new Date(nextLink.start); const currentLinkEndDate = new Date(currentLink.end); if (isBefore(nextLinkStartDate, currentLinkEndDate) || isEqual(nextLinkStartDate, currentLinkEndDate)) { accumulatorLink.end = currentLink.end; // @ts-ignore links[index] = null; } else { accumulatorLink.className = accumulatorLink.className.replace('last-link', ''); currentLink.className = currentLink.className.replace('first-link', ''); accumulatorLink.end = currentLink.start; } } else { accumulatorLink = currentLink; } } else { accumulatorLink = currentLink; } }); }); prescriptionItems = prescriptionItems.filter((prescriptionItem) => prescriptionItem instanceof Point); prescriptionItems.push( // @ts-ignore ...[].concat( // @ts-ignore ...linksGroupedByGroup.map((x) => x.links.filter((x) => x !== null)))); return prescriptionItems; } export function buildTimelineData(fullMedicationDispenses, classifiedBy, groupedBy) { const subGroups = new Array(); let prescriptionItems = new Array(); const groups = new Array(); function compare() { if (classifiedBy === 'ATC') { return compareByATCCode(); } return compareByVidalClassName(); } fullMedicationDispenses.sort(compare()).forEach((fullMedicalDispense) => { var _b; const drug = fullMedicalDispense.drug; const rootAtc = fullMedicalDispense.actClassifications.root; const leafAtc = fullMedicalDispense.actClassifications.leaf; const rootVidalClassification = fullMedicalDispense.vidalClassifications.root; let className = ''; if (classifiedBy === 'ATC') { className = !fullMedicalDispense.actClassifications .hasMultipleClassifications ? kebabCase(rootAtc.name) : 'autre'; } else { className = !fullMedicalDispense.vidalClassifications .hasMultipleClassifications ? kebabCase(rootVidalClassification.name) : 'autre'; } if (groupedBy === 'molecule') { subGroups.push(new SubGroup((classifiedBy === 'ATC' ? rootAtc.id : rootVidalClassification.id) + '-' + leafAtc.id, classifiedBy === 'ATC' ? rootAtc.id : rootVidalClassification.id, leafAtc.name, 2, className, classifiedBy === 'ATC' ? leafAtc.arborescence : rootVidalClassification.arborescence)); } else if (groupedBy === 'VMP') { const vmpName = drug.vmpName; subGroups.push(new SubGroup((classifiedBy === 'ATC' ? rootAtc.id : rootVidalClassification.id) + '-' + leafAtc.id, classifiedBy === 'ATC' ? rootAtc.id : rootVidalClassification.id, vmpName.length > 0 ? vmpName : drug.name + '<span>⚠</span>', 2, className, classifiedBy === 'ATC' ? leafAtc.arborescence : rootVidalClassification.arborescence)); } else if (groupedBy === 'prescribable') { const prescribableName = drug.prescribableName; subGroups.push(new SubGroup((classifiedBy === 'ATC' ? rootAtc.id : rootVidalClassification.id) + '-' + leafAtc.id, classifiedBy === 'ATC' ? rootAtc.id : rootVidalClassification.id, (_b = prescribableName === null || prescribableName === void 0 ? void 0 : prescribableName.toLowerCase()) !== null && _b !== void 0 ? _b : drug.name + '<span>⚠</span>', 2, className, classifiedBy === 'ATC' ? leafAtc.arborescence : rootVidalClassification.arborescence)); } if (classifiedBy === 'ATC') { if (fullMedicalDispense.actClassifications.hasMultipleClassifications) { addToOtherGroup(groups, rootAtc, leafAtc); } else { addToCorrespondingGroup(groups, rootAtc, leafAtc); } } if (classifiedBy === 'Vidal class') { if (fullMedicalDispense.vidalClassifications.hasMultipleClassifications) { addToOtherGroup(groups, rootVidalClassification, leafAtc); } else { addToCorrespondingGroup(groups, rootVidalClassification, leafAtc); } } fullMedicalDispense.dispensations .sort((dispensation1, dispensation2) => Date.parse(dispensation1.startDate) > Date.parse(dispensation2.startDate) ? 1 : Date.parse(dispensation1.startDate) === Date.parse(dispensation2.startDate) ? 0 : -1) .forEach((dispensation, index) => { if (Number.isNaN(Date.parse(dispensation.startDate))) { return; } if (dispensation.startDate) createPointsAndLinks(drug, fullMedicalDispense.dispensations, dispensation, leafAtc, classifiedBy === 'ATC' ? rootAtc : rootVidalClassification, prescriptionItems, index, className); }); }); prescriptionItems = reduceLinksBasedOnTheirGroup(prescriptionItems); return [ removeDuplicates(groups.sort((_a, b) => (b.id === OTHER_GROUP_ID ? -1 : 0))), removeDuplicates(subGroups), removeDuplicates(prescriptionItems), ]; } function compareByATCCode() { return (dispense1, dispense2) => dispense1.actClassifications.root.code > dispense2.actClassifications.root.code ? 1 : dispense1.actClassifications.root.code === dispense2.actClassifications.root.code ? 0 : -1; } function compareByVidalClassName() { return (dispense1, dispense2) => dispense1.vidalClassifications.root.name > dispense2.vidalClassifications.root.name ? 1 : dispense1.vidalClassifications.root.name === dispense2.vidalClassifications.root.name ? 0 : -1; } function addToOtherGroup(groups, rootAtc, leafAtc) { const otherGroup = groups.find((group) => group.id === OTHER_GROUP_ID); if (otherGroup) { otherGroup.nestedGroups.push(rootAtc.id + '-' + leafAtc.id); } else { groups.push(new Group(OTHER_GROUP_ID, 'AUTRE', 1, kebabCase('autre'), [ rootAtc.id + '-' + leafAtc.id, ])); } } function addToCorrespondingGroup(groups, rootClassification, leafClassification) { if (groups.find((group) => group.id === rootClassification.id)) { groups.forEach((group) => { if (group.id === rootClassification.id && !group.nestedGroups.includes(rootClassification.id + '-' + leafClassification.id)) { group.nestedGroups.push(rootClassification.id + '-' + leafClassification.id); } }); } else { groups.push(new Group(rootClassification.id, rootClassification.name, 1, kebabCase(rootClassification.name), [rootClassification.id + '-' + leafClassification.id])); } } function addLinks(currentDispensation, prescriptionItems, drug, rootAtc, leafAtc, className, index, dispensations) { var _b, _c, _d, _e, _f; const currentStartDatePlus30Days = format(addDays(Date.parse(currentDispensation.startDate), 30), 'yyyy-MM-dd'); if (currentDispensation === null || currentDispensation === void 0 ? void 0 : currentDispensation.endDate) { prescriptionItems.push(new Link(`link-${drug.id}-${hashCode(currentDispensation.startDate)}-${hashCode(currentDispensation.endDate)}`, rootAtc.id + '-' + leafAtc.id, drug.name, drug.name, `link ${className} first-link last-link`, currentDispensation.startDate, currentDispensation.endDate)); // Possède une prochaine dispensation } else if (dispensations[index + 1]) { // Periode entre 2 dispensation inférieur à 30 jours if (Date.parse(currentStartDatePlus30Days) > Date.parse((_b = dispensations[index + 1]) === null || _b === void 0 ? void 0 : _b.startDate)) { prescriptionItems.push(new Link(`link-${drug.id}-${hashCode(currentDispensation.startDate)}-${hashCode((_c = dispensations[index + 1]) === null || _c === void 0 ? void 0 : _c.startDate)}`, rootAtc.id + '-' + leafAtc.id, drug.name, drug.name, `link ${className} first-link`, currentDispensation.startDate, (_d = dispensations[index + 1]) === null || _d === void 0 ? void 0 : _d.startDate)); } else { prescriptionItems.push(new Link(`link-${drug.id}-${hashCode(currentDispensation.startDate)}-${hashCode((_e = dispensations[index + 1]) === null || _e === void 0 ? void 0 : _e.startDate)}`, rootAtc.id + '-' + leafAtc.id, drug.name, drug.name, `fading link ${className} first-link last-link`, currentDispensation.startDate, currentStartDatePlus30Days)); } } else { const previousStartDatePlus30Days = dispensations[index - 1] ? format(addDays(Date.parse((_f = dispensations[index - 1]) === null || _f === void 0 ? void 0 : _f.startDate), 30), 'yyyy-MM-dd') : ''; prescriptionItems.push(new Link(`link-${drug.id}-${hashCode(currentDispensation.startDate)}-${hashCode(currentStartDatePlus30Days)}`, rootAtc.id + '-' + leafAtc.id, drug.name, drug.name, `fading link ${className} ${!previousStartDatePlus30Days || Date.parse(previousStartDatePlus30Days) <= Date.parse(currentDispensation.startDate) ? 'first-link' : ''} last-link`, currentDispensation.startDate, currentStartDatePlus30Days)); } } function createPointsAndLinks(drug, dispensations, currentDispensation, leafAtc, rootAtc, prescriptionItems, index, className) { addPoint(drug, currentDispensation, leafAtc, rootAtc, prescriptionItems, className); addLinks(currentDispensation, prescriptionItems, drug, rootAtc, leafAtc, className, index, dispensations); } function addPoint(drug, currentDispensation, leafAtc, rootAtc, prescriptionItems, className) { const name = `<span>${drug.name}</span>`; const cip13 = drug.cip13 ? `<br><span><small>CIP13 : ${drug.cip13}</small></span>` : ''; const cip = drug.cip ? `<br><span><small>CIP : ${drug.cip}</small></span>` : ''; const ucd = drug.ucd ? `<br><span><small>UCD : ${drug.ucd}</small></span>` : ''; const ucd13 = drug.ucd13 ? `<br><span><small>UCD13 : ${drug.ucd13}</small></span>` : ''; const quantity = currentDispensation.quantity ? `<br><span>Quantité : ${currentDispensation.quantity}</span>` : ''; const endDateTitle = currentDispensation.endDate ? `<br>Fin : ${format(new Date(currentDispensation.endDate), 'dd/MM/yyyy')}` : ''; const offMarket = drug.offMarketDate ? `<br><span><i>non commercialisé depuis le ${format(drug.offMarketDate, 'dd/MM/yyyy')}</i></span>` : ''; const title = `<b>${name}</b>${offMarket}<br><br>Début : ${format(new Date(currentDispensation.startDate), 'dd/MM/yyyy')}${endDateTitle}${quantity}<br>${cip13}${cip}${ucd}${ucd13}`; const point = new Point(`${drug.id}-${hashCode(currentDispensation.startDate)}`, rootAtc.id + '-' + leafAtc.id, '*', title, className, currentDispensation.startDate, drug.id); prescriptionItems.push(point); } export function removeDuplicates(array) { return [ ...new Set(array.filter((v, i, a) => // @ts-ignore a.findIndex((t) => JSON.stringify(t.id) === JSON.stringify(v.id)) === i)), ]; } function kebabCase(string) { return string.toLowerCase().replace(/\s/g, '-'); } //# sourceMappingURL=build.js.map