feat(agent): 管理端 agent 支持陪诊员并按角色收敛工具
陪诊员身份以 Employee 在职档案为准,显式管理员优先;管理端小程序首次登录会写入 role:['user'],不能据此判断。管理员保留跨用户查询与医院资料写入,陪诊员只开放本人订单查询/改单(归属校验、状态与字段白名单、并发保护)与本人档案,患者档案与财务操作不可用。
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { buildEmployeeQueryTool } from "../query/employee_query.js";
|
||||
|
||||
/**
|
||||
* 陪诊员端员工查询工具
|
||||
*
|
||||
* 只允许查本人档案:强制按当前登录用户 ID 隔离,schema 不暴露 userId;
|
||||
* 输出走用户端白名单,不返回身份证、手机号、证书编号等敏感字段。
|
||||
*/
|
||||
const createAttendantEmployeeQueryTool = (userInfo) =>
|
||||
buildEmployeeQueryTool({ userId: userInfo?._id });
|
||||
|
||||
export { createAttendantEmployeeQueryTool };
|
||||
@@ -0,0 +1,13 @@
|
||||
import { buildEscortRecordQueryTool } from "../query/escort_record_query.js";
|
||||
|
||||
/**
|
||||
* 陪诊员端陪诊记录查询工具
|
||||
*
|
||||
* 只传入当前登录陪诊员的用户 ID,作为「我的订单」强制归属条件:
|
||||
* - schema 不暴露手机号、陪诊员 ID 等跨用户筛选;
|
||||
* - 所有查询(含日期范围)都自动限定为 attendant.id = 当前用户。
|
||||
*/
|
||||
const createAttendantEscortRecordQueryTool = (userInfo) =>
|
||||
buildEscortRecordQueryTool({ boundAttendantId: userInfo?._id });
|
||||
|
||||
export { createAttendantEscortRecordQueryTool };
|
||||
@@ -0,0 +1,159 @@
|
||||
import { tool } from "@langchain/core/tools";
|
||||
import { z } from "zod";
|
||||
import { DBModel } from "../../../../../models/index.js";
|
||||
import { canTransition, STATUS_TRANSITIONS } from "../../../../../utils/record_state.js";
|
||||
import { parseObjectId } from "../utils.js";
|
||||
|
||||
// 陪诊员只能推进自己订单的服务进度,确认/取消/改期/改金额都属于平台管理员
|
||||
const ALLOWED_STATUS_VALUES = ["in_progress", "completed"];
|
||||
const ALLOWED_STATUS_DESC =
|
||||
"Service progress: in_progress (开始服务), completed (服务完成)";
|
||||
// 未确认、已取消的订单不允许陪诊员写入任何内容
|
||||
const EDITABLE_RECORD_STATUS = ["confirmed", "in_progress", "completed"];
|
||||
|
||||
/**
|
||||
* 陪诊员端订单更新工具
|
||||
*
|
||||
* 与管理端 escort_record_set 的差异(防止陪诊员越权):
|
||||
* - 只能改分配给本人的订单(attendant.id 必须等于当前用户);
|
||||
* - 只能 confirmed -> in_progress、in_progress -> completed;
|
||||
* - 只能写陪诊备注与就诊小结,不能改患者备注、费用、排班、陪诊员归属。
|
||||
*
|
||||
* @param {Object} [options]
|
||||
* @param {string} [options.attendantId] 当前登录陪诊员的用户 ID
|
||||
*/
|
||||
function buildAttendantEscortRecordSetTool({ attendantId = null } = {}) {
|
||||
const boundAttendantId = parseObjectId(attendantId);
|
||||
|
||||
return tool(
|
||||
async ({ orderId, status, notes }) => {
|
||||
try {
|
||||
if (!boundAttendantId) {
|
||||
return { success: false, error: "陪诊员身份无效,无法修改订单" };
|
||||
}
|
||||
|
||||
const recordObjectId = parseObjectId(orderId);
|
||||
if (!recordObjectId) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Invalid record ID: ${orderId}. A valid record _id is required.`,
|
||||
};
|
||||
}
|
||||
|
||||
const record = await DBModel.EscortRecord.findOne({
|
||||
_id: recordObjectId,
|
||||
deletedAt: null,
|
||||
});
|
||||
if (!record) {
|
||||
return {
|
||||
success: false,
|
||||
error: `No escort record found for order ID: ${orderId}`,
|
||||
};
|
||||
}
|
||||
|
||||
// 归属校验:不属于本人一律按无权处理,不透露订单内容
|
||||
if (String(record.attendant?.id ?? "") !== String(boundAttendantId)) {
|
||||
return {
|
||||
success: false,
|
||||
error: "无权操作该订单:该订单未分配给你。",
|
||||
};
|
||||
}
|
||||
|
||||
if (!EDITABLE_RECORD_STATUS.includes(record.status)) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
record.status === "pending"
|
||||
? "订单还未确认,不能操作,请联系管理员确认。"
|
||||
: "订单已取消,不能修改。",
|
||||
};
|
||||
}
|
||||
|
||||
const update = {};
|
||||
|
||||
if (status !== undefined && status !== record.status) {
|
||||
if (!ALLOWED_STATUS_VALUES.includes(status)) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
"陪诊员只能把订单从 confirmed 改为 in_progress(开始服务),或从 in_progress 改为 completed(服务完成)。确认、取消、改期、退款请联系管理员。",
|
||||
};
|
||||
}
|
||||
if (!canTransition(record.status, status)) {
|
||||
const allowed = STATUS_TRANSITIONS[record.status] || [];
|
||||
return {
|
||||
success: false,
|
||||
error: `Illegal status transition: ${record.status} -> ${status}. Allowed: ${
|
||||
allowed.length ? allowed.join(", ") : "none (terminal state)"
|
||||
}`,
|
||||
};
|
||||
}
|
||||
update.status = status;
|
||||
}
|
||||
|
||||
// 患者备注属于用户输入,陪诊员只能写自己的服务备注和就诊小结
|
||||
if (notes !== undefined) {
|
||||
if (notes.escortNote !== undefined) {
|
||||
update["notes.escortNote"] = notes.escortNote;
|
||||
}
|
||||
if (notes.medicalSummary !== undefined) {
|
||||
update["notes.medicalSummary"] = notes.medicalSummary;
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(update).length === 0) {
|
||||
return { success: true, data: record, message: "No fields to update" };
|
||||
}
|
||||
|
||||
update["meta.updatetime"] = Date.now();
|
||||
|
||||
// 以读取时的状态作为更新条件,避免并发下跳过中间状态;
|
||||
// 同时再次绑定陪诊员归属,防止更新期间订单被改派
|
||||
const filter = {
|
||||
_id: record._id,
|
||||
deletedAt: null,
|
||||
"attendant.id": boundAttendantId,
|
||||
};
|
||||
if (update.status !== undefined) filter.status = record.status;
|
||||
|
||||
const updatedRecord = await DBModel.EscortRecord.findOneAndUpdate(
|
||||
filter,
|
||||
{ $set: update },
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
|
||||
if (!updatedRecord) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Record state changed while updating. Re-query the record and retry.",
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, data: updatedRecord };
|
||||
} catch (error) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "escort_record_set",
|
||||
description:
|
||||
"Update one of the current attendant's assigned escort records by record ID (_id). Only orders assigned to the current attendant can be updated. Supports advancing status (confirmed -> in_progress, in_progress -> completed) and writing the attendant's service notes (escortNote, medicalSummary). Payment, rebooking, cancellation and patient notes are not editable.",
|
||||
schema: z.object({
|
||||
orderId: z.string().describe("Record ID (_id) used as the lookup key"),
|
||||
status: z
|
||||
.enum(ALLOWED_STATUS_VALUES)
|
||||
.optional()
|
||||
.describe(ALLOWED_STATUS_DESC),
|
||||
notes: z
|
||||
.object({
|
||||
escortNote: z.string().optional().describe("Escort service notes"),
|
||||
medicalSummary: z.string().optional().describe("Medical visit summary"),
|
||||
})
|
||||
.optional()
|
||||
.describe("Service notes to update"),
|
||||
}),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export { buildAttendantEscortRecordSetTool };
|
||||
@@ -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