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 3
Expand file tree
/
Copy pathFileDiscoveryFunction.cs
More file actions
71 lines (58 loc) · 2.33 KB
/
Copy pathFileDiscoveryFunction.cs
File metadata and controls
71 lines (58 loc) · 2.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
using Microsoft.Azure.Functions.Worker;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NHS.MESH.Client.Contracts.Services;
using ServiceLayer.Data;
using ServiceLayer.Data.Models;
using ServiceLayer.Mesh.Configuration;
using ServiceLayer.Mesh.Messaging;
namespace ServiceLayer.Mesh.Functions;
public class FileDiscoveryFunction(
ILogger<FileDiscoveryFunction> logger,
IFileDiscoveryFunctionConfiguration configuration,
IMeshInboxService meshInboxService,
ServiceLayerDbContext serviceLayerDbContext,
IFileExtractQueueClient fileExtractQueueClient)
: MeshFileFunctionBase(serviceLayerDbContext)
{
[Function("FileDiscoveryFunction")]
public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo myTimer)
{
logger.LogInformation("{FunctionName} started.", nameof(FileDiscoveryFunction));
var response = await meshInboxService.GetMessagesAsync(configuration.NbssMeshMailboxId);
// TODO - check if response.IsSuccessful before proceeding to dereference the Response.Messages
foreach (var messageId in response.Response.Messages)
{
await using var transaction = await ServiceLayerDbContext.Database.BeginTransactionAsync();
var existing = await ServiceLayerDbContext.MeshFiles
.AnyAsync(f => f.FileId == messageId);
if (!existing)
{
var file = await CreateMeshFile(messageId);
await transaction.CommitAsync();
await fileExtractQueueClient.EnqueueFileExtractAsync(file);
}
else
{
await transaction.RollbackAsync();
}
}
}
private async Task<MeshFile> CreateMeshFile(string messageId)
{
var now = DateTime.UtcNow;
var file = new MeshFile
{
FileId = messageId,
FileType = MeshFileType.NbssAppointmentEvents,
MailboxId = configuration.NbssMeshMailboxId,
Status = MeshFileStatus.Discovered,
FirstSeenUtc = now,
LastUpdatedUtc = now
};
ServiceLayerDbContext.MeshFiles.Add(file);
await UpdateMeshFile(file, MeshFileStatus.Discovered);
return file;
}
protected override FileEventSource Source => FileEventSource.DiscoveryFunction;
}