From d167b58a0560adb812381641bf6b7b1674874847 Mon Sep 17 00:00:00 2001 From: que01 Date: Sat, 19 Sep 2026 16:50:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=90=8E=E5=8F=B0=E9=85=8D=E7=BD=AE=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migration.sql | 2 ++ apps/api/prisma/schema.prisma | 2 ++ apps/api/src/admin.dto.ts | 15 +++++++++++++++ apps/api/src/main.ts | 11 +++++++++-- apps/api/test/http.integration.test.js | 15 +++++++++++++++ packages/api-client/src/index.ts | 4 +++- 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 apps/api/prisma/migrations/20260919123500_add_service_config/migration.sql diff --git a/apps/api/prisma/migrations/20260919123500_add_service_config/migration.sql b/apps/api/prisma/migrations/20260919123500_add_service_config/migration.sql new file mode 100644 index 0000000..0297c63 --- /dev/null +++ b/apps/api/prisma/migrations/20260919123500_add_service_config/migration.sql @@ -0,0 +1,2 @@ +ALTER TABLE "Service" ADD COLUMN "categoryKey" TEXT NOT NULL DEFAULT 'standard'; +ALTER TABLE "Service" ADD COLUMN "config" JSONB; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 81e8c4c..bc0223f 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -59,6 +59,8 @@ model Service { brand BrandProfile @relation(fields: [brandId], references: [id]) name String description String + categoryKey String @default("standard") + config Json? coverUrl String? priceText String? priceAmount Int? diff --git a/apps/api/src/admin.dto.ts b/apps/api/src/admin.dto.ts index ef660fd..73f8e45 100644 --- a/apps/api/src/admin.dto.ts +++ b/apps/api/src/admin.dto.ts @@ -23,6 +23,21 @@ export class UpdateServiceDto { @IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number; @IsOptional() @IsBoolean() bookingEnabled?: boolean; @IsOptional() @IsBoolean() isEnabled?: boolean; + @IsOptional() @IsString() categoryKey?: string; + @IsOptional() config?: Record; +} + +export class CreateServiceDto { + @IsString() name!: string; + @IsString() description!: string; + @IsOptional() @IsString() categoryKey?: 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() bookingEnabled?: boolean; + @IsOptional() @IsBoolean() isEnabled?: boolean; + @IsOptional() config?: Record; } export class UpdateBookingStatusDto { diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index cfa31d0..3ae5317 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -20,7 +20,7 @@ import { PrismaService } from "./prisma.service.js"; import { WechatAuthService } from "./wechat-auth.js"; import { BookingService } from "./booking.service.js"; import { CreateBookingDto } from "./booking.dto.js"; -import { CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto, UpdateBrandDto, UpdateServiceDto } from "./admin.dto.js"; +import { CreateServiceDto, CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto, UpdateBrandDto, UpdateServiceDto } from "./admin.dto.js"; import { AdminAuthService } from "./admin-auth.service.js"; import { AdminLoginDto } from "./admin-auth.dto.js"; import type { FastifyRequest } from "fastify"; @@ -75,6 +75,8 @@ export class PublicController { priceText: true, priceAmount: true, bookingEnabled: true, + categoryKey: true, + config: true, }, }); } @@ -294,12 +296,17 @@ export class AdminConfigController { getBrand(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); return this.prisma.brandProfile.findUnique({ where: { id: this.brandId() } }); } @Patch("brand") updateBrand(@Req() request: FastifyRequest, @Body() body: UpdateBrandDto): Promise { this.requireAdmin(request); return this.prisma.brandProfile.update({ where: { id: this.brandId() }, data: body }); } + @Get("services") + listServices(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); return this.prisma.service.findMany({ where: { brandId: this.brandId() }, orderBy: { sortOrder: "asc" } }); } + @Post("services") + createService(@Req() request: FastifyRequest, @Body() body: CreateServiceDto): Promise { this.requireAdmin(request); const { config, ...fields } = body; return this.prisma.service.create({ data: { ...fields, brandId: this.brandId(), categoryKey: body.categoryKey ?? "standard", ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }) } }); } @Patch("services/:id") async updateService(@Req() request: FastifyRequest, @Param("id") id: string, @Body() body: UpdateServiceDto): Promise { this.requireAdmin(request); const service = await this.prisma.service.findFirst({ where: { id, brandId: this.brandId() } }); if (!service) throw new BadRequestException("服务不存在"); - return this.prisma.service.update({ where: { id }, data: body }); + const { config, ...fields } = body; + return this.prisma.service.update({ where: { id }, data: { ...fields, ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }) } }); } } diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index e2c1db0..640b4a9 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -98,6 +98,7 @@ test("admin can read and update brand and service configuration", async () => { assert.ok(brandBefore); const serviceBefore = await prisma.service.findFirst({ where: { brandId, isEnabled: true } }); assert.ok(serviceBefore); + let createdServiceId; try { const brand = await request("/admin/config/brand", { headers: { authorization: `Bearer ${adminToken}` } }); @@ -121,9 +122,23 @@ test("admin can read and update brand and service configuration", async () => { assert.equal(updatedService.statusCode, 200); assert.equal(JSON.parse(updatedService.body).priceText, "集成测试价格"); assert.equal(JSON.parse(updatedService.body).sortOrder, 99); + + const listedServices = await request("/admin/config/services", { headers: { authorization: `Bearer ${adminToken}` } }); + assert.equal(listedServices.statusCode, 200); + assert.ok(JSON.parse(listedServices.body).some((item) => item.id === serviceBefore.id)); + + const createdService = await request("/admin/config/services", { + method: "POST", + headers: { authorization: `Bearer ${adminToken}` }, + payload: { name: "集成测试通用服务", description: "通用品牌服务", categoryKey: "consulting", config: { duration: 60 } }, + }); + assert.equal(createdService.statusCode, 201); + createdServiceId = JSON.parse(createdService.body).id; + assert.equal(JSON.parse(createdService.body).categoryKey, "consulting"); } finally { await prisma.brandProfile.update({ where: { id: brandId }, data: { description: brandBefore.description, contactPhone: brandBefore.contactPhone, templateKey: brandBefore.templateKey } }); await prisma.service.update({ where: { id: serviceBefore.id }, data: { priceText: serviceBefore.priceText, sortOrder: serviceBefore.sortOrder } }); + if (createdServiceId) await prisma.service.delete({ where: { id: createdServiceId } }); } }); diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index d6f5368..65df60d 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -23,7 +23,7 @@ export interface BrandProfile { contactWechat: string | null; } export type BrandUpdate = Partial>; -export interface ServiceUpdate { name?: string; description?: string; coverUrl?: string; priceText?: string; priceAmount?: number; sortOrder?: number; bookingEnabled?: boolean; isEnabled?: boolean } +export interface ServiceUpdate { name?: string; description?: string; coverUrl?: string; priceText?: string; priceAmount?: number; sortOrder?: number; bookingEnabled?: boolean; isEnabled?: boolean; categoryKey?: string; config?: Record } export interface BookingItemInput { serviceId: string; quantity: number } export interface CreateBookingInput { @@ -109,6 +109,8 @@ export class ApiClient { getAdminBrand(): Promise { return this.request("/admin/config/brand"); } updateAdminBrand(body: BrandUpdate): Promise { return this.request("/admin/config/brand", { method: "PATCH", body }); } updateAdminService(id: string, body: ServiceUpdate): Promise { return this.request(`/admin/config/services/${encodeURIComponent(id)}`, { method: "PATCH", body }); } + listAdminServices(): Promise { return this.request("/admin/config/services"); } + createAdminService(body: ServiceUpdate & { name: string; description: string }): Promise { return this.request("/admin/config/services", { method: "POST", body }); } private async request(path: string, options: { method?: string; body?: unknown } = {}): Promise { const headers: Record = { accept: "application/json" }; if (options.body !== undefined) headers["content-type"] = "application/json";