UNPKG

@vidal-community/vidal-web-components

Version:

Vidal Web Components

355 lines 21.2 kB
import { Drug } from '../model/drug'; import { DrugType } from '../enums/drug-types'; import { Package } from '../model/package'; import { Product } from '../model/product'; import { Vmp } from '../model/vmp'; import { Unit } from '../model/unit'; import { ConversionRate } from '../model/conversion-rate'; import { ActivePrinciple } from '../model/active-principle'; import { MultiComposableDrugData } from '../model/multi-composable-drug-data'; import { EquivalentDrug } from '../model/equivalent-drug'; import { Ucdv } from '../model/ucdv'; export function useDrugService() { return drugServiceInstance; } class DrugService { constructor() { this.getDrugsByIds = async (prescriptionDrugUris, fetchingSupplierWithBody) => { const myHeaders = new Headers(); myHeaders.append('Content-Type', 'text/xml'); try { const response = await fetchingSupplierWithBody('/rest/api/search/ids?aggregate=ucd', this.buildRequestBody(prescriptionDrugUris), { headers: myHeaders }); const drugUriMap = new Map(); const ucdWithAssociatedDrugUrisMap = new Map(); const parser = new DOMParser(); const dom = parser.parseFromString(await textOrReject(response), 'text/xml'); const packages = []; [...dom.querySelectorAll('entry')].forEach((entry) => { var _a, _b; const typeOfDrug = this.getTypeOfDrug((_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''); if (typeOfDrug === DrugType.PACKAGE) { packages.push(this.buildPackages(entry)); } else if (typeOfDrug === DrugType.UCD) { const ucdEntry = this.buildUcds(entry); ucdWithAssociatedDrugUrisMap.set(ucdEntry.key, ucdEntry.value); } else if (typeOfDrug === DrugType.PRODUCT || typeOfDrug === DrugType.VMP) { const drugEntry = this.buildDrugWithMultipleUcd(entry, typeOfDrug); drugUriMap.set(drugEntry.key, drugEntry.value); } }); return packages.concat(this.associateDrugsWithUcd(drugUriMap, ucdWithAssociatedDrugUrisMap, prescriptionDrugUris)); } catch (err) { return Promise.reject(`Technical Error: ${err}`); } }; this.getUnitsByIds = async (prescriptionDrugUris, fetchingSupplierWithBody) => { try { const myHeaders = new Headers(); myHeaders.append('Content-Type', 'text/xml'); const response = await fetchingSupplierWithBody('/rest/api/search/ids?aggregate=units', this.buildRequestBody(prescriptionDrugUris), { headers: myHeaders }); const parser = new DOMParser(); const dom = parser.parseFromString(await textOrReject(response), 'text/xml'); const drugUriWithUnits = new Map(); [...dom.querySelectorAll('entry')].forEach((entry) => { var _a, _b; const drugId = (_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; const unitsElements = entry.getElementsByTagName('vidal:unit'); const units = Array.from(unitsElements).map((element) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; const unitUri = 'vidal://unit/'; const conversionRateArray = []; const mapOfUnit = new Map(); const vidatUnit = entry.getElementsByTagName('vidal:unit'); for (const element of vidatUnit) { mapOfUnit.set((_a = element.getAttribute('vidalId')) !== null && _a !== void 0 ? _a : '', new Unit((_b = unitUri + element.getAttribute('vidalId')) !== null && _b !== void 0 ? _b : '', (_c = element.getAttribute('shortname')) !== null && _c !== void 0 ? _c : '')); } const vidatRate = entry.getElementsByTagName('vidal:rate'); for (const element of vidatRate) { conversionRateArray.push(new ConversionRate(mapOfUnit.get((_d = element.getAttribute('ref_unit')) !== null && _d !== void 0 ? _d : ''), mapOfUnit.get((_e = element.getAttribute('unit')) !== null && _e !== void 0 ? _e : ''), +((_f = element.getAttribute('numerator')) !== null && _f !== void 0 ? _f : ''), +((_g = element.getAttribute('denominator')) !== null && _g !== void 0 ? _g : ''))); } const unit = new Unit((_h = unitUri + element.getAttribute('vidalId')) !== null && _h !== void 0 ? _h : '', (_j = element.getAttribute('shortname')) !== null && _j !== void 0 ? _j : '', conversionRateArray); unit.isDispensingUnit = (_l = (_k = element.getAttribute('type')) === null || _k === void 0 ? void 0 : _k.includes('DISPENSING_UNIT')) !== null && _l !== void 0 ? _l : false; return unit; }); drugUriWithUnits.set(drugId, units); }); return drugUriWithUnits; } catch (err) { return Promise.reject(`Technical Error: ${err}`); } }; this.getIndicatorsByIds = async (prescriptionDrugUris, fetchingSupplierWithBody) => { const myHeaders = new Headers(); myHeaders.append('Content-Type', 'text/xml'); const result = await (await fetchingSupplierWithBody('/rest/api/search/ids?aggregate=indicators', this.buildRequestBody(prescriptionDrugUris), { headers: myHeaders })).text(); const parser = new DOMParser(); const dom = parser.parseFromString(result, 'text/xml'); const drugUriWithUnits = new Map(); [...dom.querySelectorAll('entry')].forEach((entry) => { var _a, _b, _c, _d, _e; const entryDrugUri = (_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; if (entryDrugUri === null || !prescriptionDrugUris.includes(entryDrugUri)) { return; } const indicators = []; const indicatorVidalId = entry.getElementsByTagName('vidal:indicator'); for (const element of indicatorVidalId) { indicators.push((_c = element.getAttribute('vidalId')) !== null && _c !== void 0 ? _c : ''); } const relatedMoleculeHref = (_e = (_d = entry.querySelector('link[title="MOLECULES"]')) === null || _d === void 0 ? void 0 : _d.getAttribute('href')) !== null && _e !== void 0 ? _e : ''; drugUriWithUnits.set(entryDrugUri, new MultiComposableDrugData(indicators, relatedMoleculeHref)); }); return drugUriWithUnits; }; } async getActivePrinciplesForMonoItems(fetchingSupplier, relatedMoleculeHref) { var _a, _b; if (!relatedMoleculeHref) { return []; } const moleculeApiResult = await (await fetchingSupplier(relatedMoleculeHref)).text(); const parser = new DOMParser(); const dom = parser.parseFromString(moleculeApiResult, 'text/xml'); let itemCounter = 0; const activePrinciples = []; for (const entry of [...dom.querySelectorAll('entry')]) { if (entry.getAttribute('vidal:categories') === 'ITEM') { itemCounter++; } if (itemCounter > 1) { return []; } const itemTypeName = (_b = (_a = entry.getElementsByTagName('vidal:itemType')[0]) === null || _a === void 0 ? void 0 : _a.getAttribute('name')) !== null && _b !== void 0 ? _b : ''; if (itemTypeName === 'ACTIVE_PRINCIPLE') { activePrinciples.push(this.buildActivePrinciple(entry)); } } return activePrinciples; } async getUcdv(drugUri, fetchingSupplier) { var _a, _b; const parser = new DOMParser(); let drugId = this.getDrugId(drugUri); let typeOfDrug = this.getTypeOfDrug(drugUri); if (typeOfDrug === DrugType.UCD) { const ucdPackage = await (await fetchingSupplier('/rest/api/ucd/' + drugId + '/packages')).text(); const ucdPackageDom = parser.parseFromString(ucdPackage, 'text/xml'); drugId = this.getElementByTagName('vidal:id', ucdPackageDom.querySelector('entry')); typeOfDrug = DrugType.PACKAGE; } const ucdvsApiResult = await (await fetchingSupplier('/rest/api/' + typeOfDrug + '/' + drugId + '?aggregate=ucdvs')).text(); const ucdvsApiResultDom = parser.parseFromString(ucdvsApiResult, 'text/xml'); const ucdvId = this.getElementByTagName('vidal:id', [...ucdvsApiResultDom.querySelectorAll('entry')].find((value) => { return this.getAttribute('vidal:categories', value) === 'UCDV'; })); const ucdvWithUnitAndMolecules = await (await fetchingSupplier('/rest/api/ucdv/' + ucdvId + '?aggregate=units&aggregate=molecules')).text(); const ucdvWithUnitAndMoleculesDom = parser.parseFromString(ucdvWithUnitAndMolecules, 'text/xml'); const molecules = []; let dispensingUnit = undefined; for (const entry of [ ...ucdvWithUnitAndMoleculesDom.querySelectorAll('entry'), ]) { const vidalCategory = this.getAttribute('vidal:categories', entry); if (vidalCategory.includes('DISPENSING_UNIT')) { const dispensingUnitUri = this.getElementByTagName('id', entry); const dispensingUnitName = this.getElementByTagName('vidal:shortName', entry); dispensingUnit = new Unit(dispensingUnitUri, dispensingUnitName); } if (vidalCategory === 'MOLECULE') { const activePrincipleId = this.getElementByTagName('vidal:id', entry); const totalQuantityValue = this.getElementByTagName('vidal:totalQuantityValue', entry); const totalQuantityValueUnitId = (_b = (_a = entry .getElementsByTagName('vidal:totalQuantityUnitId')[0]) === null || _a === void 0 ? void 0 : _a.getAttribute('vidalId')) !== null && _b !== void 0 ? _b : ''; molecules.push(new ActivePrinciple(Number(activePrincipleId), Number(totalQuantityValueUnitId), Number(totalQuantityValue))); } } return new Ucdv(molecules, dispensingUnit); } buildActivePrinciple(entry) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; const isPerVolume = (_a = entry.getElementsByTagName('vidal:perVolume')[0]) === null || _a === void 0 ? void 0 : _a.textContent; if (isPerVolume) { const activePrincipleId = (_c = (_b = entry.getElementsByTagName('vidal:id')[0]) === null || _b === void 0 ? void 0 : _b.textContent) !== null && _c !== void 0 ? _c : ''; const perVolumeValue = (_e = (_d = entry.getElementsByTagName('vidal:perVolumeValue')[0]) === null || _d === void 0 ? void 0 : _d.textContent) !== null && _e !== void 0 ? _e : ''; const perVolumeUnitId = (_g = (_f = entry .getElementsByTagName('vidal:perVolumeUnit')[0]) === null || _f === void 0 ? void 0 : _f.getAttribute('vidalId')) !== null && _g !== void 0 ? _g : ''; return new ActivePrinciple(Number(activePrincipleId), Number(perVolumeUnitId), Number(perVolumeValue)); } const subConstituentMoleculeId = (_j = (_h = entry .getElementsByTagName('vidal:subConstituentMolecule')[0]) === null || _h === void 0 ? void 0 : _h.getAttribute('vidalId')) !== null && _j !== void 0 ? _j : ''; const subConstituentPerVolume = (_l = (_k = entry.getElementsByTagName('vidal:subConstituentPerVolume')[0]) === null || _k === void 0 ? void 0 : _k.textContent) !== null && _l !== void 0 ? _l : ''; const subConstituentPerVolumeUnitId = (_o = (_m = entry .getElementsByTagName('vidal:subConstituentPerVolumeUnit')[0]) === null || _m === void 0 ? void 0 : _m.getAttribute('vidalId')) !== null && _o !== void 0 ? _o : ''; return new ActivePrinciple(Number(subConstituentMoleculeId), Number(subConstituentPerVolumeUnitId), Number(subConstituentPerVolume)); } async getUcdsForMolecule(moleculeId, fetchingSupplier) { const ucdsOfMolecule = []; const response = await (await fetchingSupplier('/rest/api/ucds/molecules?moleculeIds=' + moleculeId + '&substanceTypes=ACTIVE_PRINCIPLE&withChildrens=true&operatorType=AND&page-size=100000')).text(); const parser = new DOMParser(); const dom = parser.parseFromString(response, 'text/xml'); [...dom.querySelectorAll('entry')].forEach((entry) => { var _a, _b, _c, _d, _e, _f; const divisibilityAsString = (_b = (_a = entry.getElementsByTagName('vidal:divisibility')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; let divisibility; if (divisibilityAsString && !isNaN(Number(divisibilityAsString))) { divisibility = Number(divisibilityAsString); } const uri = (_d = (_c = entry.getElementsByTagName('id')[0]) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : ''; const name = (_f = (_e = entry.getElementsByTagName('vidal:name')[0]) === null || _e === void 0 ? void 0 : _e.textContent) !== null && _f !== void 0 ? _f : ''; const ucd = new EquivalentDrug(uri, name, [], divisibility); ucdsOfMolecule.push(ucd); }); return ucdsOfMolecule; } async getIndicatorsAndRoutes(ucdUri, fetchingSupplier) { const response = await (await fetchingSupplier(`/rest/api/ucd/${this.getDrugId(ucdUri)}?aggregate=routes&aggregate=indicators`)).text(); const parser = new DOMParser(); const dom = parser.parseFromString(response, 'text/xml'); const indicators = []; let href = ''; const routes = []; [...dom.querySelectorAll('entry')].forEach((entry) => { const vidalCategory = this.getAttribute('vidal:categories', entry); if (vidalCategory === 'UCD') { for (const linkElement of this.getElementsByTagName('link', entry)) { if (this.getAttribute('title', linkElement) === 'MOLECULES') { href = this.getAttribute('href', linkElement); } } } if (vidalCategory === 'ROUTE') { routes.push(this.getElementByTagName('id', entry)); } if (vidalCategory === 'INDICATORS') { for (const indicatorElement of this.getElementsByTagName('vidal:indicator', entry)) { indicators.push(this.getAttribute('vidalId', indicatorElement)); } } }); const multiComposableDrugData = new MultiComposableDrugData(indicators, href); multiComposableDrugData.routeUris = routes; return multiComposableDrugData; } getDrugId(uri) { return uri.split('/').pop(); } getElementByTagName(tagName, entry) { var _a, _b; return (_b = (_a = entry.getElementsByTagName(tagName)[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; } getElementsByTagName(tagName, entry) { return entry.getElementsByTagName(tagName); } getAttribute(attribute, entry) { var _a; return (_a = entry.getAttribute(attribute)) !== null && _a !== void 0 ? _a : ''; } buildPackages(entry) { var _a, _b, _c, _d, _e, _f, _g; const packageUri = (_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; const packageName = (_d = (_c = entry.getElementsByTagName('vidal:name')[0]) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : ''; if (entry.getElementsByTagName('vidal:ucd').length > 0) { const ucdId = (_e = entry.getElementsByTagName('vidal:ucd')[0].getAttribute('id')) !== null && _e !== void 0 ? _e : ''; const ucdName = (_g = (_f = entry.getElementsByTagName('vidal:ucd')[0]) === null || _f === void 0 ? void 0 : _f.textContent) !== null && _g !== void 0 ? _g : ''; const ucdUri = 'vidal://ucd/' + ucdId; return new Package(packageUri, packageName, new Drug(ucdUri, ucdName)); } return new Package(packageUri, packageName, undefined); } getTypeOfDrug(drugUri) { if (drugUri.includes(DrugType.UCD)) { return DrugType.UCD; } if (drugUri.includes(DrugType.PACKAGE)) { return DrugType.PACKAGE; } if (drugUri.includes(DrugType.PRODUCT)) { return DrugType.PRODUCT; } if (drugUri.includes(DrugType.VMP)) { return DrugType.VMP; } return undefined; } buildRequestBody(drugUris) { return `<?xml version="1.0" encoding="UTF-8"?> <request> <ids> ${drugUris.map((id) => `<id>${id}</id>`).join('')} </ids> </request>`; } getInlineAssociatedDrugUris(entry) { var _a; const associatedDrugUris = []; for (const element of entry.getElementsByTagName('link')) { const el = element; if (el.getAttribute('rel') === 'inline' && (el.getAttribute('title') === 'PRODUCT' || el.getAttribute('title') === 'VMP')) { associatedDrugUris.push((_a = el.getAttribute('href')) !== null && _a !== void 0 ? _a : ''); } } return associatedDrugUris; } associateDrugsWithUcd(drugUriMap, ucdWithAssociatedProductOrVmpUrisMap, prescriptionDrugUri) { const result = []; ucdWithAssociatedProductOrVmpUrisMap.forEach((associatedInlineDrugUris, ucd) => { associatedInlineDrugUris.forEach((associatedInlineDrugUri) => { if (this.getTypeOfDrug(associatedInlineDrugUri) === DrugType.PRODUCT) { drugUriMap.get(associatedInlineDrugUri).ucd.push(ucd); } if (this.getTypeOfDrug(associatedInlineDrugUri) === DrugType.VMP) { drugUriMap.get(associatedInlineDrugUri).ucd.push(ucd); } }); }); drugUriMap.forEach((drug, _) => result.push(drug)); ucdWithAssociatedProductOrVmpUrisMap.forEach((_, key) => { if (prescriptionDrugUri.includes(key.uri)) { result.push(key); } }); return result; } buildUcds(entry) { var _a, _b, _c, _d, _e, _f; const ucdUri = (_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; const ucdName = (_d = (_c = entry.getElementsByTagName('vidal:name')[0]) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : ''; const divisibilityAsString = (_f = (_e = entry.getElementsByTagName('vidal:divisibility')[0]) === null || _e === void 0 ? void 0 : _e.textContent) !== null && _f !== void 0 ? _f : ''; let divisibility; if (divisibilityAsString && !isNaN(Number(divisibilityAsString))) { divisibility = Number(divisibilityAsString); } return { key: new Drug(ucdUri, ucdName, divisibility), value: this.getInlineAssociatedDrugUris(entry), }; } buildDrugWithMultipleUcd(entry, typeOfDrug) { var _a, _b, _c, _d; const drugUri = (_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; const drugName = (_d = (_c = entry.getElementsByTagName('vidal:name')[0]) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : ''; if (typeOfDrug === DrugType.PRODUCT) { return { key: drugUri, value: new Product([], drugUri, drugName) }; } return { key: drugUri, value: new Vmp([], drugUri, drugName) }; } } const drugServiceInstance = new DrugService(); function textOrReject(response) { if (!response.ok) { return Promise.reject(`API call failed: ${response}`); } return response.text(); } //# sourceMappingURL=drug-service.js.map