feat: 收口多品牌与模板构建基础
This commit is contained in:
1 parent
0425a1c672
commit
77224ea51f
317 files changed
+11290
-1158
No files matched your search
@@ -0,0 +1,129 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import {
|
||||
getBrandTemplate,
|
||||
getVisualTemplate,
|
||||
getVisualTemplateManifest,
|
||||
getDefaultVisualTemplateKey,
|
||||
} from "@personal-brand/config";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
|
||||
export interface BrandReleaseSnapshot {
|
||||
schemaVersion: "1.0.0";
|
||||
generatedAt: string;
|
||||
templateKey: string;
|
||||
templateDefinition?: ReturnType<typeof getBrandTemplate> | null;
|
||||
visualTemplateDefinition?: ReturnType<typeof getVisualTemplate> | null;
|
||||
visualTemplateManifest?: ReturnType<typeof getVisualTemplateManifest> | null;
|
||||
brand: Record<string, unknown> | null;
|
||||
installation: Record<string, unknown> | null;
|
||||
modules: unknown[];
|
||||
fields: unknown[];
|
||||
offerings: unknown[];
|
||||
forms: unknown[];
|
||||
trustContents: unknown[];
|
||||
capabilities: unknown[];
|
||||
}
|
||||
|
||||
export async function buildBrandReleaseSnapshot(
|
||||
database: Prisma.TransactionClient,
|
||||
brandId: string,
|
||||
): Promise<BrandReleaseSnapshot> {
|
||||
const [brand, installation, modules, fields, offerings, forms, trustContents, capabilities] =
|
||||
await Promise.all([
|
||||
database.brandProfile.findUnique({ where: { id: brandId } }),
|
||||
database.brandTemplateInstallation.findFirst({
|
||||
where: { brandId },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
}),
|
||||
database.brandModule.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
||||
database.brandFieldDefinition.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
||||
database.offering.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
||||
database.inquiryForm.findMany({ where: { brandId }, orderBy: { updatedAt: "desc" } }),
|
||||
database.trustContent.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
||||
database.capabilityInstallation.findMany({ where: { brandId } }),
|
||||
]);
|
||||
const templateKey = installation?.templateKey ?? brand?.templateKey ?? "personal";
|
||||
const config =
|
||||
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
|
||||
? (brand.config as Record<string, unknown>)
|
||||
: {};
|
||||
const visualKey =
|
||||
typeof config.visualTemplateKey === "string"
|
||||
? config.visualTemplateKey
|
||||
: getDefaultVisualTemplateKey(brand?.templateKey ?? "personal");
|
||||
const jsonObject = (value: object): Record<string, unknown> =>
|
||||
JSON.parse(JSON.stringify(value)) as Record<string, unknown>;
|
||||
return {
|
||||
schemaVersion: "1.0.0",
|
||||
generatedAt: new Date().toISOString(),
|
||||
templateKey,
|
||||
templateDefinition: getBrandTemplate(templateKey) ?? null,
|
||||
visualTemplateDefinition: getVisualTemplate(visualKey) ?? null,
|
||||
visualTemplateManifest: getVisualTemplateManifest(visualKey) ?? null,
|
||||
brand: brand ? jsonObject(brand) : null,
|
||||
installation: installation ? jsonObject(installation) : null,
|
||||
modules: modules.map((item) => jsonObject(item)),
|
||||
fields: fields.map((item) => jsonObject(item)),
|
||||
offerings: offerings.map((item) => jsonObject(item)),
|
||||
forms: forms.map((item) => jsonObject(item)),
|
||||
trustContents: trustContents.map((item) => jsonObject(item)),
|
||||
capabilities: capabilities.map((item) => jsonObject(item)),
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BrandReleaseService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getPublishedSnapshot(brandId: string): Promise<BrandReleaseSnapshot | null> {
|
||||
const release = await this.prisma.brandRelease.findFirst({
|
||||
where: { brandId, status: "published" },
|
||||
orderBy: { version: "desc" },
|
||||
select: { snapshot: true },
|
||||
});
|
||||
return release ? (release.snapshot as unknown as BrandReleaseSnapshot) : null;
|
||||
}
|
||||
|
||||
publishedOfferingIds(snapshot: BrandReleaseSnapshot): Set<string> {
|
||||
return new Set(
|
||||
snapshot.offerings.flatMap((raw) => {
|
||||
const item = this.record(raw);
|
||||
return item.isEnabled === true && typeof item.id === "string" ? [item.id] : [];
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
publishedForm(snapshot: BrandReleaseSnapshot, formId: string): Record<string, unknown> | null {
|
||||
const offeringIds = this.publishedOfferingIds(snapshot);
|
||||
return (
|
||||
snapshot.forms
|
||||
.map((raw) => this.record(raw))
|
||||
.find((form) => {
|
||||
return (
|
||||
form.id === formId &&
|
||||
form.isEnabled === true &&
|
||||
(form.offeringId === null ||
|
||||
(typeof form.offeringId === "string" && offeringIds.has(form.offeringId)))
|
||||
);
|
||||
}) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
private record(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
async buildSnapshot(
|
||||
brandId: string,
|
||||
database: Prisma.TransactionClient = this.prisma,
|
||||
): Promise<BrandReleaseSnapshot> {
|
||||
return buildBrandReleaseSnapshot(database, brandId);
|
||||
}
|
||||
|
||||
toPrismaJson(snapshot: BrandReleaseSnapshot): Prisma.InputJsonValue {
|
||||
return snapshot as unknown as Prisma.InputJsonValue;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user