diff --git a/apps/api/prisma/migrations/20260919124500_add_trust_content/migration.sql b/apps/api/prisma/migrations/20260919124500_add_trust_content/migration.sql new file mode 100644 index 0000000..da87f33 --- /dev/null +++ b/apps/api/prisma/migrations/20260919124500_add_trust_content/migration.sql @@ -0,0 +1,2 @@ +CREATE TABLE "TrustContent" ("id" TEXT NOT NULL,"brandId" TEXT NOT NULL,"type" TEXT NOT NULL,"title" TEXT NOT NULL,"summary" TEXT,"content" TEXT NOT NULL,"coverUrl" TEXT,"sortOrder" INTEGER NOT NULL DEFAULT 0,"isPublished" BOOLEAN NOT NULL DEFAULT true,"config" JSONB,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"updatedAt" TIMESTAMP(3) NOT NULL,CONSTRAINT "TrustContent_pkey" PRIMARY KEY ("id"),CONSTRAINT "TrustContent_brandId_fkey" FOREIGN KEY ("brandId") REFERENCES "BrandProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE); +CREATE INDEX "TrustContent_brandId_type_sortOrder_idx" ON "TrustContent"("brandId","type","sortOrder"); diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index bc0223f..4d73d7e 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -51,6 +51,7 @@ model BrandProfile { bookings Booking[] customerUsers CustomerUser[] tags Tag[] + trustContents TrustContent[] } model Service { @@ -75,6 +76,23 @@ model Service { @@index([brandId, isEnabled, sortOrder]) } +model TrustContent { + id String @id @default(uuid()) + brandId String + brand BrandProfile @relation(fields: [brandId], references: [id], onDelete: Cascade) + type String + title String + summary String? + content String + coverUrl String? + sortOrder Int @default(0) + isPublished Boolean @default(true) + config Json? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + @@index([brandId, type, sortOrder]) +} + model CustomerUser { id String @id @default(uuid()) brandId String diff --git a/apps/api/src/admin.dto.ts b/apps/api/src/admin.dto.ts index 73f8e45..4b0467e 100644 --- a/apps/api/src/admin.dto.ts +++ b/apps/api/src/admin.dto.ts @@ -40,6 +40,17 @@ export class CreateServiceDto { @IsOptional() config?: Record; } +export class CreateTrustContentDto { + @IsIn(["profile", "experience", "credential", "portfolio", "process", "testimonial"]) type!: string; + @IsString() title!: string; + @IsOptional() @IsString() summary?: string; + @IsString() content!: string; + @IsOptional() @IsString() coverUrl?: string; + @IsOptional() @IsInt() @Min(0) @Max(9999) sortOrder?: number; + @IsOptional() @IsBoolean() isPublished?: boolean; + @IsOptional() config?: Record; +} + export class UpdateBookingStatusDto { @IsIn(["received", "contacting", "confirmed", "completed", "cancelled"]) status!: "received" | "contacting" | "confirmed" | "completed" | "cancelled"; diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 3ae5317..b13dbb1 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -3,6 +3,7 @@ import { BadRequestException, Body, Controller, + Delete, Get, Module, Param, @@ -20,7 +21,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 { CreateServiceDto, CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto, UpdateBrandDto, UpdateServiceDto } from "./admin.dto.js"; +import { CreateServiceDto, CreateTagDto, CreateTrustContentDto, 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"; @@ -296,6 +297,14 @@ 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("trust-content") + listTrustContent(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); return this.prisma.trustContent.findMany({ where: { brandId: this.brandId() }, orderBy: [{ type: "asc" }, { sortOrder: "asc" }] }); } + @Post("trust-content") + createTrustContent(@Req() request: FastifyRequest, @Body() body: CreateTrustContentDto): Promise { this.requireAdmin(request); const { config, ...fields } = body; return this.prisma.trustContent.create({ data: { ...fields, brandId: this.brandId(), ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }) } }); } + @Patch("trust-content/:id") + async updateTrustContent(@Req() request: FastifyRequest, @Param("id") id: string, @Body() body: Partial): Promise { this.requireAdmin(request); const item = await this.prisma.trustContent.findFirst({ where: { id, brandId: this.brandId() } }); if (!item) throw new BadRequestException("信任内容不存在"); const { config, ...fields } = body; return this.prisma.trustContent.update({ where: { id }, data: { ...fields, ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }) } }); } + @Delete("trust-content/:id") + async deleteTrustContent(@Req() request: FastifyRequest, @Param("id") id: string): Promise { this.requireAdmin(request); const item = await this.prisma.trustContent.findFirst({ where: { id, brandId: this.brandId() } }); if (!item) throw new BadRequestException("信任内容不存在"); await this.prisma.trustContent.delete({ where: { id } }); return { deleted: true }; } @Get("services") listServices(@Req() request: FastifyRequest): Promise { this.requireAdmin(request); return this.prisma.service.findMany({ where: { brandId: this.brandId() }, orderBy: { sortOrder: "asc" } }); } @Post("services") diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 640b4a9..a384ce8 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -142,6 +142,25 @@ test("admin can read and update brand and service configuration", async () => { } }); +test("admin can manage trust content", 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; + assert.equal((await request("/admin/config/trust-content")).statusCode, 400); + const created = await request("/admin/config/trust-content", { method: "POST", headers: { authorization: `Bearer ${token}` }, payload: { type: "profile", title: "关于我", content: "集成测试内容", summary: "测试摘要" } }); + assert.equal(created.statusCode, 201); + const id = JSON.parse(created.body).id; + try { + const list = await request("/admin/config/trust-content", { headers: { authorization: `Bearer ${token}` } }); + assert.equal(list.statusCode, 200); + const updated = await request(`/admin/config/trust-content/${id}`, { method: "PATCH", headers: { authorization: `Bearer ${token}` }, payload: { content: "更新后的内容" } }); + assert.equal(updated.statusCode, 200); + assert.equal(JSON.parse(updated.body).content, "更新后的内容"); + } finally { + const deleted = await request(`/admin/config/trust-content/${id}`, { method: "DELETE", headers: { authorization: `Bearer ${token}` } }); + assert.equal(deleted.statusCode, 200); + } +}); + test("customer booking endpoints require a valid customer session", async () => { const missing = await request("/customer/bookings"); assert.equal(missing.statusCode, 400);