import { Body, Controller, Delete, Get, NotFoundException, Param, Post, Put, Query, UseGuards, } from '@nestjs/common'; import { HqAuthGuard } from '../../common/guards/hq-auth.guard'; import { HqPermissionGuard, RequireHqPermissions, } from '../../common/guards/hq-permission.guard'; import { AuthUser } from '../../common/guards/jwt-auth.guard'; import { CurrentUser } from '../../common/decorators/current-user.decorator'; import { HqOperation } from '../../common/hq-operation/hq-operation.decorator'; import { HqOperationAction } from '../../common/hq-operation/hq-operation.constants'; import { PrismaService } from '../../common/prisma/prisma.module'; import { DevPlanService } from './dev-plan.service'; import { CreateDevPlanTaskDto, CreateDevPlanVersionDto, DevPlanTaskDispatchDto, DevPlanTaskListQueryDto, ReplaceVersionTasksDto, UpdateDevPlanSettingsDto, UpdateDevPlanTaskDto, UpdateDevPlanVersionDto, DevPlanTaskBatchUpdateDto, } from './dto/dev-plan.dto'; @Controller('admin/dev-plan') @UseGuards(HqAuthGuard, HqPermissionGuard) @RequireHqPermissions('dev_plan') export class AdminDevPlanController { constructor( private readonly service: DevPlanService, private readonly prisma: PrismaService, ) {} private async resolveHqAccount(user: AuthUser) { const account = await this.prisma.hqAccount.findUnique({ where: { id: user.actorId }, select: { id: true, name: true }, }); if (!account) throw new NotFoundException('HQ 账户不存在'); return account; } @Get('tasks') listTasks(@Query() query: DevPlanTaskListQueryDto) { return this.service.listTasks(query); } @Post('tasks') @HqOperation({ action: HqOperationAction.DEV_PLAN_TASK_CREATE, refType: 'DEV_PLAN_TASK', includeBody: true }) async createTask(@CurrentUser() user: AuthUser, @Body() body: CreateDevPlanTaskDto) { const account = await this.resolveHqAccount(user); return this.service.createTask(body, account.id); } @Get('tasks/:id') getTask(@Param('id') id: string) { return this.service.getTask(BigInt(id)); } @Put('tasks/:id') @HqOperation({ action: HqOperationAction.DEV_PLAN_TASK_UPDATE, refType: 'DEV_PLAN_TASK', refIdParam: 'id', includeBody: true, }) updateTask(@Param('id') id: string, @Body() body: UpdateDevPlanTaskDto) { return this.service.updateTask(BigInt(id), body); } @Delete('tasks/:id') @HqOperation({ action: HqOperationAction.DEV_PLAN_TASK_DELETE, refType: 'DEV_PLAN_TASK', refIdParam: 'id' }) deleteTask(@Param('id') id: string) { return this.service.deleteTask(BigInt(id)); } @Post('tasks/dispatch') @HqOperation({ action: HqOperationAction.DEV_PLAN_TASK_DISPATCH, refType: 'DEV_PLAN_TASK', includeBody: true }) async dispatchTasks(@CurrentUser() user: AuthUser, @Body() body: DevPlanTaskDispatchDto) { const account = await this.resolveHqAccount(user); return this.service.dispatchTasks(body, account.id); } @Post('tasks/batch-update') @HqOperation({ action: HqOperationAction.DEV_PLAN_TASK_UPDATE, refType: 'DEV_PLAN_TASK', includeBody: true, batch: true }) batchUpdateTasks(@Body() body: DevPlanTaskBatchUpdateDto) { return this.service.batchUpdateTasks(body); } @Get('versions') listVersions( @Query('status') status?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.service.listVersions({ status, page: page ? Number(page) : undefined, pageSize: pageSize ? Number(pageSize) : undefined, }); } @Post('versions') @HqOperation({ action: HqOperationAction.DEV_PLAN_VERSION_CREATE, refType: 'DEV_PLAN_VERSION', includeBody: true }) createVersion(@Body() body: CreateDevPlanVersionDto) { return this.service.createVersion(body); } @Get('versions/:id') getVersion(@Param('id') id: string) { return this.service.getVersion(BigInt(id)); } @Put('versions/:id') @HqOperation({ action: HqOperationAction.DEV_PLAN_VERSION_UPDATE, refType: 'DEV_PLAN_VERSION', refIdParam: 'id', includeBody: true, }) updateVersion(@Param('id') id: string, @Body() body: UpdateDevPlanVersionDto) { return this.service.updateVersion(BigInt(id), body); } @Delete('versions/:id') @HqOperation({ action: HqOperationAction.DEV_PLAN_VERSION_DELETE, refType: 'DEV_PLAN_VERSION', refIdParam: 'id' }) deleteVersion(@Param('id') id: string) { return this.service.deleteVersion(BigInt(id)); } @Put('versions/:id/tasks') @HqOperation({ action: HqOperationAction.DEV_PLAN_VERSION_LINK_TASKS, refType: 'DEV_PLAN_VERSION', refIdParam: 'id', includeBody: true, }) replaceVersionTasks(@Param('id') id: string, @Body() body: ReplaceVersionTasksDto) { return this.service.replaceVersionTasksApi(BigInt(id), body.taskIds); } @Post('versions/:id/tasks') @HqOperation({ action: HqOperationAction.DEV_PLAN_VERSION_ADD_TASKS, refType: 'DEV_PLAN_VERSION', refIdParam: 'id', includeBody: true, }) addVersionTasks(@Param('id') id: string, @Body() body: ReplaceVersionTasksDto) { return this.service.addVersionTasksApi(BigInt(id), body.taskIds); } @Get('settings') getSettings() { return this.service.getSettings(); } @Put('settings') @HqOperation({ action: HqOperationAction.DEV_PLAN_SETTINGS_UPDATE, refType: 'DEV_PLAN_SETTINGS', includeBody: true }) updateSettings(@Body() body: UpdateDevPlanSettingsDto) { return this.service.updateSettings(body); } }