refactor(escort): 取消状态与 status 解耦

- 新增 cancelStatus(none/requested/cancelled),status 只描述服务生命周期
- record_state 拆分 canCancelTransition 与 CANCEL_TRANSITIONS
- 管理端 Agent 工具、查询工具与接口层统一走取消状态机
- 列表默认隐藏已取消订单,支持 cancelStatus / includeCancelled 筛选
This commit is contained in:
lik
2026-11-05 18:54:52 +08:00
parent 23b9de16a6
commit fb1210106e
8 changed files with 286 additions and 74 deletions
@@ -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({