using DigitalData.TaskFlow.API.Contracts.Workflows; using DigitalData.TaskFlow.API.Contracts.Common; using DigitalData.TaskFlow.Application.Features.Workflows.Commands.CloseWorkflow; using DigitalData.TaskFlow.Application.Features.Workflows.Queries.GetWorkflows; using MediatR; using Microsoft.AspNetCore.Mvc; namespace DigitalData.TaskFlow.API.Controllers; /// /// Provides workflow read and close endpoints. /// [ApiController] [Route("api/workflows")] public sealed class WorkflowsController(ISender sender) : ControllerBase { /// /// Returns open workflows for a WISAG user identifier. /// /// WISAG user identifier. /// Cancellation token. /// Workflow response envelope grouped by source. [HttpGet("{wisagId:int}")] [ProducesResponseType(typeof(WorkflowsResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status500InternalServerError)] public async Task> GetAsync( [FromRoute] int wisagId, CancellationToken cancellationToken) { var response = await sender.Send(new GetWorkflowsQuery(wisagId), cancellationToken); var sources = response.Data?.Sources .Cast() .ToList() ?? []; return Ok(new WorkflowsResponse { Meta = response.Meta, Sources = sources }); } /// /// Applies a closing decision to a workflow process. /// /// WISAG user identifier. /// Source system name. /// Process identifier. /// Closing request payload. /// Cancellation token. /// Close result envelope. [HttpPost("{wisagId:int}/{source}/{processId:int}/closing")] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status403Forbidden)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status409Conflict)] [ProducesResponseType(typeof(MetaOnlyResponse), StatusCodes.Status500InternalServerError)] public async Task> CloseAsync( [FromRoute] int wisagId, [FromRoute] string source, [FromRoute] int processId, [FromBody] CloseWorkflowRequest request, CancellationToken cancellationToken) { var command = new CloseWorkflowCommand( wisagId, source, processId, request.ClosingMethodId, request.Comment, request.RejectionReasonId); var response = await sender.Send(command, cancellationToken); return Ok(new MetaOnlyResponse { Meta = response.Meta }); } }