hn-hlw-service/hlw/drug-info/applicable-diagnosis.js

58 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2026-08-06 16:32:57 +08:00
let ensureIndexesPromise = null;
async function ensureApplicableDiagnosisIndexes(db) {
if (!ensureIndexesPromise) {
ensureIndexesPromise = Promise.all([
2026-08-25 13:39:09 +08:00
db.collection("drug-info").createIndex({ applicableDiagnosisCodes: 1 }),
db.collection("online-drug-info").createIndex({ applicableDiagnosisCodes: 1 }),
2026-08-06 16:32:57 +08:00
]).catch((error) => {
ensureIndexesPromise = null;
throw error;
});
}
await ensureIndexesPromise;
}
2026-08-25 13:39:09 +08:00
async function validateApplicableDiagnosisCodes(params, db) {
if (!Object.prototype.hasOwnProperty.call(params, "applicableDiagnosisCodes")) {
2026-08-06 16:32:57 +08:00
return { present: false, value: [] };
}
2026-08-25 13:39:09 +08:00
const rawCodes = params.applicableDiagnosisCodes;
if (!Array.isArray(rawCodes)) {
2026-08-06 16:32:57 +08:00
throw new Error("适应诊断格式错误");
}
2026-08-25 13:39:09 +08:00
const uniqueCodes = [];
2026-08-06 16:32:57 +08:00
const seen = new Set();
2026-08-25 13:39:09 +08:00
for (const rawCode of rawCodes) {
if (typeof rawCode !== "string" || !rawCode.trim()) {
throw new Error("适应诊断包含无效编码");
2026-08-06 16:32:57 +08:00
}
2026-08-25 13:39:09 +08:00
const code = rawCode.trim();
if (!seen.has(code)) {
seen.add(code);
uniqueCodes.push(code);
2026-08-06 16:32:57 +08:00
}
}
2026-08-25 13:39:09 +08:00
if (uniqueCodes.length) {
const diagnoses = await db.collection("hlw-diagnosis").find(
{ code: { $in: uniqueCodes } },
{ projection: { _id: 0, code: 1 } }
).toArray();
const existingCodes = new Set(diagnoses.map(item => item.code));
if (uniqueCodes.some(code => !existingCodes.has(code))) {
2026-08-06 16:32:57 +08:00
throw new Error("部分适应诊断不存在,请重新选择");
}
}
await ensureApplicableDiagnosisIndexes(db);
2026-08-25 13:39:09 +08:00
return { present: true, value: uniqueCodes };
2026-08-06 16:32:57 +08:00
}
module.exports = {
ensureApplicableDiagnosisIndexes,
2026-08-25 13:39:09 +08:00
validateApplicableDiagnosisCodes,
2026-08-06 16:32:57 +08:00
};