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 { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||||
import { BookingStatus, Prisma } from "@prisma/client";
|
import { BookingStatus, Prisma } from "@prisma/client";
|
||||||
import { PrismaService } from "./prisma.service.js";
|
import { PrismaService } from "./prisma.service.js";
|
||||||
|
import { BASIC_BOOKING_WORKFLOW, canTransitionBasicBooking } from "./workflows.js";
|
||||||
|
|
||||||
export interface CreateBookingInput {
|
export interface CreateBookingInput {
|
||||||
requestType: "package" | "private_chef" | "inquiry";
|
requestType: "package" | "private_chef" | "inquiry";
|
||||||
@@ -99,8 +100,8 @@ export class BookingService {
|
|||||||
items: { create: items },
|
items: { create: items },
|
||||||
stateTransitions: {
|
stateTransitions: {
|
||||||
create: {
|
create: {
|
||||||
workflowKey: "booking.basic",
|
workflowKey: BASIC_BOOKING_WORKFLOW.key,
|
||||||
workflowVersion: "1.0.0",
|
workflowVersion: BASIC_BOOKING_WORKFLOW.version,
|
||||||
toState: "received",
|
toState: "received",
|
||||||
actorRole: "customer",
|
actorRole: "customer",
|
||||||
},
|
},
|
||||||
@@ -179,14 +180,7 @@ export class BookingService {
|
|||||||
) {
|
) {
|
||||||
const booking = await this.prisma.booking.findFirst({ where: { id, brandId } });
|
const booking = await this.prisma.booking.findFirst({ where: { id, brandId } });
|
||||||
if (!booking) throw new NotFoundException("订单不存在");
|
if (!booking) throw new NotFoundException("订单不存在");
|
||||||
const allowed: Record<string, string[]> = {
|
if (!canTransitionBasicBooking(booking.currentState, status))
|
||||||
received: ["contacting", "cancelled"],
|
|
||||||
contacting: ["confirmed", "cancelled"],
|
|
||||||
confirmed: ["completed", "cancelled"],
|
|
||||||
completed: [],
|
|
||||||
cancelled: [],
|
|
||||||
};
|
|
||||||
if (status !== booking.status && !allowed[booking.status]?.includes(status))
|
|
||||||
throw new BadRequestException("不允许的订单状态流转");
|
throw new BadRequestException("不允许的订单状态流转");
|
||||||
return this.prisma.$transaction(async (tx) => {
|
return this.prisma.$transaction(async (tx) => {
|
||||||
const updated = await tx.booking.update({
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -517,6 +517,14 @@ test("customer can create and cancel a package booking", async () => {
|
|||||||
assert.equal(createdBody.estimatedAmount, service.priceAmount * 2);
|
assert.equal(createdBody.estimatedAmount, service.priceAmount * 2);
|
||||||
const booking = await prisma.booking.findUnique({ where: { bookingNo: createdBody.bookingNo } });
|
const booking = await prisma.booking.findUnique({ where: { bookingNo: createdBody.bookingNo } });
|
||||||
assert.ok(booking);
|
assert.ok(booking);
|
||||||
|
assert.equal(booking.workflowKey, "booking.basic");
|
||||||
|
assert.equal(booking.currentState, "received");
|
||||||
|
assert.equal(booking.offeringId, service.offeringId);
|
||||||
|
const initialTransitions = await prisma.bookingStateTransition.findMany({
|
||||||
|
where: { bookingId: booking.id },
|
||||||
|
});
|
||||||
|
assert.equal(initialTransitions.length, 1);
|
||||||
|
assert.equal(initialTransitions[0].toState, "received");
|
||||||
createdBookingIds.push(booking.id);
|
createdBookingIds.push(booking.id);
|
||||||
|
|
||||||
const cancelled = await request(`/customer/bookings/${booking.id}/cancel`, {
|
const cancelled = await request(`/customer/bookings/${booking.id}/cancel`, {
|
||||||
@@ -525,6 +533,14 @@ test("customer can create and cancel a package booking", async () => {
|
|||||||
});
|
});
|
||||||
assert.equal(cancelled.statusCode, 201);
|
assert.equal(cancelled.statusCode, 201);
|
||||||
assert.equal(JSON.parse(cancelled.body).status, "cancelled");
|
assert.equal(JSON.parse(cancelled.body).status, "cancelled");
|
||||||
|
const transitions = await prisma.bookingStateTransition.findMany({
|
||||||
|
where: { bookingId: booking.id },
|
||||||
|
orderBy: { createdAt: "asc" },
|
||||||
|
});
|
||||||
|
assert.deepEqual(
|
||||||
|
transitions.map((item) => item.toState),
|
||||||
|
["received", "cancelled"],
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("admin can advance booking status but cannot skip the state machine", async () => {
|
test("admin can advance booking status but cannot skip the state machine", async () => {
|
||||||
@@ -561,6 +577,8 @@ test("admin can advance booking status but cannot skip the state machine", async
|
|||||||
});
|
});
|
||||||
assert.equal(contacting.statusCode, 201);
|
assert.equal(contacting.statusCode, 201);
|
||||||
assert.equal(JSON.parse(contacting.body).status, "contacting");
|
assert.equal(JSON.parse(contacting.body).status, "contacting");
|
||||||
|
const contactingState = await prisma.booking.findUnique({ where: { id: booking.id } });
|
||||||
|
assert.equal(contactingState?.currentState, "contacting");
|
||||||
|
|
||||||
const skipped = await request(`/admin/bookings/${booking.id}/status`, {
|
const skipped = await request(`/admin/bookings/${booking.id}/status`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|||||||
Reference in new issue
Block a user