Files
DigitalData.TaskFlow/src/presentation/DigitalData.TaskFlow.API/Controllers/WorkflowsController.cs
TekH 8c6102d161 Refactor workflow closure logic and enhance API
Updated `CloseWorkflowCommand` to use nullable `Code` and `Message` properties for better result representation. Refactored `CloseWorkflowCommandHandler` to use a capitalized `Repo` and improved exception handling with `InvalidWorkflowPayloadException`. Enhanced `WorkflowsController` by implementing the `CloseAsync` method, which now processes `CloseWorkflowReqBody` and returns appropriate HTTP status codes. Updated XML documentation for clarity.
2026-11-04 10:07:33 +01:00

77 lines
3.0 KiB
C#

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;
/// <summary>
/// Provides workflow read and close endpoints.
/// </summary>
[ApiController]
[Route("api/workflows")]
public sealed class WorkflowsController(ISender sender) : ControllerBase
{
/// <summary>
/// Returns open workflows for a WISAG user identifier.
/// </summary>
/// <param name="wisagId">WISAG user identifier.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Workflow response envelope grouped by source.</returns>
[HttpGet("{wisagId:int}")]
[ProducesResponseType(typeof(JsonElement), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status502BadGateway)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<JsonElement>> GetAsync([FromRoute] int wisagId, CancellationToken cancellationToken)
{
return Ok(await sender.Send(new GetWorkflowsQuery(wisagId), cancellationToken));
}
/// <summary>
/// Closes a workflow for a WISAG user identifier, source, and process identifier.
/// </summary>
/// <param name="wisagId"></param>
/// <param name="processId"></param>
/// <param name="body"></param>
/// <param name="source"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[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<IActionResult> 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)
};
}
}