feat(agent): 管理端 agent 支持陪诊员并按角色收敛工具
陪诊员身份以 Employee 在职档案为准,显式管理员优先;管理端小程序首次登录会写入 role:['user'],不能据此判断。管理员保留跨用户查询与医院资料写入,陪诊员只开放本人订单查询/改单(归属校验、状态与字段白名单、并发保护)与本人档案,患者档案与财务操作不可用。
This commit is contained in:
@@ -28,11 +28,16 @@ function parseDay(value) {
|
||||
/**
|
||||
* 陪诊记录查询内部实现
|
||||
*
|
||||
* 仅供 db/user 与 db/admin 下的工具包装使用,不直接暴露给 Agent:
|
||||
* 仅供 db/user、db/attendant 与 db/admin 下的工具包装使用,不直接暴露给 Agent:
|
||||
* - 用户端包装只传 userId,强制按当前登录用户隔离;
|
||||
* - 管理端包装传 allowMobile,才开放跨用户查询与手机号/陪诊员/日期筛选。
|
||||
* - 陪诊员端包装传 boundAttendantId,强制按陪诊员归属隔离;
|
||||
* - 管理端包装传 allowMobile,才开放跨用户查询与手机号/陪诊员筛选。
|
||||
*/
|
||||
function buildEscortRecordQueryTool({ userId = null, allowMobile = false } = {}) {
|
||||
function buildEscortRecordQueryTool({ userId = null, allowMobile = false, boundAttendantId = null } = {}) {
|
||||
const attendantObjectId = boundAttendantId ? parseObjectId(boundAttendantId) : null;
|
||||
const attendantScope = Boolean(attendantObjectId);
|
||||
const allowDateRange = allowMobile || attendantScope;
|
||||
|
||||
const shape = {
|
||||
patientName: z.string().optional().describe("Patient name for fuzzy search"),
|
||||
status: z.enum(STATUS_VALUES).optional().describe(STATUS_DESC),
|
||||
@@ -52,6 +57,9 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false } = {})
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Attendant (escort staff) user ID (_id) for exact match");
|
||||
}
|
||||
|
||||
if (allowDateRange) {
|
||||
shape.dateFrom = z
|
||||
.string()
|
||||
.optional()
|
||||
@@ -65,7 +73,7 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false } = {})
|
||||
return tool(
|
||||
async ({ patientName, mobile, attendantId, dateFrom, dateTo, status, page = 1, pageSize = 20 }) => {
|
||||
try {
|
||||
if (!userId && !allowMobile) {
|
||||
if (!userId && !allowMobile && !attendantScope) {
|
||||
return { success: false, error: "用户未登录,无法查询陪诊记录" };
|
||||
}
|
||||
|
||||
@@ -79,16 +87,21 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false } = {})
|
||||
filter["patient.mobile"] = mobile;
|
||||
}
|
||||
|
||||
if (allowMobile && attendantId) {
|
||||
const attendantObjectId = parseObjectId(attendantId);
|
||||
if (!attendantObjectId) {
|
||||
return { success: false, error: `Invalid attendant ID: ${attendantId}` };
|
||||
}
|
||||
// 陪诊员只能看到分配给自己的订单:模型侧不暴露该筛选,条件在服务端强制绑定
|
||||
if (attendantScope) {
|
||||
filter["attendant.id"] = attendantObjectId;
|
||||
}
|
||||
|
||||
if (allowMobile && attendantId) {
|
||||
const queryAttendantId = parseObjectId(attendantId);
|
||||
if (!queryAttendantId) {
|
||||
return { success: false, error: `Invalid attendant ID: ${attendantId}` };
|
||||
}
|
||||
filter["attendant.id"] = queryAttendantId;
|
||||
}
|
||||
|
||||
// 日期按东八区自然日解释,dateTo 含当天
|
||||
if (allowMobile && (dateFrom || dateTo)) {
|
||||
if (allowDateRange && (dateFrom || dateTo)) {
|
||||
const range = {};
|
||||
if (dateFrom) {
|
||||
const from = parseDay(dateFrom);
|
||||
@@ -145,7 +158,9 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false } = {})
|
||||
name: "escort_record_query",
|
||||
description: allowMobile
|
||||
? "Query all escort records (admin scope). Supports filtering by patient name, mobile phone number, attendant ID, schedule date range (YYYY-MM-DD, inclusive), and appointment status. Returns paginated results sorted by appointment date in descending order."
|
||||
: "Query escort records of the current logged-in user. Supports filtering by patient name and appointment status. Returns paginated results sorted by appointment date in descending order. The query is automatically scoped to the current user.",
|
||||
: attendantScope
|
||||
? "Query escort records assigned to the current attendant. Supports filtering by patient name, appointment status and schedule date range (YYYY-MM-DD, inclusive). Returns paginated results sorted by appointment date in descending order. The query is automatically scoped to the current attendant."
|
||||
: "Query escort records of the current logged-in user. Supports filtering by patient name and appointment status. Returns paginated results sorted by appointment date in descending order. The query is automatically scoped to the current user.",
|
||||
schema: z.object(shape),
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user