-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuditWriter.cs
More file actions
137 lines (120 loc) · 4.33 KB
/
AuditWriter.cs
File metadata and controls
137 lines (120 loc) · 4.33 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
namespace NHS.CohortManager.AuditServices;
using System.Text.Json;
using Common;
using DataServices.Database;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Model;
public class AuditWriter
{
private const string AuditBlobContainer = "participant-audit";
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
private readonly DataServicesContext _dbContext;
private readonly IBlobStorageHelper _blobStorageHelper;
private readonly AuditConfig _config;
private readonly ILogger<AuditWriter> _logger;
public AuditWriter(
DataServicesContext dbContext,
IBlobStorageHelper blobStorageHelper,
IOptions<AuditConfig> config,
ILogger<AuditWriter> logger)
{
_dbContext = dbContext;
_blobStorageHelper = blobStorageHelper;
_config = config.Value;
_logger = logger;
}
[Function(nameof(AuditWriter))]
public async Task Run(
[ServiceBusTrigger(topicName: "%AuditTopicName%", subscriptionName: "%AuditSubscription%", Connection = "ServiceBusConnectionString")] string messageText, FunctionContext context)
{
ParticipantAuditMessage? audit;
try
{
audit = JsonSerializer.Deserialize<ParticipantAuditMessage>(messageText, JsonOptions);
}
catch (JsonException ex)
{
_logger.LogError(ex, "Failed to deserialise audit message.");
return;
}
if (audit is null)
{
_logger.LogError("Audit message deserialised to null.");
return;
}
var rawDataRef = await WriteSnapshotToBlobAsync(audit);
var auditLog = new ParticipantAuditLog
{
CorrelationId = audit.CorrelationId,
NhsNumber = audit.NhsNumber,
BatchId = audit.BatchId,
CreatedDatetime = audit.CreatedDatetime,
RecordSource = (int)audit.Source,
RecordSourceDesc = audit.RecordSourceDesc,
CreatedBy = audit.CreatedBy,
ScreeningId = audit.ScreeningId,
RawDataRef = rawDataRef
};
try
{
_dbContext.participantAuditLogs.Add(auditLog);
var rowsAffected = await _dbContext.SaveChangesAsync();
if (rowsAffected <= 0)
{
_logger.LogError(
"SaveChangesAsync reported 0 rows affected for CorrelationId {CorrelationId}.",
audit.CorrelationId);
return;
}
_logger.LogInformation(
"Audit written | Source: {Source} | Correlation: {CorrelationId} | Rows: {Rows}",
audit.Source, audit.CorrelationId, rowsAffected);
}
catch (Exception ex)
{
_logger.LogError(ex,
"Failed to persist audit log for CorrelationId {CorrelationId}.",
audit.CorrelationId);
}
}
private async Task<string?> WriteSnapshotToBlobAsync(ParticipantAuditMessage message)
{
if (message.RequestSnapshot is null)
{
return null;
}
var blobPath = $"{message.CreatedDatetime:dd-MM-yyyy}/{message.CorrelationId}.json";
var payload = JsonSerializer.SerializeToUtf8Bytes(message.RequestSnapshot, JsonOptions);
var blobFile = new BlobFile(payload, blobPath);
try
{
var uri = await _blobStorageHelper.UploadFileToBlobStorageAndGetUri(
_config.AzureWebJobsStorage,
AuditBlobContainer,
blobFile,
overwrite: true);
if (uri is null)
{
_logger.LogError(
"Blob write returned null URI for CorrelationId {CorrelationId}.",
message.CorrelationId);
throw new InvalidOperationException(
$"Blob write returned null URI for CorrelationId {message.CorrelationId}.");
}
return uri;
}
catch (Exception ex)
{
_logger.LogError(
ex,
"Failed to write audit snapshot to blob for CorrelationId {CorrelationId}.",
message.CorrelationId);
throw;
}
}
}