test: 补充订单业务测试和数据库验证
This commit is contained in:
1 parent
7dfa603ba8
commit
50f405932b
13 files changed
+421
-20
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { compare } from "bcryptjs";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
import { signIdentity } from "./auth.js";
|
||||
|
||||
@@ -9,9 +9,7 @@ export class AdminAuthService {
|
||||
|
||||
async login(username: string, password: string): Promise<{ token: string; displayName: string }> {
|
||||
const user = await this.prisma.adminUser.findUnique({ where: { username } });
|
||||
// bcryptjs ships incomplete typings in this dependency version.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
if (!user || !user.isEnabled || !(await compare(password, user.passwordHash))) {
|
||||
if (!user || !user.isEnabled || !(await bcrypt.compare(password, user.passwordHash))) {
|
||||
throw new UnauthorizedException("账号或密码错误");
|
||||
}
|
||||
await this.prisma.adminUser.update({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { BookingStatus, Prisma } from "@prisma/client";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
|
||||
export interface CreateBookingInput {
|
||||
@@ -116,14 +116,35 @@ export class BookingService {
|
||||
return this.prisma.booking.update({ where: { id }, data: { status: "cancelled" } });
|
||||
}
|
||||
|
||||
async listForAdmin(brandId: string) {
|
||||
async listForAdmin(
|
||||
brandId: string,
|
||||
options: { status?: BookingStatus; unreadOnly?: boolean; tagId?: string } = {},
|
||||
) {
|
||||
return this.prisma.booking.findMany({
|
||||
where: { brandId },
|
||||
where: {
|
||||
brandId,
|
||||
...(options.status ? { status: options.status } : {}),
|
||||
...(options.unreadOnly ? { isRead: false } : {}),
|
||||
...(options.tagId ? { tags: { some: { tagId: options.tagId } } } : {}),
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: { items: true, tags: { include: { tag: true } } },
|
||||
});
|
||||
}
|
||||
|
||||
async getForAdmin(brandId: string, id: string) {
|
||||
const booking = await this.prisma.booking.findFirst({
|
||||
where: { id, brandId },
|
||||
include: { items: true, tags: { include: { tag: true } }, service: true },
|
||||
});
|
||||
if (!booking) throw new NotFoundException("订单不存在");
|
||||
return booking;
|
||||
}
|
||||
|
||||
async unreadCountForAdmin(brandId: string) {
|
||||
return { count: await this.prisma.booking.count({ where: { brandId, isRead: false } }) };
|
||||
}
|
||||
|
||||
async updateStatus(
|
||||
brandId: string,
|
||||
id: string,
|
||||
|
||||
@@ -7,7 +7,14 @@ import { randomUUID } from "node:crypto";
|
||||
export class RequestIdMiddleware {
|
||||
use(request: FastifyRequest, reply: FastifyReply, next: () => void): void {
|
||||
const requestId = request.headers["x-request-id"]?.toString() ?? randomUUID();
|
||||
reply.header("x-request-id", requestId);
|
||||
if (typeof reply.header === "function") {
|
||||
reply.header("x-request-id", requestId);
|
||||
} else {
|
||||
(reply as unknown as { setHeader: (name: string, value: string) => void }).setHeader(
|
||||
"x-request-id",
|
||||
requestId,
|
||||
);
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
+47
-2
@@ -7,11 +7,12 @@ import {
|
||||
Module,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
ValidationPipe,
|
||||
} from "@nestjs/common";
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { BookingStatus, Prisma } from "@prisma/client";
|
||||
import { FastifyAdapter } from "@nestjs/platform-fastify";
|
||||
import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
@@ -22,6 +23,9 @@ import { CreateTagDto, UpdateBookingStatusDto, UpdateBookingTagsDto } from "./ad
|
||||
import { AdminAuthService } from "./admin-auth.service.js";
|
||||
import { AdminLoginDto } from "./admin-auth.dto.js";
|
||||
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, signIdentity } from "./auth.js";
|
||||
import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js";
|
||||
|
||||
@@ -104,6 +108,26 @@ export class AdminSessionController {
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
export class CustomerBookingController {
|
||||
constructor(
|
||||
@@ -162,10 +186,31 @@ export class CustomerBookingController {
|
||||
export class AdminBookingController {
|
||||
constructor(private readonly bookings: BookingService) {}
|
||||
@Get()
|
||||
list(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
list(
|
||||
@Req() request: FastifyRequest,
|
||||
@Query() query: AdminBookingQueryDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.listForAdmin(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
query,
|
||||
);
|
||||
}
|
||||
|
||||
@Get("unread-count")
|
||||
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.unreadCountForAdmin(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
);
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.getForAdmin(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
id,
|
||||
);
|
||||
}
|
||||
@Post(":id/status")
|
||||
|
||||
Vendored
+5
@@ -1,4 +1,9 @@
|
||||
declare module "bcryptjs" {
|
||||
const bcrypt: {
|
||||
compare(password: string, hash: string): Promise<boolean>;
|
||||
hash(password: string, saltRounds: number): Promise<string>;
|
||||
};
|
||||
export default bcrypt;
|
||||
export function compare(password: string, hash: string): Promise<boolean>;
|
||||
export function hash(password: string, saltRounds: number): Promise<string>;
|
||||
}
|
||||
Reference in new issue
Block a user