-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNemsSubscriptionManager.cs
More file actions
406 lines (357 loc) · 15.4 KB
/
NemsSubscriptionManager.cs
File metadata and controls
406 lines (357 loc) · 15.4 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
namespace NHS.CohortManager.DemographicServices;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Model;
using Common;
using DataServices.Core;
using System.Security.Cryptography.X509Certificates;
using Hl7.Fhir.Serialization;
// Use explicit STU3 aliases to avoid R4 conflicts
using STU3Subscription = Hl7.Fhir.Model.Subscription;
using STU3Meta = Hl7.Fhir.Model.Meta;
using STU3ContactPoint = Hl7.Fhir.Model.ContactPoint;
using System.Text.RegularExpressions;
public class NemsSubscriptionManager
{
private readonly INemsHttpClientFunction _httpClient;
private readonly ILogger<NemsSubscriptionManager> _logger;
private readonly ManageNemsSubscriptionConfig _config;
private readonly IDataServiceAccessor<NemsSubscription> _nemsSubscriptionAccessor;
private readonly X509Certificate2 _nemsCertificate; // injected!
public NemsSubscriptionManager(
INemsHttpClientFunction httpClient,
IOptions<ManageNemsSubscriptionConfig> config,
ILogger<NemsSubscriptionManager> logger,
IDataServiceAccessor<NemsSubscription> nemsSubscriptionAccessor,
X509Certificate2 nemsCertificate)
{
_httpClient = httpClient;
_config = config.Value;
_logger = logger;
_nemsSubscriptionAccessor = nemsSubscriptionAccessor;
_nemsCertificate = nemsCertificate;
}
/// <summary>
/// Looks up the subscription ID for a given NHS number in the database
/// </summary>
/// <param name="nhsNumber">The NHS number to look up.</param>
/// <returns>
/// The subscription ID if found, otherwise null.
/// </returns>
public async Task<string?> LookupSubscriptionIdAsync(string nhsNumber)
{
try
{
var subscription = await _nemsSubscriptionAccessor.GetSingle(i => i.NhsNumber == long.Parse(nhsNumber));
if (subscription != null)
{
return subscription.SubscriptionId;
}
return null;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to lookup subscription ID");
return null;
}
}
/// <summary>
/// Deletes a subscription from the NEMS API using the provided subscription ID.
/// </summary>
/// <param name="subscriptionId">The subscription ID to delete.</param>
/// <returns>
/// True if deletion was successful, otherwise false.
/// </returns>
public async Task<bool> DeleteSubscriptionFromNemsAsync(string subscriptionId)
{
try
{
// Generate JWT token for delete operation - use base URL for audience
var baseUri = new Uri(_config.NemsFhirEndpoint);
var baseUrl = $"{baseUri.Scheme}://{baseUri.Host}";
var jwtToken = _httpClient.GenerateJwtToken(
_config.NemsFromAsid,
baseUrl,
"patient/Subscription.write"
);
string deleteUrl = $"{_config.NemsFhirEndpoint}/Subscription/{subscriptionId}";
var bypassCert = _config.NemsBypassServerCertificateValidation;
var deleteRequest = new NemsSubscriptionRequest
{
Url = deleteUrl,
JwtToken = jwtToken,
FromAsid = _config.NemsFromAsid,
ToAsid = _config.NemsToAsid,
ClientCertificate = _nemsCertificate,
BypassCertValidation = bypassCert
};
var response = await _httpClient.SendSubscriptionDelete(deleteRequest, _config.NemsHttpClientTimeoutSeconds);
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to delete subscription ID {SubscriptionId}", subscriptionId);
return false;
}
}
/// <summary>
/// Sends the given subscription to NEMS
/// </summary>
/// <param name="subscriptionJson">The serialised subscription object</param>
/// <returns>
/// The subscription ID if successful, otherwise null.
/// </returns>
public async Task<string?> SendSubscriptionToNemsAsync(string subscriptionJson)
{
try
{
var baseUrl = _config.NemsFhirEndpoint.Replace("/STU3", "");
var jwtToken = _httpClient.GenerateJwtToken(
_config.NemsFromAsid,
baseUrl,
"patient/Subscription.write"
);
var url = $"{_config.NemsFhirEndpoint}/Subscription";
var bypassCert = _config.NemsBypassServerCertificateValidation;
var postRequest = new NemsSubscriptionPostRequest
{
Url = url,
SubscriptionJson = subscriptionJson,
JwtToken = jwtToken,
FromAsid = _config.NemsFromAsid,
ToAsid = _config.NemsToAsid,
ClientCertificate = _nemsCertificate,
BypassCertValidation = bypassCert
};
var response = await _httpClient.SendSubscriptionPost(postRequest, _config.NemsHttpClientTimeoutSeconds);
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
_logger.LogWarning("NEMS returned error response: {Response}", errorContent);
// Handle known duplicate error
if (errorContent.Contains("DUPLICATE_REJECTED", StringComparison.OrdinalIgnoreCase))
{
var match = Regex.Match(
errorContent,
@"subscription already exists\s*:\s*""?([a-zA-Z0-9-_]+)""?",
RegexOptions.None,
TimeSpan.FromMilliseconds(100)
);
if (match.Success)
{
var existingId = match.Groups[1].Value;
_logger.LogWarning("Subscription already exists in Spine with ID: {Id}", existingId);
return existingId;
}
_logger.LogWarning("Duplicate response received but failed to parse existing subscription ID.");
}
_logger.LogError("Failed to create NEMS subscription. Status: {StatusCode}, Response: {Response}",
response.StatusCode, errorContent);
return null;
}
if (response.Headers.Location == null)
{
_logger.LogWarning("Subscription created but no Location header found in response");
return null;
}
var locationPath = response.Headers.Location.ToString();
var subscriptionId = locationPath.Split('/').LastOrDefault();
_logger.LogInformation("Successfully created NEMS subscription with ID: {SubscriptionId}", subscriptionId);
return subscriptionId;
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception occurred while sending subscription to NEMS");
return null;
}
}
/// <summary>
/// Deletes a subscription from the NEMS subscriptions table in the cohort manager database.
/// </summary>
/// <param name="nhsNumber">The NHS number associated with the subscription to delete.</param>
/// <returns>
/// A boolean indicating whether the deletion was successful.
/// </returns>
public async Task<bool> DeleteSubscriptionFromDatabaseAsync(string nhsNumberStr)
{
try
{
_logger.LogInformation("Deleting subscription from database");
long nhsNumber = long.Parse(nhsNumberStr);
bool deleted = await _nemsSubscriptionAccessor.Remove(i => i.NhsNumber == nhsNumber);
return deleted;
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception occurred while deleting the subscription");
return false;
}
}
/// <summary>
/// Saves a subscription in the NEMS subscriptions table in the cohort manager database.
/// </summary>
/// <param name="nhsNumber">The NHS number associated with the subscription.</param>
/// <param name="subscriptionId">The subscription ID to save.</param>
/// <returns>
/// A boolean indicating whether the subscription was successfully saved.
/// </returns>
public async Task<bool> SaveSubscriptionInDatabase(string nhsNumber, string subscriptionId)
{
try
{
_logger.LogInformation("Start saving the SubscriptionId in the database.");
var subscription = new NemsSubscription
{
SubscriptionId = subscriptionId,
NhsNumber = Convert.ToInt64(nhsNumber),
RecordInsertDateTime = DateTime.UtcNow,
SubscriptionSource = SubscriptionSource.NEMS
};
bool subscriptionCreated = await _nemsSubscriptionAccessor.InsertSingle(subscription);
return subscriptionCreated;
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception occurred while saving the subscription in the database");
return false;
}
}
/// <summary>
/// Creates a new subscription object for the NEMS API using proper FHIR STU3 types.
/// </summary>
/// <param name="nhsNumber">The NHS number to create the subscription for.</param>
/// <param name="eventType">The event type to subscribe to (e.g., pds-record-change-1)</param>
/// <returns>
/// A STU3 FHIR Subscription object
/// </returns>
public STU3Subscription CreateSubscription(string nhsNumber, string eventType = "pds-record-change-1")
{
var subscription = new STU3Subscription
{
Meta = new STU3Meta
{
Profile = new[] { _config.NemsSubscriptionProfile },
LastUpdated = DateTimeOffset.UtcNow
},
Status = STU3Subscription.SubscriptionStatus.Requested,
Reason = $"Subscribe to {eventType} events for patient",
Criteria = $"/Bundle?type=message&Patient.identifier={_config.NemsSubscriptionCriteria}|{nhsNumber}&MessageHeader.event={eventType}",
Channel = new STU3Subscription.ChannelComponent
{
Type = STU3Subscription.SubscriptionChannelType.Message, // Use 'message' for MESH delivery
Endpoint = _config.NemsMeshMailboxId, // Use MESH mailbox ID, not ODS code
Payload = "application/fhir+json"
}
};
_logger.LogInformation("Creating subscription with endpoint: {Endpoint}", subscription.Channel.Endpoint);
subscription.Contact = new List<STU3ContactPoint>
{
new STU3ContactPoint
{
System = STU3ContactPoint.ContactPointSystem.Url,
Value = $"https://directory.spineservices.nhs.uk/STU3/Organization/{_config.NemsOdsCode}",
Use = STU3ContactPoint.ContactPointUse.Work
}
};
return subscription;
}
/// <summary>
/// Serializes a subscription object to JSON for sending to NEMS using proper FHIR serialisation
/// </summary>
/// <param name="subscription">The subscription to serialise</param>
/// <returns>JSON string representation of the subscription</returns>
public static string SerialiseSubscription(STU3Subscription subscription)
{
var serialiser = new FhirJsonSerializer();
return serialiser.SerializeToString(subscription);
}
/// <summary>
/// Creates and sends a subscription to NEMS, then saves it to the database.
/// Returns a SubscriptionResult with the subscription ID or error details.
/// </summary>
/// <param name="nhsNumber">NHS number to subscribe to</param>
/// <param name="eventType">Event type to subscribe to</param>
/// <returns>SubscriptionResult with success status and subscription ID or error message</returns>
public async Task<SubscriptionResult> CreateAndSendSubscriptionAsync(string nhsNumber, string eventType = "pds-record-change-1")
{
try
{
// Check if subscription already exists
var existingSubscriptionId = await LookupSubscriptionIdAsync(nhsNumber);
if (!string.IsNullOrEmpty(existingSubscriptionId))
{
_logger.LogInformation("Subscription already exists with ID {SubscriptionId}",
existingSubscriptionId);
return SubscriptionResult.CreateSuccess(existingSubscriptionId);
}
// Create subscription object using proper FHIR STU3 types
var subscription = CreateSubscription(nhsNumber, eventType);
// Serialize to JSON using FHIR serializer
var subscriptionJson = SerialiseSubscription(subscription);
// Send to NEMS
var subscriptionId = await SendSubscriptionToNemsAsync(subscriptionJson);
if (string.IsNullOrEmpty(subscriptionId))
{
return SubscriptionResult.CreateFailure("Failed to create subscription in NEMS");
}
// Save to database
var saved = await SaveSubscriptionInDatabase(nhsNumber, subscriptionId);
if (saved)
{
_logger.LogInformation("Successfully created and saved subscription");
return SubscriptionResult.CreateSuccess(subscriptionId);
}
_logger.LogError("Failed to save subscription to database");
// Cleanup: delete from NEMS since database save failed
await DeleteSubscriptionFromNemsAsync(subscriptionId);
return SubscriptionResult.CreateFailure("Failed to save subscription to database");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create and send subscription");
return SubscriptionResult.CreateFailure("An error occurred while creating subscription");
}
}
/// <summary>
/// Removes a subscription both from NEMS and the local database
/// </summary>
/// <param name="nhsNumber">NHS number to unsubscribe</param>
/// <returns>True if successful, false otherwise</returns>
public async Task<bool> RemoveSubscriptionAsync(string nhsNumber)
{
try
{
// Look up subscription ID
var subscriptionId = await LookupSubscriptionIdAsync(nhsNumber);
if (string.IsNullOrEmpty(subscriptionId))
{
_logger.LogWarning("No subscription found in database");
return false;
}
// Delete from NEMS first
var nemsDeleted = await DeleteSubscriptionFromNemsAsync(subscriptionId);
// Delete from database regardless of NEMS result (cleanup)
var dbDeleted = await DeleteSubscriptionFromDatabaseAsync(nhsNumber);
if (nemsDeleted && dbDeleted)
{
_logger.LogInformation("Successfully removed subscription");
return true;
}
else
{
_logger.LogWarning("Partial removal completed. NEMS: {NemsDeleted}, DB: {DbDeleted}",
nemsDeleted, dbDeleted);
return false;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to remove subscription");
return false;
}
}
}