feat: 新增独立工坊宣传模板及配置导入验证

This commit is contained in:
que01 committed 2026-10-15 17:44:52 +08:00
1 parent bf4bbf81c7
commit 04e42b3a80
43 files changed
+5726 -13

No files matched your search

@@ -1,6 +1,6 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { getTemplateSampleContent } from "@personal-brand/config";
import { getTemplateSampleContent, getDefaultVisualTemplateKey } from "@personal-brand/config";
import { validateFieldSnapshot } from "@personal-brand/validation";
import { PrismaService } from "../../prisma.service.js";
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
@@ -14,7 +14,20 @@ export class AdminSampleContentService {
async preview(brandId: string) {
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(brandId);
const sample = getTemplateSampleContent(templateKey);
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 sample = getTemplateSampleContent(
templateKey,
typeof config.visualTemplateKey === "string"
? config.visualTemplateKey
: getDefaultVisualTemplateKey(templateKey),
);
if (!sample) throw new BadRequestException("模板示例内容不存在");
const [offerings, trustContent, fields, enabledForms] = await Promise.all([
this.prisma.offering.count({ where: { brandId } }),
@@ -37,7 +50,20 @@ export class AdminSampleContentService {
async import(brandId: string, actorId: string) {
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(brandId);
const sample = getTemplateSampleContent(templateKey);
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 sample = getTemplateSampleContent(
templateKey,
typeof config.visualTemplateKey === "string"
? config.visualTemplateKey
: getDefaultVisualTemplateKey(templateKey),
);
if (!sample) throw new BadRequestException("模板示例内容不存在");
if (validateFieldSnapshot(sample.fields).length)
throw new BadRequestException("模板咨询表单示例字段无效");
@@ -49,12 +75,42 @@ export class AdminSampleContentService {
});
if (existing || existingOffering) throw new BadRequestException("示例内容已经导入");
const result = await this.prisma.$transaction(async (tx) => {
if (sample.brandConfig) {
// 仅显式导入时补齐缺失配置,已有品牌内容(包括空值)始终优先。
const merge = (
defaults: Record<string, unknown>,
saved: Record<string, unknown>,
): Record<string, unknown> =>
Object.fromEntries(
[...new Set([...Object.keys(defaults), ...Object.keys(saved)])].map((key) => {
const a = defaults[key];
const b = saved[key];
if (!Object.prototype.hasOwnProperty.call(saved, key)) return [key, a];
return [
key,
a &&
b &&
typeof a === "object" &&
typeof b === "object" &&
!Array.isArray(a) &&
!Array.isArray(b)
? merge(a as Record<string, unknown>, b as Record<string, unknown>)
: b,
];
}),
);
await tx.brandProfile.update({
where: { id: brandId },
data: { config: merge(sample.brandConfig, config) as Prisma.InputJsonValue },
});
}
const offerings = await tx.offering.createMany({
data: sample.offerings.map((item, sortOrder) => ({
brandId,
kind: item.kind,
name: item.name,
summary: item.summary,
...(item.coverUrl ? { coverUrl: item.coverUrl } : {}),
config: { categoryKey: item.categoryKey },
sortOrder,
isEnabled: true,