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

@@ -3,20 +3,23 @@ import {
NotFoundException,
BadRequestException,
ForbiddenException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Group } from '../../entities/group.entity';
import { GroupMember } from '../../entities/group-member.entity';
import { User } from '../../entities/user.entity';
import { CreateGroupDto, UpdateGroupDto, JoinGroupDto } from './dto/group.dto';
import { GroupMemberRole } from '../../common/enums';
import { ErrorCode, ErrorMessage } from '../../common/interfaces/response.interface';
import { CacheService } from '../../common/services/cache.service';
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Group } from "../../entities/group.entity";
import { GroupMember } from "../../entities/group-member.entity";
import { User } from "../../entities/user.entity";
import { CreateGroupDto, UpdateGroupDto, JoinGroupDto } from "./dto/group.dto";
import { GroupMemberRole } from "../../common/enums";
import {
ErrorCode,
ErrorMessage,
} from "../../common/interfaces/response.interface";
import { CacheService } from "../../common/services/cache.service";
@Injectable()
export class GroupsService {
private readonly CACHE_PREFIX = 'group';
private readonly CACHE_PREFIX = "group";
private readonly CACHE_TTL = 300; // 5 minutes
constructor(
@@ -50,14 +53,14 @@ export class GroupsService {
if (!user.isMember && ownedGroupsCount >= 1) {
throw new BadRequestException({
code: ErrorCode.GROUP_LIMIT_EXCEEDED,
message: '非会员最多只能创建1个小组',
message: "非会员最多只能创建1个小组",
});
}
if (user.isMember && ownedGroupsCount >= 10) {
throw new BadRequestException({
code: ErrorCode.GROUP_LIMIT_EXCEEDED,
message: '会员最多只能创建10个小组',
message: "会员最多只能创建10个小组",
});
}
@@ -66,7 +69,7 @@ export class GroupsService {
if (!user.isMember) {
throw new ForbiddenException({
code: ErrorCode.NO_PERMISSION,
message: '非会员不能创建子组',
message: "非会员不能创建子组",
});
}
@@ -77,7 +80,7 @@ export class GroupsService {
if (!parentGroup) {
throw new NotFoundException({
code: ErrorCode.GROUP_NOT_FOUND,
message: '父组不存在',
message: "父组不存在",
});
}
}
@@ -117,7 +120,9 @@ export class GroupsService {
});
}
const group = await this.groupRepository.findOne({ where: { id: groupId } });
const group = await this.groupRepository.findOne({
where: { id: groupId },
});
if (!group) {
throw new NotFoundException({
code: ErrorCode.GROUP_NOT_FOUND,
@@ -154,10 +159,10 @@ export class GroupsService {
.createQueryBuilder()
.update(Group)
.set({
currentMembers: () => 'currentMembers + 1',
currentMembers: () => "currentMembers + 1",
})
.where('id = :id', { id: groupId })
.andWhere('currentMembers < maxMembers')
.where("id = :id", { id: groupId })
.andWhere("currentMembers < maxMembers")
.execute();
// 如果影响的行数为0说明小组已满
@@ -200,20 +205,22 @@ export class GroupsService {
if (member.role === GroupMemberRole.OWNER) {
throw new BadRequestException({
code: ErrorCode.NO_PERMISSION,
message: '组长不能退出小组,请先转让组长或解散小组',
message: "组长不能退出小组,请先转让组长或解散小组",
});
}
await this.groupMemberRepository.remove(member);
// 更新小组成员数
const group = await this.groupRepository.findOne({ where: { id: groupId } });
const group = await this.groupRepository.findOne({
where: { id: groupId },
});
if (group) {
group.currentMembers = Math.max(0, group.currentMembers - 1);
await this.groupRepository.save(group);
}
return { message: '退出成功' };
return { message: "退出成功" };
}
/**
@@ -231,7 +238,7 @@ export class GroupsService {
const group = await this.groupRepository.findOne({
where: { id },
relations: ['owner', 'members', 'members.user'],
relations: ["owner", "members", "members.user"],
});
if (!group) {
@@ -269,7 +276,7 @@ export class GroupsService {
async findUserGroups(userId: string) {
const members = await this.groupMemberRepository.find({
where: { userId },
relations: ['group', 'group.owner'],
relations: ["group", "group.owner"],
});
return members.map((member) => ({
@@ -282,8 +289,14 @@ export class GroupsService {
/**
* 更新小组信息
*/
async update(userId: string, groupId: string, updateGroupDto: UpdateGroupDto) {
const group = await this.groupRepository.findOne({ where: { id: groupId } });
async update(
userId: string,
groupId: string,
updateGroupDto: UpdateGroupDto,
) {
const group = await this.groupRepository.findOne({
where: { id: groupId },
});
if (!group) {
throw new NotFoundException({
@@ -326,7 +339,7 @@ export class GroupsService {
if (!member) {
throw new NotFoundException({
code: ErrorCode.NOT_IN_GROUP,
message: '该用户不在小组中',
message: "该用户不在小组中",
});
}
@@ -334,14 +347,14 @@ export class GroupsService {
if (member.role === GroupMemberRole.OWNER) {
throw new BadRequestException({
code: ErrorCode.NO_PERMISSION,
message: '不能修改组长角色',
message: "不能修改组长角色",
});
}
member.role = role;
await this.groupMemberRepository.save(member);
return { message: '角色设置成功' };
return { message: "角色设置成功" };
}
/**
@@ -361,7 +374,7 @@ export class GroupsService {
if (!member) {
throw new NotFoundException({
code: ErrorCode.NOT_IN_GROUP,
message: '该用户不在小组中',
message: "该用户不在小组中",
});
}
@@ -369,27 +382,31 @@ export class GroupsService {
if (member.role === GroupMemberRole.OWNER) {
throw new BadRequestException({
code: ErrorCode.NO_PERMISSION,
message: '不能踢出组长',
message: "不能踢出组长",
});
}
await this.groupMemberRepository.remove(member);
// 更新小组成员数
const group = await this.groupRepository.findOne({ where: { id: groupId } });
const group = await this.groupRepository.findOne({
where: { id: groupId },
});
if (group) {
group.currentMembers = Math.max(0, group.currentMembers - 1);
await this.groupRepository.save(group);
}
return { message: '成员已移除' };
return { message: "成员已移除" };
}
/**
* 解散小组
*/
async disband(userId: string, groupId: string) {
const group = await this.groupRepository.findOne({ where: { id: groupId } });
const group = await this.groupRepository.findOne({
where: { id: groupId },
});
if (!group) {
throw new NotFoundException({
@@ -402,14 +419,14 @@ export class GroupsService {
if (group.ownerId !== userId) {
throw new ForbiddenException({
code: ErrorCode.NO_PERMISSION,
message: '只有组长可以解散小组',
message: "只有组长可以解散小组",
});
}
group.isActive = false;
await this.groupRepository.save(group);
return { message: '小组已解散' };
return { message: "小组已解散" };
}
/**