chore: 代码风格统一和项目文档添加

主要变更:

1. 代码风格统一
   - 统一使用双引号替代单引号
   - 保持项目代码风格一致性
   - 涵盖所有模块、配置、实体和服务文件

2. 项目文档
   - 新增 SECURITY_FIXES_SUMMARY.md - 安全修复总结文档
   - 新增 项目问题评估报告.md - 项目问题评估文档

3. 包含修改的文件类别
   - 配置文件:app, database, jwt, redis, cache, performance
   - 实体文件:所有 TypeORM 实体
   - 模块文件:所有业务模块
   - 公共模块:guards, decorators, interceptors, filters, utils
   - 测试文件:单元测试和 E2E 测试

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
UGREEN USER
2026-01-28 13:03:28 +08:00
parent d73a6e28b3
commit 575a29ac8f
103 changed files with 3651 additions and 2710 deletions

View File

@@ -1,27 +1,27 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Test, TestingModule } from "@nestjs/testing";
import { getRepositoryToken } from "@nestjs/typeorm";
import {
NotFoundException,
BadRequestException,
ForbiddenException,
} from '@nestjs/common';
import { AppointmentsService } from './appointments.service';
import { Appointment } from '../../entities/appointment.entity';
import { AppointmentParticipant } from '../../entities/appointment-participant.entity';
import { Group } from '../../entities/group.entity';
import { GroupMember } from '../../entities/group-member.entity';
import { Game } from '../../entities/game.entity';
import { User } from '../../entities/user.entity';
import { CacheService } from '../../common/services/cache.service';
} from "@nestjs/common";
import { AppointmentsService } from "./appointments.service";
import { Appointment } from "../../entities/appointment.entity";
import { AppointmentParticipant } from "../../entities/appointment-participant.entity";
import { Group } from "../../entities/group.entity";
import { GroupMember } from "../../entities/group-member.entity";
import { Game } from "../../entities/game.entity";
import { User } from "../../entities/user.entity";
import { CacheService } from "../../common/services/cache.service";
enum AppointmentStatus {
PENDING = 'pending',
CONFIRMED = 'confirmed',
CANCELLED = 'cancelled',
COMPLETED = 'completed',
PENDING = "pending",
CONFIRMED = "confirmed",
CANCELLED = "cancelled",
COMPLETED = "completed",
}
describe('AppointmentsService', () => {
describe("AppointmentsService", () => {
let service: AppointmentsService;
let mockAppointmentRepository: any;
let mockParticipantRepository: any;
@@ -30,26 +30,26 @@ describe('AppointmentsService', () => {
let mockGameRepository: any;
let mockUserRepository: any;
const mockUser = { id: 'user-1', username: 'testuser' };
const mockGroup = { id: 'group-1', name: '测试小组', isActive: true };
const mockGame = { id: 'game-1', name: '测试游戏' };
const mockUser = { id: "user-1", username: "testuser" };
const mockGroup = { id: "group-1", name: "测试小组", isActive: true };
const mockGame = { id: "game-1", name: "测试游戏" };
const mockMembership = {
id: 'member-1',
userId: 'user-1',
groupId: 'group-1',
role: 'member',
id: "member-1",
userId: "user-1",
groupId: "group-1",
role: "member",
isActive: true,
};
const mockAppointment = {
id: 'appointment-1',
groupId: 'group-1',
gameId: 'game-1',
creatorId: 'user-1',
title: '周末开黑',
description: '描述',
startTime: new Date('2024-01-20T19:00:00Z'),
endTime: new Date('2024-01-20T23:00:00Z'),
id: "appointment-1",
groupId: "group-1",
gameId: "game-1",
creatorId: "user-1",
title: "周末开黑",
description: "描述",
startTime: new Date("2024-01-20T19:00:00Z"),
endTime: new Date("2024-01-20T23:00:00Z"),
maxParticipants: 5,
status: AppointmentStatus.PENDING,
createdAt: new Date(),
@@ -57,10 +57,10 @@ describe('AppointmentsService', () => {
};
const mockParticipant = {
id: 'participant-1',
appointmentId: 'appointment-1',
userId: 'user-1',
status: 'accepted',
id: "participant-1",
appointmentId: "appointment-1",
userId: "user-1",
status: "accepted",
joinedAt: new Date(),
};
@@ -145,8 +145,8 @@ describe('AppointmentsService', () => {
service = module.get<AppointmentsService>(AppointmentsService);
});
describe('create', () => {
it('应该成功创建预约', async () => {
describe("create", () => {
it("应该成功创建预约", async () => {
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
mockGameRepository.findOne.mockResolvedValue(mockGame);
@@ -162,68 +162,68 @@ describe('AppointmentsService', () => {
participants: [mockParticipant],
});
const result = await service.create('user-1', {
groupId: 'group-1',
gameId: 'game-1',
title: '周末开黑',
startTime: new Date('2024-01-20T19:00:00Z'),
const result = await service.create("user-1", {
groupId: "group-1",
gameId: "game-1",
title: "周末开黑",
startTime: new Date("2024-01-20T19:00:00Z"),
maxParticipants: 5,
});
expect(result).toHaveProperty('id');
expect(result.title).toBe('周末开黑');
expect(result).toHaveProperty("id");
expect(result.title).toBe("周末开黑");
expect(mockAppointmentRepository.save).toHaveBeenCalled();
expect(mockParticipantRepository.save).toHaveBeenCalled();
});
it('应该在小组不存在时抛出异常', async () => {
it("应该在小组不存在时抛出异常", async () => {
mockGroupRepository.findOne.mockResolvedValue(null);
await expect(
service.create('user-1', {
groupId: 'group-1',
gameId: 'game-1',
title: '周末开黑',
startTime: new Date('2024-01-20T19:00:00Z'),
service.create("user-1", {
groupId: "group-1",
gameId: "game-1",
title: "周末开黑",
startTime: new Date("2024-01-20T19:00:00Z"),
maxParticipants: 5,
}),
).rejects.toThrow(NotFoundException);
});
it('应该在用户不在小组中时抛出异常', async () => {
it("应该在用户不在小组中时抛出异常", async () => {
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
mockGroupMemberRepository.findOne.mockResolvedValue(null);
await expect(
service.create('user-1', {
groupId: 'group-1',
gameId: 'game-1',
title: '周末开黑',
startTime: new Date('2024-01-20T19:00:00Z'),
service.create("user-1", {
groupId: "group-1",
gameId: "game-1",
title: "周末开黑",
startTime: new Date("2024-01-20T19:00:00Z"),
maxParticipants: 5,
}),
).rejects.toThrow(ForbiddenException);
});
it('应该在游戏不存在时抛出异常', async () => {
it("应该在游戏不存在时抛出异常", async () => {
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
mockGameRepository.findOne.mockResolvedValue(null);
await expect(
service.create('user-1', {
groupId: 'group-1',
gameId: 'game-1',
title: '周末开黑',
startTime: new Date('2024-01-20T19:00:00Z'),
service.create("user-1", {
groupId: "group-1",
gameId: "game-1",
title: "周末开黑",
startTime: new Date("2024-01-20T19:00:00Z"),
maxParticipants: 5,
}),
).rejects.toThrow(NotFoundException);
});
});
describe('findAll', () => {
it('应该成功获取预约列表', async () => {
describe("findAll", () => {
it("应该成功获取预约列表", async () => {
const mockQueryBuilder = {
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
@@ -234,23 +234,25 @@ describe('AppointmentsService', () => {
getManyAndCount: jest.fn().mockResolvedValue([[mockAppointment], 1]),
};
mockAppointmentRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
mockAppointmentRepository.createQueryBuilder.mockReturnValue(
mockQueryBuilder,
);
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
const result = await service.findAll('user-1', {
groupId: 'group-1',
const result = await service.findAll("user-1", {
groupId: "group-1",
page: 1,
limit: 10,
});
expect(result).toHaveProperty('items');
expect(result).toHaveProperty('total');
expect(result).toHaveProperty("items");
expect(result).toHaveProperty("total");
expect(result.items).toHaveLength(1);
});
});
describe('findOne', () => {
it('应该成功获取预约详情', async () => {
describe("findOne", () => {
it("应该成功获取预约详情", async () => {
mockAppointmentRepository.findOne.mockResolvedValue({
...mockAppointment,
group: mockGroup,
@@ -258,77 +260,77 @@ describe('AppointmentsService', () => {
creator: mockUser,
});
const result = await service.findOne('appointment-1');
const result = await service.findOne("appointment-1");
expect(result).toHaveProperty('id');
expect(result.id).toBe('appointment-1');
expect(result).toHaveProperty("id");
expect(result.id).toBe("appointment-1");
});
it('应该在预约不存在时抛出异常', async () => {
it("应该在预约不存在时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(null);
await expect(service.findOne('appointment-1')).rejects.toThrow(
await expect(service.findOne("appointment-1")).rejects.toThrow(
NotFoundException,
);
});
});
describe('update', () => {
it('应该成功更新预约', async () => {
describe("update", () => {
it("应该成功更新预约", async () => {
mockAppointmentRepository.findOne
.mockResolvedValueOnce(mockAppointment)
.mockResolvedValueOnce({
...mockAppointment,
title: '更新后的标题',
title: "更新后的标题",
group: mockGroup,
game: mockGame,
creator: mockUser,
});
mockAppointmentRepository.save.mockResolvedValue({
...mockAppointment,
title: '更新后的标题',
title: "更新后的标题",
});
const result = await service.update('user-1', 'appointment-1', {
title: '更新后的标题',
const result = await service.update("user-1", "appointment-1", {
title: "更新后的标题",
});
expect(result.title).toBe('更新后的标题');
expect(result.title).toBe("更新后的标题");
});
it('应该在非创建者更新时抛出异常', async () => {
it("应该在非创建者更新时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
await expect(
service.update('user-2', 'appointment-1', { title: '新标题' }),
service.update("user-2", "appointment-1", { title: "新标题" }),
).rejects.toThrow(ForbiddenException);
});
});
describe('cancel', () => {
it('应该成功取消预约', async () => {
describe("cancel", () => {
it("应该成功取消预约", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
mockAppointmentRepository.save.mockResolvedValue({
...mockAppointment,
status: AppointmentStatus.CANCELLED,
});
const result = await service.cancel('user-1', 'appointment-1');
const result = await service.cancel("user-1", "appointment-1");
expect(result).toHaveProperty('message');
expect(result).toHaveProperty("message");
});
it('应该在非创建者取消时抛出异常', async () => {
it("应该在非创建者取消时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
await expect(
service.cancel('user-2', 'appointment-1'),
).rejects.toThrow(ForbiddenException);
await expect(service.cancel("user-2", "appointment-1")).rejects.toThrow(
ForbiddenException,
);
});
});
describe('join', () => {
it('应该成功加入预约', async () => {
describe("join", () => {
it("应该成功加入预约", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
mockParticipantRepository.findOne.mockResolvedValue(null);
@@ -336,61 +338,61 @@ describe('AppointmentsService', () => {
mockParticipantRepository.create.mockReturnValue(mockParticipant);
mockParticipantRepository.save.mockResolvedValue(mockParticipant);
const result = await service.join('user-2', 'appointment-1');
const result = await service.join("user-2", "appointment-1");
expect(result).toHaveProperty('message');
expect(result).toHaveProperty("message");
expect(mockParticipantRepository.save).toHaveBeenCalled();
});
it('应该在预约已满时抛出异常', async () => {
it("应该在预约已满时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
mockParticipantRepository.findOne.mockResolvedValue(null);
mockParticipantRepository.count.mockResolvedValue(5);
await expect(
service.join('user-2', 'appointment-1'),
).rejects.toThrow(BadRequestException);
await expect(service.join("user-2", "appointment-1")).rejects.toThrow(
BadRequestException,
);
});
it('应该在已加入时抛出异常', async () => {
it("应该在已加入时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
mockParticipantRepository.findOne.mockResolvedValue(mockParticipant);
await expect(
service.join('user-1', 'appointment-1'),
).rejects.toThrow(BadRequestException);
await expect(service.join("user-1", "appointment-1")).rejects.toThrow(
BadRequestException,
);
});
});
describe('leave', () => {
it('应该成功离开预约', async () => {
describe("leave", () => {
it("应该成功离开预约", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
mockParticipantRepository.findOne.mockResolvedValue(mockParticipant);
mockParticipantRepository.remove.mockResolvedValue(mockParticipant);
const result = await service.leave('user-1', 'appointment-1');
const result = await service.leave("user-1", "appointment-1");
expect(result).toHaveProperty('message');
expect(result).toHaveProperty("message");
expect(mockParticipantRepository.remove).toHaveBeenCalled();
});
it('应该在创建者尝试离开时抛出异常', async () => {
it("应该在创建者尝试离开时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
await expect(
service.leave('user-1', 'appointment-1'),
).rejects.toThrow(BadRequestException);
await expect(service.leave("user-1", "appointment-1")).rejects.toThrow(
BadRequestException,
);
});
it('应该在未加入时抛出异常', async () => {
it("应该在未加入时抛出异常", async () => {
mockAppointmentRepository.findOne.mockResolvedValue(mockAppointment);
mockParticipantRepository.findOne.mockResolvedValue(null);
await expect(
service.leave('user-2', 'appointment-1'),
).rejects.toThrow(BadRequestException);
await expect(service.leave("user-2", "appointment-1")).rejects.toThrow(
BadRequestException,
);
});
});
});