feat: 建立预约工作流状态历史
This commit is contained in:
1 parent
0381c0a902
commit
e9d3f77639
3 files changed
+93
-4
No files matched your search
@@ -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";
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user