|
| 1 | +using api.Controllers.Models; |
| 2 | +using api.Database.Models; |
| 3 | +using api.Services; |
| 4 | +using Microsoft.AspNetCore.Authorization; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | + |
| 7 | +namespace api.Controllers; |
| 8 | + |
| 9 | +public class StidWorkflowStartedNotification |
| 10 | +{ |
| 11 | + public required string InspectionId { get; set; } |
| 12 | + public required string WorkflowName { get; set; } |
| 13 | +} |
| 14 | + |
| 15 | +public class StidWorkflowExitedNotification |
| 16 | +{ |
| 17 | + public required string InspectionId { get; set; } |
| 18 | + public required int StidMediaId { get; set; } |
| 19 | + public required string WorkflowStatus { get; set; } |
| 20 | + public required string WorkflowFailures { get; set; } |
| 21 | +} |
| 22 | + |
| 23 | +[ApiController] |
| 24 | +[Route("[controller]")] |
| 25 | +public class StidWorkflowController( |
| 26 | + IStidWorkflowService stidWorkflowService, |
| 27 | + ILogger<StidWorkflowController> logger |
| 28 | +) : ControllerBase |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// Updates status of plant data to started |
| 32 | + /// </summary> |
| 33 | + [HttpPut] |
| 34 | + [AllowAnonymous] |
| 35 | + [Route("notify-workflow-started")] |
| 36 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 37 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 38 | + public async Task<ActionResult<StidDataResponse>> WorkflowStarted( |
| 39 | + [FromBody] StidWorkflowStartedNotification notification |
| 40 | + ) |
| 41 | + { |
| 42 | + var updatedStidData = await stidWorkflowService.UpdateStidWorkflowStatus( |
| 43 | + notification.InspectionId, |
| 44 | + WorkflowStatus.Started |
| 45 | + ); |
| 46 | + logger.LogInformation( |
| 47 | + "Stid uploader workflow for inspection {inspectionId} started with workflow name {workflowName}", |
| 48 | + notification.InspectionId, |
| 49 | + notification.WorkflowName |
| 50 | + ); |
| 51 | + if (updatedStidData == null) |
| 52 | + { |
| 53 | + return NotFound( |
| 54 | + $"Could not find workflow with inspection id {notification.InspectionId}" |
| 55 | + ); |
| 56 | + } |
| 57 | + return Ok(updatedStidData); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Updates status of the workflow to exit with success or failure |
| 62 | + /// </summary> |
| 63 | + [HttpPut] |
| 64 | + [AllowAnonymous] |
| 65 | + [Route("notify-workflow-exited")] |
| 66 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 67 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 68 | + public async Task<ActionResult<StidDataResponse>> WorkflowExited( |
| 69 | + [FromBody] StidWorkflowExitedNotification notification |
| 70 | + ) |
| 71 | + { |
| 72 | + WorkflowStatus status; |
| 73 | + int? mediaId = null; |
| 74 | + |
| 75 | + if (notification.WorkflowStatus == "Succeeded") |
| 76 | + { |
| 77 | + status = WorkflowStatus.ExitSuccess; |
| 78 | + mediaId = notification.StidMediaId; |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + logger.LogWarning( |
| 83 | + "Stid uploader workflow failed with status {status}", |
| 84 | + notification.WorkflowStatus |
| 85 | + ); |
| 86 | + status = WorkflowStatus.ExitFailure; |
| 87 | + } |
| 88 | + |
| 89 | + await stidWorkflowService.UpdateStidMediaId(notification.InspectionId, mediaId); |
| 90 | + |
| 91 | + var updatedStidData = await stidWorkflowService.UpdateStidWorkflowStatus( |
| 92 | + notification.InspectionId, |
| 93 | + status |
| 94 | + ); |
| 95 | + |
| 96 | + logger.LogInformation( |
| 97 | + "Stid uploader workflow for inspection {inspectionId} exited with status {status}", |
| 98 | + notification.InspectionId, |
| 99 | + notification.WorkflowStatus |
| 100 | + ); |
| 101 | + |
| 102 | + logger.LogInformation( |
| 103 | + "StidMediaId for inspection {inspectionId} set to {mediaId}", |
| 104 | + notification.InspectionId, |
| 105 | + mediaId |
| 106 | + ); |
| 107 | + |
| 108 | + return Ok(updatedStidData); |
| 109 | + } |
| 110 | +} |
0 commit comments