From 066f6595e4f7969bbe322fedba842483d99f9eb6 Mon Sep 17 00:00:00 2001 From: que01 Date: Sun, 20 Sep 2026 08:55:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E9=A1=B9=E7=AE=A1=E7=90=86=E4=B8=8E=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/src/admin.dto.ts | 18 ++++++++ apps/api/src/main.ts | 63 ++++++++++++++++++++++++++ apps/api/test/http.integration.test.js | 39 ++++++++++++++++ packages/api-client/src/index.ts | 32 +++++++++++++ 4 files changed, 152 insertions(+) diff --git a/apps/api/src/admin.dto.ts b/apps/api/src/admin.dto.ts index 3174b37..a61c4de 100644 --- a/apps/api/src/admin.dto.ts +++ b/apps/api/src/admin.dto.ts @@ -86,3 +86,21 @@ export class InstallTemplateDto { @IsIn(["personal", "food"]) templateKey!: "personal" | "food"; } + +export class CreateOfferingDto { + @IsIn(["service", "product", "package", "subscription"]) kind!: string; + @IsString() name!: string; + @IsOptional() @IsString() summary?: string; + @IsOptional() @IsString() description?: string; + @IsOptional() @IsString() coverUrl?: string; + @IsOptional() @IsString() priceText?: string; + @IsOptional() @IsInt() @Min(0) priceAmount?: number; + @IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number; + @IsOptional() @IsBoolean() isEnabled?: boolean; + @IsOptional() config?: Record; +} + +export class UpdateOfferingDto extends CreateOfferingDto { + @IsOptional() @IsIn(["service", "product", "package", "subscription"]) kind?: string; + @IsOptional() @IsString() name?: string; +} diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 860fbc7..cf52219 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -33,6 +33,8 @@ import { UpdateServiceDto, UpdateTrustContentDto, InstallTemplateDto, + CreateOfferingDto, + UpdateOfferingDto, } from "./admin.dto.js"; import { AdminAuthService } from "./admin-auth.service.js"; import { AdminLoginDto } from "./admin-auth.dto.js"; @@ -146,12 +148,73 @@ export class PublicWorkspaceController { } } +@Controller("public/offerings") +export class PublicOfferingController { + constructor(private readonly prisma: PrismaService) {} + @Get(":id") + async get(@Param("id") id: string): Promise { + const item = await this.prisma.offering.findFirst({ + where: { + id, + brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001", + isEnabled: true, + }, + }); + if (!item) throw new BadRequestException("业务提供项不存在"); + return item; + } +} + @Controller("admin/workspace") export class AdminWorkspaceController { constructor(private readonly prisma: PrismaService) {} private brandId(): string { return process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001"; } + + @Get("offerings") + listOfferings(@Req() request: FastifyRequest): Promise { + this.requireAdmin(request); + return this.prisma.offering.findMany({ + where: { brandId: this.brandId() }, + orderBy: { sortOrder: "asc" }, + }); + } + + @Post("offerings") + createOffering( + @Req() request: FastifyRequest, + @Body() body: CreateOfferingDto, + ): Promise { + this.requireAdmin(request); + const { config, ...fields } = body; + return this.prisma.offering.create({ + data: { + ...fields, + brandId: this.brandId(), + ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }), + }, + }); + } + + @Patch("offerings/:id") + async updateOffering( + @Req() request: FastifyRequest, + @Param("id") id: string, + @Body() body: UpdateOfferingDto, + ): Promise { + this.requireAdmin(request); + const item = await this.prisma.offering.findFirst({ where: { id, brandId: this.brandId() } }); + if (!item) throw new BadRequestException("业务提供项不存在"); + const { config, ...fields } = body; + return this.prisma.offering.update({ + where: { id }, + data: { + ...fields, + ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }), + }, + }); + } private requireAdmin(request: FastifyRequest): void { const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); if (!token) throw new UnauthorizedException("缺少管理员登录态"); diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 8986104..35a6a46 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -78,6 +78,45 @@ test("admin can install a template and public workspace exposes enabled configur } }); +test("admin can manage offerings and public endpoint hides disabled offerings", async () => { + const login = await request("/admin/session/login", { + method: "POST", + payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD }, + }); + const token = JSON.parse(login.body).token; + const created = await request("/admin/workspace/offerings", { + method: "POST", + headers: { authorization: `Bearer ${token}` }, + payload: { + kind: "product", + name: `测试提供项-${Date.now()}`, + summary: "测试摘要", + isEnabled: true, + }, + }); + assert.equal(created.statusCode, 201); + const id = JSON.parse(created.body).id; + try { + const listed = await request("/admin/workspace/offerings", { + headers: { authorization: `Bearer ${token}` }, + }); + assert.equal(listed.statusCode, 200); + assert.ok(JSON.parse(listed.body).some((item) => item.id === id)); + const publicItem = await request(`/public/offerings/${id}`); + assert.equal(publicItem.statusCode, 200); + const updated = await request(`/admin/workspace/offerings/${id}`, { + method: "PATCH", + headers: { authorization: `Bearer ${token}` }, + payload: { isEnabled: false }, + }); + assert.equal(updated.statusCode, 200); + const hidden = await request(`/public/offerings/${id}`); + assert.equal(hidden.statusCode, 400); + } finally { + await prisma.offering.delete({ where: { id } }); + } +}); + test("admin booking endpoints reject missing or customer credentials", async () => { const missing = await request("/admin/bookings"); assert.equal(missing.statusCode, 401); diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index ea48355..c9169ef 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -108,6 +108,20 @@ export interface BrandTemplateDefinition { serviceCategories: string[]; trustContentTypes: string[]; } +export interface Offering { + id: string; + brandId?: string; + kind: "service" | "product" | "package" | "subscription"; + name: string; + summary?: string | null; + description?: string | null; + coverUrl?: string | null; + priceText?: string | null; + priceAmount?: number | null; + isEnabled: boolean; + sortOrder: number; + config?: Record | null; +} export class ApiClientError extends Error { constructor( @@ -218,6 +232,24 @@ export class ApiClient { getPublicTemplate(key: "personal" | "food"): Promise { return this.request(`/public/templates/${key}`); } + getPublicOffering(id: string): Promise { + return this.request(`/public/offerings/${encodeURIComponent(id)}`); + } + listAdminOfferings(): Promise { + return this.request("/admin/workspace/offerings"); + } + createAdminOffering(body: Omit): Promise { + return this.request("/admin/workspace/offerings", { method: "POST", body }); + } + updateAdminOffering( + id: string, + body: Partial>, + ): Promise { + return this.request(`/admin/workspace/offerings/${encodeURIComponent(id)}`, { + method: "PATCH", + body, + }); + } updateAdminService(id: string, body: ServiceUpdate): Promise { return this.request(`/admin/config/services/${encodeURIComponent(id)}`, { method: "PATCH",