diff --git a/backend/src/admin/approvals.controller.ts b/backend/src/admin/approvals.controller.ts new file mode 100644 index 00000000..ce0ce6f3 --- /dev/null +++ b/backend/src/admin/approvals.controller.ts @@ -0,0 +1,134 @@ +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"; +import { + CreateApprovalRequestDto, + DecideApprovalDto, + QueryApprovalDto, +} from "./dto/approval-request.dto"; + +@ApiTags("Admin - CEO Approvals & Governance") +@ApiBearerAuth() +@UseGuards(JwtAuthGuard, RolesGuard) +@Roles("Admin", "SUPER_ADMIN") +@Controller("admin/approvals") +export class ApprovalsController { + constructor(private readonly prisma: PrismaService) {} + + @Get() + @ApiOperation({ summary: "لیست موارد کارتابل تاییدات مدیریت" }) + async findAll(@Query() query: QueryApprovalDto) { + 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.category) where.category = query.category; + if (query.status) where.status = query.status; + + const [total, items, pendingCount] = await Promise.all([ + (this.prisma as any).approvalRequest.count({ where }), + (this.prisma as any).approvalRequest.findMany({ + where, + skip, + take: limit, + orderBy: { createdAt: "desc" }, + }), + (this.prisma as any).approvalRequest.count({ + where: { status: "PENDING" }, + }), + ]); + + return { + success: true, + data: items, + meta: { + total, + page, + limit, + totalPages: Math.ceil(total / limit), + pendingCount, + }, + }; + } + + @Get(":id") + @ApiOperation({ summary: "دریافت جزییات یک آیتم کارتابل" }) + async findOne(@Param("id") id: string) { + const item = await (this.prisma as any).approvalRequest.findUnique({ + where: { id }, + }); + if (!item) { + throw new NotFoundException(`Approval request ${id} not found`); + } + return { success: true, data: item }; + } + + @Post() + @ApiOperation({ summary: "ثبت آیتم جدید جهت تایید مدیریت (از طرف سیستم یا دپارتمان‌ها)" }) + async create(@Body() dto: CreateApprovalRequestDto) { + throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled."); + } + + @Post(":id/test") + @ApiOperation({ summary: "اجرای شبیه‌سازی تست زنده برای آیتم کارتابل" }) + async runTestSimulation(@Param("id") id: string) { + const item = await (this.prisma as any).approvalRequest.findUnique({ + where: { id }, + }); + if (!item) { + throw new NotFoundException(`Approval request ${id} not found`); + } + + let testResult: any = { status: "SIMULATION_SUCCESS" }; + if (item.category === "PRICING") { + const payload = item.payload as any; + testResult = { + impact: "Price change simulation", + currentPrice: payload.currentPrice, + newPrice: payload.newPrice, + marginDiff: payload.newPrice && payload.buyPrice + ? (((payload.newPrice - payload.buyPrice) / payload.newPrice) * 100).toFixed(1) + "%" + : "N/A", + torobFeedReflectEstimate: "< 5 seconds", + }; + } else if (item.category === "PURCHASE_ORDER") { + const payload = item.payload as any; + testResult = { + document: "PO Pre-check", + itemsCount: payload.items ? payload.items.length : 1, + totalEstimatedCost: payload.totalCost || "محاسبه بر اساس نرخ خرید", + fulfillmentCapacity: "انبار آماده پذیرش", + }; + } else { + testResult = { + executionSafe: true, + summary: "تست سلامت اولیه و اعتبارسنجی مقادیر با موفقیت انجام شد.", + }; + } + + return { success: true, testResult }; + } + + @Put(":id/decide") + @ApiOperation({ summary: "تصمیم‌گیری مدیرعامل (تایید یا رد همراه با یادداشت)" }) + async decide(@Param("id") id: string, @Body() dto: DecideApprovalDto) { + throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled."); + } + +}