All files / src/interfaces/mobile-api/manager mobile-approvals.controller.ts

0% Statements 0/18
0% Branches 0/20
0% Functions 0/4
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                                       
import {
  Controller,
  Get,
  Post,
  Param,
  Body,
  Req,
  UseGuards,
  Query,
} from '@nestjs/common';
import { ApprovalService } from '../../../platform/approvals/approval.service';
import { JwtAuthGuard } from '../../../platform/auth/jwt-auth.guard';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/mobile/manager/approvals')
@ApiTags('MobileApprovals')
@UseGuards(JwtAuthGuard)
export class MobileApprovalsController {
  constructor(private readonly approvalService: ApprovalService) {}
 
  @Get()
  @ApiOperation({ summary: 'List pending manager approval requests' })
  @ApiResponse({ status: 200, description: 'List of approval requests' })
  async getPendingApprovals(
    @Req() req: any,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
 
    const result = await this.approvalService.getRequests(tenantId, {
      status: 'PENDING',
      page: page || 1,
      limit: limit || 10,
    });
 
    // Cursor-like or offset pagination return with compact DTO
    return {
      items: result.data.map((r: any) => ({
        id: r.id,
        entityType: r.entityType,
        entityId: r.entityId,
        submittedBy: r.submittedBy,
        requestNumber: r.requestNumber || `AP-${r.id.substring(0, 8)}`,
        createdAt: r.createdAt,
      })),
      totalCount: result.meta.total,
    };
  }
 
  @Post(':id/action')
  @ApiOperation({ summary: 'Process approval decision (Approve/Reject)' })
  @ApiResponse({ status: 200, description: 'Action processing result' })
  async processAction(
    @Param('id') requestId: string,
    @Req() req: any,
    @Body()
    body: {
      action: 'APPROVE' | 'REJECT';
      comment?: string;
      expectedVersion?: number;
    },
  ) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || 'SYSTEM';
 
    const result = await this.approvalService.performAction({
      requestId,
      tenantId,
      userId,
      action: body.action,
      comment: body.comment,
      expectedVersion: body.expectedVersion,
    });
 
    return {
      id: requestId,
      status: result.status || body.action + 'D',
    };
  }
}