refactor: 按领域拆分会话和订单控制器
This commit is contained in:
1 parent
4c8bda57b1
commit
ee7f64c5f3
3 files changed
+107
-106
No files matched your search
@@ -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