test: 收口预约工作流状态历史验证
This commit is contained in:
1 parent
e9d3f77639
commit
36f5415483
3 files changed
+42
-10
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { BookingStatus, Prisma } from "@prisma/client";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
import { BASIC_BOOKING_WORKFLOW, canTransitionBasicBooking } from "./workflows.js";
|
||||
|
||||
export interface CreateBookingInput {
|
||||
requestType: "package" | "private_chef" | "inquiry";
|
||||
@@ -99,8 +100,8 @@ export class BookingService {
|
||||
items: { create: items },
|
||||
stateTransitions: {
|
||||
create: {
|
||||
workflowKey: "booking.basic",
|
||||
workflowVersion: "1.0.0",
|
||||
workflowKey: BASIC_BOOKING_WORKFLOW.key,
|
||||
workflowVersion: BASIC_BOOKING_WORKFLOW.version,
|
||||
toState: "received",
|
||||
actorRole: "customer",
|
||||
},
|
||||
@@ -179,14 +180,7 @@ export class BookingService {
|
||||
) {
|
||||
const booking = await this.prisma.booking.findFirst({ where: { id, brandId } });
|
||||
if (!booking) throw new NotFoundException("订单不存在");
|
||||
const allowed: Record<string, string[]> = {
|
||||
received: ["contacting", "cancelled"],
|
||||
contacting: ["confirmed", "cancelled"],
|
||||
confirmed: ["completed", "cancelled"],
|
||||
completed: [],
|
||||
cancelled: [],
|
||||
};
|
||||
if (status !== booking.status && !allowed[booking.status]?.includes(status))
|
||||
if (!canTransitionBasicBooking(booking.currentState, status))
|
||||
throw new BadRequestException("不允许的订单状态流转");
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const updated = await tx.booking.update({
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
export const BASIC_BOOKING_WORKFLOW = {
|
||||
key: "booking.basic",
|
||||
version: "1.0.0",
|
||||
transitions: {
|
||||
received: ["contacting", "cancelled"],
|
||||
contacting: ["confirmed", "cancelled"],
|
||||
confirmed: ["completed", "cancelled"],
|
||||
completed: [],
|
||||
cancelled: [],
|
||||
},
|
||||
} as const;
|
||||
|
||||
export type BasicBookingState = keyof typeof BASIC_BOOKING_WORKFLOW.transitions;
|
||||
|
||||
export function canTransitionBasicBooking(from: string, to: string): boolean {
|
||||
return (
|
||||
from === to ||
|
||||
BASIC_BOOKING_WORKFLOW.transitions[from as BasicBookingState]?.includes(to as never) === true
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user