All files / src/platform/approvals approval.controller.ts

0% Statements 0/53
0% Branches 0/26
0% Functions 0/16
0% Lines 0/49

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                                                                                                                                                                                                                                                                                                                                                                                                     
import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Query,
  Req,
} from '@nestjs/common';
import { ApprovalService } from './approval.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/approval-definitions')
@ApiTags('ApprovalDefinition')
export class ApprovalDefinitionController {
  constructor(private readonly approvalService: ApprovalService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Get()
  @ApiOperation({ summary: 'List operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async list(@Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.getDefinitions(ctx.tenantId);
  }
 
  @Post()
  @ApiOperation({ summary: 'Create operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async create(@Body() body: any, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.createDefinition({
      ...body,
      tenantId: ctx.tenantId,
      createdBy: ctx.userId,
    });
  }
 
  @Get(':id')
  @ApiOperation({ summary: 'Get one operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getOne(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.getDefinition(id, ctx.tenantId);
  }
 
  @Post(':id/publish')
  @ApiOperation({ summary: 'Publish operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async publish(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.publishDefinition(id, ctx.tenantId);
  }
}
 
@Controller('api/v1/approvals')
@ApiTags('ApprovalRequest')
export class ApprovalRequestController {
  constructor(private readonly approvalService: ApprovalService) {}
 
  private getContext(req: any) {
    const tenantId =
      req.user?.tenantId || req.headers['x-tenant-id'] || 'SYSTEM';
    const userId = req.user?.id || req.headers['x-user-id'] || 'system-user-id';
    return { tenantId, userId };
  }
 
  @Get()
  @ApiOperation({ summary: 'List operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async list(
    @Query('status') status: string,
    @Query('page') page: string,
    @Query('limit') limit: string,
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.approvalService.getRequests(ctx.tenantId, {
      status: status || undefined,
      page: page ? parseInt(page) : 1,
      limit: limit ? parseInt(limit) : 20,
    });
  }
 
  @Get(':id')
  @ApiOperation({ summary: 'Get one operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getOne(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.getRequest(id, ctx.tenantId);
  }
 
  @Post()
  @ApiOperation({ summary: 'Submit operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async submit(
    @Body() body: { entityType: string; entityId: string; metadata?: any },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.approvalService.submitRequest({
      ...body,
      tenantId: ctx.tenantId,
      submittedBy: ctx.userId,
    });
  }
 
  @Post(':id/approve')
  @ApiOperation({ summary: 'Approve operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async approve(
    @Param('id') id: string,
    @Body() body: { comment?: string; expectedVersion?: number },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.approvalService.performAction({
      requestId: id,
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      action: 'APPROVE',
      ...body,
    });
  }
 
  @Post(':id/reject')
  @ApiOperation({ summary: 'Reject operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async reject(
    @Param('id') id: string,
    @Body() body: { comment?: string; expectedVersion?: number },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.approvalService.performAction({
      requestId: id,
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      action: 'REJECT',
      ...body,
    });
  }
 
  @Post(':id/request-changes')
  @ApiOperation({ summary: 'Request changes operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async requestChanges(
    @Param('id') id: string,
    @Body() body: { comment?: string; expectedVersion?: number },
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.approvalService.performAction({
      requestId: id,
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      action: 'REQUEST_CHANGES',
      ...body,
    });
  }
 
  @Post(':id/cancel')
  @ApiOperation({ summary: 'Cancel operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async cancel(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.performAction({
      requestId: id,
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      action: 'CANCEL',
    });
  }
 
  @Post(':id/resubmit')
  @ApiOperation({ summary: 'Resubmit operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async resubmit(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.approvalService.performAction({
      requestId: id,
      tenantId: ctx.tenantId,
      userId: ctx.userId,
      action: 'RESUBMIT',
    });
  }
}