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 2
Expand file tree
/
Copy pathFileDiscoveryFunction.cs
More file actions
59 lines (51 loc) · 2.2 KB
/
FileDiscoveryFunction.cs
File metadata and controls
59 lines (51 loc) · 2.2 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
using Microsoft.Azure.Functions.Worker;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NHS.MESH.Client.Contracts.Services;
using ServiceLayer.Mesh.Configuration;
using ServiceLayer.Mesh.Data;
using ServiceLayer.Mesh.Messaging;
using ServiceLayer.Mesh.Models;
namespace ServiceLayer.Mesh.Functions
{
public class FileDiscoveryFunction(
ILogger<FileDiscoveryFunction> logger,
IFileDiscoveryFunctionConfiguration configuration,
IMeshInboxService meshInboxService,
ServiceLayerDbContext serviceLayerDbContext,
IFileExtractQueueClient fileExtractQueueClient)
{
[Function("FileDiscoveryFunction")]
public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo myTimer)
{
logger.LogInformation("{functionName} started at: {time}", nameof(FileDiscoveryFunction), DateTime.UtcNow);
var response = await meshInboxService.GetMessagesAsync(configuration.NbssMeshMailboxId);
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 = new MeshFile
{
FileId = messageId,
FileType = MeshFileType.NbssAppointmentEvents,
MailboxId = configuration.NbssMeshMailboxId,
Status = MeshFileStatus.Discovered,
FirstSeenUtc = DateTime.UtcNow,
LastUpdatedUtc = DateTime.UtcNow
};
serviceLayerDbContext.MeshFiles.Add(file);
await serviceLayerDbContext.SaveChangesAsync();
await transaction.CommitAsync();
await fileExtractQueueClient.EnqueueFileExtractAsync(file);
}
else
{
await transaction.RollbackAsync();
}
}
}
}
}