- 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
138 lines
5.3 KiB
JavaScript
138 lines
5.3 KiB
JavaScript
import { tool } from "@langchain/core/tools";
|
|
import { z } from "zod";
|
|
import { DBModel } from "../../../../../models/index.js";
|
|
import { parseObjectId } from "../utils.js";
|
|
|
|
const STATUS_VALUES = ["published", "hidden"];
|
|
const STATUS_DESC = "Review status: published (已发布,计入评分), hidden (已隐藏,不计入评分)";
|
|
|
|
// 白名单投影:只返回 schema 已定义字段,避免未来新增字段被整文档带出
|
|
const REVIEW_FIELDS = "recordId employeeId userId score content status meta";
|
|
|
|
/**
|
|
* 员工评价查询内部实现
|
|
*
|
|
* 仅供 db/user 与 db/admin 下的工具包装使用,不直接暴露给 Agent:
|
|
* - 用户端包装只传 userId,强制按评价人隔离,只返回当前登录用户自己提交的评价;
|
|
* - 管理端包装传 allowCrossUser,才允许跨用户查询。
|
|
*/
|
|
function buildEmployeeReviewQueryTool({ userId = null, allowCrossUser = false } = {}) {
|
|
const boundUserId = allowCrossUser ? null : parseObjectId(userId);
|
|
|
|
const shape = {
|
|
reviewId: z
|
|
.string()
|
|
.optional()
|
|
.describe("Review ID (_id) for exact lookup; when provided, other filters are ignored"),
|
|
employeeId: z.string().optional().describe("Reviewed employee ID (_id) for exact match"),
|
|
recordId: z.string().optional().describe("Related escort record ID (_id) for exact match"),
|
|
score: z.number().int().min(1).max(5).optional().describe("Rating score (1-5)"),
|
|
status: z.enum(STATUS_VALUES).optional().describe(STATUS_DESC),
|
|
page: z.number().int().min(1).optional().describe("Page number, starting from 1"),
|
|
pageSize: z
|
|
.number()
|
|
.int()
|
|
.min(1)
|
|
.max(50)
|
|
.optional()
|
|
.describe("Records per page (1-50), default 20"),
|
|
};
|
|
|
|
if (allowCrossUser) {
|
|
shape.userId = z.string().optional().describe("Reviewer user ID (_id) for exact match");
|
|
}
|
|
|
|
return tool(
|
|
async ({ reviewId, employeeId, recordId, userId: queryUserId, score, status, page = 1, pageSize = 20 }) => {
|
|
try {
|
|
if (!allowCrossUser && !boundUserId) {
|
|
return { success: false, error: "用户未登录,无法查询员工评价" };
|
|
}
|
|
|
|
// 用户端强制评价人归属;按评价 ID 精确查询时同样并入该条件
|
|
const scope = boundUserId ? { userId: boundUserId } : {};
|
|
|
|
if (reviewId) {
|
|
const reviewObjectId = parseObjectId(reviewId);
|
|
if (!reviewObjectId) {
|
|
return { success: false, error: `Invalid review ID: ${reviewId}` };
|
|
}
|
|
const doc = await DBModel.EmployeeReview.findOne({ _id: reviewObjectId, ...scope })
|
|
.select(REVIEW_FIELDS)
|
|
.lean();
|
|
if (!doc) {
|
|
// 用户端不区分「不存在」与「不属于本人」,避免凭 ID 探测他人评价
|
|
return {
|
|
success: false,
|
|
error: allowCrossUser ? `No review found for ID: ${reviewId}` : "未找到对应的员工评价",
|
|
};
|
|
}
|
|
return { success: true, data: [doc], total: 1, page: 1, pageSize: 1 };
|
|
}
|
|
|
|
const filter = { ...scope };
|
|
|
|
if (allowCrossUser && queryUserId) {
|
|
const reviewerId = parseObjectId(queryUserId);
|
|
if (!reviewerId) {
|
|
return { success: false, error: `Invalid user ID: ${queryUserId}` };
|
|
}
|
|
filter.userId = reviewerId;
|
|
}
|
|
if (employeeId) {
|
|
const employeeObjectId = parseObjectId(employeeId);
|
|
if (!employeeObjectId) {
|
|
return { success: false, error: `Invalid employee ID: ${employeeId}` };
|
|
}
|
|
filter.employeeId = employeeObjectId;
|
|
}
|
|
if (recordId) {
|
|
const recordObjectId = parseObjectId(recordId);
|
|
if (!recordObjectId) {
|
|
return { success: false, error: `Invalid record ID: ${recordId}` };
|
|
}
|
|
filter.recordId = recordObjectId;
|
|
}
|
|
if (score !== undefined) {
|
|
filter.score = score;
|
|
}
|
|
if (status) {
|
|
filter.status = status;
|
|
}
|
|
|
|
const skip = (page - 1) * pageSize;
|
|
|
|
const [list, total] = await Promise.all([
|
|
DBModel.EmployeeReview.find(filter)
|
|
.select(REVIEW_FIELDS)
|
|
.sort({ "meta.createtime": -1 })
|
|
.skip(skip)
|
|
.limit(pageSize)
|
|
.lean(),
|
|
DBModel.EmployeeReview.countDocuments(filter),
|
|
]);
|
|
|
|
return {
|
|
success: true,
|
|
data: list,
|
|
total,
|
|
page,
|
|
pageSize,
|
|
};
|
|
} catch (error) {
|
|
return { success: false, error: error.message };
|
|
}
|
|
},
|
|
{
|
|
name: "employee_review_query",
|
|
description: allowCrossUser
|
|
? "Query employee reviews across all users (admin scope). Supports exact review ID lookup, or filters by employee ID, escort record ID, reviewer user ID, score and status. Returns paginated results sorted by creation time in descending order."
|
|
: "Query employee reviews submitted by the current logged-in user. The query is automatically scoped to the current user ID and never returns reviews submitted by other users. Supports filtering by employee ID, escort record ID, score and status.",
|
|
schema: z.object(shape),
|
|
}
|
|
);
|
|
}
|
|
|
|
/** 内部实现:由 db/user 与 db/admin 包装后暴露给对应 Agent */
|
|
export { buildEmployeeReviewQueryTool };
|