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 all 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,22 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Design; | ||
| using ServiceLayer.Mesh.Data; | ||
|
|
||
| namespace ParticipantManager.API.Data; | ||
|
|
||
| public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ServiceLayerDbContext> | ||
| { | ||
| public ServiceLayerDbContext CreateDbContext(string[] args) | ||
| { | ||
| var connectionString = Environment.GetEnvironmentVariable("DatabaseConnectionString"); | ||
| if (string.IsNullOrEmpty(connectionString)) | ||
| { | ||
| throw new InvalidOperationException("Connection string 'DatabaseConnectionString' is not configured."); | ||
| } | ||
|
|
||
| var optionsBuilder = new DbContextOptionsBuilder<ServiceLayerDbContext>(); | ||
| optionsBuilder.UseSqlServer(connectionString); | ||
|
|
||
| return new ServiceLayerDbContext(optionsBuilder.Options); | ||
| } | ||
| } |
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,69 @@ | ||
| 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("BSSMailBox") | ||
| ?? throw new InvalidOperationException($"Environment variable 'BSSMailBox' 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 | ||
| .AnyAsync(f => f.FileId == messageId); | ||
|
|
||
| if (!existing) | ||
| { | ||
| _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(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
src/ServiceLayer.Mesh/Migrations/20250512113115_InitialCreate.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
src/ServiceLayer.Mesh/Migrations/20250512113115_InitialCreate.cs
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,39 @@ | ||
| using System; | ||
| using Microsoft.EntityFrameworkCore.Migrations; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace ServiceLayer.Mesh.Migrations | ||
| { | ||
| /// <inheritdoc /> | ||
| public partial class InitialCreate : Migration | ||
| { | ||
| /// <inheritdoc /> | ||
| protected override void Up(MigrationBuilder migrationBuilder) | ||
| { | ||
| migrationBuilder.CreateTable( | ||
| name: "MeshFiles", | ||
| columns: table => new | ||
| { | ||
| FileId = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false), | ||
| FileType = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false), | ||
| MailboxId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false), | ||
| Status = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false), | ||
| BlobPath = table.Column<string>(type: "nvarchar(1024)", maxLength: 1024, nullable: true), | ||
| FirstSeenUtc = table.Column<DateTime>(type: "datetime2", nullable: false), | ||
| LastUpdatedUtc = table.Column<DateTime>(type: "datetime2", nullable: false) | ||
| }, | ||
| constraints: table => | ||
| { | ||
| table.PrimaryKey("PK_MeshFiles", x => x.FileId); | ||
| }); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void Down(MigrationBuilder migrationBuilder) | ||
| { | ||
| migrationBuilder.DropTable( | ||
| name: "MeshFiles"); | ||
| } | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
src/ServiceLayer.Mesh/Migrations/ServiceLayerDbContextModelSnapshot.cs
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,63 @@ | ||
| // <auto-generated /> | ||
| using System; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||
| using Microsoft.EntityFrameworkCore.Metadata; | ||
| using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
| using ServiceLayer.Mesh.Data; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace ServiceLayer.Mesh.Migrations | ||
| { | ||
| [DbContext(typeof(ServiceLayerDbContext))] | ||
| partial class ServiceLayerDbContextModelSnapshot : ModelSnapshot | ||
| { | ||
| protected override void BuildModel(ModelBuilder modelBuilder) | ||
| { | ||
| #pragma warning disable 612, 618 | ||
| modelBuilder | ||
| .HasAnnotation("ProductVersion", "9.0.1") | ||
| .HasAnnotation("Relational:MaxIdentifierLength", 128); | ||
|
|
||
| SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); | ||
|
|
||
| modelBuilder.Entity("ServiceLayer.Mesh.Models.MeshFile", b => | ||
| { | ||
| b.Property<string>("FileId") | ||
| .HasMaxLength(255) | ||
| .HasColumnType("nvarchar(255)"); | ||
|
|
||
| b.Property<string>("BlobPath") | ||
| .HasMaxLength(1024) | ||
| .HasColumnType("nvarchar(1024)"); | ||
|
|
||
| b.Property<string>("FileType") | ||
| .IsRequired() | ||
| .HasMaxLength(50) | ||
| .HasColumnType("nvarchar(50)"); | ||
|
|
||
| b.Property<DateTime>("FirstSeenUtc") | ||
| .HasColumnType("datetime2"); | ||
|
|
||
| b.Property<DateTime>("LastUpdatedUtc") | ||
| .HasColumnType("datetime2"); | ||
|
|
||
| b.Property<string>("MailboxId") | ||
| .IsRequired() | ||
| .HasMaxLength(50) | ||
| .HasColumnType("nvarchar(50)"); | ||
|
|
||
| b.Property<string>("Status") | ||
| .IsRequired() | ||
| .HasMaxLength(20) | ||
| .HasColumnType("nvarchar(20)"); | ||
|
|
||
| b.HasKey("FileId"); | ||
|
|
||
| b.ToTable("MeshFiles"); | ||
| }); | ||
| #pragma warning restore 612, 618 | ||
| } | ||
| } | ||
| } |
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,20 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
|
|
||
| namespace ServiceLayer.Mesh.Models; | ||
|
|
||
| public class MeshFile | ||
| { | ||
| [MaxLength(255)] | ||
| public required string FileId { get; set; } | ||
| [MaxLength(50)] | ||
| public required MeshFileType FileType { get; set; } | ||
| [MaxLength(50)] | ||
| public required string MailboxId { get; set; } | ||
| [MaxLength(20)] | ||
| public required MeshFileStatus Status { get; set; } | ||
| [MaxLength(1024)] | ||
| 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 | ||
| } |
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.