diff --git a/apps/api/src/admin.dto.ts b/apps/api/src/admin.dto.ts index 88d759c..3174b37 100644 --- a/apps/api/src/admin.dto.ts +++ b/apps/api/src/admin.dto.ts @@ -81,3 +81,8 @@ export class CreateTagDto { export class UpdateBookingTagsDto { @IsArray() @IsString({ each: true }) tagIds!: string[]; } + +export class InstallTemplateDto { + @IsIn(["personal", "food"]) + templateKey!: "personal" | "food"; +} diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index bc29914..860fbc7 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -32,6 +32,7 @@ import { UpdateBrandDto, UpdateServiceDto, UpdateTrustContentDto, + InstallTemplateDto, } from "./admin.dto.js"; import { AdminAuthService } from "./admin-auth.service.js"; import { AdminLoginDto } from "./admin-auth.dto.js"; @@ -120,6 +121,120 @@ export class PublicTemplateController { } } +@Controller("public/workspace") +export class PublicWorkspaceController { + constructor(private readonly prisma: PrismaService) {} + @Get() + async get(): Promise { + const brandId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001"; + const [brand, modules, fields, offerings] = await Promise.all([ + this.prisma.brandProfile.findUnique({ where: { id: brandId } }), + this.prisma.brandModule.findMany({ + where: { brandId, isEnabled: true }, + orderBy: { sortOrder: "asc" }, + }), + this.prisma.brandFieldDefinition.findMany({ + where: { brandId, isVisible: true }, + orderBy: { sortOrder: "asc" }, + }), + this.prisma.offering.findMany({ + where: { brandId, isEnabled: true }, + orderBy: { sortOrder: "asc" }, + }), + ]); + return { brand, modules, fields, offerings }; + } +} + +@Controller("admin/workspace") +export class AdminWorkspaceController { + constructor(private readonly prisma: PrismaService) {} + private brandId(): string { + return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001"; + } + private requireAdmin(request: FastifyRequest): void { + const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); + if (!token) throw new UnauthorizedException("缺少管理员登录态"); + let identity; + try { + identity = readIdentity(token); + } catch { + throw new UnauthorizedException("管理员登录态无效或已过期"); + } + if (identity.role !== "admin") throw new ForbiddenException("无管理员权限"); + } + @Get() + async get(@Req() request: FastifyRequest): Promise { + this.requireAdmin(request); + const brandId = this.brandId(); + const [brand, installations, modules, fields, offerings, forms] = await Promise.all([ + this.prisma.brandProfile.findUnique({ where: { id: brandId } }), + this.prisma.brandTemplateInstallation.findMany({ where: { brandId } }), + this.prisma.brandModule.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }), + this.prisma.brandFieldDefinition.findMany({ + where: { brandId }, + orderBy: { sortOrder: "asc" }, + }), + this.prisma.offering.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }), + this.prisma.inquiryForm.findMany({ where: { brandId }, orderBy: { updatedAt: "desc" } }), + ]); + return { brand, installations, modules, fields, offerings, forms }; + } + @Post("install") + async install( + @Req() request: FastifyRequest, + @Body() body: InstallTemplateDto, + ): Promise { + this.requireAdmin(request); + const brandId = this.brandId(); + const template = brandTemplates[body.templateKey]; + const existing = await this.prisma.brandTemplateInstallation.findUnique({ + where: { brandId_templateKey: { brandId, templateKey: body.templateKey } }, + }); + if (existing) throw new BadRequestException("该模板已经安装"); + const modules = [ + ["text", "品牌介绍"], + ["trust-content", "信任证明"], + ["services", "服务与合作"], + ["inquiry", "咨询入口"], + ] as const; + const fields = [ + ["profile", "name", "品牌名称", "text"], + ["profile", "tagline", "一句话定位", "text"], + ["profile", "audience", "服务对象", "textarea"], + ["profile", "valueProposition", "提供的价值", "textarea"], + ["profile", "trustProof", "信任证明入口", "textarea"], + ] as const; + return this.prisma.$transaction(async (tx) => { + const installation = await tx.brandTemplateInstallation.create({ + data: { brandId, templateKey: body.templateKey, templateVersion: "1.0.0" }, + }); + await tx.brandModule.createMany({ + data: modules.map(([type, title], sortOrder) => ({ + brandId, + type, + title, + sortOrder, + source: "template", + })), + }); + await tx.brandFieldDefinition.createMany({ + data: fields.map(([scope, key, label, type], sortOrder) => ({ + brandId, + scope, + key, + label, + type, + sortOrder, + source: "core", + required: true, + })), + }); + return { installation, template }; + }); + } +} + @Controller("customer/session") export class CustomerSessionController { constructor( @@ -453,6 +568,8 @@ export class AdminConfigController { PublicController, PublicTrustContentController, PublicTemplateController, + PublicWorkspaceController, + AdminWorkspaceController, CustomerSessionController, AdminSessionController, CustomerBookingController, diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 9f2be78..8986104 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -46,6 +46,38 @@ test("health and public content are available without authentication", async () assert.ok(JSON.parse(services.body).length >= 3); }); +test("admin can install a template and public workspace exposes enabled configuration", async () => { + const login = await request("/admin/session/login", { + method: "POST", + payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD }, + }); + const token = JSON.parse(login.body).token; + const before = await request("/admin/workspace"); + assert.equal(before.statusCode, 401); + const installed = await request("/admin/workspace/install", { + method: "POST", + headers: { authorization: `Bearer ${token}` }, + payload: { templateKey: "personal" }, + }); + assert.equal(installed.statusCode, 201); + assert.equal(JSON.parse(installed.body).template.key, "personal"); + try { + const workspace = await request("/admin/workspace", { + headers: { authorization: `Bearer ${token}` }, + }); + assert.equal(workspace.statusCode, 200); + assert.equal(JSON.parse(workspace.body).installations.length, 1); + assert.equal(JSON.parse(workspace.body).fields.length, 5); + const publicWorkspace = await request("/public/workspace"); + assert.equal(publicWorkspace.statusCode, 200); + assert.equal(JSON.parse(publicWorkspace.body).modules.length, 4); + } finally { + await prisma.brandFieldDefinition.deleteMany({ where: { brandId } }); + await prisma.brandModule.deleteMany({ where: { brandId } }); + await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } }); + } +}); + test("admin booking endpoints reject missing or customer credentials", async () => { const missing = await request("/admin/bookings"); assert.equal(missing.statusCode, 401);