refactor: 按领域拆分会话和订单控制器
This commit is contained in:
1 parent
4c8bda57b1
commit
ee7f64c5f3
3 files changed
+107
-106
No files matched your search
@@ -12,3 +12,4 @@
|
|||||||
- Header 显示当前认证品牌名称。
|
- Header 显示当前认证品牌名称。
|
||||||
- 保持 membership 授权和现有 API 契约不变。
|
- 保持 membership 授权和现有 API 契约不变。
|
||||||
- `CustomerSessionController` 和 `AdminSessionController` 已迁移到 `modules/session/session.controller.ts`。
|
- `CustomerSessionController` 和 `AdminSessionController` 已迁移到 `modules/session/session.controller.ts`。
|
||||||
|
- `AdminBookingController` 已迁移到 `modules/admin/admin-booking.controller.ts`。
|
||||||
+7
-106
@@ -17,26 +17,16 @@ import {
|
|||||||
ValidationPipe,
|
ValidationPipe,
|
||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
import { NestFactory } from "@nestjs/core";
|
import { NestFactory } from "@nestjs/core";
|
||||||
import { BookingStatus, Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { FastifyAdapter } from "@nestjs/platform-fastify";
|
import { FastifyAdapter } from "@nestjs/platform-fastify";
|
||||||
import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
||||||
import { PrismaService } from "./prisma.service.js";
|
import { PrismaService } from "./prisma.service.js";
|
||||||
import { WechatAuthService } from "./wechat-auth.js";
|
import { WechatAuthService } from "./wechat-auth.js";
|
||||||
import { BookingService } from "./booking.service.js";
|
import { BookingService } from "./booking.service.js";
|
||||||
import { CreateBookingDto, CreateInquirySubmissionDto } from "./booking.dto.js";
|
import { CreateBookingDto, CreateInquirySubmissionDto } from "./booking.dto.js";
|
||||||
import {
|
import { CreateTrustContentDto, UpdateBrandDto, UpdateTrustContentDto } from "./admin.dto.js";
|
||||||
CreateTagDto,
|
|
||||||
CreateTrustContentDto,
|
|
||||||
UpdateBookingStatusDto,
|
|
||||||
UpdateBookingTagsDto,
|
|
||||||
UpdateBrandDto,
|
|
||||||
UpdateTrustContentDto,
|
|
||||||
} from "./admin.dto.js";
|
|
||||||
import { AdminAuthService } from "./admin-auth.service.js";
|
import { AdminAuthService } from "./admin-auth.service.js";
|
||||||
import type { FastifyRequest } from "fastify";
|
import type { FastifyRequest } from "fastify";
|
||||||
import { Transform } from "class-transformer";
|
|
||||||
import type { TransformFnParams } from "class-transformer";
|
|
||||||
import { IsBoolean, IsEnum, IsOptional, IsString } from "class-validator";
|
|
||||||
import { readIdentity } from "./auth.js";
|
import { readIdentity } from "./auth.js";
|
||||||
import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
|
import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
|
||||||
import {
|
import {
|
||||||
@@ -51,7 +41,11 @@ import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
|||||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||||
import { AdminAccessService } from "./admin-access.service.js";
|
import { AdminAccessService } from "./admin-access.service.js";
|
||||||
import { AdminMembershipGuard } from "./admin-membership.guard.js";
|
import { AdminMembershipGuard } from "./admin-membership.guard.js";
|
||||||
import { CustomerSessionController, AdminSessionController } from "./modules/session/session.controller.js";
|
import {
|
||||||
|
CustomerSessionController,
|
||||||
|
AdminSessionController,
|
||||||
|
} from "./modules/session/session.controller.js";
|
||||||
|
import { AdminBookingController } from "./modules/admin/admin-booking.controller.js";
|
||||||
import type {
|
import type {
|
||||||
RuntimeInquiryField,
|
RuntimeInquiryField,
|
||||||
RuntimeOffering,
|
RuntimeOffering,
|
||||||
@@ -597,26 +591,6 @@ export class CustomerInquirySubmissionController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AdminBookingQueryDto {
|
|
||||||
@IsOptional()
|
|
||||||
@IsEnum(BookingStatus)
|
|
||||||
status?: BookingStatus;
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@Transform((params: TransformFnParams): unknown => {
|
|
||||||
const value: unknown = params.value;
|
|
||||||
if (value === "true") return true;
|
|
||||||
if (value === "false") return false;
|
|
||||||
return value;
|
|
||||||
})
|
|
||||||
@IsBoolean()
|
|
||||||
unreadOnly?: boolean;
|
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
tagId?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Controller("customer/bookings")
|
@Controller("customer/bookings")
|
||||||
export class CustomerBookingController {
|
export class CustomerBookingController {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -671,79 +645,6 @@ export class CustomerBookingController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Controller("admin/bookings")
|
|
||||||
export class AdminBookingController {
|
|
||||||
constructor(private readonly bookings: BookingService) {}
|
|
||||||
@Get()
|
|
||||||
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.listForAdmin(brandId, query);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get("unread-count")
|
|
||||||
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.unreadCountForAdmin(brandId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get(":id")
|
|
||||||
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.getForAdmin(brandId, id);
|
|
||||||
}
|
|
||||||
@Post(":id/status")
|
|
||||||
update(
|
|
||||||
@Req() request: FastifyRequest,
|
|
||||||
@Param("id") id: string,
|
|
||||||
@Body()
|
|
||||||
body: UpdateBookingStatusDto,
|
|
||||||
): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.updateStatus(brandId, id, body.status, body.adminNote);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post(":id/read")
|
|
||||||
markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.markRead(brandId, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post(":id/tags")
|
|
||||||
updateTags(
|
|
||||||
@Req() request: FastifyRequest,
|
|
||||||
@Param("id") id: string,
|
|
||||||
@Body() body: UpdateBookingTagsDto,
|
|
||||||
): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.updateTags(brandId, id, body.tagIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get("/tags")
|
|
||||||
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.listTags(brandId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post("/tags")
|
|
||||||
createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
|
||||||
const brandId = this.requireAdmin(request);
|
|
||||||
return this.bookings.createTag(brandId, body.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
private requireAdmin(request: FastifyRequest): string {
|
|
||||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
|
||||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
|
||||||
let identity;
|
|
||||||
try {
|
|
||||||
identity = readIdentity(token);
|
|
||||||
} catch {
|
|
||||||
throw new UnauthorizedException("管理员登录态无效或已过期");
|
|
||||||
}
|
|
||||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
|
||||||
return identity.brandId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Controller("admin/config")
|
@Controller("admin/config")
|
||||||
export class AdminConfigController {
|
export class AdminConfigController {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
ForbiddenException,
|
||||||
|
Get,
|
||||||
|
Param,
|
||||||
|
Post,
|
||||||
|
Query,
|
||||||
|
Req,
|
||||||
|
UnauthorizedException,
|
||||||
|
} from "@nestjs/common";
|
||||||
|
import { Transform } from "class-transformer";
|
||||||
|
import type { TransformFnParams } from "class-transformer";
|
||||||
|
import { BookingStatus } from "@prisma/client";
|
||||||
|
import { IsBoolean, IsEnum, IsOptional, IsString } from "class-validator";
|
||||||
|
import type { FastifyRequest } from "fastify";
|
||||||
|
import { BookingService } from "../../booking.service.js";
|
||||||
|
import { readIdentity } from "../../auth.js";
|
||||||
|
import { UpdateBookingStatusDto, UpdateBookingTagsDto, CreateTagDto } from "../../admin.dto.js";
|
||||||
|
|
||||||
|
class AdminBookingQueryDto {
|
||||||
|
@IsOptional() @IsEnum(BookingStatus) status?: BookingStatus;
|
||||||
|
@IsOptional()
|
||||||
|
@Transform((params: TransformFnParams): unknown => {
|
||||||
|
if (params.value === "true") return true;
|
||||||
|
if (params.value === "false") return false;
|
||||||
|
return params.value;
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
unreadOnly?: boolean;
|
||||||
|
@IsOptional() @IsString() tagId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Controller("admin/bookings")
|
||||||
|
export class AdminBookingController {
|
||||||
|
constructor(private readonly bookings: BookingService) {}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
||||||
|
const brandId = this.requireAdmin(request);
|
||||||
|
return this.bookings.listForAdmin(brandId, query);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("unread-count")
|
||||||
|
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
|
return this.bookings.unreadCountForAdmin(this.requireAdmin(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get(":id")
|
||||||
|
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||||
|
return this.bookings.getForAdmin(this.requireAdmin(request), id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post(":id/status")
|
||||||
|
update(
|
||||||
|
@Req() request: FastifyRequest,
|
||||||
|
@Param("id") id: string,
|
||||||
|
@Body() body: UpdateBookingStatusDto,
|
||||||
|
): Promise<unknown> {
|
||||||
|
return this.bookings.updateStatus(this.requireAdmin(request), id, body.status, body.adminNote);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post(":id/read")
|
||||||
|
markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||||
|
return this.bookings.markRead(this.requireAdmin(request), id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post(":id/tags")
|
||||||
|
updateTags(
|
||||||
|
@Req() request: FastifyRequest,
|
||||||
|
@Param("id") id: string,
|
||||||
|
@Body() body: UpdateBookingTagsDto,
|
||||||
|
): Promise<unknown> {
|
||||||
|
return this.bookings.updateTags(this.requireAdmin(request), id, body.tagIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("tags")
|
||||||
|
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
|
return this.bookings.listTags(this.requireAdmin(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("tags")
|
||||||
|
createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
||||||
|
return this.bookings.createTag(this.requireAdmin(request), body.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private requireAdmin(request: FastifyRequest): string {
|
||||||
|
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||||
|
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||||
|
let identity;
|
||||||
|
try {
|
||||||
|
identity = readIdentity(token);
|
||||||
|
} catch {
|
||||||
|
throw new UnauthorizedException("管理员登录态无效或已过期");
|
||||||
|
}
|
||||||
|
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||||
|
return identity.brandId;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user