|
| 1 | +using AutoMapper; |
| 2 | +using ECER.Infrastructure.Common; |
| 3 | +using ECER.Managers.Registry.Contract.ICRA; |
| 4 | +using ECER.Utilities.Hosting; |
| 5 | +using ECER.Utilities.Security; |
| 6 | +using MediatR; |
| 7 | +using Microsoft.AspNetCore.Http.HttpResults; |
| 8 | + |
| 9 | +namespace ECER.Clients.RegistryPortal.Server.ICRA; |
| 10 | + |
| 11 | +public class ICRAEligibilitiesEndpoints : IRegisterEndpoints |
| 12 | +{ |
| 13 | + public void Register(IEndpointRouteBuilder endpointRouteBuilder) |
| 14 | + { |
| 15 | + endpointRouteBuilder.MapPut("/api/icra/{id?}", async Task<Results<Ok<DraftICRAEligibilityResponse>, BadRequest<string>, NotFound>> (string? id, SaveDraftICRAEligibilityRequest request, HttpContext ctx, CancellationToken ct, IMediator messageBus, IMapper mapper) => |
| 16 | + { |
| 17 | + bool IdIsNotGuid = !Guid.TryParse(id, out _); if (IdIsNotGuid && id != null) { id = null; } |
| 18 | + bool PayloadIdIsNotGuid = !Guid.TryParse(request.Eligibility.Id, out _); if (PayloadIdIsNotGuid && request.Eligibility.Id != null) { request.Eligibility.Id = null; } |
| 19 | + |
| 20 | + if (request.Eligibility.Id != id) return TypedResults.BadRequest("resource id and payload id do not match"); |
| 21 | + var userContext = ctx.User.GetUserContext(); |
| 22 | + var eligibility = mapper.Map<Managers.Registry.Contract.ICRA.ICRAEligibility>(request.Eligibility, opts => opts.Items.Add("ApplicantId", userContext!.UserId))!; |
| 23 | + |
| 24 | + if (id != null) |
| 25 | + { |
| 26 | + var query = new ICRAEligibilitiesQuery |
| 27 | + { |
| 28 | + ById = id, |
| 29 | + ByApplicantId = userContext!.UserId, |
| 30 | + ByStatus = [Managers.Registry.Contract.ICRA.ICRAStatus.Draft] |
| 31 | + }; |
| 32 | + var results = await messageBus.Send(query, ct); |
| 33 | + if (!results.Items.Any()) return TypedResults.NotFound(); |
| 34 | + } |
| 35 | + |
| 36 | + var freshEligibility = await messageBus.Send(new SaveICRAEligibilityCommand(eligibility), ct); |
| 37 | + return TypedResults.Ok(new DraftICRAEligibilityResponse(mapper.Map<ICRAEligibility>(freshEligibility))); |
| 38 | + }) |
| 39 | + .WithOpenApi("Save a draft icra eligibility for the current user", string.Empty, "icra_put") |
| 40 | + .RequireAuthorization() |
| 41 | + .WithParameterValidation(); |
| 42 | + |
| 43 | + endpointRouteBuilder.MapGet("/api/icra/{id?}", async (string? id, ICRAStatus[]? byStatus, HttpContext ctx, IMediator messageBus, IMapper mapper, CancellationToken ct) => |
| 44 | + { |
| 45 | + var userId = ctx.User.GetUserContext()?.UserId; |
| 46 | + |
| 47 | + bool IdIsNotGuid = !Guid.TryParse(id, out _); if (IdIsNotGuid) { id = null; } |
| 48 | + var query = new ICRAEligibilitiesQuery |
| 49 | + { |
| 50 | + ById = id, |
| 51 | + ByApplicantId = userId, |
| 52 | + ByStatus = byStatus?.Convert<ICRAStatus, Managers.Registry.Contract.ICRA.ICRAStatus>() |
| 53 | + }; |
| 54 | + var results = await messageBus.Send(query, ct); |
| 55 | + return TypedResults.Ok(mapper.Map<IEnumerable<ICRAEligibility>>(results.Items)); |
| 56 | + }) |
| 57 | + .WithOpenApi("Handles icra queries", string.Empty, "icra_get") |
| 58 | + .RequireAuthorization() |
| 59 | + .WithParameterValidation(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +public record SaveDraftICRAEligibilityRequest(ICRAEligibility Eligibility); |
| 64 | + |
| 65 | +public record DraftICRAEligibilityResponse(ICRAEligibility Eligibility); |
| 66 | + |
| 67 | +public record ICRAEligibilityQueryResponse(IEnumerable<ICRAEligibility> Items); |
| 68 | + |
| 69 | +public record ICRAEligibility() |
| 70 | +{ |
| 71 | + public string? Id { get; set; } |
| 72 | + public string ApplicantId { get; set; } = string.Empty; |
| 73 | + public string? PortalStage { get; set; } |
| 74 | + |
| 75 | + public ICRAStatus Status { get; set; } |
| 76 | +} |
| 77 | + |
| 78 | +public enum ICRAStatus |
| 79 | +{ |
| 80 | + Active, |
| 81 | + Draft, |
| 82 | + Eligible, |
| 83 | + Inactive, |
| 84 | + Ineligible, |
| 85 | + InReview, |
| 86 | + ReadyforReview, |
| 87 | + Submitted |
| 88 | +} |
0 commit comments