import { Controller, Post, Get, Put, Delete, Body, Param, Query, Req, UseGuards, HttpCode, HttpStatus, ParseIntPipe, } from '@nestjs/common'; import { Request } from 'express'; import { JwtAuthGuard } from '../../auth/guards/jwt.guard'; import { WorkflowScheduleService } from '../service/workflow-schedule.service'; import { CreateScheduleDto } from '../dto/create-schedule.dto'; import { UpdateScheduleDto } from '../dto/update-schedule.dto'; import { GetExecutionLogsDto } from '../dto/get-execution-logs.dto'; /** * Workflow Schedule Controller * Provides REST API endpoints for managing scheduled workflows */ @Controller('workflow-schedule') @UseGuards(JwtAuthGuard) export class WorkflowScheduleController { constructor(private readonly workflowScheduleService: WorkflowScheduleService) {} /** * 1. Create a new scheduled workflow * POST /workflow-schedule/create */ @Post('/create') @HttpCode(HttpStatus.CREATED) async createSchedule( @Body() createScheduleDto: CreateScheduleDto, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const schedule = await this.workflowScheduleService.createSchedule( createScheduleDto, loggedInUser, ); return { success: true, message: 'Scheduled workflow created successfully', data: schedule, }; } /** * 2. Update an existing scheduled workflow * PUT /workflow-schedule/update */ @Put('/update') @HttpCode(HttpStatus.OK) async updateSchedule( @Body() updateScheduleDto: UpdateScheduleDto, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const schedule = await this.workflowScheduleService.updateSchedule( updateScheduleDto, loggedInUser, ); return { success: true, message: 'Scheduled workflow updated successfully', data: schedule, }; } /** * 3. Get a scheduled workflow by ID * GET /workflow-schedule/:id */ @Get('/:id') @HttpCode(HttpStatus.OK) async getScheduleById( @Param('id', ParseIntPipe) id: number, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const schedule = await this.workflowScheduleService.getScheduleById(id); return { success: true, data: schedule, }; } /** * 4. Get all scheduled workflows with pagination and filters * GET /workflow-schedule/list */ @Get('/list') @HttpCode(HttpStatus.OK) async getAllSchedules( @Query('workflow_id') workflow_id: string | undefined, @Query('schedule_status') schedule_status: string | undefined, @Query('is_enabled') is_enabled: string | undefined, @Query('page') page: string | undefined, @Query('size') size: string | undefined, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const filters: Record = {}; if (workflow_id) filters.workflow_id = Number.parseInt(workflow_id, 10); if (schedule_status) filters.schedule_status = schedule_status; if (is_enabled !== undefined) filters.is_enabled = is_enabled === 'true'; const result = await this.workflowScheduleService.getAllSchedules( page ? Number.parseInt(page, 10) : 1, size ? Number.parseInt(size, 10) : 10, filters, loggedInUser, ); return { success: true, ...result, }; } /** * 5. Pause a scheduled workflow * POST /workflow-schedule/:id/pause */ @Post('/:id/pause') @HttpCode(HttpStatus.OK) async pauseSchedule( @Param('id', ParseIntPipe) id: number, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const schedule = await this.workflowScheduleService.pauseSchedule(id, loggedInUser); return { success: true, message: 'Scheduled workflow paused successfully', data: schedule, }; } /** * 6. Resume a paused scheduled workflow * POST /workflow-schedule/:id/resume */ @Post('/:id/resume') @HttpCode(HttpStatus.OK) async resumeSchedule( @Param('id', ParseIntPipe) id: number, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const schedule = await this.workflowScheduleService.resumeSchedule(id, loggedInUser); return { success: true, message: 'Scheduled workflow resumed successfully', data: schedule, }; } /** * 7. Delete a scheduled workflow * DELETE /workflow-schedule/:id */ @Delete('/:id') @HttpCode(HttpStatus.OK) async deleteSchedule( @Param('id', ParseIntPipe) id: number, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; await this.workflowScheduleService.deleteSchedule(id, loggedInUser); return { success: true, message: 'Scheduled workflow deleted successfully', }; } /** * 8. Manually trigger a scheduled workflow execution * POST /workflow-schedule/:id/trigger */ @Post('/:id/trigger') @HttpCode(HttpStatus.OK) async triggerManualExecution( @Param('id', ParseIntPipe) id: number, @Body('metadata') metadata: Record, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const result = await this.workflowScheduleService.triggerManualExecution( id, loggedInUser, metadata, ); return { success: true, message: 'Workflow execution triggered successfully', data: result, }; } /** * 9. Get execution logs with filters and pagination * POST /workflow-schedule/execution-logs */ @Post('/execution-logs') @HttpCode(HttpStatus.OK) async getExecutionLogs( @Body() getExecutionLogsDto: GetExecutionLogsDto, @Req() req: Request & { user: any }, ) { const loggedInUser = req.user.userData; const result = await this.workflowScheduleService.getExecutionLogs( getExecutionLogsDto, loggedInUser, ); return { success: true, ...result, }; } /** * BONUS: Get execution statistics for a schedule * GET /workflow-schedule/:id/stats */ @Get('/:id/stats') @HttpCode(HttpStatus.OK) async getExecutionStats( @Param('id', ParseIntPipe) id: number ) { const stats = await this.workflowScheduleService.getExecutionStats(id); return { success: true, data: stats, }; } }