|
| 1 | +using Azure.Identity; |
| 2 | +using Azure.Storage.Queues; |
1 | 3 | using Microsoft.Azure.Functions.Worker; |
2 | 4 | using Microsoft.EntityFrameworkCore; |
3 | 5 | using Microsoft.Extensions.Logging; |
4 | 6 | using NHS.MESH.Client.Contracts.Services; |
5 | 7 | using ServiceLayer.Mesh.Data; |
| 8 | +using ServiceLayer.Mesh.Models; |
6 | 9 |
|
7 | 10 | namespace ServiceLayer.Mesh.Functions |
8 | 11 | { |
@@ -33,16 +36,38 @@ public async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer) |
33 | 36 | // dotnet-mesh-client needs to be updated to support pagination for when inbox containers more than 500 messages |
34 | 37 | } |
35 | 38 |
|
36 | | - foreach (var message in response.Response.Messages) |
| 39 | + foreach (var messageId in response.Response.Messages) |
37 | 40 | { |
38 | 41 | // Check if message has been seen before |
39 | | - var doesFileIdExist = await _serviceLayerDbContext.MeshFiles.AnyAsync(m => m.FileId == message.ToString()); |
| 42 | + var doesFileIdExist = await _serviceLayerDbContext.MeshFiles.AnyAsync(m => m.FileId == messageId.ToString()); |
40 | 43 |
|
| 44 | + if (!doesFileIdExist) |
| 45 | + { |
| 46 | + var meshFile = new MeshFile() |
| 47 | + { |
| 48 | + FileId = messageId, |
| 49 | + FileType = "", |
| 50 | + MailboxId = Environment.GetEnvironmentVariable("MailboxId"), |
| 51 | + Status = "Discovered" |
| 52 | + }; |
41 | 53 |
|
42 | | - // If no then insert into db |
43 | | - // Then enqueue message |
| 54 | + _serviceLayerDbContext.MeshFiles.Add(meshFile); |
44 | 55 |
|
45 | | - // If no then do nothing |
| 56 | + QueueClient queueClient; |
| 57 | + |
| 58 | + if (Environment.GetEnvironmentVariable("ASPNETCORE_ENV") == "Dev") |
| 59 | + { |
| 60 | + queueClient = new QueueClient("UseDevelopmentStorage=true", "my-local-queue"); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + var credential = new ManagedIdentityCredential(); |
| 65 | + queueClient = new QueueClient(new Uri(Environment.GetEnvironmentVariable("QueueUrl")), credential); |
| 66 | + } |
| 67 | + |
| 68 | + queueClient.CreateIfNotExists(); |
| 69 | + queueClient.SendMessage(messageId); |
| 70 | + } |
46 | 71 | } |
47 | 72 |
|
48 | 73 | if (myTimer.ScheduleStatus is not null) |
|
0 commit comments