using DigitalData.TaskFlow.API.Models; using DigitalData.TaskFlow.Application.Common.Exceptions; using DigitalData.TaskFlow.Application.Features.Workflows.Commands; using DigitalData.TaskFlow.Application.Features.Workflows.Queries; using MediatR; using Microsoft.AspNetCore.Mvc; using System.Text.Json; 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(JsonElement), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status502BadGateway)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> GetAsync([FromRoute] int wisagId, CancellationToken cancellationToken) { return Ok(await sender.Send(new GetWorkflowsQuery(wisagId), cancellationToken)); } /// /// Closes a workflow for a WISAG user identifier, source, and process identifier. /// /// /// /// /// /// /// [HttpPost("{wisagId:int}/{source}/{processId:long}/closing")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task CloseAsync( [FromRoute] int wisagId, [FromRoute] long processId, [FromBody] CloseWorkflowReqBody body, [FromRoute] string source = "Ferdinand", CancellationToken cancellationToken = default) { var result = await sender.Send( new CloseWorkflowCommand( WisagId: wisagId, Source: source, ProcessId: processId, ClosingMethodId: body.ClosingMethodId, Comment: body.Comment, RejectionReasonId: body.RejectionReasonId), cancellationToken); var metaResult = new CloseWorkflowReqResponse() { Meta = [result] }; return result.Code switch { 0 => Ok(metaResult), 4 => BadRequest(metaResult), 6 => NotFound(metaResult), 7 => Conflict(metaResult), _ => StatusCode(StatusCodes.Status500InternalServerError, metaResult) }; } }