From 8abc0eca13841b620db2428211cc2a3fa6a720a8 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sun, 6 Sep 2026 11:04:29 -0400 Subject: [PATCH] feat(central-ui): Notification report accepts ?correlationId= and opens the notification's detail Claude-Session: https://claude.ai/code/session_01NcMG6xXdsJbmD2wYAgkK29 --- .../Notifications/NotificationReport.razor | 71 +++++++++- .../Pages/NotificationReportPageTests.cs | 110 ++++++++++++++- .../Pages/QueryStringDrillInTests.cs | 132 ++++++++++++++++++ 3 files changed, 310 insertions(+), 3 deletions(-) diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Notifications/NotificationReport.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Notifications/NotificationReport.razor index 5af9ffb2..09f29ef5 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Notifications/NotificationReport.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Notifications/NotificationReport.razor @@ -5,7 +5,9 @@ @using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories @using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification @using ZB.MOM.WW.ScadaBridge.Communication +@using Microsoft.AspNetCore.WebUtilities @inject CommunicationService CommunicationService +@inject NavigationManager Navigation @inject ISiteRepository SiteRepository @inject SiteScopeService SiteScope @inject IDialogService Dialog @@ -88,6 +90,20 @@ + @if (_notificationIdFilter is { } notifFilter) + { + @* Register row 111 (D2): read-only exact-match pill seeded from the + ?correlationId= deep link (Flow Debugger / Audit Log). Cleared by + its × (re-queries) or by Clear. *@ +
+ Notification @ShortId(notifFilter) + +
+ }
@@ -439,6 +455,11 @@ private DateTime? _fromFilter; private DateTime? _toFilter; + // Register row 111 (D2): exact-match NotificationId seeded from ?correlationId=. + // Not bound to a control — surfaced as a read-only pill in the filter card. + private string? _notificationIdFilter; + private const int MaxNotificationIdFilterLength = 64; + protected override async Task OnInitializedAsync() { try @@ -457,7 +478,52 @@ Logger.LogWarning(ex, "Failed to load sites for the report source-site filter."); } + // Seed the ?correlationId= filter BEFORE the first fetch so the initial grid + // load is already narrowed to the linked notification. + ApplyQueryStringFilters(); + await RefreshAll(); + + // Deep link landed on exactly the row it named — open its detail so the + // operator does not have to find and double-click it. + if (_notificationIdFilter is { } wanted + && _notifications is { Count: 1 } + && _notifications[0].NotificationId == wanted) + { + await ShowDetail(_notifications[0]); + } + } + + /// + /// Pre-apply the ?correlationId= deep-link filter from the URL query + /// string (Flow Debugger External-lane node / Audit Log convention). The value + /// is the notification's own id (a string, not a GUID) — accepted trimmed when + /// non-blank and at most chars; + /// anything else is silently dropped, leaving the page unfiltered. + /// + private void ApplyQueryStringFilters() + { + var uri = Navigation.ToAbsoluteUri(Navigation.Uri); + var query = QueryHelpers.ParseQuery(uri.Query); + if (query.Count == 0) + { + return; + } + + if (query.TryGetValue("correlationId", out var values)) + { + var candidate = values.ToString()?.Trim(); + if (!string.IsNullOrEmpty(candidate) && candidate.Length <= MaxNotificationIdFilterLength) + { + _notificationIdFilter = candidate; + } + } + } + + private async Task ClearNotificationFilter() + { + _notificationIdFilter = null; + await Search(); } private async Task RefreshAll() @@ -491,7 +557,8 @@ To: ToUtc(_toFilter), PageNumber: _pageNumber, PageSize: _pageSize, - SourceNodeFilter: NullIfEmpty(_nodeFilter)); + SourceNodeFilter: NullIfEmpty(_nodeFilter), + NotificationIdFilter: _notificationIdFilter); var response = await CommunicationService.QueryNotificationOutboxAsync(request); if (response.Success) @@ -693,9 +760,11 @@ _stuckOnly = false; _fromFilter = null; _toFilter = null; + _notificationIdFilter = null; } private bool HasActiveFilters => + _notificationIdFilter != null || !string.IsNullOrEmpty(_statusFilter) || !string.IsNullOrEmpty(_typeFilter) || !string.IsNullOrEmpty(_siteFilter) || diff --git a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/NotificationReportPageTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/NotificationReportPageTests.cs index bb6a97b1..a8f58006 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/NotificationReportPageTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/NotificationReportPageTests.cs @@ -1,6 +1,8 @@ using System.Security.Claims; using Akka.Actor; using Bunit; +using Bunit.TestDoubles; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; @@ -46,7 +48,9 @@ public class NotificationReportPageTests : BunitContext DeliveredAt: DateTimeOffset.UtcNow.AddHours(-2), IsStuck: false), }, TotalCount: 2); - // Records the most recent retry/discard requests the actor received. + // Records every outbox query the page issued (filter assertions) and the + // most recent retry/discard requests the actor received. + private readonly List _queryRequests = new(); private readonly List _retryRequests = new(); private readonly List _discardRequests = new(); @@ -237,6 +241,91 @@ public class NotificationReportPageTests : BunitContext Assert.Contains("View audit history", link.TextContent); } + // ───────────────────────────────────────────────────────────────────────── + // Flow Debugger follow-ups (register row 111, plan D2): ?correlationId= + // seeds a read-only Notification pill filter, rides the query as + // NotificationIdFilter, and auto-opens the detail when exactly one row + // with that id comes back. + // ───────────────────────────────────────────────────────────────────────── + + [Fact] + public void NotificationReport_CorrelationIdQuery_SeedsTheNotificationFilterAndAutoOpensDetail() + { + const string id = "notif-aaaaaaaa-1111"; + _queryReply = new NotificationOutboxQueryResponse( + "q", true, null, new List + { + new(id, "Email", "Ops On-Call", "Pump fault at Plant-A", + "Parked", RetryCount: 3, LastError: "SMTP timeout", SourceSiteId: "plant-a", + SourceInstanceId: "Pump-001", CreatedAt: DateTimeOffset.UtcNow.AddMinutes(-30), + DeliveredAt: null, IsStuck: true), + }, TotalCount: 1); + + var nav = (BunitNavigationManager)Services.GetRequiredService(); + nav.NavigateTo($"/notifications/report?correlationId={id}"); + + var cut = Render(); + + cut.WaitForAssertion(() => + { + // The very first fetch already carries the exact-match filter. + Assert.Single(_queryRequests); + Assert.Equal(id, _queryRequests[0].NotificationIdFilter); + + // The read-only pill names the notification by its short id. + var pill = cut.Find("[data-test='notif-filter-notification']"); + Assert.Contains("Notification", pill.TextContent); + Assert.Contains(id[..12], pill.TextContent); + + // Exactly one matching row came back → the detail modal is open on it. + var modal = cut.Find(".modal.show"); + Assert.Contains("Pump fault at Plant-A", modal.TextContent); + }); + } + + [Fact] + public void ClearFilters_DropsTheNotificationFilter() + { + var nav = (BunitNavigationManager)Services.GetRequiredService(); + nav.NavigateTo("/notifications/report?correlationId=notif-aaaaaaaa-1111"); + + var cut = Render(); + cut.WaitForState(() => cut.FindAll("[data-test='notif-filter-notification']").Count == 1); + + // The pill alone makes the filter set "active" — Clear is enabled. + var clear = cut.FindAll("button").First(b => b.TextContent.Trim() == "Clear"); + Assert.False(clear.HasAttribute("disabled")); + + clear.Click(); + + cut.WaitForAssertion(() => + { + Assert.Empty(cut.FindAll("[data-test='notif-filter-notification']")); + var clearAfter = cut.FindAll("button").First(b => b.TextContent.Trim() == "Clear"); + Assert.True(clearAfter.HasAttribute("disabled")); + }); + } + + [Fact] + public void NotificationFilterPill_Clear_DropsTheFilterAndRequeries() + { + var nav = (BunitNavigationManager)Services.GetRequiredService(); + nav.NavigateTo("/notifications/report?correlationId=notif-aaaaaaaa-1111"); + + var cut = Render(); + cut.WaitForState(() => cut.FindAll("[data-test='notif-filter-notification-clear']").Count == 1); + + cut.Find("[data-test='notif-filter-notification-clear']").Click(); + + cut.WaitForAssertion(() => + { + Assert.Empty(cut.FindAll("[data-test='notif-filter-notification']")); + // The × re-runs the query without the id filter. + Assert.Equal(2, _queryRequests.Count); + Assert.Null(_queryRequests[1].NotificationIdFilter); + }); + } + protected override void Dispose(bool disposing) { if (disposing) @@ -254,7 +343,24 @@ public class NotificationReportPageTests : BunitContext { public ScriptedOutboxActor(NotificationReportPageTests test) { - Receive(_ => Sender.Tell(test._queryReply)); + Receive(req => + { + test._queryRequests.Add(req); + Sender.Tell(test._queryReply with { CorrelationId = req.CorrelationId }); + }); + // The ?correlationId= auto-open path fetches the row's full detail; + // reply with a minimal detail so the modal settles instead of the + // Ask timing out. + Receive(r => Sender.Tell(new NotificationDetailResponse( + r.CorrelationId, true, null, + new NotificationDetail( + r.NotificationId, "Email", "Ops On-Call", "Pump fault at Plant-A", + "Body", "Parked", RetryCount: 3, LastError: "SMTP timeout", + ResolvedTargets: null, TypeData: null, SourceSiteId: "plant-a", + SourceInstanceId: "Pump-001", SourceScript: null, + SiteEnqueuedAt: DateTimeOffset.UtcNow.AddMinutes(-31), + CreatedAt: DateTimeOffset.UtcNow.AddMinutes(-30), + LastAttemptAt: null, NextAttemptAt: null, DeliveredAt: null)))); Receive(r => { test._retryRequests.Add(r); diff --git a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/QueryStringDrillInTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/QueryStringDrillInTests.cs index de1864aa..1c8cc84b 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/QueryStringDrillInTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/QueryStringDrillInTests.cs @@ -19,10 +19,12 @@ using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport; using ZB.MOM.WW.ScadaBridge.Commons.Messages.Audit; +using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification; using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport; using ZB.MOM.WW.ScadaBridge.Communication; using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase; using ZB.MOM.WW.ScadaBridge.Transport; +using NotificationReportPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Notifications.NotificationReport; using SiteCallsReportPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.SiteCalls.SiteCallsReport; using TransportImportPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Design.TransportImport; @@ -104,6 +106,75 @@ public sealed class QueryStringDrillInTests }); } + // ----------------------------------------------------------------- + // SiteCallsReport — ?correlationId= (Flow Debugger / Audit Log deep link, D2) + // ----------------------------------------------------------------- + + [Fact] + public void SiteCallsReport_CorrelationIdQuery_SeedsTheOperationFilter() + { + // A well-formed GUID rides on the first query as the exact-match + // TrackedOperationIdFilter and renders the read-only Operation pill. + var op = Guid.Parse("44444444-4444-4444-4444-444444444444"); + using var ctx = new SiteCallsReportFixture(); + var nav = (BunitNavigationManager)ctx.Services.GetRequiredService(); + nav.NavigateTo($"/site-calls/report?correlationId={op}"); + + var cut = ctx.Render(); + + cut.WaitForAssertion(() => + { + Assert.Single(ctx.QueryRequests); + Assert.Equal(op, ctx.QueryRequests[0].TrackedOperationIdFilter); + var pill = cut.Find("[data-test='site-calls-filter-operation']"); + Assert.Contains(op.ToString("N")[..12], pill.TextContent); + }); + } + + [Fact] + public void SiteCallsReport_CorrelationIdQuery_Malformed_IsIgnored() + { + // Lax parsing, like ?status= — a value that is not a GUID leaves the + // filter null and renders no pill, so a hand-crafted bad URL loads the + // page unfiltered instead of breaking it. + using var ctx = new SiteCallsReportFixture(); + var nav = (BunitNavigationManager)ctx.Services.GetRequiredService(); + nav.NavigateTo("/site-calls/report?correlationId=nope"); + + var cut = ctx.Render(); + + cut.WaitForAssertion(() => + { + Assert.Single(ctx.QueryRequests); + Assert.Null(ctx.QueryRequests[0].TrackedOperationIdFilter); + }); + Assert.Empty(cut.FindAll("[data-test='site-calls-filter-operation']")); + } + + // ----------------------------------------------------------------- + // NotificationReport — ?correlationId= (Flow Debugger follow-ups, D2) + // ----------------------------------------------------------------- + + [Fact] + public void NotificationReport_CorrelationIdQuery_Blank_IsIgnored() + { + // A blank/whitespace ?correlationId= is dropped: the first query carries + // no NotificationIdFilter and no pill renders, so a hand-crafted empty + // deep link degrades to the plain unfiltered page. + using var ctx = new NotificationReportFixture(); + var nav = (BunitNavigationManager)ctx.Services.GetRequiredService(); + nav.NavigateTo("/notifications/report?correlationId=%20"); + + var cut = ctx.Render(); + + cut.WaitForAssertion(() => + { + Assert.Single(ctx.QueryRequests); + Assert.Null(ctx.QueryRequests[0].NotificationIdFilter); + Assert.Empty(cut.FindAll("[data-test='notif-filter-notification']")); + }); + } + // ----------------------------------------------------------------- // TransportImport — no query-string parameters on this route // ----------------------------------------------------------------- @@ -209,6 +280,67 @@ public sealed class QueryStringDrillInTests } } + private sealed class NotificationReportFixture : BunitContext + { + private readonly ActorSystem _system = ActorSystem.Create("qs-drillin-notif-tests"); + public readonly CommunicationService Comms; + public readonly List QueryRequests = new(); + + public NotificationReportFixture() + { + Comms = new CommunicationService( + Options.Create(new CommunicationOptions()), + NullLogger.Instance); + + var outbox = _system.ActorOf(Props.Create(() => new ScriptedNotificationOutboxActor(this))); + Comms.SetNotificationOutbox(outbox); + + Services.AddSingleton(Comms); + Services.AddSingleton(new AlwaysConfirmDialogService()); + + var siteRepo = Substitute.For(); + siteRepo.GetAllSitesAsync(Arg.Any()) + .Returns(Task.FromResult>(new List + { + new("Plant A", "plant-a") { Id = 1 }, + })); + Services.AddSingleton(siteRepo); + + var claims = new[] + { + new Claim(JwtTokenService.UsernameClaimType, "tester"), + new Claim(JwtTokenService.RoleClaimType, "Deployer"), + }; + var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth")); + Services.AddSingleton(new TestAuthStateProvider(user)); + Services.AddAuthorizationCore(); + Services.AddScoped(); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _system.Terminate().Wait(TimeSpan.FromSeconds(5)); + } + base.Dispose(disposing); + } + } + + private sealed class ScriptedNotificationOutboxActor : ReceiveActor + { + public ScriptedNotificationOutboxActor(NotificationReportFixture fixture) + { + Receive(req => + { + fixture.QueryRequests.Add(req); + Sender.Tell(new NotificationOutboxQueryResponse( + req.CorrelationId, true, null, + new List(), TotalCount: 0)); + }); + } + } + private sealed class AlwaysConfirmDialogService : IDialogService { public Task ConfirmAsync(string title, string message, bool danger = false, string? confirmLabel = null)