feat: 收口多品牌与模板构建基础
This commit is contained in:
1 parent
0425a1c672
commit
77224ea51f
317 files changed
+11290
-1158
No files matched your search
@@ -2,12 +2,14 @@ import { Controller, Get, Query } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { PrismaService } from "../../prisma.service.js";
|
||||
import { PublicBrandContextService } from "./public-brand-context.service.js";
|
||||
import type { BrandReleaseSnapshot } from "../../brand-release.service.js";
|
||||
import {
|
||||
getBrandTemplate,
|
||||
getVisualTemplate,
|
||||
getVisualTemplateManifest,
|
||||
resolveCapabilities,
|
||||
capabilityPlugins,
|
||||
getDefaultVisualTemplateKey,
|
||||
} from "@personal-brand/config";
|
||||
import type {
|
||||
RuntimeInquiryField,
|
||||
@@ -22,60 +24,77 @@ export class PublicWorkspaceController {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly brandContext: PublicBrandContextService,
|
||||
) {}
|
||||
private readonly cache = new Map<string, { expiresAt: number; value: unknown }>();
|
||||
@Get()
|
||||
async get(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
|
||||
const brandId = await this.brandContext.resolveId(brandSlug);
|
||||
const cached = process.env.NODE_ENV === "test" ? undefined : this.cache.get(brandId);
|
||||
if (cached && cached.expiresAt > Date.now()) return cached.value;
|
||||
const [
|
||||
brand,
|
||||
modules,
|
||||
fields,
|
||||
offerings,
|
||||
inquiryForms,
|
||||
installation,
|
||||
capabilityInstallations,
|
||||
trustContents,
|
||||
] = 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" },
|
||||
}),
|
||||
this.prisma.inquiryForm.findMany({
|
||||
where: {
|
||||
brandId,
|
||||
isEnabled: true,
|
||||
OR: [{ offeringId: null }, { offering: { isEnabled: true } }],
|
||||
},
|
||||
orderBy: { updatedAt: "desc" },
|
||||
}),
|
||||
this.prisma.brandTemplateInstallation.findFirst({
|
||||
where: { brandId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
}),
|
||||
this.prisma.capabilityInstallation.findMany({ where: { brandId } }),
|
||||
this.prisma.trustContent.findMany({
|
||||
where: { brandId, isPublished: true },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
}),
|
||||
]);
|
||||
const templateKey = installation?.templateKey ?? brand?.templateKey ?? "personal";
|
||||
const template = getBrandTemplate(templateKey);
|
||||
const runtimeCapabilities = capabilityInstallations.length
|
||||
? capabilityInstallations.map((item) => ({
|
||||
key: item.capabilityKey,
|
||||
version: item.capabilityVersion,
|
||||
status: item.status as "enabled" | "disabled" | "degraded",
|
||||
const release = await this.prisma.brandRelease.findFirst({
|
||||
where: { brandId, status: "published" },
|
||||
orderBy: { version: "desc" },
|
||||
select: { version: true, createdAt: true, snapshot: true },
|
||||
});
|
||||
const releaseSnapshot = this.jsonObject(release?.snapshot) as unknown as BrandReleaseSnapshot;
|
||||
if (!release) {
|
||||
return {
|
||||
contractVersion: "1.0",
|
||||
schemaVersion: "1.0.0",
|
||||
publication: { status: "unpublished", version: null },
|
||||
brand: null,
|
||||
template: null,
|
||||
visualTemplate: null,
|
||||
modules: [],
|
||||
offerings: [],
|
||||
inquiryForms: [],
|
||||
capabilities: [],
|
||||
runtimeCapabilities: [],
|
||||
runtimeModules: [],
|
||||
fields: [],
|
||||
trustContent: [],
|
||||
trustPresentation: {},
|
||||
inquiryForm: null,
|
||||
navigation: { tabs: [] },
|
||||
plugins: [],
|
||||
theme: { colors: {} },
|
||||
};
|
||||
}
|
||||
const brandData = this.jsonObject(releaseSnapshot.brand);
|
||||
const modulesData = this.jsonArray(releaseSnapshot.modules).filter(
|
||||
(item) => this.jsonObject(item).isEnabled === true,
|
||||
) as Array<Record<string, unknown>>;
|
||||
const fieldsData = this.jsonArray(releaseSnapshot.fields).filter(
|
||||
(item) => this.jsonObject(item).isVisible === true,
|
||||
);
|
||||
const offeringsData = this.jsonArray(releaseSnapshot.offerings).filter(
|
||||
(item) => this.jsonObject(item).isEnabled === true,
|
||||
) as Array<Record<string, unknown>>;
|
||||
const enabledOfferingIds = new Set(offeringsData.map((item) => item.id));
|
||||
const formsData = this.jsonArray(releaseSnapshot.forms).filter((item) => {
|
||||
const form = this.jsonObject(item);
|
||||
return (
|
||||
form.isEnabled === true &&
|
||||
(form.offeringId === null || enabledOfferingIds.has(form.offeringId))
|
||||
);
|
||||
});
|
||||
const trustData = this.jsonArray(releaseSnapshot.trustContents).filter(
|
||||
(item) => this.jsonObject(item).isPublished === true,
|
||||
) as Array<Record<string, unknown>>;
|
||||
const installationData = this.jsonObject(releaseSnapshot.installation);
|
||||
const templateKeyFromSnapshot =
|
||||
typeof releaseSnapshot.templateKey === "string" ? releaseSnapshot.templateKey : undefined;
|
||||
const templateKey =
|
||||
templateKeyFromSnapshot ??
|
||||
(typeof installationData.templateKey === "string"
|
||||
? installationData.templateKey
|
||||
: undefined) ??
|
||||
(typeof brandData.templateKey === "string" ? brandData.templateKey : "personal");
|
||||
const template = releaseSnapshot.templateDefinition ?? getBrandTemplate(templateKey);
|
||||
const capabilityData = this.jsonArray(releaseSnapshot.capabilities) as Array<
|
||||
Record<string, unknown>
|
||||
>;
|
||||
const runtimeCapabilities = capabilityData.length
|
||||
? capabilityData.map((item) => ({
|
||||
key: this.string(item.capabilityKey),
|
||||
version: this.string(item.capabilityVersion, "1.0.0"),
|
||||
status: this.capabilityStatus(item.status),
|
||||
config:
|
||||
item.config && typeof item.config === "object" && !Array.isArray(item.config)
|
||||
? (item.config as Record<string, unknown>)
|
||||
@@ -90,43 +109,96 @@ export class PublicWorkspaceController {
|
||||
}))
|
||||
: [];
|
||||
const brandConfig =
|
||||
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
|
||||
? (brand.config as Record<string, unknown>)
|
||||
brandData.config && typeof brandData.config === "object" && !Array.isArray(brandData.config)
|
||||
? (brandData.config as Record<string, unknown>)
|
||||
: {};
|
||||
const visualTemplateKey =
|
||||
typeof brandConfig.visualTemplateKey === "string"
|
||||
? brandConfig.visualTemplateKey
|
||||
: "linjing-editorial";
|
||||
: getDefaultVisualTemplateKey(templateKey);
|
||||
const visualTemplate =
|
||||
getVisualTemplate(visualTemplateKey) ?? getVisualTemplate("linjing-editorial");
|
||||
releaseSnapshot.visualTemplateDefinition ?? getVisualTemplate(visualTemplateKey);
|
||||
const visualTemplateManifest =
|
||||
getVisualTemplateManifest(visualTemplateKey) ??
|
||||
getVisualTemplateManifest("linjing-editorial");
|
||||
releaseSnapshot.visualTemplateManifest ?? getVisualTemplateManifest(visualTemplateKey);
|
||||
const runtimeBrand = {
|
||||
...(brand ?? {}),
|
||||
...brandData,
|
||||
...brandConfig,
|
||||
};
|
||||
const runtimeModules: RuntimeModule[] = modules.map((module) => ({
|
||||
key: module.type,
|
||||
type: this.moduleType(module.type),
|
||||
const runtimeModules: RuntimeModule[] = modulesData.map((item) => ({
|
||||
key: this.string(item.type, "custom"),
|
||||
type: this.moduleType(this.string(item.type, "custom")),
|
||||
version: "1.0.0",
|
||||
propsSchema: `module.${module.type}.v1`,
|
||||
dataSource: module.type,
|
||||
config: this.jsonObject(module.config),
|
||||
propsSchema: `module.${this.string(item.type, "custom")}.v1`,
|
||||
dataSource: this.string(item.type, "custom"),
|
||||
config: this.jsonObject(item.config),
|
||||
source:
|
||||
module.source === "custom" ? "custom" : module.source === "template" ? "template" : "core",
|
||||
item.source === "custom" ? "custom" : item.source === "template" ? "template" : "core",
|
||||
schemaVersion: "1.0",
|
||||
}));
|
||||
const runtimeOfferings: RuntimeOffering[] = offerings.map((item) => {
|
||||
const runtimeOfferings: RuntimeOffering[] = offeringsData.map((raw) => {
|
||||
const item = raw;
|
||||
const config = this.jsonObject(item.config);
|
||||
const sections = Array.isArray(config.sections) ? config.sections : [];
|
||||
const details = this.jsonObject(config.details);
|
||||
const media = Array.isArray(config.media)
|
||||
? config.media.flatMap((entry) => {
|
||||
const value = this.jsonObject(entry);
|
||||
return typeof value.url === "string"
|
||||
? [
|
||||
{
|
||||
url: value.url,
|
||||
alt:
|
||||
typeof value.alt === "string"
|
||||
? value.alt
|
||||
: this.string(item.name, "业务提供项"),
|
||||
type: "image" as const,
|
||||
},
|
||||
]
|
||||
: [];
|
||||
})
|
||||
: [];
|
||||
const materialsList = Array.isArray(details.materialsList)
|
||||
? details.materialsList.flatMap((entry) => {
|
||||
const value = this.jsonObject(entry);
|
||||
return typeof value.name === "string" && typeof value.spec === "string"
|
||||
? [{ name: value.name, spec: value.spec }]
|
||||
: [];
|
||||
})
|
||||
: [];
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
summary: item.summary ?? item.description ?? "",
|
||||
enabled: item.isEnabled,
|
||||
id: this.string(item.id),
|
||||
name: this.string(item.name),
|
||||
summary: typeof item.summary === "string" ? item.summary : this.string(item.description),
|
||||
enabled: item.isEnabled !== false,
|
||||
kind: this.offeringKind(this.string(item.kind, "service")),
|
||||
coverUrl: typeof item.coverUrl === "string" ? item.coverUrl : null,
|
||||
priceText: typeof item.priceText === "string" ? item.priceText : null,
|
||||
priceAmount: typeof item.priceAmount === "number" ? item.priceAmount : null,
|
||||
schemaKey: typeof config.schemaKey === "string" ? config.schemaKey : "offering.default",
|
||||
schemaVersion: typeof config.schemaVersion === "string" ? config.schemaVersion : "1.0.0",
|
||||
presentation: {
|
||||
categoryKey: typeof config.categoryKey === "string" ? config.categoryKey : "custom",
|
||||
categoryLabel:
|
||||
typeof config.categoryLabel === "string" ? config.categoryLabel : "业务提供项",
|
||||
heroImage:
|
||||
typeof config.detailHero === "string"
|
||||
? config.detailHero
|
||||
: typeof item.coverUrl === "string"
|
||||
? item.coverUrl
|
||||
: null,
|
||||
keywords: Array.isArray(config.keywords)
|
||||
? config.keywords.filter((keyword): keyword is string => typeof keyword === "string")
|
||||
: [],
|
||||
media,
|
||||
details: {
|
||||
background: typeof details.background === "string" ? details.background : "",
|
||||
designConcept: typeof details.designConcept === "string" ? details.designConcept : "",
|
||||
crafts: Array.isArray(details.crafts)
|
||||
? details.crafts.filter((craft): craft is string => typeof craft === "string")
|
||||
: [],
|
||||
materialsList,
|
||||
},
|
||||
},
|
||||
sections: sections
|
||||
.filter((section): section is Record<string, unknown> =>
|
||||
Boolean(section && typeof section === "object"),
|
||||
@@ -152,11 +224,12 @@ export class PublicWorkspaceController {
|
||||
"checkbox",
|
||||
"textarea",
|
||||
"date",
|
||||
].includes(String(value.type))
|
||||
].includes(this.string(value.type))
|
||||
? (value.type as RuntimeInquiryField["type"])
|
||||
: "text";
|
||||
const options = Array.isArray(value.options)
|
||||
? value.options.flatMap((option) => {
|
||||
if (typeof option === "string") return [{ label: option, value: option }];
|
||||
if (!option || typeof option !== "object") return [];
|
||||
const item = option as Record<string, unknown>;
|
||||
return typeof item.label === "string" && typeof item.value === "string"
|
||||
@@ -174,9 +247,23 @@ export class PublicWorkspaceController {
|
||||
},
|
||||
];
|
||||
});
|
||||
const inquiryForm = inquiryForms[0] ?? null;
|
||||
const inquiryForm = formsData[0] ?? null;
|
||||
const trustPresentation = trustData.reduce<Record<string, unknown>>((result, raw) => {
|
||||
const item = this.jsonObject(raw);
|
||||
const config = this.jsonObject(item.config);
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
if (!(key in result)) result[key] = value;
|
||||
}
|
||||
if (item.type === "profile" && typeof result.bio !== "string") result.bio = item.content;
|
||||
return result;
|
||||
}, {});
|
||||
const runtimeWorkspace: RuntimeWorkspace = {
|
||||
schemaVersion: "1.0.0",
|
||||
publication: {
|
||||
status: "published",
|
||||
version: release.version,
|
||||
publishedAt: release.createdAt.toISOString(),
|
||||
},
|
||||
brand: runtimeBrand,
|
||||
navigation: {
|
||||
tabs: [
|
||||
@@ -187,15 +274,24 @@ export class PublicWorkspaceController {
|
||||
},
|
||||
modules: runtimeModules,
|
||||
offerings: runtimeOfferings,
|
||||
inquiryForms: inquiryForms.map((form) => ({
|
||||
key: form.id,
|
||||
version: String(form.version),
|
||||
purpose: this.formPurpose(form.purpose),
|
||||
target: form.targetType === "offering" ? "offering" : "brand",
|
||||
...(form.offeringId ? { targetId: form.offeringId } : {}),
|
||||
submissionAction: this.submissionAction(form.submissionAction),
|
||||
fields: toInquiryFields(form.fieldsSnapshot),
|
||||
})),
|
||||
inquiryForms: formsData.map((raw) => {
|
||||
const form = this.jsonObject(raw);
|
||||
return {
|
||||
key: this.string(form.id),
|
||||
version:
|
||||
typeof form.version === "number"
|
||||
? String(form.version)
|
||||
: this.string(form.version, "1"),
|
||||
purpose: this.formPurpose(this.string(form.purpose, "inquiry")),
|
||||
target: form.targetType === "offering" ? "offering" : "brand",
|
||||
...(typeof form.offeringId === "string" ? { targetId: form.offeringId } : {}),
|
||||
submissionAction: this.submissionAction(
|
||||
this.string(form.submissionAction, "create-inquiry"),
|
||||
),
|
||||
fields: toInquiryFields(form.fieldsSnapshot as Prisma.JsonValue),
|
||||
};
|
||||
}),
|
||||
trustPresentation,
|
||||
capabilities: runtimeCapabilities,
|
||||
plugins: runtimeCapabilities
|
||||
.filter((capability) => capability.status === "enabled")
|
||||
@@ -215,7 +311,10 @@ export class PublicWorkspaceController {
|
||||
template: template
|
||||
? {
|
||||
key: template.key,
|
||||
version: installation?.templateVersion ?? template.version,
|
||||
version:
|
||||
typeof installationData.templateVersion === "string"
|
||||
? installationData.templateVersion
|
||||
: template.version,
|
||||
name: template.name,
|
||||
}
|
||||
: null,
|
||||
@@ -224,21 +323,19 @@ export class PublicWorkspaceController {
|
||||
key: visualTemplate.key,
|
||||
version: visualTemplate.version,
|
||||
name: visualTemplate.name,
|
||||
supportedIndustryTemplates: visualTemplate.supportedIndustryTemplates,
|
||||
variants: visualTemplate.variants,
|
||||
}
|
||||
: null,
|
||||
visualTemplateManifest,
|
||||
capabilities: runtimeCapabilities,
|
||||
runtimeCapabilities,
|
||||
modules,
|
||||
runtimeModules,
|
||||
fields,
|
||||
offerings,
|
||||
trustContent: trustContents,
|
||||
fields: fieldsData,
|
||||
trustContent: trustData,
|
||||
trustPresentation,
|
||||
inquiryForm,
|
||||
};
|
||||
if (process.env.NODE_ENV !== "test")
|
||||
this.cache.set(brandId, { expiresAt: Date.now() + 30_000, value: response });
|
||||
return response;
|
||||
}
|
||||
private jsonObject(value: unknown): Record<string, unknown> {
|
||||
@@ -246,10 +343,22 @@ export class PublicWorkspaceController {
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
private string(value: unknown, fallback = ""): string {
|
||||
return typeof value === "string" ? value : fallback;
|
||||
}
|
||||
private capabilityStatus(value: unknown): "enabled" | "disabled" | "degraded" {
|
||||
return value === "disabled" || value === "degraded" ? value : "enabled";
|
||||
}
|
||||
private jsonArray(value: unknown): Prisma.JsonValue[] {
|
||||
return Array.isArray(value) ? (value as Prisma.JsonValue[]) : [];
|
||||
}
|
||||
private moduleType(value: string): RuntimeModule["type"] {
|
||||
const aliases: Record<string, RuntimeModule["type"]> = {
|
||||
identity: "brand-hero",
|
||||
trust: "trust-proof",
|
||||
offering: "offering-list",
|
||||
contact: "founder-card",
|
||||
};
|
||||
const known: RuntimeModule["type"][] = [
|
||||
"brand-hero",
|
||||
"founder-card",
|
||||
@@ -264,8 +373,9 @@ export class PublicWorkspaceController {
|
||||
"records",
|
||||
"custom",
|
||||
];
|
||||
const type = aliases[value] ?? value;
|
||||
return (
|
||||
known.includes(value as RuntimeModule["type"]) ? value : "custom"
|
||||
known.includes(type as RuntimeModule["type"]) ? type : "custom"
|
||||
) as RuntimeModule["type"];
|
||||
}
|
||||
private sectionType(value: unknown): RuntimeOffering["sections"][number]["type"] {
|
||||
@@ -274,6 +384,12 @@ export class PublicWorkspaceController {
|
||||
? (value as (typeof known)[number])
|
||||
: "text";
|
||||
}
|
||||
private offeringKind(value: string): RuntimeOffering["kind"] {
|
||||
const known: RuntimeOffering["kind"][] = ["service", "product", "package", "subscription"];
|
||||
return known.includes(value as RuntimeOffering["kind"])
|
||||
? (value as RuntimeOffering["kind"])
|
||||
: "service";
|
||||
}
|
||||
private formPurpose(value: string): RuntimeWorkspace["inquiryForms"][number]["purpose"] {
|
||||
return value === "booking" || value === "quote-request" ? value : "inquiry";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user