import { Controller, Get, Post, Put, Body, Param, Query, UseGuards, NotFoundException, GoneException, } from "@nestjs/common"; import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger"; import { JwtAuthGuard } from "../auth/jwt-auth.guard"; import { RolesGuard } from "../common/guards/roles.guard"; import { Roles } from "../common/decorators/roles.decorator"; import { PrismaService } from "../prisma/prisma.service"; @ApiTags("Admin - Department & Agent Tasks") @ApiBearerAuth() @UseGuards(JwtAuthGuard, RolesGuard) @Roles("Admin", "SUPER_ADMIN") @Controller("admin/department-tasks") export class DepartmentTasksController { constructor(private readonly prisma: PrismaService) {} @Get() @ApiOperation({ summary: "لیست تسک‌های دپارتمان‌ها و ایجنت‌ها" }) async findAll(@Query() query: any) { const page = Number(query.page) || 1; const limit = Number(query.limit) || 100; const skip = (page - 1) * limit; const where: any = {}; if (query.department) where.department = query.department; if (query.status) where.status = query.status; if (query.priority) where.priority = query.priority; if (query.search) { where.OR = [ { code: { contains: query.search, mode: "insensitive" } }, { title: { contains: query.search, mode: "insensitive" } }, { description: { contains: query.search, mode: "insensitive" } }, ]; } const [total, items] = await Promise.all([ (this.prisma as any).departmentTask.count({ where }), (this.prisma as any).departmentTask.findMany({ where, skip, take: limit, orderBy: [{ priority: "desc" }, { createdAt: "desc" }], }), ]); return { success: true, data: items, meta: { total, page, limit, totalPages: Math.ceil(total / limit), }, }; } @Get("stats") @ApiOperation({ summary: "آمار تسک‌ها به تفکیک استیت و دپارتمان" }) async getStats() { const tasks = await (this.prisma as any).departmentTask.findMany({ select: { department: true, status: true, priority: true }, }); const statusCounts: Record = { TODO: 0, IN_PROGRESS: 0, TEST: 0, DONE: 0, BACKLOG: 0, IN_REVIEW: 0, APPROVED: 0, DEPLOYING: 0, }; const departmentCounts: Record = {}; for (const t of tasks) { statusCounts[t.status] = (statusCounts[t.status] || 0) + 1; departmentCounts[t.department] = (departmentCounts[t.department] || 0) + 1; } return { success: true, data: { total: tasks.length, byStatus: statusCounts, byDepartment: departmentCounts, }, }; } @Get(":id") @ApiOperation({ summary: "دریافت جزییات یک تسک" }) async findOne(@Param("id") id: string) { const task = await (this.prisma as any).departmentTask.findUnique({ where: { id }, }); if (!task) throw new NotFoundException(`Task ${id} not found`); return { success: true, data: task }; } @Post() @ApiOperation({ summary: "ایجاد تسک جدید سازمانی" }) async create(@Body() body: any) { throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled."); } @Put(":id/status") @ApiOperation({ summary: "تغییر وضعیت تسک همراه با لاگ اجرا" }) async updateStatus( @Param("id") id: string, @Body() body: { status: string; logNote?: string; deliverableUrl?: string } ) { throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled."); } }