From ca85a583cedae66dc8f74dd68696928075ff9004 Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 21 Sep 2026 16:38:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=85=BC=E5=AE=B9=E6=8E=A5=E5=8F=A3=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/src/main.ts | 49 ++++++++++++++++---------- apps/api/test/http.integration.test.js | 6 ++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 4505b39..f417f9a 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -79,21 +79,16 @@ export class PublicController { @Get("services") async services(): Promise { - return this.prisma.service.findMany({ + const offerings = await this.prisma.offering.findMany({ where: { isEnabled: true }, orderBy: { sortOrder: "asc" }, - select: { - id: true, - name: true, - description: true, - coverUrl: true, - priceText: true, - priceAmount: true, - bookingEnabled: true, - categoryKey: true, - config: true, - }, + include: { services: { where: { isEnabled: true }, take: 1 } }, }); + return offerings.map(({ services, ...offering }) => ({ + ...offering, + bookingEnabled: services[0]?.bookingEnabled ?? false, + categoryKey: services[0]?.categoryKey ?? offering.kind, + })); } } @@ -613,12 +608,30 @@ export class AdminConfigController { const service = await this.prisma.service.findFirst({ where: { id, brandId: this.brandId() } }); if (!service) throw new BadRequestException("服务不存在"); const { config, ...fields } = body; - return this.prisma.service.update({ - where: { id }, - data: { - ...fields, - ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }), - }, + return this.prisma.$transaction(async (tx) => { + const updated = await tx.service.update({ + where: { id }, + data: { + ...fields, + ...(config === undefined ? {} : { config: config as Prisma.InputJsonValue }), + }, + }); + if (service.offeringId) { + await tx.offering.update({ + where: { id: service.offeringId }, + data: { + name: updated.name, + description: updated.description, + coverUrl: updated.coverUrl, + priceText: updated.priceText, + priceAmount: updated.priceAmount, + isEnabled: updated.isEnabled, + sortOrder: updated.sortOrder, + config: updated.config ?? Prisma.JsonNull, + }, + }); + } + return updated; }); } } diff --git a/apps/api/test/http.integration.test.js b/apps/api/test/http.integration.test.js index 7239bb1..cadf2a3 100644 --- a/apps/api/test/http.integration.test.js +++ b/apps/api/test/http.integration.test.js @@ -378,6 +378,12 @@ 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 publicServices = await request("/public/services"); + assert.equal(publicServices.statusCode, 200); + const projectedService = JSON.parse(publicServices.body).find( + (item) => item.id === serviceBefore.offeringId, + ); + assert.equal(projectedService.priceText, "集成测试价格"); const listedServices = await request("/admin/config/services", { headers: { authorization: `Bearer ${adminToken}` },