|
| 1 | +using System.ComponentModel.DataAnnotations; |
| 2 | +using System.Globalization; |
| 3 | +using System.Text.Json; |
| 4 | +using Azure.Messaging; |
| 5 | +using Azure.Messaging.EventGrid; |
| 6 | +using Microsoft.AspNetCore.Mvc; |
| 7 | +using Microsoft.Azure.Functions.Worker; |
| 8 | +using Microsoft.Azure.Functions.Worker.Http; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using ServiceLayer.API.Models; |
| 11 | +using ServiceLayer.API.Shared; |
| 12 | + |
| 13 | +namespace ServiceLayer.API.Functions; |
| 14 | + |
| 15 | +public class BSSelectFunctions(ILogger<BSSelectFunctions> logger, EventGridPublisherClient eventGridPublisherClient) |
| 16 | +{ |
| 17 | + [Function("BSSelectIngressEpisode")] |
| 18 | + public async Task<IActionResult> IngressEpisode([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "bsselect/episodes/ingress")] HttpRequestData req) |
| 19 | + { |
| 20 | + BSSelectEpisode? bssEpisodeEvent; |
| 21 | + |
| 22 | + try |
| 23 | + { |
| 24 | + bssEpisodeEvent = await JsonSerializer.DeserializeAsync<BSSelectEpisode>(req.Body); |
| 25 | + |
| 26 | + if (bssEpisodeEvent == null) |
| 27 | + { |
| 28 | + logger.LogError("Deserialization returned null"); |
| 29 | + return new BadRequestObjectResult("Deserialization returned null"); |
| 30 | + } |
| 31 | + |
| 32 | + var validationContext = new ValidationContext(bssEpisodeEvent); |
| 33 | + |
| 34 | + Validator.ValidateObject(bssEpisodeEvent, validationContext, true); |
| 35 | + } |
| 36 | + catch (Exception ex) |
| 37 | + { |
| 38 | + logger.LogError(ex, "An error occured when reading request body"); |
| 39 | + return new BadRequestObjectResult(ex.Message); |
| 40 | + } |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + var createPathwayEnrolment = new CreatePathwayParticipantDto |
| 45 | + { |
| 46 | + PathwayTypeId = new Guid("11111111-1111-1111-1111-111111111113"), |
| 47 | + PathwayTypeName = "Breast Screening Routine", |
| 48 | + ScreeningName = "Breast Screening", |
| 49 | + NhsNumber = bssEpisodeEvent.NhsNumber!, |
| 50 | + DOB = DateOnly.Parse(bssEpisodeEvent.DateOfBirth!, CultureInfo.CurrentCulture), |
| 51 | + Name = $"{bssEpisodeEvent.FirstGivenName} {bssEpisodeEvent.FamilyName}", |
| 52 | + }; |
| 53 | + |
| 54 | + var cloudEvent = new CloudEvent( |
| 55 | + "ServiceLayer", |
| 56 | + "EpisodeEvent", |
| 57 | + createPathwayEnrolment |
| 58 | + ); |
| 59 | + |
| 60 | + var response = await eventGridPublisherClient.SendEventAsync(cloudEvent); |
| 61 | + |
| 62 | + if (response.IsError) |
| 63 | + { |
| 64 | + logger.LogError( |
| 65 | + "Failed to send event to Event Grid.\nSource: {Source}\nType: {Type}\n Response status code: {Status}", |
| 66 | + cloudEvent.Source, cloudEvent.Type, response.Status); |
| 67 | + return new StatusCodeResult(500); |
| 68 | + } |
| 69 | + |
| 70 | + return new OkResult(); |
| 71 | + } |
| 72 | + catch (Exception ex) |
| 73 | + { |
| 74 | + logger.LogError(ex, "Failed to send event to Event Grid"); |
| 75 | + return new StatusCodeResult(500); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments