@vidal-community/vidal-web-components
Version:
Vidal Web Components
303 lines • 15.8 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { Package } from '../model/package';
import { Product } from '../model/product';
import { Vmp } from '../model/vmp';
import { EquivalentDrug } from '../model/equivalent-drug';
import './vidal-equivalent-form';
import { usePrescribableService } from '../services/prescribables-service';
import { useDrugService } from '../services/drug-service';
import { EquivalentFormularyFilter } from './equivalent-formulary-filter';
let VidalEquivalents = class VidalEquivalents extends LitElement {
constructor() {
super(...arguments);
this.associatedFormularyAndEquivalents = new Map();
this.prescriptionLines = [];
this.availableFormularyDrugUris = [];
this.emitSubmitEvent = () => { };
this.prescribableService = usePrescribableService();
this.drugService = useDrugService();
this.equivalentFormularyFilter = new EquivalentFormularyFilter();
this.loading = false;
}
updated(changedProperties) {
if (this.prescribableFetchingSupplier &&
this.fetchingSupplierWithBody &&
(changedProperties.has('prescribableFetchingSupplier') ||
changedProperties.has('fetchingSupplierWithBody') ||
changedProperties.has('prescriptionLines') ||
changedProperties.has('availableFormularyDrugUris') ||
changedProperties.has('emitSubmitEvent'))) {
this.loading = true;
this.getEquivalentFormulary(this.prescriptionLines, this.prescribableFetchingSupplier)
.then((equivalentDrugs) => {
const equivalentUcds = [...equivalentDrugs.values()].flat();
return this.drugService
.getUnitsByIds(equivalentUcds.map((equivalentUcd) => equivalentUcd.uri), this.fetchingSupplierWithBody)
.then((unitMap) => {
equivalentDrugs.forEach((mapValue) => {
mapValue.forEach((ucd) => (ucd.units = unitMap.get(ucd.uri)));
});
this.associatedFormularyAndEquivalents = equivalentDrugs;
});
})
.catch((error) => console.error(error))
.finally(() => (this.loading = false));
}
}
render() {
if (this.loading) {
return html ` <vidal-loading-spinner></vidal-loading-spinner>`;
}
return html ` <div class="content">
${Array.from(this.associatedFormularyAndEquivalents).map((prescriptionLineWithEquivalent) => {
const prescriptionLine = prescriptionLineWithEquivalent[0];
const equivalentUcds = prescriptionLineWithEquivalent[1];
return html ` <vidal-equivalent-form
.prescriptionLine="${prescriptionLine}"
.equivalentUcds="${equivalentUcds}"
></vidal-equivalent-form>`;
})}
</div>`;
}
async getEquivalentFormulary(prescriptionLines, prescribableFetchingSupplier) {
const equivalentFormulary = new Map();
for (const pL of prescriptionLines) {
const drug = pL.drug;
if (drug instanceof Package) {
equivalentFormulary.set(pL, await this.findEquivalentPackage(drug, prescribableFetchingSupplier));
}
else if (drug instanceof Product) {
equivalentFormulary.set(pL, await this.findEquivalentProduct(drug, prescribableFetchingSupplier));
}
else if (drug instanceof Vmp) {
equivalentFormulary.set(pL, await this.findEquivalentVmp(drug, prescribableFetchingSupplier));
}
else if (!this.exist(drug.uri)) {
equivalentFormulary.set(pL, await this.findEquivalentsUcd(drug, prescribableFetchingSupplier));
}
else {
equivalentFormulary.set(pL, [
new EquivalentDrug(drug.uri, drug.name, [], drug.divisibility),
]);
}
}
return this.findMonoComposedEquivalentUcdForMultiComposedPrescriptionLinesWithoutEquivalentDrugs(equivalentFormulary);
}
async findEquivalentPackage(pack, prescribableFetchingSupplier) {
var _a;
if ((_a = pack.ucd) === null || _a === void 0 ? void 0 : _a.uri) {
if (this.exist(pack.ucd.uri)) {
return [
new EquivalentDrug(pack.ucd.uri, pack.ucd.name, [], pack.divisibility),
];
}
return await this.prescribableService
.associatePrescribablesWithUcd(pack, prescribableFetchingSupplier)
.then((prescribables) => {
return prescribables.flatMap((prescribable) => {
var _a;
pack.prescribable = prescribable;
const availableUcd = (_a = prescribable.ucds) === null || _a === void 0 ? void 0 : _a.filter((ucd) => this.exist(ucd.uri)).map((ucd) => new EquivalentDrug(ucd.uri, ucd.name, [], ucd.divisibility));
return availableUcd !== null && availableUcd !== void 0 ? availableUcd : [];
});
});
}
return [];
}
async findEquivalentVmp(vmp, prescribableFetchingSupplier) {
const availableUcd = vmp.ucd
.filter((ucd) => this.exist(ucd.uri))
.map((value) => new EquivalentDrug(value.uri, value.name, [], value.divisibility));
if (availableUcd.length > 0) {
return availableUcd;
}
return await this.prescribableService
.associatePrescribablesWithUcd(vmp, prescribableFetchingSupplier)
.then((prescribables) => {
return prescribables.flatMap((prescribable) => {
var _a;
const availableUcd = (_a = prescribable.ucds) === null || _a === void 0 ? void 0 : _a.filter((ucd) => this.exist(ucd.uri)).map((ucd) => new EquivalentDrug(ucd.uri, ucd.name, [], ucd.divisibility));
vmp.prescribable = prescribable;
return availableUcd !== null && availableUcd !== void 0 ? availableUcd : [];
});
});
}
async findEquivalentProduct(product, prescribableFetchingSupplier) {
const availableUcd = product.ucd
.filter((ucd) => this.exist(ucd.uri))
.map((ucd) => new EquivalentDrug(ucd.uri, ucd.name, [], ucd.divisibility));
if (availableUcd.length > 0) {
return availableUcd;
}
const prescribablesOfProduct = [];
for (const ucd of product.ucd) {
const drugs = await this.prescribableService
.associatePrescribablesWithUcd(ucd, prescribableFetchingSupplier)
.then((value) => {
return value.flatMap((prescribable) => {
var _a;
const availableUcd = (_a = prescribable.ucds) === null || _a === void 0 ? void 0 : _a.filter((ucd) => this.exist(ucd.uri));
product.prescribable = prescribable;
return availableUcd !== null && availableUcd !== void 0 ? availableUcd : [];
});
});
drugs.forEach((drug) => {
if (!this.isDrugAlreadyPresent(drug, prescribablesOfProduct)) {
prescribablesOfProduct.push(new EquivalentDrug(drug.uri, drug.name, [], drug.divisibility));
}
});
}
return prescribablesOfProduct;
}
isDrugAlreadyPresent(drug, prescribablesOfProduct) {
let isDrugAlreadyPresent = false;
prescribablesOfProduct.forEach((drugInArray) => {
isDrugAlreadyPresent = drug.uri === drugInArray.uri;
});
return isDrugAlreadyPresent;
}
exist(uri) {
return this.availableFormularyDrugUris.includes(uri);
}
async findEquivalentsUcd(ucd, prescribableFetchingSupplier) {
return await this.prescribableService
.associatePrescribablesWithUcd(ucd, prescribableFetchingSupplier)
.then((value) => {
return value.flatMap((prescribable) => {
var _a;
const availableUcd = (_a = prescribable.ucds) === null || _a === void 0 ? void 0 : _a.filter((ucd) => this.exist(ucd.uri)).map((value) => new EquivalentDrug(value.uri, value.name, [], value.divisibility));
ucd.prescribable = prescribable;
return availableUcd !== null && availableUcd !== void 0 ? availableUcd : [];
});
});
}
async hydrateIndicatorsForPrescriptionLineWithoutEquivalents(equivalentFormulary) {
const prescriptionLineWithoutEquivalentUris = [];
equivalentFormulary.forEach((equivalentDrugs, prescriptionLine) => {
if (equivalentDrugs.length === 0) {
prescriptionLineWithoutEquivalentUris.push(prescriptionLine.drug.uri);
}
});
try {
const pLDrugUrisWithIndicatorsMap = await this.drugService.getIndicatorsByIds(prescriptionLineWithoutEquivalentUris, this.fetchingSupplierWithBody);
equivalentFormulary.forEach((equivalentDrugs, prescriptionLine) => {
if (equivalentDrugs.length === 0) {
prescriptionLine.multiComposableDrugData =
pLDrugUrisWithIndicatorsMap.get(prescriptionLine.drug.uri);
}
});
return equivalentFormulary;
}
catch (err) {
return Promise.reject(`Technical Error: ${err}`);
}
}
async findMonoComposedEquivalentUcdForMultiComposedPrescriptionLinesWithoutEquivalentDrugs(equivalentFormulary) {
const filteredEquivalentFormulary = this.equivalentFormularyFilter.byRouteAndIndicators(await this.hydrateIndicatorsForPrescriptionLineWithoutEquivalents(equivalentFormulary));
try {
for (const equivalentFormularyElement of filteredEquivalentFormulary) {
const currentPrescriptionLine = equivalentFormularyElement[0];
if (currentPrescriptionLine.multiComposableDrugData) {
const prescriptionLineActivePrinciples = await this.drugService.getActivePrinciplesForMonoItems(this.prescribableFetchingSupplier, currentPrescriptionLine.multiComposableDrugData
.relatedMoleculeHref);
if (prescriptionLineActivePrinciples.length > 1) {
const currentPrescriptionLineUcdv = await this.drugService.getUcdv(currentPrescriptionLine.drug.uri, this.prescribableFetchingSupplier);
currentPrescriptionLine.multiComposableDrugData.activePrinciples =
currentPrescriptionLineUcdv.activePrinciples;
currentPrescriptionLine.multiComposableDrugData.ucdvDispensingUnit =
currentPrescriptionLineUcdv.dispensingUnit;
const multiComposedEquivalents = await this.findMultiComposableEquivalents(currentPrescriptionLine, this.prescribableFetchingSupplier);
equivalentFormulary.set(currentPrescriptionLine, multiComposedEquivalents);
}
}
}
}
catch (err) {
console.error(err);
}
return equivalentFormulary;
}
async findMultiComposableEquivalents(currentPrescriptionLine, prescribableFetchingSupplier) {
var _a;
if (!currentPrescriptionLine.multiComposableDrugData) {
return [];
}
let multiComposableEquivalents = [];
try {
for (const activePrinciple of currentPrescriptionLine
.multiComposableDrugData.activePrinciples) {
const equivalentDrugsForActivePrinciple = await this.drugService.getUcdsForMolecule(activePrinciple.id, prescribableFetchingSupplier);
const equivalentDrugsInFormularyForActivePrinciple = equivalentDrugsForActivePrinciple.filter((ucd) => this.exist(ucd.uri));
if (equivalentDrugsInFormularyForActivePrinciple.length === 0) {
return [];
}
const equivalentDrugs = [];
for (const eq of equivalentDrugsInFormularyForActivePrinciple) {
const eqIndicatorsAndRoutes = await this.drugService.getIndicatorsAndRoutes(eq.uri, prescribableFetchingSupplier);
if (this.equivalentFormularyFilter.isRouteAndIndicatorIncompatibleForMultiComposable(eqIndicatorsAndRoutes, currentPrescriptionLine.routeUri)) {
continue;
}
eq.multiComposableDrugData = eqIndicatorsAndRoutes;
const equivalentActivePrinciples = await this.drugService.getActivePrinciplesForMonoItems(prescribableFetchingSupplier, (_a = eq.multiComposableDrugData) === null || _a === void 0 ? void 0 : _a.relatedMoleculeHref);
if (equivalentActivePrinciples.length !== 1 ||
equivalentActivePrinciples[0].id !== activePrinciple.id) {
continue;
}
const equivalentDrugUcdv = await this.drugService.getUcdv(eq.uri, prescribableFetchingSupplier);
eq.multiComposableDrugData.activePrinciples =
equivalentDrugUcdv.activePrinciples;
eq.multiComposableDrugData.ucdvDispensingUnit =
equivalentDrugUcdv.dispensingUnit;
eq.multiComposableDrugData.activePrinciples[0].id =
activePrinciple.id;
equivalentDrugs.push(eq);
}
if (equivalentDrugs.length === 0) {
return [];
}
multiComposableEquivalents = [
...equivalentDrugs,
...multiComposableEquivalents,
];
}
return multiComposableEquivalents;
}
catch (err) {
console.error(`Technical Error: ${err}`);
return [];
}
}
};
__decorate([
state()
], VidalEquivalents.prototype, "associatedFormularyAndEquivalents", void 0);
__decorate([
property({ attribute: false })
], VidalEquivalents.prototype, "prescriptionLines", void 0);
__decorate([
property({ type: Array })
], VidalEquivalents.prototype, "availableFormularyDrugUris", void 0);
__decorate([
property({ attribute: false })
], VidalEquivalents.prototype, "prescribableFetchingSupplier", void 0);
__decorate([
property({ attribute: false })
], VidalEquivalents.prototype, "fetchingSupplierWithBody", void 0);
__decorate([
property()
], VidalEquivalents.prototype, "emitSubmitEvent", void 0);
__decorate([
state()
], VidalEquivalents.prototype, "loading", void 0);
VidalEquivalents = __decorate([
customElement('vidal-equivalent')
], VidalEquivalents);
export { VidalEquivalents };
//# sourceMappingURL=vidal-equivalents.js.map