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 { 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 { 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 { return value && typeof value === "object" && !Array.isArray(value) ? (value as Record) : {}; } private present(item: { config?: unknown; [key: string]: unknown }): unknown { const config = (item.config ?? {}) as Record; return { ...item, sections: config.sections ?? [], media: config.media ?? [], cta: config.cta ?? null, }; } }