UNPKG

@vidal-community/vidal-web-components

Version:

Vidal Web Components

93 lines 5 kB
import { Drug } from '../model/drug'; import { Vmp } from '../model/vmp'; import { DrugType } from '../enums/drug-types'; import { Prescribable } from '../model/prescribable'; import { Product } from '../model/product'; import { Package } from '../model/package'; import { Unit } from '../model/unit'; export function usePrescribableService() { return prescribableServiceInstance; } class PrescribablesService { async associatePrescribablesWithUcd(drug, prescribableFetchingSupplier) { const prescribables = await this.getPrescribablesFromDrug(drug, prescribableFetchingSupplier); for (const prescribable of prescribables) { prescribable.ucds = await this.getUcdsFromPrescribables(prescribable.id, prescribableFetchingSupplier); } return prescribables; } async getPrescribablesFromDrug(drug, prescribableFetchingSupplier) { const processPrescribables = async (drug) => { return await this.callPrescribablesApi(prescribableFetchingSupplier, drug); }; if (drug instanceof Product) { let prescribables = []; for (const ucd of drug.ucd) { const ucdPrescribables = await processPrescribables(ucd); prescribables = [...prescribables, ...ucdPrescribables]; } return prescribables; } if (drug instanceof Package) { return await processPrescribables(drug.ucd); } return await processPrescribables(drug); } async getUcdsFromPrescribables(prescribablesId, prescribableFetchingSupplier) { const drugs = []; const response = await prescribableFetchingSupplier(`/rest/api/prescribable/${prescribablesId}/ucds`).then((response) => response.text()); const domParser = new DOMParser(); const dom = domParser.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); } drugs.push(new Drug((_d = (_c = entry.getElementsByTagName('id')[0]) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : '', (_f = (_e = entry.getElementsByTagName('vidal:name')[0]) === null || _e === void 0 ? void 0 : _e.textContent) !== null && _f !== void 0 ? _f : '', divisibility)); }); return drugs; } async getPrescribableRefUnit(prescribableId, fetchingSupplier) { var _a, _b, _c, _d; const response = await fetchingSupplier(`/rest/api/prescribable/${prescribableId}/units`); if (!response.ok) { return Promise.reject(`Cannot call prescribable API: ${response}`); } const domParser = new DOMParser(); const dom = domParser.parseFromString(await response.text(), 'text/xml'); const entry = dom.getElementsByTagName('entry')[0]; const unitUri = (_b = (_a = entry.getElementsByTagName('id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''; const unitShortName = (_d = (_c = entry.getElementsByTagName('vidal:shortName')[0]) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : ''; return new Unit(unitUri, unitShortName); } async callPrescribablesApi(prescribableFetchingSupplier, drug) { var _a, _b; if (!drug) { return []; } const response = await prescribableFetchingSupplier(this.buildUri(drug)); if (!response.ok) { return Promise.reject(`Cannot call prescribable API: ${response}`); } const domParser = new DOMParser(); const dom = domParser.parseFromString(await response.text(), 'text/xml'); const prescribablesIds = []; for (const entry of [...dom.querySelectorAll('entry')]) { const prescribable = new Prescribable((_b = (_a = entry.getElementsByTagName('vidal:id')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ''); prescribable.refUnit = await this.getPrescribableRefUnit(prescribable.id, prescribableFetchingSupplier); prescribablesIds.push(prescribable); } return prescribablesIds; } buildUri(drug) { const typeOfDrug = drug instanceof Vmp ? DrugType.VMP : DrugType.UCD; return `/rest/api/${typeOfDrug}/${this.getDrugId(drug)}/prescribables`; } getDrugId(drug) { return drug.uri.split('/').pop(); } } const prescribableServiceInstance = new PrescribablesService(); //# sourceMappingURL=prescribables-service.js.map