92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
import { AdminBrand } from "../../admin-auth.decorators.js";
|
|
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 { 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;
|
|
}
|
|
|
|
@AdminBrand()
|
|
@Controller("admin/bookings")
|
|
export class AdminBookingController {
|
|
constructor(
|
|
private readonly access: AdminAccessService,
|
|
private readonly bookings: BookingService,
|
|
) {}
|
|
|
|
@Get()
|
|
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")
|
|
async unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
|
const { brandId } = await this.access.requireBrand(request);
|
|
return this.bookings.unreadCountForAdmin(brandId);
|
|
}
|
|
|
|
@Get(":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")
|
|
async update(
|
|
@Req() request: FastifyRequest,
|
|
@Param("id") id: string,
|
|
@Body() body: UpdateBookingStatusDto,
|
|
): Promise<unknown> {
|
|
const { brandId } = await this.access.requireBrand(request);
|
|
return this.bookings.updateStatus(brandId, id, body.status, body.adminNote);
|
|
}
|
|
|
|
@Post(":id/read")
|
|
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")
|
|
async updateTags(
|
|
@Req() request: FastifyRequest,
|
|
@Param("id") id: string,
|
|
@Body() body: UpdateBookingTagsDto,
|
|
): Promise<unknown> {
|
|
const { brandId } = await this.access.requireBrand(request);
|
|
return this.bookings.updateTags(brandId, id, body.tagIds);
|
|
}
|
|
|
|
@Get("tags")
|
|
async listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
|
const { brandId } = await this.access.requireBrand(request);
|
|
return this.bookings.listTags(brandId);
|
|
}
|
|
|
|
@Post("tags")
|
|
async createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
|
const { brandId } = await this.access.requireBrand(request);
|
|
return this.bookings.createTag(brandId, body.name);
|
|
}
|
|
}
|