|
7 | 7 | using ServiceLayer.Mesh.Configuration; |
8 | 8 | using ServiceLayer.Mesh.Messaging; |
9 | 9 |
|
10 | | -namespace ServiceLayer.Mesh.Functions |
| 10 | +namespace ServiceLayer.Mesh.Functions; |
| 11 | + |
| 12 | +public class FileDiscoveryFunction( |
| 13 | + ILogger<FileDiscoveryFunction> logger, |
| 14 | + IFileDiscoveryFunctionConfiguration configuration, |
| 15 | + IMeshInboxService meshInboxService, |
| 16 | + ServiceLayerDbContext serviceLayerDbContext, |
| 17 | + IFileExtractQueueClient fileExtractQueueClient) |
11 | 18 | { |
12 | | - public class FileDiscoveryFunction( |
13 | | - ILogger<FileDiscoveryFunction> logger, |
14 | | - IFileDiscoveryFunctionConfiguration configuration, |
15 | | - IMeshInboxService meshInboxService, |
16 | | - ServiceLayerDbContext serviceLayerDbContext, |
17 | | - IFileExtractQueueClient fileExtractQueueClient) |
| 19 | + [Function("FileDiscoveryFunction")] |
| 20 | + public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo myTimer) |
18 | 21 | { |
19 | | - [Function("FileDiscoveryFunction")] |
20 | | - public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo myTimer) |
| 22 | + logger.LogInformation("{functionName} started at: {time}", nameof(FileDiscoveryFunction), DateTime.UtcNow); |
| 23 | + |
| 24 | + var response = await meshInboxService.GetMessagesAsync(configuration.NbssMeshMailboxId); |
| 25 | + |
| 26 | + foreach (var messageId in response.Response.Messages) |
21 | 27 | { |
22 | | - logger.LogInformation("{functionName} started at: {time}", nameof(FileDiscoveryFunction), DateTime.UtcNow); |
| 28 | + await using var transaction = await serviceLayerDbContext.Database.BeginTransactionAsync(); |
23 | 29 |
|
24 | | - var response = await meshInboxService.GetMessagesAsync(configuration.NbssMeshMailboxId); |
| 30 | + var existing = await serviceLayerDbContext.MeshFiles |
| 31 | + .AnyAsync(f => f.FileId == messageId); |
25 | 32 |
|
26 | | - foreach (var messageId in response.Response.Messages) |
| 33 | + if (!existing) |
27 | 34 | { |
28 | | - await using var transaction = await serviceLayerDbContext.Database.BeginTransactionAsync(); |
| 35 | + var file = new MeshFile |
| 36 | + { |
| 37 | + FileId = messageId, |
| 38 | + FileType = MeshFileType.NbssAppointmentEvents, |
| 39 | + MailboxId = configuration.NbssMeshMailboxId, |
| 40 | + Status = MeshFileStatus.Discovered, |
| 41 | + FirstSeenUtc = DateTime.UtcNow, |
| 42 | + LastUpdatedUtc = DateTime.UtcNow |
| 43 | + }; |
29 | 44 |
|
30 | | - var existing = await serviceLayerDbContext.MeshFiles |
31 | | - .AnyAsync(f => f.FileId == messageId); |
| 45 | + serviceLayerDbContext.MeshFiles.Add(file); |
32 | 46 |
|
33 | | - if (!existing) |
34 | | - { |
35 | | - var file = new MeshFile |
36 | | - { |
37 | | - FileId = messageId, |
38 | | - FileType = MeshFileType.NbssAppointmentEvents, |
39 | | - MailboxId = configuration.NbssMeshMailboxId, |
40 | | - Status = MeshFileStatus.Discovered, |
41 | | - FirstSeenUtc = DateTime.UtcNow, |
42 | | - LastUpdatedUtc = DateTime.UtcNow |
43 | | - }; |
44 | | - |
45 | | - serviceLayerDbContext.MeshFiles.Add(file); |
46 | | - |
47 | | - await serviceLayerDbContext.SaveChangesAsync(); |
48 | | - await transaction.CommitAsync(); |
49 | | - |
50 | | - await fileExtractQueueClient.EnqueueFileExtractAsync(file); |
51 | | - } |
52 | | - else |
53 | | - { |
54 | | - await transaction.RollbackAsync(); |
55 | | - } |
| 47 | + await serviceLayerDbContext.SaveChangesAsync(); |
| 48 | + await transaction.CommitAsync(); |
| 49 | + |
| 50 | + await fileExtractQueueClient.EnqueueFileExtractAsync(file); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + await transaction.RollbackAsync(); |
56 | 55 | } |
57 | 56 | } |
58 | 57 | } |
|
0 commit comments