统一模板包 visual 契约并补齐完成证据;修复 release 版本一致性、管理员 token 撤销、CORS 与默认品牌配置;收敛依赖安全风险并完成全量回归验证。
59 lines
2.3 KiB
TypeScript
59 lines
2.3 KiB
TypeScript
import { Controller, Get, NotFoundException, Param, Query } from "@nestjs/common";
|
|
import { PublicBrandContextService } from "./public-brand-context.service.js";
|
|
import { BrandReleaseService } from "../../brand-release.service.js";
|
|
|
|
@Controller("public/offerings")
|
|
export class PublicOfferingController {
|
|
constructor(
|
|
private readonly releases: BrandReleaseService,
|
|
private readonly brandContext: PublicBrandContextService,
|
|
) {}
|
|
@Get()
|
|
async list(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
|
|
const brandId = await this.brandContext.resolveId(brandSlug);
|
|
const snapshot = await this.releases.getPublishedSnapshot(brandId);
|
|
const forms = snapshot?.forms ?? [];
|
|
return (snapshot?.offerings ?? [])
|
|
.filter((raw) => this.object(raw).isEnabled === true)
|
|
.map((raw) => this.object(raw))
|
|
.sort((a, b) => Number(a.sortOrder ?? 0) - Number(b.sortOrder ?? 0))
|
|
.map((item) =>
|
|
this.present({
|
|
...item,
|
|
inquiryForms: forms.filter((raw) => {
|
|
const form = this.object(raw);
|
|
return form.offeringId === item.id && form.isEnabled === true;
|
|
}),
|
|
}),
|
|
);
|
|
}
|
|
@Get(":id")
|
|
async get(@Param("id") id: string, @Query("brandSlug") brandSlug?: string): Promise<unknown> {
|
|
const brandId = await this.brandContext.resolveId(brandSlug);
|
|
const snapshot = await this.releases.getPublishedSnapshot(brandId);
|
|
const item = (snapshot?.offerings ?? [])
|
|
.map((raw) => this.object(raw))
|
|
.find((offering) => offering.id === id && offering.isEnabled === true);
|
|
if (!item) throw new NotFoundException("业务提供项不存在");
|
|
const forms = (snapshot?.forms ?? []).filter((raw) => {
|
|
const form = this.object(raw);
|
|
return form.offeringId === id && form.isEnabled === true;
|
|
});
|
|
return this.present({ ...item, inquiryForms: forms });
|
|
}
|
|
private object(value: unknown): Record<string, unknown> {
|
|
return value && typeof value === "object" && !Array.isArray(value)
|
|
? (value as Record<string, unknown>)
|
|
: {};
|
|
}
|
|
private present(item: { config?: unknown; [key: string]: unknown }): unknown {
|
|
const config = (item.config ?? {}) as Record<string, unknown>;
|
|
return {
|
|
...item,
|
|
sections: config.sections ?? [],
|
|
media: config.media ?? [],
|
|
cta: config.cta ?? null,
|
|
};
|
|
}
|
|
}
|