Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 641b030

Browse files
ianfnelsonalex-clayton-1SamTyrrellNHS
authored
feat: DTOSS-8739 Extract function (#9)
Co-authored-by: alex-clayton-1 <[email protected]> Co-authored-by: Tyrrellion <[email protected]>
1 parent f597484 commit 641b030

28 files changed

+891
-187
lines changed

.env.example

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# Database Configuration
22
DATABASE_USER=SA
33
DATABASE_PASSWORD=YourStrong@Passw0rd
4-
DATABASE_NAME=PathwayCoordinator
4+
DATABASE_NAME=ServiceLayer
55
DATABASE_HOST=db
66
AZURE_WEB_JOBS_STORAGE=UseDevelopmentStorage=true
77
DatabaseConnectionString=Server=${DATABASE_HOST};Database=${DATABASE_NAME};User Id=${DATABASE_USER};Password=${DATABASE_PASSWORD};TrustServerCertificate=True
88
AzureWebJobsStorage=UseDevelopmentStorage=true
99
FUNCTIONS_WORKER_RUNTIME=dotnet-isolated
10-
MailboxId=X26ABC1
1110
MeshSharedKey=TestKey
1211
MeshPassword=password
13-
BSSMailBox=X26ABC1
12+
NbssMailboxId=X26ABC1
1413
MeshApiBaseUrl=http://localhost:8700/messageexchange
1514
ASPNETCORE_ENVIRONMENT=Development
16-
DiscoveryTimerExpression=*/5 * * * *
15+
FileDiscoveryTimerExpression=*/5 * * * *
1716
QueueUrl=http://127.0.0.1:10001
17+
FileExtractQueueName=file-extract
18+
FileTransformQueueName=file-transform
1819

1920
# API Configuration
2021
API_PORT=7071
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public class AppConfiguration :
4+
IFileDiscoveryFunctionConfiguration,
5+
IFileExtractFunctionConfiguration,
6+
IFileExtractQueueClientConfiguration,
7+
IFileTransformQueueClientConfiguration
8+
{
9+
public string NbssMeshMailboxId => GetRequired("NbssMailboxId");
10+
11+
public string FileExtractQueueName => GetRequired("FileExtractQueueName");
12+
13+
public string FileTransformQueueName => GetRequired("FileTransformQueueName");
14+
15+
private static string GetRequired(string key)
16+
{
17+
var value = Environment.GetEnvironmentVariable(key);
18+
19+
if (string.IsNullOrEmpty(value))
20+
{
21+
throw new InvalidOperationException($"Environment variable '{key}' is not set or is empty.");
22+
}
23+
24+
return value;
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public interface IFileDiscoveryFunctionConfiguration
4+
{
5+
string NbssMeshMailboxId { get; }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public interface IFileExtractFunctionConfiguration
4+
{
5+
string NbssMeshMailboxId { get; }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public interface IFileExtractQueueClientConfiguration
4+
{
5+
string FileExtractQueueName { get; }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ServiceLayer.Mesh.Configuration;
2+
3+
public interface IFileTransformQueueClientConfiguration
4+
{
5+
string FileTransformQueueName { get; }
6+
}

src/ServiceLayer.Mesh/Data/DesignTimeDbContextFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.Design;
3-
using ServiceLayer.Mesh.Data;
43

5-
namespace ParticipantManager.API.Data;
4+
namespace ServiceLayer.Mesh.Data;
65

76
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ServiceLayerDbContext>
87
{
File renamed without changes.

src/ServiceLayer.Mesh/Functions/DiscoveryFunction.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Microsoft.Azure.Functions.Worker;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Logging;
4+
using NHS.MESH.Client.Contracts.Services;
5+
using ServiceLayer.Mesh.Configuration;
6+
using ServiceLayer.Mesh.Data;
7+
using ServiceLayer.Mesh.Messaging;
8+
using ServiceLayer.Mesh.Models;
9+
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)
18+
{
19+
[Function("FileDiscoveryFunction")]
20+
public async Task Run([TimerTrigger("%FileDiscoveryTimerExpression%")] TimerInfo myTimer)
21+
{
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)
27+
{
28+
await using var transaction = await serviceLayerDbContext.Database.BeginTransactionAsync();
29+
30+
var existing = await serviceLayerDbContext.MeshFiles
31+
.AnyAsync(f => f.FileId == messageId);
32+
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+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)