refactor(escort): 取消状态与 status 解耦
- 新增 cancelStatus(none/requested/cancelled),status 只描述服务生命周期 - record_state 拆分 canCancelTransition 与 CANCEL_TRANSITIONS - 管理端 Agent 工具、查询工具与接口层统一走取消状态机 - 列表默认隐藏已取消订单,支持 cancelStatus / includeCancelled 筛选
This commit is contained in:
@@ -2,10 +2,15 @@ import { tool } from "@langchain/core/tools";
|
||||
import { z } from "zod";
|
||||
import mongoose from "mongoose";
|
||||
import { DBModel } from "../../../../../models/index.js";
|
||||
import { canTransition, STATUS_TRANSITIONS } from "../../../../../utils/record_state.js";
|
||||
import {
|
||||
canTransition,
|
||||
canCancelTransition,
|
||||
STATUS_TRANSITIONS,
|
||||
CANCEL_STATUS_VALUES,
|
||||
} from "../../../../../utils/record_state.js";
|
||||
|
||||
const escortRecordSetTool = tool(
|
||||
async ({ orderId, status, notes, payment }) => {
|
||||
async ({ orderId, status, cancelStatus, notes, payment }) => {
|
||||
try {
|
||||
if (!orderId) {
|
||||
return {
|
||||
@@ -32,8 +37,37 @@ const escortRecordSetTool = tool(
|
||||
// 状态迁移必须与接口层同一套规则:否则 Agent 能把已完成订单改回进行中,
|
||||
// 绕过状态机并造成费用/服务记录不一致
|
||||
const update = {};
|
||||
// 兼容旧数据:取消曾存放在 status 中
|
||||
const currentCancel = record.status === "cancelled"
|
||||
? "cancelled"
|
||||
: (record.cancelStatus || "none");
|
||||
|
||||
// 取消与 status 解耦,单独走取消状态机
|
||||
if (cancelStatus !== undefined && cancelStatus !== currentCancel) {
|
||||
if (!canCancelTransition(currentCancel, cancelStatus)) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Illegal cancel status transition: ${currentCancel} -> ${cancelStatus}`,
|
||||
};
|
||||
}
|
||||
update.cancelStatus = cancelStatus;
|
||||
if (cancelStatus === "requested") update.cancelRequestedAt = new Date();
|
||||
if (cancelStatus === "cancelled") update.cancelledAt = new Date();
|
||||
}
|
||||
|
||||
if (status !== undefined && status !== record.status) {
|
||||
if (currentCancel !== "none") {
|
||||
return {
|
||||
success: false,
|
||||
error: "Order is cancelling or cancelled; service status cannot change.",
|
||||
};
|
||||
}
|
||||
if (update.cancelStatus) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Do not update service status and cancel status in the same request.",
|
||||
};
|
||||
}
|
||||
if (!canTransition(record.status, status)) {
|
||||
const allowed = STATUS_TRANSITIONS[record.status] || [];
|
||||
return {
|
||||
@@ -75,6 +109,11 @@ const escortRecordSetTool = tool(
|
||||
// 以读取时的状态作为更新条件,避免并发下跳过中间状态;同时排除已软删除的订单
|
||||
const filter = { _id: record._id, deletedAt: null };
|
||||
if (status !== undefined && status !== record.status) filter.status = record.status;
|
||||
if (cancelStatus !== undefined && cancelStatus !== currentCancel) {
|
||||
filter.cancelStatus = currentCancel === "none"
|
||||
? { $in: ["none", null] }
|
||||
: currentCancel;
|
||||
}
|
||||
|
||||
const updatedRecord = await DBModel.EscortRecord.findOneAndUpdate(
|
||||
filter,
|
||||
@@ -103,16 +142,22 @@ const escortRecordSetTool = tool(
|
||||
{
|
||||
name: "escort_record_set",
|
||||
description:
|
||||
"Update escort record fields by record ID (_id). Supports updating status, notes (patientNote, escortNote, medicalSummary), and payment (totalFee, paidFee, status). Only provided fields will be updated.",
|
||||
"Update escort record fields by record ID (_id). Supports updating service status, cancel status (none/requested/cancelled), notes (patientNote, escortNote, medicalSummary), and payment (totalFee, paidFee, status). Only provided fields will be updated.",
|
||||
schema: z.object({
|
||||
orderId: z
|
||||
.string()
|
||||
.describe("Record ID (_id) used as the lookup key"),
|
||||
status: z
|
||||
.enum(["pending", "confirmed", "in_progress", "completed", "cancelled"])
|
||||
.enum(["pending", "confirmed", "in_progress", "completed"])
|
||||
.optional()
|
||||
.describe(
|
||||
"Appointment status: pending (待确认), confirmed (已确认), in_progress (进行中), completed (已完成), cancelled (已取消)"
|
||||
"Service status: pending (待确认), confirmed (已确认), in_progress (进行中), completed (已完成). Cancellation is controlled by cancelStatus."
|
||||
),
|
||||
cancelStatus: z
|
||||
.enum(CANCEL_STATUS_VALUES)
|
||||
.optional()
|
||||
.describe(
|
||||
"Cancel status: none (未取消), requested (待取消), cancelled (已取消)"
|
||||
),
|
||||
notes: z
|
||||
.object({
|
||||
|
||||
@@ -59,13 +59,17 @@ function buildAttendantEscortRecordSetTool({ attendantId = null } = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
if ((record.cancelStatus || "none") !== "none" || record.status === "cancelled") {
|
||||
return {
|
||||
success: false,
|
||||
error: "订单已取消或正在取消,不能修改。",
|
||||
};
|
||||
}
|
||||
|
||||
if (!EDITABLE_RECORD_STATUS.includes(record.status)) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
record.status === "pending"
|
||||
? "订单还未确认,不能操作,请联系管理员确认。"
|
||||
: "订单已取消,不能修改。",
|
||||
error: "订单还未确认或已结束,不能操作。",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,12 @@ import { z } from "zod";
|
||||
import { DBModel } from "../../../../../models/index.js";
|
||||
import { escapeRegex, parseObjectId } from "../utils.js";
|
||||
|
||||
const STATUS_VALUES = ["pending", "confirmed", "in_progress", "completed", "cancelled"];
|
||||
const STATUS_VALUES = ["pending", "confirmed", "in_progress", "completed"];
|
||||
const STATUS_DESC =
|
||||
"Appointment status: pending (待确认), confirmed (已确认), in_progress (进行中), completed (已完成), cancelled (已取消)";
|
||||
"Service status: pending (待确认), confirmed (已确认), in_progress (进行中), completed (已完成). Cancellation is a separate cancelStatus.";
|
||||
const CANCEL_STATUS_VALUES = ["none", "requested", "cancelled"];
|
||||
const CANCEL_STATUS_DESC =
|
||||
"Cancel status: none (未取消), requested (待取消), cancelled (已取消)";
|
||||
|
||||
/**
|
||||
* 严格解析 YYYY-MM-DD(东八区零点)
|
||||
@@ -41,6 +44,7 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false, boundA
|
||||
const shape = {
|
||||
patientName: z.string().optional().describe("Patient name for fuzzy search"),
|
||||
status: z.enum(STATUS_VALUES).optional().describe(STATUS_DESC),
|
||||
cancelStatus: z.enum(CANCEL_STATUS_VALUES).optional().describe(CANCEL_STATUS_DESC),
|
||||
page: z.number().int().min(1).optional().describe("Page number, starting from 1"),
|
||||
pageSize: z
|
||||
.number()
|
||||
@@ -71,7 +75,7 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false, boundA
|
||||
}
|
||||
|
||||
return tool(
|
||||
async ({ patientName, mobile, attendantId, dateFrom, dateTo, status, page = 1, pageSize = 20 }) => {
|
||||
async ({ patientName, mobile, attendantId, dateFrom, dateTo, status, cancelStatus, page = 1, pageSize = 20 }) => {
|
||||
try {
|
||||
if (!userId && !allowMobile && !attendantScope) {
|
||||
return { success: false, error: "用户未登录,无法查询陪诊记录" };
|
||||
@@ -124,6 +128,16 @@ function buildEscortRecordQueryTool({ userId = null, allowMobile = false, boundA
|
||||
filter.status = status;
|
||||
}
|
||||
|
||||
if (cancelStatus === "cancelled") {
|
||||
// 兼容旧数据:取消曾存放在 status 中
|
||||
filter.$or = [{ cancelStatus: "cancelled" }, { status: "cancelled" }];
|
||||
} else if (cancelStatus) {
|
||||
filter.cancelStatus = cancelStatus;
|
||||
} else if (!status) {
|
||||
// 与列表口径一致:未显式查询时隐藏旧数据里的 status=cancelled
|
||||
filter.status = { $ne: "cancelled" };
|
||||
}
|
||||
|
||||
// 用户端按订单归属过滤;不能用手机号作为隔离键(手机号可由用户自行修改)
|
||||
if (userId) {
|
||||
filter.userId = userId;
|
||||
|
||||
Reference in New Issue
Block a user