67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
|
|
const { ObjectId } = require("mongodb");
|
||
|
|
|
||
|
|
const ACCESS_LEVEL_MIN = 0;
|
||
|
|
const ACCESS_LEVEL_MAX = 3;
|
||
|
|
|
||
|
|
function normalizeAccessLevel(value) {
|
||
|
|
return Number.isInteger(value) && value >= ACCESS_LEVEL_MIN && value <= ACCESS_LEVEL_MAX
|
||
|
|
? value
|
||
|
|
: ACCESS_LEVEL_MIN;
|
||
|
|
}
|
||
|
|
|
||
|
|
function getHighestDrugAccessLevels(drugs = []) {
|
||
|
|
return drugs.reduce(
|
||
|
|
(levels, drug) => ({
|
||
|
|
antibioticLevel: Math.max(levels.antibioticLevel, normalizeAccessLevel(drug && drug.antibiotic_level)),
|
||
|
|
tumorUse: Math.max(levels.tumorUse, normalizeAccessLevel(drug && drug.tumor_use)),
|
||
|
|
}),
|
||
|
|
{ antibioticLevel: ACCESS_LEVEL_MIN, tumorUse: ACCESS_LEVEL_MIN }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
async function getRequiredDrugAccessLevels(db, drugs) {
|
||
|
|
const drugIds = Array.isArray(drugs)
|
||
|
|
? [...new Set(drugs
|
||
|
|
.map(drug => drug && drug._id)
|
||
|
|
.filter(id => ObjectId.isValid(id))
|
||
|
|
.map(id => String(id)))]
|
||
|
|
: [];
|
||
|
|
|
||
|
|
if (drugIds.length === 0) {
|
||
|
|
return getHighestDrugAccessLevels([]);
|
||
|
|
}
|
||
|
|
|
||
|
|
const drugList = await db.collection("drug-info").find(
|
||
|
|
{ _id: { $in: drugIds.map(id => new ObjectId(id)) } },
|
||
|
|
{ projection: { antibiotic_level: 1, tumor_use: 1 } }
|
||
|
|
).toArray();
|
||
|
|
|
||
|
|
return getHighestDrugAccessLevels(drugList);
|
||
|
|
}
|
||
|
|
|
||
|
|
function doctorMeetsDrugAccessLevels(doctor, requiredLevels) {
|
||
|
|
return normalizeAccessLevel(doctor && doctor.antibiotic_level) >= normalizeAccessLevel(requiredLevels && requiredLevels.antibioticLevel)
|
||
|
|
&& normalizeAccessLevel(doctor && doctor.tumor_use) >= normalizeAccessLevel(requiredLevels && requiredLevels.tumorUse);
|
||
|
|
}
|
||
|
|
|
||
|
|
function getDoctorAccessLevelQuery(minimumAntibioticLevel, minimumTumorUse) {
|
||
|
|
const query = {};
|
||
|
|
const antibioticLevel = normalizeAccessLevel(minimumAntibioticLevel);
|
||
|
|
const tumorUse = normalizeAccessLevel(minimumTumorUse);
|
||
|
|
if (antibioticLevel > 0) {
|
||
|
|
query.antibiotic_level = { $gte: antibioticLevel };
|
||
|
|
}
|
||
|
|
if (tumorUse > 0) {
|
||
|
|
query.tumor_use = { $gte: tumorUse };
|
||
|
|
}
|
||
|
|
return query;
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
normalizeAccessLevel,
|
||
|
|
getHighestDrugAccessLevels,
|
||
|
|
getRequiredDrugAccessLevels,
|
||
|
|
doctorMeetsDrugAccessLevels,
|
||
|
|
getDoctorAccessLevelQuery,
|
||
|
|
};
|