feat(tools): 数据库查询按权限拆分为 user/admin,强制 userId 隔离

- query/ 为内部实现,user/ 仅供 bot-escort(绑定登录用户、schema 不暴露 userId),admin/ 仅供 escort-admin(显式跨用户)

- 新增 employee、employee_review 查询;health_profile 用户端补齐 userId 隔离,按 _id 精确查询也带归属条件

- organization 是公共数据且无 userId,不进用户端;机构与写入工具统一移入 admin/

- utils 增加严格 ObjectId 解析,避免 CastError 与畸形 ID 进入查询

- 用户端员工视图剔除手机号、证件号、证书编号、紧急联系人与内部备注

- 新增 test/db_tools.test.js,用模型桩断言查询条件确实带 userId
This commit is contained in:
lik
2026-11-01 11:42:27 +08:00
parent 9825ac376f
commit f3a1ef89bc
22 changed files with 1031 additions and 207 deletions
@@ -0,0 +1,117 @@
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { DBModel } from "../../../../../models/index.js";
import { escapeRegex, parseObjectId } from "../utils.js";
/**
* 健康档案查询内部实现
*
* 仅供 db/user 与 db/admin 下的工具包装使用,不直接暴露给 Agent:
* - 用户端包装只传 userId,强制按当前登录用户隔离,且不暴露 userId 筛选;
* - 管理端包装传 allowCrossUser,才允许跨用户查询。
*/
function buildHealthProfileQueryTool({ userId = null, allowCrossUser = false } = {}) {
// 用户端只认调用方绑定的登录用户,管理端不绑定归属
const boundUserId = allowCrossUser ? null : parseObjectId(userId);
const shape = {
profileId: z
.string()
.optional()
.describe("健康档案 ID(_id),提供时精确查询单条,忽略其他筛选条件"),
name: z.string().optional().describe("患者姓名(支持中文模糊、全拼、拼音首字母匹配,如 张三/zhangsan/zs)"),
mobile: z.string().optional().describe("患者电话(精确匹配)"),
page: z.number().int().min(1).optional().describe("页码,从 1 开始"),
pageSize: z
.number()
.int()
.min(1)
.max(50)
.optional()
.describe("每页数量(1-50),默认 20"),
};
if (allowCrossUser) {
shape.userId = z.string().optional().describe("所属用户ID(精确匹配,仅管理端可用)");
}
return tool(
async ({ profileId, userId: queryUserId, name, mobile, page = 1, pageSize = 20 }) => {
try {
// 用户端未登录/身份异常时直接拒绝,绝不回落到跨用户查询
if (!allowCrossUser && !boundUserId) {
return { success: false, error: "用户未登录,无法查询健康档案" };
}
// 用户端把归属条件强制并入所有查询(包括按档案 ID 精确查询)
const scope = boundUserId ? { userId: boundUserId } : {};
// 按档案 _id 精确查询单条
if (profileId) {
const profileObjectId = parseObjectId(profileId);
if (!profileObjectId) {
return { success: false, error: `Invalid profile ID: ${profileId}` };
}
const doc = await DBModel.HealthProfile.findOne({ _id: profileObjectId, ...scope }).lean();
if (!doc) {
// 用户端不区分「不存在」与「不属于本人」,避免凭 ID 探测他人档案
return {
success: false,
error: allowCrossUser ? `未找到 ID 为 ${profileId} 的健康档案` : "未找到对应的健康档案",
};
}
return { success: true, data: [doc], total: 1, page: 1, pageSize: 1 };
}
const filter = { ...scope };
if (allowCrossUser && queryUserId) {
const ownerId = parseObjectId(queryUserId);
if (!ownerId) {
return { success: false, error: `Invalid user ID: ${queryUserId}` };
}
filter.userId = ownerId;
}
if (name) {
// 转义正则特殊字符,避免查询报错或 ReDoS;姓名支持中文、全拼、拼音首字母匹配
const re = { $regex: escapeRegex(name), $options: "i" };
filter.$or = [{ "profile.name": re }, { "profile.pinyin": re }, { "profile.pinyinFL": re }];
}
if (mobile) {
filter["profile.mobile"] = mobile;
}
const skip = (page - 1) * pageSize;
const [list, total] = await Promise.all([
DBModel.HealthProfile.find(filter)
.sort({ "meta.createtime": -1 })
.skip(skip)
.limit(pageSize)
.lean(),
DBModel.HealthProfile.countDocuments(filter),
]);
return {
success: true,
data: list,
total,
page,
pageSize,
};
} catch (error) {
return { success: false, error: error.message };
}
},
{
name: "health_profile_query",
description: allowCrossUser
? "查询患者健康档案(管理端)。支持按档案 ID 精确查询,或按用户 ID、患者姓名(模糊)、患者电话筛选,返回分页结果。"
: "查询当前登录用户本人的健康档案。支持按档案 ID 精确查询,或按患者姓名(模糊)、患者电话筛选,返回分页结果。查询自动限定为当前登录用户,不会返回他人档案。",
schema: z.object(shape),
}
);
}
/** 内部实现:由 db/user 与 db/admin 包装后暴露给对应 Agent */
export { buildHealthProfileQueryTool };