From f4a93bba23ef167ed6409f0926e66a31b80b3946 Mon Sep 17 00:00:00 2001 From: que01 Date: Wed, 23 Sep 2026 11:52:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8B=86=E5=88=86=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E8=AE=A2=E5=8D=95=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tenant-architecture-epic/PROGRESS.md | 1 + apps/api/src/main.ts | 57 +---------------- .../customer/customer-booking.controller.ts | 61 +++++++++++++++++++ 3 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 apps/api/src/modules/customer/customer-booking.controller.ts diff --git a/.codex-tasks/tenant-architecture-epic/PROGRESS.md b/.codex-tasks/tenant-architecture-epic/PROGRESS.md index cd82ab1..f93dce4 100644 --- a/.codex-tasks/tenant-architecture-epic/PROGRESS.md +++ b/.codex-tasks/tenant-architecture-epic/PROGRESS.md @@ -13,3 +13,4 @@ - 保持 membership 授权和现有 API 契约不变。 - `CustomerSessionController` 和 `AdminSessionController` 已迁移到 `modules/session/session.controller.ts`。 - `AdminBookingController` 已迁移到 `modules/admin/admin-booking.controller.ts`。 +- `CustomerBookingController` 已迁移到 `modules/customer/customer-booking.controller.ts`。 diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index fb04406..a4a231a 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -23,7 +23,7 @@ import type { NestFastifyApplication } from "@nestjs/platform-fastify"; import { PrismaService } from "./prisma.service.js"; import { WechatAuthService } from "./wechat-auth.js"; import { BookingService } from "./booking.service.js"; -import { CreateBookingDto, CreateInquirySubmissionDto } from "./booking.dto.js"; +import { CreateInquirySubmissionDto } from "./booking.dto.js"; import { CreateTrustContentDto, UpdateBrandDto, UpdateTrustContentDto } from "./admin.dto.js"; import { AdminAuthService } from "./admin-auth.service.js"; import type { FastifyRequest } from "fastify"; @@ -46,6 +46,7 @@ import { AdminSessionController, } from "./modules/session/session.controller.js"; import { AdminBookingController } from "./modules/admin/admin-booking.controller.js"; +import { CustomerBookingController } from "./modules/customer/customer-booking.controller.js"; import type { RuntimeInquiryField, RuntimeOffering, @@ -591,60 +592,6 @@ export class CustomerInquirySubmissionController { } } -@Controller("customer/bookings") -export class CustomerBookingController { - constructor( - private readonly prisma: PrismaService, - private readonly bookings: BookingService, - ) {} - - @Post() - async create(@Req() request: FastifyRequest, @Body() body: CreateBookingDto): Promise { - const identity = this.identity(request); - const customer = await this.prisma.customerUser.findUnique({ - where: { brandId_openid: identity }, - }); - if (!customer) throw new BadRequestException("请先完成微信登录"); - return this.bookings.create(identity.brandId, customer.id, { - ...body, - extraData: body.extraData as Prisma.InputJsonObject, - }); - } - - @Get() - async list(@Req() request: FastifyRequest): Promise { - const identity = this.identity(request); - const customer = await this.prisma.customerUser.findUnique({ - where: { brandId_openid: identity }, - }); - if (!customer) throw new BadRequestException("请先完成微信登录"); - return this.bookings.listForCustomer(identity.brandId, customer.id); - } - - @Post(":id/cancel") - async cancel(@Req() request: FastifyRequest, @Param("id") id: string): Promise { - const identity = this.identity(request); - const customer = await this.prisma.customerUser.findUnique({ - where: { brandId_openid: identity }, - }); - if (!customer) throw new BadRequestException("请先完成微信登录"); - return this.bookings.cancelForCustomer(identity.brandId, customer.id, id); - } - - private identity(request: FastifyRequest): { brandId: string; openid: string } { - const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); - if (!token) throw new BadRequestException("缺少客户登录态"); - try { - const identity = readIdentity(token); - if (identity.role !== "customer") throw new BadRequestException("客户登录态无效"); - return { brandId: identity.brandId, openid: identity.subject }; - } catch (error) { - if (error instanceof BadRequestException) throw error; - throw new BadRequestException("客户登录态无效"); - } - } -} - @Controller("admin/config") export class AdminConfigController { constructor(private readonly prisma: PrismaService) {} diff --git a/apps/api/src/modules/customer/customer-booking.controller.ts b/apps/api/src/modules/customer/customer-booking.controller.ts new file mode 100644 index 0000000..8b26478 --- /dev/null +++ b/apps/api/src/modules/customer/customer-booking.controller.ts @@ -0,0 +1,61 @@ +import { BadRequestException, Body, Controller, Get, Param, Post, Req } from "@nestjs/common"; +import type { FastifyRequest } from "fastify"; +import { Prisma } from "@prisma/client"; +import { BookingService } from "../../booking.service.js"; +import { PrismaService } from "../../prisma.service.js"; +import { CreateBookingDto } from "../../booking.dto.js"; +import { readIdentity } from "../../auth.js"; + +@Controller("customer/bookings") +export class CustomerBookingController { + constructor( + private readonly prisma: PrismaService, + private readonly bookings: BookingService, + ) {} + + @Post() + async create(@Req() request: FastifyRequest, @Body() body: CreateBookingDto): Promise { + const identity = this.identity(request); + const customer = await this.prisma.customerUser.findUnique({ + where: { brandId_openid: identity }, + }); + if (!customer) throw new BadRequestException("请先完成微信登录"); + return this.bookings.create(identity.brandId, customer.id, { + ...body, + extraData: body.extraData as Prisma.InputJsonObject, + }); + } + + @Get() + async list(@Req() request: FastifyRequest): Promise { + const identity = this.identity(request); + const customer = await this.prisma.customerUser.findUnique({ + where: { brandId_openid: identity }, + }); + if (!customer) throw new BadRequestException("请先完成微信登录"); + return this.bookings.listForCustomer(identity.brandId, customer.id); + } + + @Post(":id/cancel") + async cancel(@Req() request: FastifyRequest, @Param("id") id: string): Promise { + const identity = this.identity(request); + const customer = await this.prisma.customerUser.findUnique({ + where: { brandId_openid: identity }, + }); + if (!customer) throw new BadRequestException("请先完成微信登录"); + return this.bookings.cancelForCustomer(identity.brandId, customer.id, id); + } + + private identity(request: FastifyRequest): { brandId: string; openid: string } { + const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); + if (!token) throw new BadRequestException("缺少客户登录态"); + try { + const identity = readIdentity(token); + if (identity.role !== "customer") throw new BadRequestException("客户登录态无效"); + return { brandId: identity.brandId, openid: identity.subject }; + } catch (error) { + if (error instanceof BadRequestException) throw error; + throw new BadRequestException("客户登录态无效"); + } + } +}