All files / src/platform/workflows workflow.controller.ts

0% Statements 0/43
0% Branches 0/18
0% Functions 0/13
0% Lines 0/39

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                                                                                                                                                                                                                                                         
import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Query,
  Req,
} from '@nestjs/common';
import { WorkflowService } from './workflow.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
 
@Controller('api/v1/workflows')
@ApiTags('Workflow')
export class WorkflowController {
  constructor(private readonly workflowService: WorkflowService) {}
 
  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.workflowService.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.workflowService.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.workflowService.getDefinition(id, ctx.tenantId);
  }
 
  @Put(':id')
  @ApiOperation({ summary: 'Update operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async update(@Param('id') id: string, @Body() body: any, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.workflowService.updateDefinition(id, ctx.tenantId, body);
  }
 
  @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.workflowService.publishDefinition(id, ctx.tenantId);
  }
 
  @Post(':id/activate')
  @ApiOperation({ summary: 'Activate operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async activate(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.workflowService.activate(id, ctx.tenantId);
  }
 
  @Post(':id/deactivate')
  @ApiOperation({ summary: 'Deactivate operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async deactivate(@Param('id') id: string, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.workflowService.deactivate(id, ctx.tenantId);
  }
 
  @Post(':id/execute')
  @ApiOperation({ summary: 'Execute operation' })
  @ApiResponse({ status: 201, description: 'Operation successful' })
  async execute(@Param('id') id: string, @Body() body: any, @Req() req: any) {
    const ctx = this.getContext(req);
    return this.workflowService.triggerExecution(id, ctx.tenantId, body);
  }
 
  @Get(':id/executions')
  @ApiOperation({ summary: 'Get executions operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getExecutions(
    @Param('id') id: string,
    @Query('page') page: string,
    @Query('limit') limit: string,
    @Req() req: any,
  ) {
    const ctx = this.getContext(req);
    return this.workflowService.getExecutions(
      id,
      ctx.tenantId,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 20,
    );
  }
}
 
@Controller('api/v1/workflow-executions')
@ApiTags('WorkflowExecution')
export class WorkflowExecutionController {
  constructor(private readonly workflowService: WorkflowService) {}
 
  @Get(':executionId')
  @ApiOperation({ summary: 'Get execution operation' })
  @ApiResponse({ status: 200, description: 'Operation successful' })
  async getExecution(@Param('executionId') executionId: string) {
    return this.workflowService.getExecution(executionId);
  }
}