fix(agent): 医院工具切换至 hospital 集合并补齐支付约束

hospital_info_list/query/set 改为读写 hospital 集合,适配 entityName/nicknames/fudanRank;医院子代理与提示词同步字段口径;health_profile_set 更新启用 schema 校验;escort_record_set 限制线下收款方式,存在未支付流水时禁止改价。
This commit is contained in:
lik
2026-11-06 17:38:28 +08:00
parent 623ecf2651
commit 9ab2a39d08
8 changed files with 342 additions and 343 deletions
+134 -89
View File
@@ -12,135 +12,180 @@ import {
const MAIN_ID = "64b000000000000000000101";
const BRANCH_ID = "64b000000000000000000102";
const MAIN_NAME = "中国医学科学院北京协和医院";
const BRANCH_NAME = "中国医学科学院北京协和医院西单院区";
const BRANCH_NAME = "中国医学科学院北京协和医院-西单院区";
const main = {
_id: new mongoose.Types.ObjectId(MAIN_ID),
name: MAIN_NAME,
shortName: "北京协和医院",
parentId: null,
type: "hospital",
status: "active",
address: { city: "北京" },
overview: "总院就医信息",
departments: [{ name: "心内科", detail: "总院心内科" }],
doctors: [],
photos: [],
guides: {},
entityName: MAIN_NAME,
nicknames: ["协和", "北京协和"],
pinyin: "zhongguoyixuekexueyuanbeijingxieheyiyuan",
pinyinFL: "zgyxkxybjxhyy",
province: "北京市",
city: "北京市",
address: "东城区帅府园1号",
phone: "010-69151188",
level: "三级甲等",
departments: ["心内科", "风湿免疫科"],
fudanRank: { rank: 1, grade: "A+++++", order: 1 },
meta: { updatetime: new Date("2026-11-05T00:00:00.000Z") },
};
const branch = {
...main,
_id: new mongoose.Types.ObjectId(BRANCH_ID),
name: BRANCH_NAME,
shortName: "协和西院",
parentId: main._id,
type: "hospital",
status: "active",
address: { city: "北京" },
overview: "西单院区就医信息",
departments: [{ name: "妇科内分泌与生殖医学中心", detail: "西单院区" }],
doctors: [],
photos: [],
guides: {},
// 历史遗留字段不应再被工具读取
forAgent: { overview: "旧字段", departments: [{ name: "旧科室" }] },
address: "西城区大木仓胡同41号",
departments: ["妇科内分泌与生殖医学中心"],
};
function parseResult(result) {
return typeof result === "string" ? JSON.parse(result) : result;
}
// Mongoose Query 链式调用的最小桩
function createQuery(docs, calls, method, filter) {
calls.push({ method, filter });
return {
sort() { return this; },
limit() { return this; },
select() { return this; },
lean() { return Promise.resolve(docs); },
exec() { return Promise.resolve(docs); },
};
}
function createOrganizationStub({ list = [], one = null, count = 0 } = {}) {
/**
* hospital 模型桩:工具通过模型静态方法读写,桩直接记录调用参数
*/
function createHospitalStub({ list = [], total = list.length, one = null } = {}) {
const calls = [];
return {
calls,
countDocuments(filter) {
calls.push({ method: "countDocuments", filter });
return Promise.resolve(count);
findHospitals(options) {
calls.push({ method: "findHospitals", options });
return Promise.resolve({
list,
total,
page: options.page ?? 1,
pageSize: options.pageSize ?? 20,
});
},
find(filter) {
return createQuery(list, calls, "find", filter);
findHospitalById(id) {
calls.push({ method: "findHospitalById", id });
return Promise.resolve(one);
},
findOne(filter) {
return createQuery(one, calls, "findOne", filter);
},
findByIdAndUpdate(id, update) {
calls.push({ method: "findByIdAndUpdate", id, update });
return {
exec() {
return Promise.resolve({ ...one, ...update.$set });
},
};
updateHospitalById(id, update) {
calls.push({ method: "updateHospitalById", id, update });
return Promise.resolve({
...one,
...update,
// 模拟模型静态方法自动补派生字段
pinyin: "paiban",
fudanRank: { rank: 1, grade: "A+++++", order: 1 },
});
},
};
}
function withOrganization(stub, fn) {
const previous = DBModel.Organization;
DBModel.Organization = stub;
function withHospital(stub, fn) {
const previous = DBModel.Hospital;
DBModel.Hospital = stub;
return Promise.resolve(fn()).finally(() => {
DBModel.Organization = previous;
DBModel.Hospital = previous;
});
}
test("医院列表只查可用医院,并可按 parentId 列院区", async () => {
const stub = createOrganizationStub({ list: [main, branch], count: 2 });
await withOrganization(stub, async () => {
test("医院列表走 hospital 集合,并支持关键词与主体名称过滤", async () => {
const stub = createHospitalStub({ list: [main, branch], total: 2 });
await withHospital(stub, async () => {
const byKeyword = parseResult(await hospitalInfoListTool.invoke({ keyword: "协和" }));
assert.equal(byKeyword.success, true);
assert.equal(byKeyword.data.length, 2);
assert.equal(byKeyword.data[1].parentId.toHexString(), MAIN_ID);
assert.equal(stub.calls[0].filter.type, "hospital");
assert.equal(stub.calls[0].filter.status, "active");
assert.ok(stub.calls[0].filter.$or.some((item) => "shortName" in item));
assert.equal(byKeyword.total, 2);
assert.equal(byKeyword.data[0].entityName, MAIN_NAME);
assert.equal(byKeyword.data[0].phone, "010-69151188");
assert.equal(stub.calls[0].options.keyword, "协和");
const byParent = parseResult(await hospitalInfoListTool.invoke({ parentId: MAIN_ID }));
assert.equal(byParent.success, true);
assert.equal(stub.calls.at(-2).filter.parentId.toHexString(), MAIN_ID);
const byEntity = parseResult(
await hospitalInfoListTool.invoke({ entityName: MAIN_NAME })
);
assert.equal(byEntity.success, true);
assert.equal(stub.calls.at(-1).options.entityName, MAIN_NAME);
});
});
test("按分院 id 查询返回平铺字段和总院名称", async () => {
const stub = createOrganizationStub({ list: [main], one: branch });
await withOrganization(stub, async () => {
test("按 id 查询返回 hospital 基础信息与派生字段", async () => {
const stub = createHospitalStub({ one: branch });
await withHospital(stub, async () => {
const result = parseResult(await hospitalInfoQueryTool.invoke({ hospitalId: BRANCH_ID }));
assert.equal(result.success, true);
assert.equal(result.data[0].name, BRANCH_NAME);
assert.equal(result.data[0].parentName, MAIN_NAME);
assert.equal(result.data[0].overview, "西单院区就医信息");
assert.equal(result.data[0].departments[0].name, "妇科内分泌与生殖医学中心");
assert.equal(result.data[0].entityName, MAIN_NAME);
assert.deepEqual(result.data[0].nicknames, ["协和", "北京协和"]);
assert.equal(result.data[0].fudanRank.grade, "A+++++");
assert.deepEqual(result.data[0].departments, ["妇科内分泌与生殖医学中心"]);
});
});
test("名称命中多家时返回候选,改用 id 后写入顶层字段", async () => {
const stub = createOrganizationStub({ list: [main, branch], one: branch, count: 2 });
await withOrganization(stub, async () => {
const ambiguous = parseResult(await hospitalInfoSetTool.invoke({ hospitalName: "协和", section: "overview", content: "x" }));
assert.equal(ambiguous.success, false);
assert.equal(ambiguous.candidates.length, 2);
assert.ok(ambiguous.candidates.every((item) => item.id));
const set = parseResult(
await hospitalInfoSetTool.invoke({ hospitalId: BRANCH_ID, section: "overview", content: "新就医信息" })
test("按名称查询可按科室过滤,科室为字符串数组", async () => {
const stub = createHospitalStub({ list: [main, branch], total: 2 });
await withHospital(stub, async () => {
const result = parseResult(
await hospitalInfoQueryTool.invoke({ hospitalName: "协和", department: "风湿" })
);
assert.equal(set.success, true);
assert.equal(set.data.overview, "新就医信息");
assert.ok(set.data.infoUpdatedAt);
const update = stub.calls.at(-1).update.$set;
assert.equal(update.overview, "新就医信息");
assert.equal("forAgent.overview" in update, false);
assert.equal(result.success, true);
assert.equal(result.total, 2);
assert.deepEqual(result.data[0].departments, ["风湿免疫科"]);
assert.deepEqual(result.data[1].departments, []);
});
});
test("查询缺少 id 与名称时明确报错", async () => {
const stub = createHospitalStub();
await withHospital(stub, async () => {
const result = parseResult(await hospitalInfoQueryTool.invoke({}));
assert.equal(result.success, false);
assert.match(result.error, /hospitalId 或 hospitalName/);
});
});
test("写入时名称命中多家返回候选,不落库", async () => {
const stub = createHospitalStub({ list: [main, branch], total: 2, one: branch });
await withHospital(stub, async () => {
const result = parseResult(
await hospitalInfoSetTool.invoke({ hospitalName: "协和", phone: "010-12345678" })
);
assert.equal(result.success, false);
assert.equal(result.candidates.length, 2);
assert.ok(result.candidates.every((item) => item.id));
assert.equal(stub.calls.filter((call) => call.method === "updateHospitalById").length, 0);
});
});
test("按 id 写入只提交基础字段,派生字段由模型同步", async () => {
const stub = createHospitalStub({ one: branch });
await withHospital(stub, async () => {
const result = parseResult(
await hospitalInfoSetTool.invoke({
hospitalId: BRANCH_ID,
phone: " 010-12345678 ",
departments: ["心内科", " ", "风湿免疫科"],
})
);
assert.equal(result.success, true);
const updateCall = stub.calls.find((call) => call.method === "updateHospitalById");
assert.equal(String(updateCall.id), BRANCH_ID);
assert.equal(updateCall.update.phone, "010-12345678");
assert.deepEqual(updateCall.update.departments, ["心内科", "风湿免疫科"]);
// 拼音/昵称/复旦榜档位不允许手写
assert.equal("pinyin" in updateCall.update, false);
assert.equal("nicknames" in updateCall.update, false);
assert.equal("fudanRank" in updateCall.update, false);
assert.equal(result.data.phone, "010-12345678");
assert.deepEqual(result.data.departments, ["心内科", "风湿免疫科"]);
});
});
test("写入未提供字段或名称为空时拒绝", async () => {
const stub = createHospitalStub({ one: branch });
await withHospital(stub, async () => {
const empty = parseResult(await hospitalInfoSetTool.invoke({ hospitalId: BRANCH_ID }));
assert.equal(empty.success, false);
assert.match(empty.error, /未提供任何要更新的字段/);
const emptyName = parseResult(
await hospitalInfoSetTool.invoke({ hospitalId: BRANCH_ID, name: " " })
);
assert.equal(emptyName.success, false);
assert.match(emptyName.error, /医院名称不能为空/);
});
});