feat: 增加模板安装与品牌工作空间接口
This commit is contained in:
1 parent
8795086ece
commit
a73e377f91
3 files changed
+154
No files matched your search
@@ -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<unknown> {
|
||||
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<unknown> {
|
||||
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<unknown> {
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user