feat: 建立工作流与能力注册表
This commit is contained in:
1 parent
c00e188392
commit
29d14378a7
5 files changed
+62
-5
No files matched your search
@@ -1,7 +1,9 @@
|
|||||||
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";
|
import { canTransitionWorkflow, workflowRegistry } from "@personal-brand/config";
|
||||||
|
|
||||||
|
const BASIC_BOOKING_WORKFLOW = workflowRegistry["booking.basic"];
|
||||||
|
|
||||||
export interface CreateBookingInput {
|
export interface CreateBookingInput {
|
||||||
requestType: "package" | "private_chef" | "inquiry";
|
requestType: "package" | "private_chef" | "inquiry";
|
||||||
@@ -123,7 +125,7 @@ export class BookingService {
|
|||||||
async cancelForCustomer(brandId: string, customerUserId: string, id: string) {
|
async cancelForCustomer(brandId: string, customerUserId: string, id: string) {
|
||||||
const booking = await this.prisma.booking.findFirst({ where: { id, brandId, customerUserId } });
|
const booking = await this.prisma.booking.findFirst({ where: { id, brandId, customerUserId } });
|
||||||
if (!booking) throw new NotFoundException("订单不存在");
|
if (!booking) throw new NotFoundException("订单不存在");
|
||||||
if (!canTransitionBasicBooking(booking.currentState, "cancelled"))
|
if (!canTransitionWorkflow(booking.workflowKey, booking.currentState, "cancelled"))
|
||||||
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({
|
||||||
@@ -181,7 +183,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("订单不存在");
|
||||||
if (!canTransitionBasicBooking(booking.currentState, status))
|
if (!canTransitionWorkflow(booking.workflowKey, booking.currentState, 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({
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ import type { TransformFnParams } from "class-transformer";
|
|||||||
import { IsBoolean, IsEnum, IsOptional, IsString } from "class-validator";
|
import { IsBoolean, IsEnum, IsOptional, IsString } from "class-validator";
|
||||||
import { readIdentity, signIdentity } from "./auth.js";
|
import { readIdentity, signIdentity } from "./auth.js";
|
||||||
import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
|
import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
|
||||||
import { getBrandTemplate } from "@personal-brand/config";
|
import { getBrandTemplate, resolveCapabilities } from "@personal-brand/config";
|
||||||
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
||||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||||
|
|
||||||
@@ -172,6 +172,7 @@ export class PublicWorkspaceController {
|
|||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
capabilities: template?.capabilities ?? [],
|
capabilities: template?.capabilities ?? [],
|
||||||
|
runtimeCapabilities: template ? resolveCapabilities(template.capabilities) : [],
|
||||||
modules,
|
modules,
|
||||||
runtimeModules,
|
runtimeModules,
|
||||||
fields,
|
fields,
|
||||||
|
|||||||
@@ -106,6 +106,10 @@ test("admin can install a template and public workspace exposes enabled configur
|
|||||||
assert.equal(workspaceBody.contractVersion, "1.0");
|
assert.equal(workspaceBody.contractVersion, "1.0");
|
||||||
assert.equal(workspaceBody.template.key, "personal");
|
assert.equal(workspaceBody.template.key, "personal");
|
||||||
assert.deepEqual(workspaceBody.capabilities, ["offerings", "inquiry"]);
|
assert.deepEqual(workspaceBody.capabilities, ["offerings", "inquiry"]);
|
||||||
|
assert.deepEqual(workspaceBody.runtimeCapabilities, [
|
||||||
|
{ key: "offerings", version: "1.0.0" },
|
||||||
|
{ key: "inquiry", version: "1.0.0" },
|
||||||
|
]);
|
||||||
assert.equal(workspaceBody.modules.length, 5);
|
assert.equal(workspaceBody.modules.length, 5);
|
||||||
assert.equal(workspaceBody.runtimeModules.length, 5);
|
assert.equal(workspaceBody.runtimeModules.length, 5);
|
||||||
assert.equal(workspaceBody.runtimeModules[0].schemaVersion, "1.0");
|
assert.equal(workspaceBody.runtimeModules[0].schemaVersion, "1.0");
|
||||||
|
|||||||
@@ -124,6 +124,11 @@ export interface BrandTemplateDefinition {
|
|||||||
required: boolean;
|
required: boolean;
|
||||||
}>;
|
}>;
|
||||||
capabilities: string[];
|
capabilities: string[];
|
||||||
|
runtimeCapabilities: Array<{
|
||||||
|
key: string;
|
||||||
|
version: string;
|
||||||
|
workflowKey?: string;
|
||||||
|
}>;
|
||||||
}
|
}
|
||||||
export interface OfferingSection {
|
export interface OfferingSection {
|
||||||
type: "text" | "image-list" | "timeline" | "faq";
|
type: "text" | "image-list" | "timeline" | "faq";
|
||||||
|
|||||||
@@ -1,6 +1,41 @@
|
|||||||
export const configPackageName = "personal-brand-config";
|
export const configPackageName = "personal-brand-config";
|
||||||
|
|
||||||
export type BrandTemplateKey = "personal" | "food";
|
export type BrandTemplateKey = "personal" | "food";
|
||||||
|
export type CapabilityKey = "offerings" | "inquiry" | "booking";
|
||||||
|
|
||||||
|
export interface WorkflowDefinition {
|
||||||
|
key: string;
|
||||||
|
version: string;
|
||||||
|
initialState: string;
|
||||||
|
transitions: Record<string, readonly string[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CapabilityDefinition {
|
||||||
|
key: CapabilityKey;
|
||||||
|
version: string;
|
||||||
|
workflowKey?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const workflowRegistry = {
|
||||||
|
"booking.basic": {
|
||||||
|
key: "booking.basic",
|
||||||
|
version: "1.0.0",
|
||||||
|
initialState: "received",
|
||||||
|
transitions: {
|
||||||
|
received: ["contacting", "cancelled"],
|
||||||
|
contacting: ["confirmed", "cancelled"],
|
||||||
|
confirmed: ["completed", "cancelled"],
|
||||||
|
completed: [],
|
||||||
|
cancelled: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const satisfies Record<string, WorkflowDefinition>;
|
||||||
|
|
||||||
|
export const capabilityRegistry = {
|
||||||
|
offerings: { key: "offerings", version: "1.0.0" },
|
||||||
|
inquiry: { key: "inquiry", version: "1.0.0" },
|
||||||
|
booking: { key: "booking", version: "1.0.0", workflowKey: "booking.basic" },
|
||||||
|
} as const satisfies Record<CapabilityKey, CapabilityDefinition>;
|
||||||
|
|
||||||
export type BrandTemplateModuleType = "identity" | "trust" | "offering" | "inquiry" | "contact";
|
export type BrandTemplateModuleType = "identity" | "trust" | "offering" | "inquiry" | "contact";
|
||||||
|
|
||||||
@@ -27,7 +62,17 @@ export interface BrandTemplateDefinition {
|
|||||||
trustContentTypes: string[];
|
trustContentTypes: string[];
|
||||||
modules: BrandTemplateModuleDefinition[];
|
modules: BrandTemplateModuleDefinition[];
|
||||||
fields: BrandTemplateFieldDefinition[];
|
fields: BrandTemplateFieldDefinition[];
|
||||||
capabilities: string[];
|
capabilities: CapabilityKey[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function canTransitionWorkflow(workflowKey: string, from: string, to: string): boolean {
|
||||||
|
const workflow = workflowRegistry[workflowKey as keyof typeof workflowRegistry] as
|
||||||
|
WorkflowDefinition | undefined;
|
||||||
|
return workflow ? from === to || workflow.transitions[from]?.includes(to) === true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveCapabilities(keys: readonly CapabilityKey[]): CapabilityDefinition[] {
|
||||||
|
return keys.map((key) => capabilityRegistry[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const brandTemplates = {
|
export const brandTemplates = {
|
||||||
|
|||||||
Reference in new issue
Block a user