refactor: 拆分模板工作区服务并统一后台访问控制
This commit is contained in:
1 parent
5051839986
commit
b4e3e86dcd
13 files changed
+441
-329
No files matched your search
@@ -1,22 +1,12 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
ForbiddenException,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Post, Query, Req } 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 { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { BookingService } from "../../booking.service.js";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
import { UpdateBookingStatusDto, UpdateBookingTagsDto, CreateTagDto } from "../../admin.dto.js";
|
||||
|
||||
class AdminBookingQueryDto {
|
||||
@@ -35,67 +25,67 @@ class AdminBookingQueryDto {
|
||||
@AdminBrand()
|
||||
@Controller("admin/bookings")
|
||||
export class AdminBookingController {
|
||||
constructor(private readonly bookings: BookingService) {}
|
||||
constructor(
|
||||
private readonly access: AdminAccessService,
|
||||
private readonly bookings: BookingService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
||||
const brandId = this.requireAdmin(request);
|
||||
async list(
|
||||
@Req() request: FastifyRequest,
|
||||
@Query() query: AdminBookingQueryDto,
|
||||
): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.listForAdmin(brandId, query);
|
||||
}
|
||||
|
||||
@Get("unread-count")
|
||||
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.bookings.unreadCountForAdmin(this.requireAdmin(request));
|
||||
async unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.unreadCountForAdmin(brandId);
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
return this.bookings.getForAdmin(this.requireAdmin(request), id);
|
||||
async detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.getForAdmin(brandId, id);
|
||||
}
|
||||
|
||||
@Post(":id/status")
|
||||
update(
|
||||
async 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);
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.updateStatus(brandId, 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);
|
||||
async markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.markRead(brandId, id);
|
||||
}
|
||||
|
||||
@Post(":id/tags")
|
||||
updateTags(
|
||||
async updateTags(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("id") id: string,
|
||||
@Body() body: UpdateBookingTagsDto,
|
||||
): Promise<unknown> {
|
||||
return this.bookings.updateTags(this.requireAdmin(request), id, body.tagIds);
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.updateTags(brandId, id, body.tagIds);
|
||||
}
|
||||
|
||||
@Get("tags")
|
||||
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.bookings.listTags(this.requireAdmin(request));
|
||||
async listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.listTags(brandId);
|
||||
}
|
||||
|
||||
@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;
|
||||
async createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
||||
const { brandId } = await this.access.requireBrand(request);
|
||||
return this.bookings.createTag(brandId, body.name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user