feat: 收口多品牌与模板构建基础
This commit is contained in:
1 parent
0425a1c672
commit
77224ea51f
317 files changed
+11290
-1158
No files matched your search
@@ -1,11 +1,33 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { getBrandTemplate, getVisualTemplateManifest } from "@personal-brand/config";
|
||||
import {
|
||||
getBrandTemplate,
|
||||
getVisualTemplate,
|
||||
getVisualTemplateManifest,
|
||||
getDefaultVisualTemplateKey,
|
||||
isVisualTemplateCompatible,
|
||||
} from "@personal-brand/config";
|
||||
import type { BrandTemplateKey } from "@personal-brand/types";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
|
||||
@Injectable()
|
||||
export class TemplateWorkspaceService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
async getEffectiveTemplateKey(brandId: string): Promise<BrandTemplateKey> {
|
||||
const [installation, brand] = await Promise.all([
|
||||
this.prisma.brandTemplateInstallation.findFirst({
|
||||
where: { brandId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
select: { templateKey: true },
|
||||
}),
|
||||
this.prisma.brandProfile.findUnique({
|
||||
where: { id: brandId },
|
||||
select: { templateKey: true },
|
||||
}),
|
||||
]);
|
||||
const key = installation?.templateKey ?? brand?.templateKey ?? "personal";
|
||||
return getBrandTemplate(key) ? (key as BrandTemplateKey) : "personal";
|
||||
}
|
||||
async install(brandId: string, templateKey: "personal" | "food" | "doors") {
|
||||
const template = getBrandTemplate(templateKey);
|
||||
if (!template) throw new BadRequestException("行业模板不存在");
|
||||
@@ -23,14 +45,22 @@ export class TemplateWorkspaceService {
|
||||
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
|
||||
? (brand.config as Record<string, unknown>)
|
||||
: {};
|
||||
const configuredVisualTemplateKey =
|
||||
typeof brandConfig.visualTemplateKey === "string" ? brandConfig.visualTemplateKey : undefined;
|
||||
const visualTemplateKey =
|
||||
typeof brandConfig.visualTemplateKey === "string"
|
||||
? brandConfig.visualTemplateKey
|
||||
: templateKey === "doors"
|
||||
? "linjing-editorial"
|
||||
: "minimal-neutral";
|
||||
configuredVisualTemplateKey &&
|
||||
getVisualTemplate(configuredVisualTemplateKey) &&
|
||||
isVisualTemplateCompatible(getVisualTemplate(configuredVisualTemplateKey)!, templateKey)
|
||||
? configuredVisualTemplateKey
|
||||
: getDefaultVisualTemplateKey(templateKey);
|
||||
const visualManifest = getVisualTemplateManifest(visualTemplateKey);
|
||||
if (!visualManifest) throw new BadRequestException("视觉模板不存在");
|
||||
const visualTemplate = getVisualTemplate(visualTemplateKey);
|
||||
if (
|
||||
!visualManifest ||
|
||||
!visualTemplate ||
|
||||
!isVisualTemplateCompatible(visualTemplate, templateKey)
|
||||
)
|
||||
throw new BadRequestException("当前视觉模板不适配行业模板");
|
||||
const templatePackageSnapshot = {
|
||||
schemaVersion: "1.0",
|
||||
version: template.version,
|
||||
@@ -39,6 +69,14 @@ export class TemplateWorkspaceService {
|
||||
capabilities: template.capabilities,
|
||||
};
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
if (brandConfig.visualTemplateKey === undefined) {
|
||||
await tx.brandProfile.update({
|
||||
where: { id: brandId },
|
||||
data: {
|
||||
config: { ...brandConfig, visualTemplateKey },
|
||||
},
|
||||
});
|
||||
}
|
||||
const installation = await tx.brandTemplateInstallation.create({
|
||||
data: {
|
||||
brandId,
|
||||
|
||||
Reference in new issue
Block a user