fix: 管理端 agent 写订单走统一状态机
escort_record_set 不再直接改 status,改为复用 utils/record_state.js 的迁移规则并以读取时状态作为更新条件;软删除订单对查询与 Agent 均不可见;支付状态枚举补 refunding。
This commit is contained in:
@@ -24,6 +24,8 @@ function createEscortRecordQueryTool(userInfo) {
|
||||
};
|
||||
}
|
||||
filter.userId = userId;
|
||||
// 软删除的订单对用户端不可见
|
||||
filter.deletedAt = null;
|
||||
|
||||
if (status) {
|
||||
filter.status = status;
|
||||
|
||||
@@ -6,7 +6,7 @@ import { escapeRegex } from "./utils.js";
|
||||
const escortRecordQueryTool = tool(
|
||||
async ({ patientName, mobile, status, page = 1, pageSize = 20 }) => {
|
||||
try {
|
||||
const filter = {};
|
||||
const filter = { deletedAt: null };
|
||||
|
||||
if (patientName) {
|
||||
filter["patient.name"] = { $regex: escapeRegex(patientName), $options: "i" };
|
||||
|
||||
@@ -2,6 +2,7 @@ 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";
|
||||
|
||||
const escortRecordSetTool = tool(
|
||||
async ({ orderId, status, notes, payment }) => {
|
||||
@@ -28,9 +29,20 @@ const escortRecordSetTool = tool(
|
||||
};
|
||||
}
|
||||
|
||||
// 状态迁移必须与接口层同一套规则:否则 Agent 能把已完成订单改回进行中,
|
||||
// 绕过状态机并造成费用/服务记录不一致
|
||||
const update = {};
|
||||
|
||||
if (status !== undefined) {
|
||||
if (status !== undefined && status !== record.status) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -60,12 +72,23 @@ const escortRecordSetTool = tool(
|
||||
|
||||
update["meta.updatetime"] = Date.now();
|
||||
|
||||
const updatedRecord = await DBModel.EscortRecord.findByIdAndUpdate(
|
||||
record._id,
|
||||
// 以读取时的状态作为更新条件,避免并发下跳过中间状态;同时排除已软删除的订单
|
||||
const filter = { _id: record._id, deletedAt: null };
|
||||
if (status !== undefined && status !== record.status) filter.status = record.status;
|
||||
|
||||
const updatedRecord = await DBModel.EscortRecord.findOneAndUpdate(
|
||||
filter,
|
||||
{ $set: update },
|
||||
{ new: true }
|
||||
{ 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,
|
||||
@@ -104,9 +127,9 @@ const escortRecordSetTool = tool(
|
||||
totalFee: z.number().optional().describe("Total service fee (RMB)"),
|
||||
paidFee: z.number().optional().describe("Amount already paid (RMB)"),
|
||||
status: z
|
||||
.enum(["unpaid", "partial", "paid", "refunded"])
|
||||
.enum(["unpaid", "partial", "paid", "refunding", "refunded"])
|
||||
.optional()
|
||||
.describe("Payment status: unpaid, partial, paid, refunded"),
|
||||
.describe("Payment status: unpaid, partial, paid, refunding, refunded"),
|
||||
})
|
||||
.optional()
|
||||
.describe("Payment information to update"),
|
||||
|
||||
Reference in New Issue
Block a user