初始化游戏小组管理系统后端项目
- 基于 NestJS + TypeScript + MySQL + Redis 架构 - 完整的模块化设计(认证、用户、小组、游戏、预约等) - JWT 认证和 RBAC 权限控制系统 - Docker 容器化部署支持 - 添加 CLAUDE.md 项目开发指南 - 配置 .gitignore 忽略文件 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
127
src/modules/schedules/dto/schedule.dto.ts
Normal file
127
src/modules/schedules/dto/schedule.dto.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import {
|
||||
IsString,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsNumber,
|
||||
Min,
|
||||
IsArray,
|
||||
ValidateNested,
|
||||
IsDateString,
|
||||
} from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class TimeSlotDto {
|
||||
@ApiProperty({ description: '开始时间', example: '2024-01-20T19:00:00Z' })
|
||||
@IsDateString()
|
||||
startTime: Date;
|
||||
|
||||
@ApiProperty({ description: '结束时间', example: '2024-01-20T23:00:00Z' })
|
||||
@IsDateString()
|
||||
endTime: Date;
|
||||
|
||||
@ApiProperty({ description: '备注', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
note?: string;
|
||||
}
|
||||
|
||||
export class CreateScheduleDto {
|
||||
@ApiProperty({ description: '小组ID' })
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: '小组ID不能为空' })
|
||||
groupId: string;
|
||||
|
||||
@ApiProperty({ description: '标题', example: '本周空闲时间' })
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: '标题不能为空' })
|
||||
title: string;
|
||||
|
||||
@ApiProperty({ description: '描述', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
@ApiProperty({ description: '空闲时间段', type: [TimeSlotDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TimeSlotDto)
|
||||
availableSlots: TimeSlotDto[];
|
||||
}
|
||||
|
||||
export class UpdateScheduleDto {
|
||||
@ApiProperty({ description: '标题', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
title?: string;
|
||||
|
||||
@ApiProperty({ description: '描述', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
@ApiProperty({ description: '空闲时间段', type: [TimeSlotDto], required: false })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TimeSlotDto)
|
||||
@IsOptional()
|
||||
availableSlots?: TimeSlotDto[];
|
||||
}
|
||||
|
||||
export class QuerySchedulesDto {
|
||||
@ApiProperty({ description: '小组ID', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
groupId?: string;
|
||||
|
||||
@ApiProperty({ description: '用户ID', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
userId?: string;
|
||||
|
||||
@ApiProperty({ description: '开始时间', required: false })
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
startTime?: Date;
|
||||
|
||||
@ApiProperty({ description: '结束时间', required: false })
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
endTime?: Date;
|
||||
|
||||
@ApiProperty({ description: '页码', example: 1, required: false })
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
page?: number;
|
||||
|
||||
@ApiProperty({ description: '每页数量', example: 10, required: false })
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export class FindCommonSlotsDto {
|
||||
@ApiProperty({ description: '小组ID' })
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: '小组ID不能为空' })
|
||||
groupId: string;
|
||||
|
||||
@ApiProperty({ description: '开始时间' })
|
||||
@IsDateString()
|
||||
startTime: Date;
|
||||
|
||||
@ApiProperty({ description: '结束时间' })
|
||||
@IsDateString()
|
||||
endTime: Date;
|
||||
|
||||
@ApiProperty({ description: '最少参与人数', example: 3, required: false })
|
||||
@IsNumber()
|
||||
@Min(1)
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
minParticipants?: number;
|
||||
}
|
||||
99
src/modules/schedules/schedules.controller.ts
Normal file
99
src/modules/schedules/schedules.controller.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiBearerAuth,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
import { SchedulesService } from './schedules.service';
|
||||
import {
|
||||
CreateScheduleDto,
|
||||
UpdateScheduleDto,
|
||||
QuerySchedulesDto,
|
||||
FindCommonSlotsDto,
|
||||
} from './dto/schedule.dto';
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
|
||||
@ApiTags('schedules')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('schedules')
|
||||
export class SchedulesController {
|
||||
constructor(private readonly schedulesService: SchedulesService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: '创建排班' })
|
||||
@ApiResponse({ status: 201, description: '创建成功' })
|
||||
async create(
|
||||
@CurrentUser('id') userId: string,
|
||||
@Body() createDto: CreateScheduleDto,
|
||||
) {
|
||||
return this.schedulesService.create(userId, createDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: '获取排班列表' })
|
||||
@ApiResponse({ status: 200, description: '获取成功' })
|
||||
@ApiQuery({ name: 'groupId', required: false, description: '小组ID' })
|
||||
@ApiQuery({ name: 'userId', required: false, description: '用户ID' })
|
||||
@ApiQuery({ name: 'startTime', required: false, description: '开始时间' })
|
||||
@ApiQuery({ name: 'endTime', required: false, description: '结束时间' })
|
||||
@ApiQuery({ name: 'page', required: false, description: '页码' })
|
||||
@ApiQuery({ name: 'limit', required: false, description: '每页数量' })
|
||||
async findAll(
|
||||
@CurrentUser('id') userId: string,
|
||||
@Query() queryDto: QuerySchedulesDto,
|
||||
) {
|
||||
return this.schedulesService.findAll(userId, queryDto);
|
||||
}
|
||||
|
||||
@Post('common-slots')
|
||||
@ApiOperation({ summary: '查找共同空闲时间' })
|
||||
@ApiResponse({ status: 200, description: '查询成功' })
|
||||
async findCommonSlots(
|
||||
@CurrentUser('id') userId: string,
|
||||
@Body() findDto: FindCommonSlotsDto,
|
||||
) {
|
||||
return this.schedulesService.findCommonSlots(userId, findDto);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: '获取排班详情' })
|
||||
@ApiResponse({ status: 200, description: '获取成功' })
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.schedulesService.findOne(id);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@ApiOperation({ summary: '更新排班' })
|
||||
@ApiResponse({ status: 200, description: '更新成功' })
|
||||
async update(
|
||||
@CurrentUser('id') userId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() updateDto: UpdateScheduleDto,
|
||||
) {
|
||||
return this.schedulesService.update(userId, id, updateDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: '删除排班' })
|
||||
@ApiResponse({ status: 200, description: '删除成功' })
|
||||
async remove(
|
||||
@CurrentUser('id') userId: string,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
return this.schedulesService.remove(userId, id);
|
||||
}
|
||||
}
|
||||
15
src/modules/schedules/schedules.module.ts
Normal file
15
src/modules/schedules/schedules.module.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SchedulesService } from './schedules.service';
|
||||
import { SchedulesController } from './schedules.controller';
|
||||
import { Schedule } from '../../entities/schedule.entity';
|
||||
import { Group } from '../../entities/group.entity';
|
||||
import { GroupMember } from '../../entities/group-member.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Schedule, Group, GroupMember])],
|
||||
controllers: [SchedulesController],
|
||||
providers: [SchedulesService],
|
||||
exports: [SchedulesService],
|
||||
})
|
||||
export class SchedulesModule {}
|
||||
394
src/modules/schedules/schedules.service.spec.ts
Normal file
394
src/modules/schedules/schedules.service.spec.ts
Normal file
@@ -0,0 +1,394 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
import {
|
||||
NotFoundException,
|
||||
ForbiddenException,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { SchedulesService } from './schedules.service';
|
||||
import { Schedule } from '../../entities/schedule.entity';
|
||||
import { Group } from '../../entities/group.entity';
|
||||
import { GroupMember } from '../../entities/group-member.entity';
|
||||
import { TimeSlotDto } from './dto/schedule.dto';
|
||||
|
||||
describe('SchedulesService', () => {
|
||||
let service: SchedulesService;
|
||||
let mockScheduleRepository: any;
|
||||
let mockGroupRepository: any;
|
||||
let mockGroupMemberRepository: any;
|
||||
|
||||
const mockUser = { id: 'user-1', username: 'testuser' };
|
||||
const mockGroup = { id: 'group-1', name: '测试小组', isActive: true };
|
||||
const mockMembership = {
|
||||
id: 'member-1',
|
||||
userId: 'user-1',
|
||||
groupId: 'group-1',
|
||||
role: 'member',
|
||||
isActive: true,
|
||||
};
|
||||
|
||||
const mockTimeSlots: TimeSlotDto[] = [
|
||||
{
|
||||
startTime: new Date('2024-01-20T19:00:00Z'),
|
||||
endTime: new Date('2024-01-20T21:00:00Z'),
|
||||
note: '晚上空闲',
|
||||
},
|
||||
{
|
||||
startTime: new Date('2024-01-21T14:00:00Z'),
|
||||
endTime: new Date('2024-01-21T17:00:00Z'),
|
||||
note: '下午空闲',
|
||||
},
|
||||
];
|
||||
|
||||
const mockSchedule = {
|
||||
id: 'schedule-1',
|
||||
userId: 'user-1',
|
||||
groupId: 'group-1',
|
||||
availableSlots: mockTimeSlots,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
mockScheduleRepository = {
|
||||
create: jest.fn(),
|
||||
save: jest.fn(),
|
||||
find: jest.fn(),
|
||||
findOne: jest.fn(),
|
||||
remove: jest.fn(),
|
||||
createQueryBuilder: jest.fn(),
|
||||
};
|
||||
|
||||
mockGroupRepository = {
|
||||
findOne: jest.fn(),
|
||||
};
|
||||
|
||||
mockGroupMemberRepository = {
|
||||
find: jest.fn(),
|
||||
findOne: jest.fn(),
|
||||
};
|
||||
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
SchedulesService,
|
||||
{
|
||||
provide: getRepositoryToken(Schedule),
|
||||
useValue: mockScheduleRepository,
|
||||
},
|
||||
{
|
||||
provide: getRepositoryToken(Group),
|
||||
useValue: mockGroupRepository,
|
||||
},
|
||||
{
|
||||
provide: getRepositoryToken(GroupMember),
|
||||
useValue: mockGroupMemberRepository,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<SchedulesService>(SchedulesService);
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('应该成功创建排班', async () => {
|
||||
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
||||
mockScheduleRepository.create.mockReturnValue(mockSchedule);
|
||||
mockScheduleRepository.save.mockResolvedValue(mockSchedule);
|
||||
mockScheduleRepository.findOne.mockResolvedValue({
|
||||
...mockSchedule,
|
||||
user: mockUser,
|
||||
group: mockGroup,
|
||||
});
|
||||
|
||||
const result = await service.create('user-1', {
|
||||
groupId: 'group-1',
|
||||
title: '测试排班',
|
||||
availableSlots: mockTimeSlots,
|
||||
});
|
||||
|
||||
expect(result).toHaveProperty('id');
|
||||
expect(mockScheduleRepository.save).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该在小组不存在时抛出异常', async () => {
|
||||
mockGroupRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.create('user-1', {
|
||||
groupId: 'group-1',
|
||||
title: '测试排班',
|
||||
availableSlots: mockTimeSlots,
|
||||
}),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it('应该在用户不在小组中时抛出异常', async () => {
|
||||
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.create('user-1', {
|
||||
groupId: 'group-1',
|
||||
title: '测试排班',
|
||||
availableSlots: mockTimeSlots,
|
||||
}),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
|
||||
it('应该在时间段为空时抛出异常', async () => {
|
||||
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
||||
|
||||
await expect(
|
||||
service.create('user-1', {
|
||||
groupId: 'group-1',
|
||||
title: '测试排班',
|
||||
availableSlots: [],
|
||||
}),
|
||||
).rejects.toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it('应该在时间段无效时抛出异常', async () => {
|
||||
mockGroupRepository.findOne.mockResolvedValue(mockGroup);
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
||||
|
||||
await expect(
|
||||
service.create('user-1', {
|
||||
groupId: 'group-1',
|
||||
title: '测试排班',
|
||||
availableSlots: [
|
||||
{
|
||||
startTime: new Date('2024-01-20T21:00:00Z'),
|
||||
endTime: new Date('2024-01-20T19:00:00Z'), // 结束时间早于开始时间
|
||||
},
|
||||
],
|
||||
}),
|
||||
).rejects.toThrow(BadRequestException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('应该成功获取排班列表', async () => {
|
||||
const mockQueryBuilder = {
|
||||
leftJoinAndSelect: jest.fn().mockReturnThis(),
|
||||
andWhere: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockReturnThis(),
|
||||
skip: jest.fn().mockReturnThis(),
|
||||
take: jest.fn().mockReturnThis(),
|
||||
getManyAndCount: jest
|
||||
.fn()
|
||||
.mockResolvedValue([[mockSchedule], 1]),
|
||||
};
|
||||
|
||||
mockScheduleRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
||||
|
||||
const result = await service.findAll('user-1', {
|
||||
groupId: 'group-1',
|
||||
page: 1,
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
expect(result).toHaveProperty('items');
|
||||
expect(result).toHaveProperty('total');
|
||||
expect(result.items).toHaveLength(1);
|
||||
expect(result.total).toBe(1);
|
||||
});
|
||||
|
||||
it('应该在指定小组且用户不在小组时抛出异常', async () => {
|
||||
const mockQueryBuilder = {
|
||||
leftJoinAndSelect: jest.fn().mockReturnThis(),
|
||||
andWhere: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockReturnThis(),
|
||||
skip: jest.fn().mockReturnThis(),
|
||||
take: jest.fn().mockReturnThis(),
|
||||
getManyAndCount: jest
|
||||
.fn()
|
||||
.mockResolvedValue([[mockSchedule], 1]),
|
||||
};
|
||||
|
||||
mockScheduleRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.findAll('user-1', {
|
||||
groupId: 'group-1',
|
||||
}),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
|
||||
it('应该在无小组ID时返回用户所在所有小组的排班', async () => {
|
||||
const mockQueryBuilder = {
|
||||
leftJoinAndSelect: jest.fn().mockReturnThis(),
|
||||
andWhere: jest.fn().mockReturnThis(),
|
||||
orderBy: jest.fn().mockReturnThis(),
|
||||
skip: jest.fn().mockReturnThis(),
|
||||
take: jest.fn().mockReturnThis(),
|
||||
getManyAndCount: jest
|
||||
.fn()
|
||||
.mockResolvedValue([[mockSchedule], 1]),
|
||||
};
|
||||
|
||||
mockScheduleRepository.createQueryBuilder.mockReturnValue(mockQueryBuilder);
|
||||
mockGroupMemberRepository.find.mockResolvedValue([
|
||||
{ groupId: 'group-1' },
|
||||
{ groupId: 'group-2' },
|
||||
]);
|
||||
|
||||
const result = await service.findAll('user-1', {});
|
||||
|
||||
expect(result.items).toHaveLength(1);
|
||||
expect(mockGroupMemberRepository.find).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
it('应该成功获取排班详情', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue({
|
||||
...mockSchedule,
|
||||
user: mockUser,
|
||||
group: mockGroup,
|
||||
});
|
||||
|
||||
const result = await service.findOne('schedule-1');
|
||||
|
||||
expect(result).toHaveProperty('id');
|
||||
expect(result.id).toBe('schedule-1');
|
||||
});
|
||||
|
||||
it('应该在排班不存在时抛出异常', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(service.findOne('schedule-1')).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('应该成功更新排班', async () => {
|
||||
mockScheduleRepository.findOne
|
||||
.mockResolvedValueOnce(mockSchedule)
|
||||
.mockResolvedValueOnce({
|
||||
...mockSchedule,
|
||||
user: mockUser,
|
||||
group: mockGroup,
|
||||
});
|
||||
mockScheduleRepository.save.mockResolvedValue(mockSchedule);
|
||||
|
||||
const result = await service.update('user-1', 'schedule-1', {
|
||||
availableSlots: mockTimeSlots,
|
||||
});
|
||||
|
||||
expect(result).toHaveProperty('id');
|
||||
expect(mockScheduleRepository.save).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该在排班不存在时抛出异常', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.update('user-1', 'schedule-1', { availableSlots: mockTimeSlots }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it('应该在非创建者更新时抛出异常', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue(mockSchedule);
|
||||
|
||||
await expect(
|
||||
service.update('user-2', 'schedule-1', { availableSlots: mockTimeSlots }),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', () => {
|
||||
it('应该成功删除排班', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue(mockSchedule);
|
||||
mockScheduleRepository.remove.mockResolvedValue(mockSchedule);
|
||||
|
||||
const result = await service.remove('user-1', 'schedule-1');
|
||||
|
||||
expect(result).toHaveProperty('message');
|
||||
expect(mockScheduleRepository.remove).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该在排班不存在时抛出异常', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(service.remove('user-1', 'schedule-1')).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('应该在非创建者删除时抛出异常', async () => {
|
||||
mockScheduleRepository.findOne.mockResolvedValue(mockSchedule);
|
||||
|
||||
await expect(service.remove('user-2', 'schedule-1')).rejects.toThrow(
|
||||
ForbiddenException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findCommonSlots', () => {
|
||||
it('应该成功查找共同空闲时间', async () => {
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
||||
mockScheduleRepository.find.mockResolvedValue([
|
||||
{
|
||||
...mockSchedule,
|
||||
userId: 'user-1',
|
||||
user: { id: 'user-1' },
|
||||
},
|
||||
{
|
||||
...mockSchedule,
|
||||
id: 'schedule-2',
|
||||
userId: 'user-2',
|
||||
user: { id: 'user-2' },
|
||||
availableSlots: [
|
||||
{
|
||||
startTime: new Date('2024-01-20T19:30:00Z'),
|
||||
endTime: new Date('2024-01-20T22:00:00Z'),
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
const result = await service.findCommonSlots('user-1', {
|
||||
groupId: 'group-1',
|
||||
startTime: new Date('2024-01-20T00:00:00Z'),
|
||||
endTime: new Date('2024-01-22T00:00:00Z'),
|
||||
minParticipants: 2,
|
||||
});
|
||||
|
||||
expect(result).toHaveProperty('commonSlots');
|
||||
expect(result).toHaveProperty('totalParticipants');
|
||||
expect(result.totalParticipants).toBe(2);
|
||||
});
|
||||
|
||||
it('应该在用户不在小组时抛出异常', async () => {
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.findCommonSlots('user-1', {
|
||||
groupId: 'group-1',
|
||||
startTime: new Date('2024-01-20T00:00:00Z'),
|
||||
endTime: new Date('2024-01-22T00:00:00Z'),
|
||||
}),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
|
||||
it('应该在没有排班数据时返回空结果', async () => {
|
||||
mockGroupMemberRepository.findOne.mockResolvedValue(mockMembership);
|
||||
mockScheduleRepository.find.mockResolvedValue([]);
|
||||
|
||||
const result = await service.findCommonSlots('user-1', {
|
||||
groupId: 'group-1',
|
||||
startTime: new Date('2024-01-20T00:00:00Z'),
|
||||
endTime: new Date('2024-01-22T00:00:00Z'),
|
||||
});
|
||||
|
||||
expect(result.commonSlots).toEqual([]);
|
||||
expect(result.message).toBe('暂无排班数据');
|
||||
});
|
||||
});
|
||||
});
|
||||
446
src/modules/schedules/schedules.service.ts
Normal file
446
src/modules/schedules/schedules.service.ts
Normal file
@@ -0,0 +1,446 @@
|
||||
import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
ForbiddenException,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, Between } from 'typeorm';
|
||||
import { Schedule } from '../../entities/schedule.entity';
|
||||
import { Group } from '../../entities/group.entity';
|
||||
import { GroupMember } from '../../entities/group-member.entity';
|
||||
import {
|
||||
CreateScheduleDto,
|
||||
UpdateScheduleDto,
|
||||
QuerySchedulesDto,
|
||||
FindCommonSlotsDto,
|
||||
} from './dto/schedule.dto';
|
||||
import { ErrorCode, ErrorMessage } from '../../common/interfaces/response.interface';
|
||||
import { PaginationUtil } from '../../common/utils/pagination.util';
|
||||
|
||||
export interface TimeSlot {
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
note?: string;
|
||||
}
|
||||
|
||||
export interface CommonSlot {
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
participants: string[];
|
||||
participantCount: number;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class SchedulesService {
|
||||
constructor(
|
||||
@InjectRepository(Schedule)
|
||||
private scheduleRepository: Repository<Schedule>,
|
||||
@InjectRepository(Group)
|
||||
private groupRepository: Repository<Group>,
|
||||
@InjectRepository(GroupMember)
|
||||
private groupMemberRepository: Repository<GroupMember>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 创建排班
|
||||
*/
|
||||
async create(userId: string, createDto: CreateScheduleDto) {
|
||||
const { groupId, availableSlots, ...rest } = createDto;
|
||||
|
||||
// 验证小组是否存在
|
||||
const group = await this.groupRepository.findOne({
|
||||
where: { id: groupId, isActive: true },
|
||||
});
|
||||
if (!group) {
|
||||
throw new NotFoundException({
|
||||
code: ErrorCode.GROUP_NOT_FOUND,
|
||||
message: ErrorMessage[ErrorCode.GROUP_NOT_FOUND],
|
||||
});
|
||||
}
|
||||
|
||||
// 验证用户权限
|
||||
const membership = await this.groupMemberRepository.findOne({
|
||||
where: { groupId, userId, isActive: true },
|
||||
});
|
||||
if (!membership) {
|
||||
throw new ForbiddenException({
|
||||
code: ErrorCode.NOT_IN_GROUP,
|
||||
message: ErrorMessage[ErrorCode.NOT_IN_GROUP],
|
||||
});
|
||||
}
|
||||
|
||||
// 验证时间段
|
||||
this.validateTimeSlots(availableSlots);
|
||||
|
||||
// 创建排班
|
||||
const schedule = this.scheduleRepository.create({
|
||||
...rest,
|
||||
groupId,
|
||||
userId,
|
||||
availableSlots: availableSlots as any,
|
||||
});
|
||||
|
||||
await this.scheduleRepository.save(schedule);
|
||||
|
||||
return this.findOne(schedule.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取排班列表
|
||||
*/
|
||||
async findAll(userId: string, queryDto: QuerySchedulesDto) {
|
||||
const {
|
||||
groupId,
|
||||
userId: targetUserId,
|
||||
startTime,
|
||||
endTime,
|
||||
page = 1,
|
||||
limit = 10,
|
||||
} = queryDto;
|
||||
const { offset } = PaginationUtil.formatPaginationParams(page, limit);
|
||||
|
||||
const queryBuilder = this.scheduleRepository
|
||||
.createQueryBuilder('schedule')
|
||||
.leftJoinAndSelect('schedule.group', 'group')
|
||||
.leftJoinAndSelect('schedule.user', 'user');
|
||||
|
||||
// 筛选条件
|
||||
if (groupId) {
|
||||
// 验证用户是否在小组中
|
||||
await this.checkGroupMembership(userId, groupId);
|
||||
queryBuilder.andWhere('schedule.groupId = :groupId', { groupId });
|
||||
} else {
|
||||
// 如果没有指定小组,只返回用户所在小组的排班
|
||||
const memberGroups = await this.groupMemberRepository.find({
|
||||
where: { userId, isActive: true },
|
||||
select: ['groupId'],
|
||||
});
|
||||
const groupIds = memberGroups.map((m) => m.groupId);
|
||||
if (groupIds.length === 0) {
|
||||
return {
|
||||
items: [],
|
||||
total: 0,
|
||||
page,
|
||||
limit,
|
||||
totalPages: 0,
|
||||
};
|
||||
}
|
||||
queryBuilder.andWhere('schedule.groupId IN (:...groupIds)', { groupIds });
|
||||
}
|
||||
|
||||
if (targetUserId) {
|
||||
queryBuilder.andWhere('schedule.userId = :userId', { userId: targetUserId });
|
||||
}
|
||||
|
||||
if (startTime && endTime) {
|
||||
queryBuilder.andWhere('schedule.createdAt BETWEEN :startTime AND :endTime', {
|
||||
startTime: new Date(startTime),
|
||||
endTime: new Date(endTime),
|
||||
});
|
||||
}
|
||||
|
||||
// 分页
|
||||
const [items, total] = await queryBuilder
|
||||
.orderBy('schedule.createdAt', 'DESC')
|
||||
.skip(offset)
|
||||
.take(limit)
|
||||
.getManyAndCount();
|
||||
|
||||
// 解析 availableSlots
|
||||
const formattedItems = items.map((item) => ({
|
||||
...item,
|
||||
availableSlots: this.normalizeAvailableSlots(item.availableSlots),
|
||||
}));
|
||||
|
||||
return {
|
||||
items: formattedItems,
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: PaginationUtil.getTotalPages(total, limit),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取排班详情
|
||||
*/
|
||||
async findOne(id: string) {
|
||||
const schedule = await this.scheduleRepository.findOne({
|
||||
where: { id },
|
||||
relations: ['group', 'user'],
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
throw new NotFoundException({
|
||||
code: ErrorCode.NOT_FOUND,
|
||||
message: '排班不存在',
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
...schedule,
|
||||
availableSlots: this.normalizeAvailableSlots(schedule.availableSlots),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新排班
|
||||
*/
|
||||
async update(userId: string, id: string, updateDto: UpdateScheduleDto) {
|
||||
const schedule = await this.scheduleRepository.findOne({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
throw new NotFoundException({
|
||||
code: ErrorCode.NOT_FOUND,
|
||||
message: '排班不存在',
|
||||
});
|
||||
}
|
||||
|
||||
// 只有创建者可以修改
|
||||
if (schedule.userId !== userId) {
|
||||
throw new ForbiddenException({
|
||||
code: ErrorCode.NO_PERMISSION,
|
||||
message: ErrorMessage[ErrorCode.NO_PERMISSION],
|
||||
});
|
||||
}
|
||||
|
||||
if (updateDto.availableSlots) {
|
||||
this.validateTimeSlots(updateDto.availableSlots);
|
||||
updateDto.availableSlots = updateDto.availableSlots as any;
|
||||
}
|
||||
|
||||
Object.assign(schedule, updateDto);
|
||||
await this.scheduleRepository.save(schedule);
|
||||
|
||||
return this.findOne(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除排班
|
||||
*/
|
||||
async remove(userId: string, id: string) {
|
||||
const schedule = await this.scheduleRepository.findOne({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
throw new NotFoundException({
|
||||
code: ErrorCode.NOT_FOUND,
|
||||
message: '排班不存在',
|
||||
});
|
||||
}
|
||||
|
||||
// 只有创建者可以删除
|
||||
if (schedule.userId !== userId) {
|
||||
throw new ForbiddenException({
|
||||
code: ErrorCode.NO_PERMISSION,
|
||||
message: ErrorMessage[ErrorCode.NO_PERMISSION],
|
||||
});
|
||||
}
|
||||
|
||||
await this.scheduleRepository.remove(schedule);
|
||||
|
||||
return { message: '排班已删除' };
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找共同空闲时间
|
||||
*/
|
||||
async findCommonSlots(userId: string, findDto: FindCommonSlotsDto) {
|
||||
const { groupId, startTime, endTime, minParticipants = 2 } = findDto;
|
||||
|
||||
// 验证用户权限
|
||||
await this.checkGroupMembership(userId, groupId);
|
||||
|
||||
// 获取时间范围内的所有排班
|
||||
const schedules = await this.scheduleRepository.find({
|
||||
where: { groupId },
|
||||
relations: ['user'],
|
||||
});
|
||||
|
||||
if (schedules.length === 0) {
|
||||
return {
|
||||
commonSlots: [],
|
||||
message: '暂无排班数据',
|
||||
};
|
||||
}
|
||||
|
||||
// 解析所有时间段
|
||||
const userSlots: Map<string, TimeSlot[]> = new Map();
|
||||
schedules.forEach((schedule) => {
|
||||
const slots = this.normalizeAvailableSlots(schedule.availableSlots);
|
||||
const filteredSlots = slots.filter((slot) => {
|
||||
const slotStart = new Date(slot.startTime);
|
||||
const slotEnd = new Date(slot.endTime);
|
||||
const rangeStart = new Date(startTime);
|
||||
const rangeEnd = new Date(endTime);
|
||||
return slotStart >= rangeStart && slotEnd <= rangeEnd;
|
||||
});
|
||||
userSlots.set(schedule.userId, filteredSlots);
|
||||
});
|
||||
|
||||
// 计算时间交集
|
||||
const commonSlots = this.calculateCommonSlots(userSlots, minParticipants);
|
||||
|
||||
// 按参与人数排序
|
||||
commonSlots.sort((a, b) => b.participantCount - a.participantCount);
|
||||
|
||||
return {
|
||||
commonSlots,
|
||||
totalParticipants: schedules.length,
|
||||
minParticipants,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算共同空闲时间
|
||||
*/
|
||||
private calculateCommonSlots(
|
||||
userSlots: Map<string, TimeSlot[]>,
|
||||
minParticipants: number,
|
||||
): CommonSlot[] {
|
||||
const allSlots: Array<{ time: Date; userId: string; type: 'start' | 'end' }> = [];
|
||||
|
||||
// 收集所有时间点
|
||||
userSlots.forEach((slots, userId) => {
|
||||
slots.forEach((slot) => {
|
||||
allSlots.push({
|
||||
time: new Date(slot.startTime),
|
||||
userId,
|
||||
type: 'start',
|
||||
});
|
||||
allSlots.push({
|
||||
time: new Date(slot.endTime),
|
||||
userId,
|
||||
type: 'end',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 按时间排序
|
||||
allSlots.sort((a, b) => a.time.getTime() - b.time.getTime());
|
||||
|
||||
// 扫描线算法计算重叠区间
|
||||
const commonSlots: CommonSlot[] = [];
|
||||
const activeUsers = new Set<string>();
|
||||
let lastTime: Date | null = null;
|
||||
|
||||
allSlots.forEach((event) => {
|
||||
if (lastTime && activeUsers.size >= minParticipants) {
|
||||
// 记录共同空闲时间段
|
||||
if (event.time.getTime() > lastTime.getTime()) {
|
||||
commonSlots.push({
|
||||
startTime: lastTime,
|
||||
endTime: event.time,
|
||||
participants: Array.from(activeUsers),
|
||||
participantCount: activeUsers.size,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === 'start') {
|
||||
activeUsers.add(event.userId);
|
||||
} else {
|
||||
activeUsers.delete(event.userId);
|
||||
}
|
||||
|
||||
lastTime = event.time;
|
||||
});
|
||||
|
||||
// 合并相邻的时间段
|
||||
return this.mergeAdjacentSlots(commonSlots);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并相邻的时间段
|
||||
*/
|
||||
private mergeAdjacentSlots(slots: CommonSlot[]): CommonSlot[] {
|
||||
if (slots.length === 0) return [];
|
||||
|
||||
const merged: CommonSlot[] = [];
|
||||
let current = slots[0];
|
||||
|
||||
for (let i = 1; i < slots.length; i++) {
|
||||
const next = slots[i];
|
||||
|
||||
// 如果参与者相同且时间连续,则合并
|
||||
if (
|
||||
current.endTime.getTime() === next.startTime.getTime() &&
|
||||
this.arraysEqual(current.participants, next.participants)
|
||||
) {
|
||||
current.endTime = next.endTime;
|
||||
} else {
|
||||
merged.push(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
merged.push(current);
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证时间段
|
||||
*/
|
||||
private validateTimeSlots(slots: TimeSlot[]): void {
|
||||
if (slots.length === 0) {
|
||||
throw new BadRequestException({
|
||||
code: ErrorCode.PARAM_ERROR,
|
||||
message: '至少需要一个时间段',
|
||||
});
|
||||
}
|
||||
|
||||
slots.forEach((slot, index) => {
|
||||
const start = new Date(slot.startTime);
|
||||
const end = new Date(slot.endTime);
|
||||
|
||||
if (start >= end) {
|
||||
throw new BadRequestException({
|
||||
code: ErrorCode.PARAM_ERROR,
|
||||
message: `时间段${index + 1}的结束时间必须大于开始时间`,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准化时间段数据
|
||||
*/
|
||||
private normalizeAvailableSlots(slots: any): TimeSlot[] {
|
||||
if (Array.isArray(slots)) {
|
||||
return slots;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个数组是否相同
|
||||
*/
|
||||
private async checkGroupMembership(userId: string, groupId: string) {
|
||||
const membership = await this.groupMemberRepository.findOne({
|
||||
where: { groupId, userId, isActive: true },
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
throw new ForbiddenException({
|
||||
code: ErrorCode.NOT_IN_GROUP,
|
||||
message: ErrorMessage[ErrorCode.NOT_IN_GROUP],
|
||||
});
|
||||
}
|
||||
|
||||
return membership;
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个数组是否相同
|
||||
*/
|
||||
private arraysEqual(arr1: string[], arr2: string[]): boolean {
|
||||
if (arr1.length !== arr2.length) return false;
|
||||
const sorted1 = [...arr1].sort();
|
||||
const sorted2 = [...arr2].sort();
|
||||
return sorted1.every((val, index) => val === sorted2[index]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user