This repository was archived by the owner on Jul 28, 2025. It is now read-only.
generated from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: adding discovery function #6
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f0fc26c
feat: adding template code for discover function
SamTyrrellNHS 941066e
feat: Discovery function checking mesh for files
SamTyrrellNHS 44f4dbb
feat: Check if mesh message id exists in db table
alex-clayton-1 d8b5ade
feat: adding push to queue
SamTyrrellNHS 6823b6e
feat: Saves changes after adding MeshFile to db context
alex-clayton-1 39f44c7
chore: Removed extra gitignore and readme files
alex-clayton-1 bfd4eb5
feat: adding unit tests and dependancy injection
SamTyrrellNHS dc26489
feat: couple more unit tests
SamTyrrellNHS 80b6045
feat: removing code not needed
SamTyrrellNHS 078f2cc
feat: changed db entity property mapping and added environment variab…
SamTyrrellNHS 626e3b7
style: Fixing formatting issues
alex-clayton-1 f37e1f2
style: Replaced tabs with spaces in sln file
alex-clayton-1 b55e36b
fix: testing fix for static analysis issue
SamTyrrellNHS ca58cb0
Merge branch 'feat/AddingDiscoveryFunction' of https://github.com/NHS…
SamTyrrellNHS a4fb3bc
feat: Set up DesignTimeDbContextFactory and created initial migration
alex-clayton-1 bc24b70
feat: responding to feedback
SamTyrrellNHS 08175ef
Merge branch 'feat/AddingDiscoveryFunction' of https://github.com/NHS…
SamTyrrellNHS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [submodule "src/dotnet-mesh-client"] | ||
| # path = src/Shared/dotnet-mesh-client | ||
| path = src/dotnet-mesh-client | ||
| url = https://github.com/NHSDigital/dotnet-mesh-client.git | ||
| branch = main | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using ServiceLayer.Mesh.Models; | ||
|
|
||
| namespace ServiceLayer.Mesh.Data; | ||
|
|
||
| public class ServiceLayerDbContext(DbContextOptions<ServiceLayerDbContext> options) : DbContext(options) | ||
| { | ||
| public DbSet<MeshFile> MeshFiles { get; set; } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| // Configure relationships, keys, etc. | ||
| modelBuilder.Entity<MeshFile>().HasKey(p => p.FileId); | ||
| modelBuilder.Entity<MeshFile>().Property(e => e.Status).HasConversion<string>(); | ||
| modelBuilder.Entity<MeshFile>().Property(e => e.FileType).HasConversion<string>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using Azure.Storage.Queues; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Logging; | ||
| using NHS.MESH.Client.Contracts.Services; | ||
| using ServiceLayer.Mesh.Data; | ||
| using ServiceLayer.Mesh.Models; | ||
|
|
||
| namespace ServiceLayer.Mesh.Functions | ||
| { | ||
| public class DiscoveryFunction | ||
| { | ||
| private readonly ILogger _logger; | ||
| private readonly IMeshInboxService _meshInboxService; | ||
| private readonly ServiceLayerDbContext _serviceLayerDbContext; | ||
| private readonly QueueClient _queueClient; | ||
|
|
||
| public DiscoveryFunction(ILogger<DiscoveryFunction> logger, IMeshInboxService meshInboxService, ServiceLayerDbContext serviceLayerDbContext, QueueClient queueClient) | ||
| { | ||
| _logger = logger; | ||
| _meshInboxService = meshInboxService; | ||
| _serviceLayerDbContext = serviceLayerDbContext; | ||
| _queueClient = queueClient; | ||
| } | ||
|
|
||
| [Function("DiscoveryFunction")] | ||
| public async Task Run([TimerTrigger("%DiscoveryTimerExpression%")] TimerInfo myTimer) | ||
| { | ||
| _logger.LogInformation($"DiscoveryFunction started at: {DateTime.Now}"); | ||
|
|
||
| var mailboxId = Environment.GetEnvironmentVariable("MailboxId") | ||
| ?? throw new InvalidOperationException($"Environment variable 'MailboxId' is not set or is empty."); | ||
|
|
||
| var response = await _meshInboxService.GetMessagesAsync(mailboxId); | ||
|
|
||
| _queueClient.CreateIfNotExists(); | ||
|
|
||
| foreach (var messageId in response.Response.Messages) | ||
| { | ||
| using var transaction = await _serviceLayerDbContext.Database.BeginTransactionAsync(); | ||
|
|
||
| var existing = await _serviceLayerDbContext.MeshFiles | ||
| .AsNoTracking() | ||
| .FirstOrDefaultAsync(f => f.FileId == messageId); | ||
SamTyrrellNHS marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (existing == null) | ||
| { | ||
| _serviceLayerDbContext.MeshFiles.Add(new MeshFile | ||
| { | ||
| FileId = messageId, | ||
| FileType = MeshFileType.NbssAppointmentEvents, | ||
| MailboxId = mailboxId, | ||
| Status = MeshFileStatus.Discovered, | ||
| FirstSeenUtc = DateTime.UtcNow, | ||
| LastUpdatedUtc = DateTime.UtcNow | ||
| }); | ||
|
|
||
| await _serviceLayerDbContext.SaveChangesAsync(); | ||
| await transaction.CommitAsync(); | ||
SamTyrrellNHS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| _queueClient.SendMessage(messageId); | ||
SamTyrrellNHS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| else | ||
| { | ||
| await transaction.RollbackAsync(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
|
|
||
| namespace ServiceLayer.Mesh.Models; | ||
|
|
||
| public class MeshFile | ||
| { | ||
| public required string FileId { get; set; } | ||
| public required MeshFileType FileType { get; set; } | ||
| public required string MailboxId { get; set; } | ||
| public required MeshFileStatus Status { get; set; } | ||
| public string? BlobPath { get; set; } | ||
| public DateTime FirstSeenUtc { get; set; } | ||
| public DateTime LastUpdatedUtc { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace ServiceLayer.Mesh.Models; | ||
|
|
||
| public enum MeshFileStatus | ||
| { | ||
| Discovered, | ||
| Extracting, | ||
| Extracted, | ||
| Transforming, | ||
| Transformed, | ||
| FailedExtract, | ||
| FailedTransform | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace ServiceLayer.Mesh.Models; | ||
|
|
||
| public enum MeshFileType | ||
| { | ||
| NbssAppointmentEvents | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using Microsoft.Azure.Functions.Worker.Builder; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Azure.Storage.Queues; | ||
| using Azure.Identity; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using NHS.MESH.Client; | ||
| using ServiceLayer.Mesh.Data; | ||
|
|
||
| var host = new HostBuilder() | ||
| .ConfigureFunctionsWebApplication() | ||
| .ConfigureServices(services => | ||
| { | ||
| // MESH Client config | ||
| services | ||
| .AddMeshClient(_ => _.MeshApiBaseUrl = Environment.GetEnvironmentVariable("MeshApiBaseUrl")) | ||
| .AddMailbox(Environment.GetEnvironmentVariable("BSSMailBox"), new NHS.MESH.Client.Configuration.MailboxConfiguration | ||
SamTyrrellNHS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Password = Environment.GetEnvironmentVariable("MeshPassword"), | ||
| SharedKey = Environment.GetEnvironmentVariable("MeshSharedKey"), | ||
| }).Build(); | ||
|
|
||
| // EF Core DbContext | ||
| services.AddDbContext<ServiceLayerDbContext>(options => | ||
| { | ||
| var connectionString = Environment.GetEnvironmentVariable("DatabaseConnectionString"); | ||
| if (string.IsNullOrEmpty(connectionString)) | ||
| throw new InvalidOperationException("The connection string has not been initialized."); | ||
|
|
||
| options.UseSqlServer(connectionString); | ||
| }); | ||
|
|
||
| // Register QueueClient as singleton | ||
| services.AddSingleton(provider => | ||
| { | ||
| var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | ||
| var queueUrl = Environment.GetEnvironmentVariable("QueueUrl"); | ||
SamTyrrellNHS marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (string.IsNullOrWhiteSpace(queueUrl)) | ||
| throw new InvalidOperationException("QueueUrl environment variable is not set."); | ||
|
|
||
| if (environment == "Development") | ||
| { | ||
| return new QueueClient("UseDevelopmentStorage=true", "my-local-queue"); | ||
| } | ||
| else | ||
| { | ||
| var credential = new ManagedIdentityCredential(); | ||
| return new QueueClient(new Uri(queueUrl), credential); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| // Application Insights isn't enabled by default. See https://aka.ms/AAt8mw4. | ||
| // builder.Services | ||
| // .AddApplicationInsightsTelemetryWorkerService() | ||
| // .ConfigureFunctionsApplicationInsights(); | ||
|
|
||
| var app = host.Build(); | ||
| await app.RunAsync(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "profiles": { | ||
| "ServiceLayer.Mesh": { | ||
| "commandName": "Project", | ||
| "commandLineArgs": "--port 7142", | ||
| "launchBrowser": false | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <AzureFunctionsVersion>v4</AzureFunctionsVersion> | ||
| <OutputType>Exe</OutputType> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
| <PackageReference Include="Azure.Identity" Version="1.13.2" /> | ||
| <PackageReference Include="Azure.Storage.Queues" Version="12.22.0" /> | ||
| <!-- Application Insights isn't enabled by default. See https://aka.ms/AAt8mw4. --> | ||
| <!-- <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" /> --> | ||
| <!-- <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" /> --> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.1" /> | ||
| <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.1" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.1" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Update="host.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="local.settings.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| <CopyToPublishDirectory>Never</CopyToPublishDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" /> | ||
| <ProjectReference Include="..\dotnet-mesh-client\application\DotNetMeshClient\NHS.Mesh.Client\NHS.Mesh.Client.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "version": "2.0", | ||
| "logging": { | ||
| "applicationInsights": { | ||
| "samplingSettings": { | ||
| "isEnabled": true, | ||
| "excludedTypes": "Request" | ||
| }, | ||
| "enableLiveMetricsFilters": true | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule dotnet-mesh-client
added at
4e8846
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.