refactor: 拆分工作台业务服务边界
This commit is contained in:
1 parent
d0902d6568
commit
4512d2601d
5 files changed
+228
-164
No files matched your search
@@ -1,7 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
UnauthorizedException,
|
|
||||||
ForbiddenException,
|
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
@@ -14,7 +12,6 @@ import {
|
|||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import type { FastifyRequest } from "fastify";
|
import type { FastifyRequest } from "fastify";
|
||||||
import { PrismaService } from "./prisma.service.js";
|
import { PrismaService } from "./prisma.service.js";
|
||||||
import { readIdentity } from "./auth.js";
|
|
||||||
import {
|
import {
|
||||||
InstallTemplateDto,
|
InstallTemplateDto,
|
||||||
CreateOfferingDto,
|
CreateOfferingDto,
|
||||||
@@ -27,44 +24,30 @@ import {
|
|||||||
UpdateCapabilityInstallationDto,
|
UpdateCapabilityInstallationDto,
|
||||||
} from "./admin.dto.js";
|
} from "./admin.dto.js";
|
||||||
import {
|
import {
|
||||||
|
capabilityRegistry,
|
||||||
capabilityPlugins,
|
capabilityPlugins,
|
||||||
getBrandTemplate,
|
getBrandTemplate,
|
||||||
getTemplateSampleContent,
|
getTemplateSampleContent,
|
||||||
getTemplatePackageManifestForVisualKey,
|
|
||||||
getDefaultVisualTemplateKey,
|
|
||||||
resolveCapabilities,
|
|
||||||
} from "@personal-brand/config";
|
} from "@personal-brand/config";
|
||||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||||
import { validateFieldSnapshot, validateOfferingConfig } from "@personal-brand/validation";
|
import { validateFieldSnapshot } from "@personal-brand/validation";
|
||||||
import { PublicWorkspaceService } from "./modules/public/public-workspace.controller.js";
|
import { AdminOfferingService } from "./modules/workspace/admin-offering.service.js";
|
||||||
|
import { AdminWorkspaceService } from "./modules/workspace/admin-workspace.service.js";
|
||||||
|
import { requireAdminIdentity } from "./modules/workspace/admin-access.identity.js";
|
||||||
|
|
||||||
@Controller("admin/workspace")
|
@Controller("admin/workspace")
|
||||||
class AdminWorkspaceController {
|
class AdminWorkspaceController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly prisma: PrismaService,
|
private readonly prisma: PrismaService,
|
||||||
private readonly templateWorkspace: TemplateWorkspaceService,
|
private readonly templateWorkspace: TemplateWorkspaceService,
|
||||||
private readonly publicWorkspace: PublicWorkspaceService,
|
private readonly offeringService: AdminOfferingService,
|
||||||
|
private readonly workspaceService: AdminWorkspaceService,
|
||||||
) {}
|
) {}
|
||||||
private brandId(): string {
|
|
||||||
return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get("offerings")
|
@Get("offerings")
|
||||||
async listOfferings(@Req() request: FastifyRequest): Promise<unknown> {
|
listOfferings(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const items = await this.prisma.offering.findMany({
|
return this.offeringService.list(brandId);
|
||||||
where: { brandId },
|
|
||||||
orderBy: { sortOrder: "asc" },
|
|
||||||
});
|
|
||||||
return items.map((item) => {
|
|
||||||
const config = (item.config ?? {}) as Record<string, unknown>;
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
sections: config.sections ?? [],
|
|
||||||
media: config.media ?? [],
|
|
||||||
cta: config.cta ?? null,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("offerings")
|
@Post("offerings")
|
||||||
@@ -72,23 +55,8 @@ class AdminWorkspaceController {
|
|||||||
@Req() request: FastifyRequest,
|
@Req() request: FastifyRequest,
|
||||||
@Body() body: CreateOfferingDto,
|
@Body() body: CreateOfferingDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const { config, sections, media, cta, ...fields } = body;
|
return this.offeringService.create(brandId, body);
|
||||||
const configValue = {
|
|
||||||
...(config ?? {}),
|
|
||||||
...(sections === undefined ? {} : { sections }),
|
|
||||||
...(media === undefined ? {} : { media }),
|
|
||||||
...(cta === undefined ? {} : { cta }),
|
|
||||||
};
|
|
||||||
const configIssues = validateOfferingConfig(configValue);
|
|
||||||
if (configIssues.length) throw new BadRequestException("业务提供项配置无效");
|
|
||||||
return this.prisma.offering.create({
|
|
||||||
data: {
|
|
||||||
...fields,
|
|
||||||
brandId,
|
|
||||||
config: configValue as Prisma.InputJsonValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch("offerings/:id")
|
@Patch("offerings/:id")
|
||||||
@@ -97,134 +65,44 @@ class AdminWorkspaceController {
|
|||||||
@Param("id") id: string,
|
@Param("id") id: string,
|
||||||
@Body() body: UpdateOfferingDto,
|
@Body() body: UpdateOfferingDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const item = await this.prisma.offering.findFirst({ where: { id, brandId } });
|
return this.offeringService.update(brandId, id, body);
|
||||||
if (!item) throw new BadRequestException("业务提供项不存在");
|
|
||||||
const { config, sections, media, cta, ...fields } = body;
|
|
||||||
const configValue = {
|
|
||||||
...(config ?? {}),
|
|
||||||
...(sections === undefined ? {} : { sections }),
|
|
||||||
...(media === undefined ? {} : { media }),
|
|
||||||
...(cta === undefined ? {} : { cta }),
|
|
||||||
};
|
|
||||||
if (validateOfferingConfig(configValue).length)
|
|
||||||
throw new BadRequestException("业务提供项配置无效");
|
|
||||||
return this.prisma.offering.update({
|
|
||||||
where: { id },
|
|
||||||
data: {
|
|
||||||
...fields,
|
|
||||||
...(config === undefined &&
|
|
||||||
sections === undefined &&
|
|
||||||
media === undefined &&
|
|
||||||
cta === undefined
|
|
||||||
? {}
|
|
||||||
: {
|
|
||||||
config: configValue as Prisma.InputJsonValue,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("offerings/:id")
|
@Delete("offerings/:id")
|
||||||
async deleteOffering(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
async deleteOffering(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const item = await this.prisma.offering.findFirst({ where: { id, brandId } });
|
return this.offeringService.delete(brandId, id);
|
||||||
if (!item) throw new BadRequestException("业务提供项不存在");
|
|
||||||
const [forms, bookings, items] = await Promise.all([
|
|
||||||
this.prisma.inquiryForm.count({ where: { offeringId: id } }),
|
|
||||||
this.prisma.booking.count({ where: { offeringId: id } }),
|
|
||||||
this.prisma.bookingItem.count({ where: { offeringId: id } }),
|
|
||||||
]);
|
|
||||||
if (forms || bookings || items)
|
|
||||||
throw new BadRequestException("业务提供项已有业务记录,只能禁用不能删除");
|
|
||||||
await this.prisma.offering.delete({ where: { id } });
|
|
||||||
return { deleted: true };
|
|
||||||
}
|
|
||||||
private requireAdmin(request: FastifyRequest): {
|
|
||||||
subject: string;
|
|
||||||
brandId: string;
|
|
||||||
role: "admin";
|
|
||||||
} {
|
|
||||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
|
||||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
|
||||||
let identity;
|
|
||||||
try {
|
|
||||||
identity = readIdentity(token);
|
|
||||||
} catch {
|
|
||||||
throw new UnauthorizedException("管理员登录态无效或已过期");
|
|
||||||
}
|
|
||||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
|
||||||
return identity as { subject: string; brandId: string; role: "admin" };
|
|
||||||
}
|
}
|
||||||
@Get()
|
@Get()
|
||||||
async get(@Req() request: FastifyRequest): Promise<unknown> {
|
get(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const [brand, installations, fields, offerings, forms] = await Promise.all([
|
return this.workspaceService.get(brandId);
|
||||||
this.prisma.brandProfile.findUnique({ where: { id: brandId } }),
|
|
||||||
this.prisma.brandTemplateInstallation.findMany({ where: { brandId } }),
|
|
||||||
this.prisma.brandFieldDefinition.findMany({
|
|
||||||
where: { brandId },
|
|
||||||
orderBy: { sortOrder: "asc" },
|
|
||||||
}),
|
|
||||||
this.prisma.offering.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
|
||||||
this.prisma.inquiryForm.findMany({ where: { brandId }, orderBy: { updatedAt: "desc" } }),
|
|
||||||
]);
|
|
||||||
const config = this.jsonObject(brand?.config);
|
|
||||||
const templateKey =
|
|
||||||
typeof config.visualTemplateKey === "string"
|
|
||||||
? config.visualTemplateKey
|
|
||||||
: getDefaultVisualTemplateKey(brand?.templateKey ?? "personal");
|
|
||||||
const manifest = getTemplatePackageManifestForVisualKey(templateKey);
|
|
||||||
return {
|
|
||||||
brand,
|
|
||||||
installations,
|
|
||||||
fields,
|
|
||||||
offerings,
|
|
||||||
forms,
|
|
||||||
templateContent: manifest
|
|
||||||
? {
|
|
||||||
displayName: manifest.displayName,
|
|
||||||
admin: manifest.admin,
|
|
||||||
navigation: manifest.navigation.map(({ key, label, page }) => ({ key, label, page })),
|
|
||||||
templateFields: (manifest.templateFields ?? []).map((field) => ({
|
|
||||||
...field,
|
|
||||||
binding: field.binding ?? `brand.config.templateData.${field.key}`,
|
|
||||||
})),
|
|
||||||
assetSlots: manifest.assetSlots,
|
|
||||||
trustContentTypes: manifest.trustContentTypes ?? [],
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
@Get("preview")
|
@Get("preview")
|
||||||
async getPreviewWorkspace(@Req() request: FastifyRequest): Promise<unknown> {
|
async getPreviewWorkspace(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const { brandId } = this.requireAdmin(request);
|
const { brandId } = requireAdminIdentity(request);
|
||||||
return this.publicWorkspace.getForBrandId(brandId, true);
|
return this.workspaceService.getPreview(brandId);
|
||||||
}
|
|
||||||
private jsonObject(value: unknown): Record<string, unknown> {
|
|
||||||
return value && typeof value === "object" && !Array.isArray(value)
|
|
||||||
? (value as Record<string, unknown>)
|
|
||||||
: {};
|
|
||||||
}
|
}
|
||||||
@Post("install")
|
@Post("install")
|
||||||
async install(
|
async install(
|
||||||
@Req() request: FastifyRequest,
|
@Req() request: FastifyRequest,
|
||||||
@Body() body: InstallTemplateDto,
|
@Body() body: InstallTemplateDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const { brandId } = requireAdminIdentity(request);
|
||||||
return this.templateWorkspace.install(this.requireAdmin(request).brandId, body.templateKey);
|
return this.templateWorkspace.install(brandId, body.templateKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("capabilities")
|
@Get("capabilities")
|
||||||
async listCapabilities(@Req() request: FastifyRequest): Promise<unknown> {
|
async listCapabilities(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const [templateKey, installations] = await Promise.all([
|
const [templateKey, installations] = await Promise.all([
|
||||||
this.templateWorkspace.getEffectiveTemplateKey(brandId),
|
this.templateWorkspace.getEffectiveTemplateKey(brandId),
|
||||||
this.prisma.capabilityInstallation.findMany({ where: { brandId } }),
|
this.prisma.capabilityInstallation.findMany({ where: { brandId } }),
|
||||||
]);
|
]);
|
||||||
const installed = new Map(installations.map((item) => [item.capabilityKey, item]));
|
const installed = new Map(installations.map((item) => [item.capabilityKey, item]));
|
||||||
const template = getBrandTemplate(templateKey);
|
const template = getBrandTemplate(templateKey);
|
||||||
return (template ? resolveCapabilities(template.capabilities) : []).map((definition) => {
|
return (template?.capabilities ?? []).map((key) => capabilityRegistry[key]).map((definition) => {
|
||||||
const item = installed.get(definition.key);
|
const item = installed.get(definition.key);
|
||||||
return {
|
return {
|
||||||
id: item?.id ?? null,
|
id: item?.id ?? null,
|
||||||
@@ -240,7 +118,7 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Get("sample-content/preview")
|
@Get("sample-content/preview")
|
||||||
async previewSampleContent(@Req() request: FastifyRequest): Promise<unknown> {
|
async previewSampleContent(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(brandId);
|
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(brandId);
|
||||||
const sample = getTemplateSampleContent(templateKey);
|
const sample = getTemplateSampleContent(templateKey);
|
||||||
if (!sample) throw new BadRequestException("模板示例内容不存在");
|
if (!sample) throw new BadRequestException("模板示例内容不存在");
|
||||||
@@ -265,7 +143,7 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Post("sample-content/import")
|
@Post("sample-content/import")
|
||||||
async importSampleContent(@Req() request: FastifyRequest): Promise<unknown> {
|
async importSampleContent(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const identity = this.requireAdmin(request);
|
const identity = requireAdminIdentity(request);
|
||||||
const brandId = identity.brandId;
|
const brandId = identity.brandId;
|
||||||
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(brandId);
|
const templateKey = await this.templateWorkspace.getEffectiveTemplateKey(brandId);
|
||||||
const sample = getTemplateSampleContent(templateKey);
|
const sample = getTemplateSampleContent(templateKey);
|
||||||
@@ -359,7 +237,7 @@ class AdminWorkspaceController {
|
|||||||
@Param("key") key: string,
|
@Param("key") key: string,
|
||||||
@Body() body: UpdateCapabilityInstallationDto,
|
@Body() body: UpdateCapabilityInstallationDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const identity = this.requireAdmin(request);
|
const identity = requireAdminIdentity(request);
|
||||||
const brandId = identity.brandId;
|
const brandId = identity.brandId;
|
||||||
const template = getBrandTemplate(
|
const template = getBrandTemplate(
|
||||||
await this.templateWorkspace.getEffectiveTemplateKey(brandId),
|
await this.templateWorkspace.getEffectiveTemplateKey(brandId),
|
||||||
@@ -402,20 +280,20 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Get("template/preview")
|
@Get("template/preview")
|
||||||
async previewTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
async previewTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const { brandId } = requireAdminIdentity(request);
|
||||||
return this.templateWorkspace.preview(this.requireAdmin(request).brandId);
|
return this.templateWorkspace.preview(brandId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("template/upgrade")
|
@Post("template/upgrade")
|
||||||
async upgradeTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
async upgradeTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const { brandId } = requireAdminIdentity(request);
|
||||||
return this.templateWorkspace.upgrade(this.requireAdmin(request).brandId);
|
return this.templateWorkspace.upgrade(brandId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("template/rollback")
|
@Post("template/rollback")
|
||||||
async rollbackTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
async rollbackTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const { brandId } = requireAdminIdentity(request);
|
||||||
return this.templateWorkspace.rollback(this.requireAdmin(request).brandId);
|
return this.templateWorkspace.rollback(brandId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("fields")
|
@Post("fields")
|
||||||
@@ -423,7 +301,7 @@ class AdminWorkspaceController {
|
|||||||
@Req() request: FastifyRequest,
|
@Req() request: FastifyRequest,
|
||||||
@Body() body: CreateFieldDefinitionDto,
|
@Body() body: CreateFieldDefinitionDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
return this.prisma.brandFieldDefinition.create({
|
return this.prisma.brandFieldDefinition.create({
|
||||||
data: {
|
data: {
|
||||||
...body,
|
...body,
|
||||||
@@ -441,7 +319,7 @@ class AdminWorkspaceController {
|
|||||||
@Param("id") id: string,
|
@Param("id") id: string,
|
||||||
@Body() body: UpdateFieldDefinitionDto,
|
@Body() body: UpdateFieldDefinitionDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const item = await this.prisma.brandFieldDefinition.findFirst({
|
const item = await this.prisma.brandFieldDefinition.findFirst({
|
||||||
where: { id, brandId },
|
where: { id, brandId },
|
||||||
});
|
});
|
||||||
@@ -459,7 +337,7 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Delete("fields/:id")
|
@Delete("fields/:id")
|
||||||
async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const item = await this.prisma.brandFieldDefinition.findFirst({
|
const item = await this.prisma.brandFieldDefinition.findFirst({
|
||||||
where: { id, brandId, source: "custom" },
|
where: { id, brandId, source: "custom" },
|
||||||
});
|
});
|
||||||
@@ -470,7 +348,7 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Get("forms")
|
@Get("forms")
|
||||||
listForms(@Req() request: FastifyRequest): Promise<unknown> {
|
listForms(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
return this.prisma.inquiryForm.findMany({
|
return this.prisma.inquiryForm.findMany({
|
||||||
where: { brandId },
|
where: { brandId },
|
||||||
orderBy: { updatedAt: "desc" },
|
orderBy: { updatedAt: "desc" },
|
||||||
@@ -479,7 +357,7 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Get("submissions")
|
@Get("submissions")
|
||||||
listSubmissions(@Req() request: FastifyRequest): Promise<unknown> {
|
listSubmissions(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
return this.prisma.inquirySubmission.findMany({
|
return this.prisma.inquirySubmission.findMany({
|
||||||
where: { brandId },
|
where: { brandId },
|
||||||
include: {
|
include: {
|
||||||
@@ -496,7 +374,7 @@ class AdminWorkspaceController {
|
|||||||
@Param("id") id: string,
|
@Param("id") id: string,
|
||||||
@Body() body: UpdateInquirySubmissionDto,
|
@Body() body: UpdateInquirySubmissionDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const item = await this.prisma.inquirySubmission.findFirst({
|
const item = await this.prisma.inquirySubmission.findFirst({
|
||||||
where: { id, brandId },
|
where: { id, brandId },
|
||||||
});
|
});
|
||||||
@@ -525,7 +403,7 @@ class AdminWorkspaceController {
|
|||||||
|
|
||||||
@Post("forms")
|
@Post("forms")
|
||||||
createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise<unknown> {
|
createForm(@Req() request: FastifyRequest, @Body() body: CreateInquiryFormDto): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
if (validateFieldSnapshot(body.fieldsSnapshot).length)
|
if (validateFieldSnapshot(body.fieldsSnapshot).length)
|
||||||
throw new BadRequestException("咨询表单字段定义无效");
|
throw new BadRequestException("咨询表单字段定义无效");
|
||||||
return this.prisma.inquiryForm.create({
|
return this.prisma.inquiryForm.create({
|
||||||
@@ -544,7 +422,7 @@ class AdminWorkspaceController {
|
|||||||
@Param("id") id: string,
|
@Param("id") id: string,
|
||||||
@Body() body: UpdateInquiryFormDto,
|
@Body() body: UpdateInquiryFormDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const brandId = this.requireAdmin(request).brandId;
|
const brandId = requireAdminIdentity(request).brandId;
|
||||||
const item = await this.prisma.inquiryForm.findFirst({
|
const item = await this.prisma.inquiryForm.findFirst({
|
||||||
where: { id, brandId },
|
where: { id, brandId },
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import {
|
|||||||
PublicWorkspaceService,
|
PublicWorkspaceService,
|
||||||
} from "./modules/public/public-workspace.controller.js";
|
} from "./modules/public/public-workspace.controller.js";
|
||||||
import { PublicBrandContextService } from "./modules/public/public-brand-context.service.js";
|
import { PublicBrandContextService } from "./modules/public/public-brand-context.service.js";
|
||||||
|
import { AdminOfferingService } from "./modules/workspace/admin-offering.service.js";
|
||||||
|
import { AdminWorkspaceService } from "./modules/workspace/admin-workspace.service.js";
|
||||||
import { AdminConfigController } from "./modules/admin/admin-config.controller.js";
|
import { AdminConfigController } from "./modules/admin/admin-config.controller.js";
|
||||||
import { AdminUploadController } from "./modules/admin/admin-upload.controller.js";
|
import { AdminUploadController } from "./modules/admin/admin-upload.controller.js";
|
||||||
import { AdminLocalUploadController } from "./modules/admin/admin-local-upload.controller.js";
|
import { AdminLocalUploadController } from "./modules/admin/admin-local-upload.controller.js";
|
||||||
@@ -78,6 +80,8 @@ export class HealthController {
|
|||||||
BrandReleaseApiService,
|
BrandReleaseApiService,
|
||||||
PublicBrandContextService,
|
PublicBrandContextService,
|
||||||
PublicWorkspaceService,
|
PublicWorkspaceService,
|
||||||
|
AdminOfferingService,
|
||||||
|
AdminWorkspaceService,
|
||||||
],
|
],
|
||||||
exports: [PrismaService, WechatAuthService],
|
exports: [PrismaService, WechatAuthService],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { ForbiddenException, UnauthorizedException } from "@nestjs/common";
|
||||||
|
import type { FastifyRequest } from "fastify";
|
||||||
|
import { readIdentity } from "../../auth.js";
|
||||||
|
|
||||||
|
export type AdminIdentity = {
|
||||||
|
subject: string;
|
||||||
|
brandId: string;
|
||||||
|
role: "admin";
|
||||||
|
};
|
||||||
|
|
||||||
|
export function requireAdminIdentity(request: FastifyRequest): AdminIdentity {
|
||||||
|
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||||
|
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||||
|
|
||||||
|
let identity;
|
||||||
|
try {
|
||||||
|
identity = readIdentity(token);
|
||||||
|
} catch {
|
||||||
|
throw new UnauthorizedException("管理员登录态无效或已过期");
|
||||||
|
}
|
||||||
|
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||||
|
return identity as AdminIdentity;
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { PrismaService } from "../../prisma.service.js";
|
||||||
|
import type { CreateOfferingDto, UpdateOfferingDto } from "../../admin.dto.js";
|
||||||
|
import { validateOfferingConfig } from "@personal-brand/validation";
|
||||||
|
|
||||||
|
type OfferingConfigInput = {
|
||||||
|
config?: CreateOfferingDto["config"] | undefined;
|
||||||
|
sections?: CreateOfferingDto["sections"] | undefined;
|
||||||
|
media?: CreateOfferingDto["media"] | undefined;
|
||||||
|
cta?: CreateOfferingDto["cta"] | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
function buildOfferingConfig(body: OfferingConfigInput): Record<string, unknown> {
|
||||||
|
const { config, sections, media, cta } = body;
|
||||||
|
return {
|
||||||
|
...(config ?? {}),
|
||||||
|
...(sections === undefined ? {} : { sections }),
|
||||||
|
...(media === undefined ? {} : { media }),
|
||||||
|
...(cta === undefined ? {} : { cta }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class AdminOfferingService {
|
||||||
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
|
async list(brandId: string): Promise<unknown> {
|
||||||
|
const items = await this.prisma.offering.findMany({
|
||||||
|
where: { brandId },
|
||||||
|
orderBy: { sortOrder: "asc" },
|
||||||
|
});
|
||||||
|
return items.map((item) => {
|
||||||
|
const config = (item.config ?? {}) as Record<string, unknown>;
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
sections: config.sections ?? [],
|
||||||
|
media: config.media ?? [],
|
||||||
|
cta: config.cta ?? null,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(brandId: string, body: CreateOfferingDto): Promise<unknown> {
|
||||||
|
const { config, sections, media, cta, ...fields } = body;
|
||||||
|
const configValue = buildOfferingConfig({ config, sections, media, cta });
|
||||||
|
if (validateOfferingConfig(configValue).length) {
|
||||||
|
throw new BadRequestException("业务提供项配置无效");
|
||||||
|
}
|
||||||
|
return this.prisma.offering.create({
|
||||||
|
data: {
|
||||||
|
...fields,
|
||||||
|
brandId,
|
||||||
|
config: configValue as Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(brandId: string, id: string, body: UpdateOfferingDto): Promise<unknown> {
|
||||||
|
const item = await this.prisma.offering.findFirst({ where: { id, brandId } });
|
||||||
|
if (!item) throw new BadRequestException("业务提供项不存在");
|
||||||
|
|
||||||
|
const { config, sections, media, cta, ...fields } = body;
|
||||||
|
const configValue = buildOfferingConfig({ config, sections, media, cta });
|
||||||
|
if (validateOfferingConfig(configValue).length) {
|
||||||
|
throw new BadRequestException("业务提供项配置无效");
|
||||||
|
}
|
||||||
|
const hasConfigInput =
|
||||||
|
config !== undefined || sections !== undefined || media !== undefined || cta !== undefined;
|
||||||
|
return this.prisma.offering.update({
|
||||||
|
where: { id },
|
||||||
|
data: {
|
||||||
|
...fields,
|
||||||
|
...(hasConfigInput ? { config: configValue as Prisma.InputJsonValue } : {}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(brandId: string, id: string): Promise<unknown> {
|
||||||
|
const item = await this.prisma.offering.findFirst({ where: { id, brandId } });
|
||||||
|
if (!item) throw new BadRequestException("业务提供项不存在");
|
||||||
|
|
||||||
|
const [forms, bookings, items] = await Promise.all([
|
||||||
|
this.prisma.inquiryForm.count({ where: { offeringId: id } }),
|
||||||
|
this.prisma.booking.count({ where: { offeringId: id } }),
|
||||||
|
this.prisma.bookingItem.count({ where: { offeringId: id } }),
|
||||||
|
]);
|
||||||
|
if (forms || bookings || items) {
|
||||||
|
throw new BadRequestException("业务提供项已有业务记录,只能禁用不能删除");
|
||||||
|
}
|
||||||
|
await this.prisma.offering.delete({ where: { id } });
|
||||||
|
return { deleted: true };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { Injectable } from "@nestjs/common";
|
||||||
|
import { PrismaService } from "../../prisma.service.js";
|
||||||
|
import {
|
||||||
|
getDefaultVisualTemplateKey,
|
||||||
|
getTemplatePackageManifestForVisualKey,
|
||||||
|
} from "@personal-brand/config";
|
||||||
|
import { PublicWorkspaceService } from "../public/public-workspace.controller.js";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class AdminWorkspaceService {
|
||||||
|
constructor(
|
||||||
|
private readonly prisma: PrismaService,
|
||||||
|
private readonly publicWorkspace: PublicWorkspaceService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async get(brandId: string): Promise<unknown> {
|
||||||
|
const [brand, installations, fields, offerings, forms] = await Promise.all([
|
||||||
|
this.prisma.brandProfile.findUnique({ where: { id: brandId } }),
|
||||||
|
this.prisma.brandTemplateInstallation.findMany({ where: { brandId } }),
|
||||||
|
this.prisma.brandFieldDefinition.findMany({
|
||||||
|
where: { brandId },
|
||||||
|
orderBy: { sortOrder: "asc" },
|
||||||
|
}),
|
||||||
|
this.prisma.offering.findMany({ where: { brandId }, orderBy: { sortOrder: "asc" } }),
|
||||||
|
this.prisma.inquiryForm.findMany({ where: { brandId }, orderBy: { updatedAt: "desc" } }),
|
||||||
|
]);
|
||||||
|
const config = this.jsonObject(brand?.config);
|
||||||
|
const templateKey =
|
||||||
|
typeof config.visualTemplateKey === "string"
|
||||||
|
? config.visualTemplateKey
|
||||||
|
: getDefaultVisualTemplateKey(brand?.templateKey ?? "personal");
|
||||||
|
const manifest = getTemplatePackageManifestForVisualKey(templateKey);
|
||||||
|
|
||||||
|
return {
|
||||||
|
brand,
|
||||||
|
installations,
|
||||||
|
fields,
|
||||||
|
offerings,
|
||||||
|
forms,
|
||||||
|
templateContent: manifest
|
||||||
|
? {
|
||||||
|
displayName: manifest.displayName,
|
||||||
|
admin: manifest.admin,
|
||||||
|
navigation: manifest.navigation.map(({ key, label, page }) => ({ key, label, page })),
|
||||||
|
templateFields: (manifest.templateFields ?? []).map((field) => ({
|
||||||
|
...field,
|
||||||
|
binding: field.binding ?? `brand.config.templateData.${field.key}`,
|
||||||
|
})),
|
||||||
|
assetSlots: manifest.assetSlots,
|
||||||
|
trustContentTypes: manifest.trustContentTypes ?? [],
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
getPreview(brandId: string): Promise<unknown> {
|
||||||
|
return this.publicWorkspace.getForBrandId(brandId, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private jsonObject(value: unknown): Record<string, unknown> {
|
||||||
|
return value && typeof value === "object" && !Array.isArray(value)
|
||||||
|
? (value as Record<string, unknown>)
|
||||||
|
: {};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user