diff --git a/apps/api/prisma/migrations/20260921150000_backfill_service_offerings/migration.sql b/apps/api/prisma/migrations/20260921150000_backfill_service_offerings/migration.sql new file mode 100644 index 0000000..fcc9d36 --- /dev/null +++ b/apps/api/prisma/migrations/20260921150000_backfill_service_offerings/migration.sql @@ -0,0 +1,32 @@ +INSERT INTO "Offering" ( + "id", "brandId", "kind", "name", "summary", "description", "coverUrl", + "priceText", "priceAmount", "isEnabled", "sortOrder", "config", "createdAt", "updatedAt" +) +SELECT + gen_random_uuid()::text, + s."brandId", + 'service', + s."name", + NULL, + s."description", + s."coverUrl", + s."priceText", + s."priceAmount", + s."isEnabled", + s."sortOrder", + s."config", + s."createdAt", + s."updatedAt" +FROM "Service" s +WHERE s."offeringId" IS NULL + AND NOT EXISTS ( + SELECT 1 FROM "Offering" o + WHERE o."brandId" = s."brandId" AND o."name" = s."name" + ); + +UPDATE "Service" s +SET "offeringId" = o."id" +FROM "Offering" o +WHERE s."offeringId" IS NULL + AND o."brandId" = s."brandId" + AND o."name" = s."name"; diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 8f93367..4505b39 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -570,16 +570,37 @@ export class AdminConfigController { }); } @Post("services") - createService(@Req() request: FastifyRequest, @Body() body: CreateServiceDto): Promise { + async 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 }), - }, + const brandId = this.brandId(); + return this.prisma.$transaction(async (tx) => { + const offering = await tx.offering.create({ + data: { + brandId, + kind: "service", + name: body.name, + description: body.description, + coverUrl: body.coverUrl ?? null, + priceText: body.priceText ?? null, + priceAmount: body.priceAmount ?? null, + isEnabled: body.isEnabled ?? true, + sortOrder: body.sortOrder ?? 0, + ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }), + }, + }); + return tx.service.create({ + data: { + ...fields, + brandId, + offeringId: offering.id, + categoryKey: body.categoryKey ?? "standard", + ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }), + }, + }); }); } @Patch("services/:id") diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index c2e2806..7239bb1 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -396,8 +396,14 @@ test("admin can read and update brand and service configuration", async () => { }, }); assert.equal(createdService.statusCode, 201); - createdServiceId = JSON.parse(createdService.body).id; - assert.equal(JSON.parse(createdService.body).categoryKey, "consulting"); + const createdServiceBody = JSON.parse(createdService.body); + createdServiceId = createdServiceBody.id; + assert.equal(createdServiceBody.categoryKey, "consulting"); + assert.ok(createdServiceBody.offeringId); + const createdOffering = await prisma.offering.findUnique({ + where: { id: createdServiceBody.offeringId }, + }); + assert.equal(createdOffering?.name, "集成测试通用服务"); } finally { await prisma.brandProfile.update({ where: { id: brandId }, @@ -411,7 +417,12 @@ test("admin can read and update brand and service configuration", async () => { where: { id: serviceBefore.id }, data: { priceText: serviceBefore.priceText, sortOrder: serviceBefore.sortOrder }, }); - if (createdServiceId) await prisma.service.delete({ where: { id: createdServiceId } }); + if (createdServiceId) { + const deletedService = await prisma.service.delete({ where: { id: createdServiceId } }); + if (deletedService.offeringId) { + await prisma.offering.delete({ where: { id: deletedService.offeringId } }); + } + } } }); @@ -521,6 +532,7 @@ test("customer can create and cancel a package booking", async () => { where: { brandId, isEnabled: true, priceAmount: { not: null } }, }); assert.ok(service); + assert.ok(service.offeringId); const created = await request("/customer/bookings", { method: "POST", headers: { authorization: `Bearer ${customerToken}` },