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