/** * 陪诊订单状态机(HTTP 接口与管理端 Agent 工具共用同一份规则) * * 之前 Agent 的 escort_record_set 直接改 status,绕过了接口层的迁移校验, * 可以把已完成订单改回进行中。规则集中在这里,任何入口都必须走 canTransition。 * * status 只描述服务生命周期;取消由独立的 cancelStatus 表达: * - none → requested:用户对已确认/已收款订单提交取消申请,等陪诊师或管理员确认 * - none / requested → cancelled:正式取消 */ // 允许的服务状态迁移;completed 不可再变 const STATUS_TRANSITIONS = { pending: ["confirmed"], confirmed: ["in_progress"], in_progress: ["completed"], completed: [], }; // 取消状态迁移;cancelled 不可再变 const CANCEL_TRANSITIONS = { none: ["requested", "cancelled"], requested: ["cancelled"], cancelled: [], }; // 取消状态枚举,供接口参数校验复用 const CANCEL_STATUS_VALUES = Object.keys(CANCEL_TRANSITIONS); /** * 判断服务状态迁移是否允许 * @param {string} from 当前状态 * @param {string} to 目标状态 * @returns {boolean} */ function canTransition(from, to) { return (STATUS_TRANSITIONS[from] || []).includes(to); } /** * 判断取消状态迁移是否允许 * @param {string} from 当前取消状态 * @param {string} to 目标取消状态 * @returns {boolean} */ function canCancelTransition(from, to) { return (CANCEL_TRANSITIONS[from] || []).includes(to); } export { STATUS_TRANSITIONS, CANCEL_TRANSITIONS, CANCEL_STATUS_VALUES, canTransition, canCancelTransition };