From e9d3f77639f0efa10d8db675684700225a2e7829 Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 21 Sep 2026 15:03:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BB=BA=E7=AB=8B=E9=A2=84=E7=BA=A6?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E6=B5=81=E7=8A=B6=E6=80=81=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migration.sql | 28 ++++++++++ apps/api/prisma/schema.prisma | 17 ++++++ apps/api/src/booking.service.ts | 52 +++++++++++++++++-- 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 apps/api/prisma/migrations/20260921130000_add_booking_workflow/migration.sql diff --git a/apps/api/prisma/migrations/20260921130000_add_booking_workflow/migration.sql b/apps/api/prisma/migrations/20260921130000_add_booking_workflow/migration.sql new file mode 100644 index 0000000..7c6f09b --- /dev/null +++ b/apps/api/prisma/migrations/20260921130000_add_booking_workflow/migration.sql @@ -0,0 +1,28 @@ +ALTER TABLE "Booking" ADD COLUMN "workflowKey" TEXT NOT NULL DEFAULT 'booking.basic'; +ALTER TABLE "Booking" ADD COLUMN "workflowVersion" TEXT NOT NULL DEFAULT '1.0.0'; +ALTER TABLE "Booking" ADD COLUMN "currentState" TEXT NOT NULL DEFAULT 'received'; + +UPDATE "Booking" SET "currentState" = "status"::text; + +CREATE TABLE "BookingStateTransition" ( + "id" TEXT NOT NULL, + "bookingId" TEXT NOT NULL, + "workflowKey" TEXT NOT NULL, + "workflowVersion" TEXT NOT NULL, + "fromState" TEXT, + "toState" TEXT NOT NULL, + "actorRole" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "BookingStateTransition_pkey" PRIMARY KEY ("id") +); + +ALTER TABLE "BookingStateTransition" ADD CONSTRAINT "BookingStateTransition_bookingId_fkey" + FOREIGN KEY ("bookingId") REFERENCES "Booking"("id") ON DELETE CASCADE ON UPDATE CASCADE; +CREATE INDEX "BookingStateTransition_bookingId_createdAt_idx" + ON "BookingStateTransition"("bookingId", "createdAt"); + +INSERT INTO "BookingStateTransition" ( + "id", "bookingId", "workflowKey", "workflowVersion", "fromState", "toState", "actorRole", "createdAt" +) +SELECT gen_random_uuid()::text, "id", "workflowKey", "workflowVersion", NULL, "currentState", 'migration', "createdAt" +FROM "Booking"; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 7ce59b4..5027541 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -247,15 +247,32 @@ model Booking { note String? adminNote String? status BookingStatus @default(received) + workflowKey String @default("booking.basic") + workflowVersion String @default("1.0.0") + currentState String @default("received") isRead Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt items BookingItem[] tags BookingTag[] + stateTransitions BookingStateTransition[] @@index([brandId, status, createdAt]) @@index([customerUserId, createdAt]) } +model BookingStateTransition { + id String @id @default(uuid()) + bookingId String + booking Booking @relation(fields: [bookingId], references: [id], onDelete: Cascade) + workflowKey String + workflowVersion String + fromState String? + toState String + actorRole String + createdAt DateTime @default(now()) + @@index([bookingId, createdAt]) +} + model BookingItem { id String @id @default(uuid()) bookingId String diff --git a/apps/api/src/booking.service.ts b/apps/api/src/booking.service.ts index eaa758f..301de17 100644 --- a/apps/api/src/booking.service.ts +++ b/apps/api/src/booking.service.ts @@ -97,6 +97,14 @@ export class BookingService { ...(input.extraData ? { extraData: input.extraData } : {}), note: input.note ?? null, items: { create: items }, + stateTransitions: { + create: { + workflowKey: "booking.basic", + workflowVersion: "1.0.0", + toState: "received", + actorRole: "customer", + }, + }, }, select: { bookingNo: true, status: true, estimatedAmount: true, createdAt: true }, }); @@ -115,7 +123,23 @@ export class BookingService { const booking = await this.prisma.booking.findFirst({ where: { id, brandId, customerUserId } }); if (!booking) throw new NotFoundException("订单不存在"); if (booking.status !== "received") throw new BadRequestException("当前状态不可取消"); - return this.prisma.booking.update({ where: { id }, data: { status: "cancelled" } }); + return this.prisma.$transaction(async (tx) => { + const updated = await tx.booking.update({ + where: { id }, + data: { status: "cancelled", currentState: "cancelled" }, + }); + await tx.bookingStateTransition.create({ + data: { + bookingId: id, + workflowKey: booking.workflowKey, + workflowVersion: booking.workflowVersion, + fromState: booking.currentState, + toState: "cancelled", + actorRole: "customer", + }, + }); + return updated; + }); } async listForAdmin( @@ -164,9 +188,29 @@ export class BookingService { }; if (status !== booking.status && !allowed[booking.status]?.includes(status)) throw new BadRequestException("不允许的订单状态流转"); - return this.prisma.booking.update({ - where: { id }, - data: { status, ...(adminNote !== undefined ? { adminNote } : {}), isRead: true }, + return this.prisma.$transaction(async (tx) => { + const updated = await tx.booking.update({ + where: { id }, + data: { + status, + currentState: status, + ...(adminNote !== undefined ? { adminNote } : {}), + isRead: true, + }, + }); + if (status !== booking.currentState) { + await tx.bookingStateTransition.create({ + data: { + bookingId: id, + workflowKey: booking.workflowKey, + workflowVersion: booking.workflowVersion, + fromState: booking.currentState, + toState: status, + actorRole: "admin", + }, + }); + } + return updated; }); }