feat: 收口多品牌与模板构建基础

This commit is contained in:
que01 committed 2026-09-25 02:55:46 +08:00
1 parent 0425a1c672
commit 77224ea51f
317 files changed
+11290 -1158

No files matched your search

@@ -15,12 +15,29 @@ import { Prisma } from "@prisma/client";
import type { FastifyRequest } from "fastify";
import { PrismaService } from "../../prisma.service.js";
import { readIdentity } from "../../auth.js";
import { CreateTrustContentDto, UpdateBrandDto, UpdateTrustContentDto } from "../../admin.dto.js";
import { getVisualTemplate } from "@personal-brand/config";
import {
CreateTrustContentDto,
UpdateBrandDto,
UpdateBrandPresentationDto,
UpdateTrustContentDto,
} from "../../admin.dto.js";
import {
getBrandTemplate,
getVisualTemplate,
isVisualTemplateCompatible,
listVisualTemplateKeys,
getDefaultVisualTemplateKey,
} from "@personal-brand/config";
import { AdminAccessService } from "../../admin-access.service.js";
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
@Controller("admin/config")
export class AdminConfigController {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly access: AdminAccessService,
private readonly templateWorkspace: TemplateWorkspaceService,
) {}
private brandId(request?: FastifyRequest): string {
if (!request) return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
@@ -52,6 +69,9 @@ export class AdminConfigController {
): Promise<unknown> {
this.requireAdmin(request);
const { config, ...fields } = body;
if (config?.visualTemplateKey !== undefined) {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
}
const current = await this.prisma.brandProfile.findUnique({
where: { id: this.brandId(request) },
select: { config: true },
@@ -62,6 +82,19 @@ export class AdminConfigController {
: {};
const nextConfig = config ? { ...currentConfig, ...config } : undefined;
if (config?.visualTemplateKey && config.visualTemplateKey !== currentConfig.visualTemplateKey) {
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(
this.brandId(request),
);
const visualTemplate =
typeof config.visualTemplateKey === "string"
? getVisualTemplate(config.visualTemplateKey)
: undefined;
if (
!visualTemplate ||
!getBrandTemplate(templateKey) ||
!isVisualTemplateCompatible(visualTemplate, templateKey)
)
throw new BadRequestException("当前视觉模板不适配该行业模板");
const installation = await this.prisma.brandTemplateInstallation.findFirst({
where: { brandId: this.brandId(request) },
});
@@ -75,7 +108,8 @@ export class AdminConfigController {
previousSnapshots: [
...history,
{
visualTemplateKey: currentConfig.visualTemplateKey ?? "linjing-editorial",
visualTemplateKey:
currentConfig.visualTemplateKey ?? getDefaultVisualTemplateKey(templateKey),
savedAt: new Date().toISOString(),
},
],
@@ -91,6 +125,74 @@ export class AdminConfigController {
},
});
}
@Patch("presentation")
async updatePresentation(
@Req() request: FastifyRequest,
@Body() body: UpdateBrandPresentationDto,
): Promise<unknown> {
this.requireAdmin(request);
const brandId = this.brandId(request);
const brand = await this.prisma.brandProfile.findUnique({
where: { id: brandId },
select: { config: true },
});
const config =
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
? (brand.config as Record<string, unknown>)
: {};
const previous =
config.visualPresentation &&
typeof config.visualPresentation === "object" &&
!Array.isArray(config.visualPresentation)
? (config.visualPresentation as Record<string, unknown>)
: {};
const nextPresentation = { ...previous };
const identity = body.identity;
const identityFields = ["founder", "title", "slogan", "phone", "wechat", "studioAddress"];
const nextIdentity = identity
? Object.fromEntries(
identityFields
.filter((key) => typeof identity[key] === "string")
.map((key) => [key, identity[key]]),
)
: {};
for (const section of [
"home",
"offerings",
"detail",
"profile",
"inquiry",
"success",
"records",
] as const) {
const input = body[section];
if (!input) continue;
const current =
previous[section] &&
typeof previous[section] === "object" &&
!Array.isArray(previous[section])
? (previous[section] as Record<string, unknown>)
: {};
const values = Object.fromEntries(
Object.entries(input).filter(([, value]) => typeof value === "string"),
);
nextPresentation[section] = { ...current, ...values };
}
const updated = await this.prisma.brandProfile.update({
where: { id: brandId },
data: {
config: {
...config,
...nextIdentity,
visualPresentation: nextPresentation,
} as Prisma.InputJsonValue,
},
select: { config: true },
});
return {
visualPresentation: (updated.config as Record<string, unknown>).visualPresentation,
};
}
@Get("visual-template/history")
getVisualTemplateHistory(@Req() request: FastifyRequest): unknown {
this.requireAdmin(request);
@@ -169,7 +271,7 @@ export class AdminConfigController {
typeof config.visualTemplateKey === "string"
? config.visualTemplateKey
: "linjing-editorial";
const current = getVisualTemplate(currentKey) ?? getVisualTemplate("linjing-editorial");
const current = getVisualTemplate(currentKey);
return {
current: current
? {
@@ -180,7 +282,7 @@ export class AdminConfigController {
variants: current.variants,
}
: null,
available: ["linjing-editorial", "minimal-neutral"],
available: listVisualTemplateKeys(),
};
});
}
@@ -0,0 +1,54 @@
import { Controller, Get, Param, ParseIntPipe, Post, Body, Req } from "@nestjs/common";
import type { FastifyRequest } from "fastify";
import { AdminAccessService } from "../../admin-access.service.js";
import { BrandReleaseApiService } from "../../brand-release-api.service.js";
import { PublishBrandReleaseDto } from "../../release.dto.js";
@Controller("admin/releases")
export class AdminReleaseController {
constructor(
private readonly access: AdminAccessService,
private readonly releases: BrandReleaseApiService,
) {}
@Get()
async list(@Req() request: FastifyRequest): Promise<unknown[]> {
const identity = await this.requireBrand(request);
return this.releases.list(identity.brandId);
}
@Get("readiness")
async readiness(@Req() request: FastifyRequest): Promise<unknown> {
const identity = await this.requireBrand(request);
return this.releases.readiness(identity.brandId);
}
@Post("publish")
async publish(
@Req() request: FastifyRequest,
@Body() body: PublishBrandReleaseDto,
): Promise<unknown> {
const identity = await this.requireBrand(request);
return this.releases.publish(identity.brandId, identity.subject, body.note);
}
@Post(":version/rollback")
async rollback(
@Req() request: FastifyRequest,
@Param("version", ParseIntPipe) version: number,
@Body() body: PublishBrandReleaseDto,
): Promise<unknown> {
const identity = await this.requireBrand(request);
return this.releases.rollback(identity.brandId, identity.subject, version, body.note);
}
@Post("revoke")
async revoke(@Req() request: FastifyRequest): Promise<unknown> {
const identity = await this.requireBrand(request);
return this.releases.revoke(identity.brandId, identity.subject);
}
private requireBrand(request: FastifyRequest): Promise<{ subject: string; brandId: string }> {
return this.access.requireBrand(request);
}
}