diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 93d0ebd..732a301 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -184,12 +184,21 @@ export class AdminWorkspaceController { } @Get("offerings") - listOfferings(@Req() request: FastifyRequest): Promise { + async listOfferings(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); - return this.prisma.offering.findMany({ + const items = await this.prisma.offering.findMany({ where: { brandId: this.brandId() }, orderBy: { sortOrder: "asc" }, }); + return items.map((item) => { + const config = (item.config ?? {}) as Record; + return { + ...item, + sections: config.sections ?? [], + media: config.media ?? [], + cta: config.cta ?? null, + }; + }); } @Post("offerings") @@ -279,10 +288,12 @@ export class AdminWorkspaceController { 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 existing = await this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } }); + if (existing) { + if (existing.templateKey === body.templateKey) + throw new BadRequestException("该模板已经安装"); + throw new BadRequestException("品牌已经安装主模板,请先完成模板切换流程"); + } const modules = [ ["text", "品牌介绍"], ["trust-content", "信任证明"], diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index e8bff65..bc23509 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -71,6 +71,12 @@ test("admin can install a template and public workspace exposes enabled configur const publicWorkspace = await request("/public/workspace"); assert.equal(publicWorkspace.statusCode, 200); assert.equal(JSON.parse(publicWorkspace.body).modules.length, 4); + const conflict = await request("/admin/workspace/install", { + method: "POST", + headers: { authorization: `Bearer ${token}` }, + payload: { templateKey: "food" }, + }); + assert.equal(conflict.statusCode, 400); } finally { await prisma.brandFieldDefinition.deleteMany({ where: { brandId } }); await prisma.brandModule.deleteMany({ where: { brandId } });