diff --git a/skills/notification-center/SKILL.md b/skills/notification-center/SKILL.md new file mode 100644 index 0000000..39b7ab0 --- /dev/null +++ b/skills/notification-center/SKILL.md @@ -0,0 +1,17 @@ +--- +name: notification-center +description: Implement and accept the authenticated Heimdall in-app notification center. +--- + +# Notification center + +The header bell is rendered by `Components/Shared/NotificationBadge.razor`; it displays the unread count delivered by `NotificationsHubClient` and must navigate to `/notifications`. The page reads the signed-in user's list through `HeimdallApiClient` and the existing `GET /api/notifications` endpoint. Read actions use the existing per-user `POST /api/notifications/{id}/read` and `POST /api/notifications/mark-all-read` endpoints, then refresh the hub count. Keep authorization on the page and user scoping in the API/application handlers; never accept a user id from the browser. + +## Acceptance + +1. Build `Heimdall.Web` with the SDK from `global.json` and run focused Web tests. +2. In disposable Central + PostgreSQL Compose, create only synthetic notifications for the disposable admin, open `/notifications` from the header bell, confirm notification content appears and `/events` is not loaded. +3. Mark one item read and all items read; verify displayed state and unread badge refresh. Confirm empty and API failure states render. +4. Use unique Compose project, generated credentials, loopback port, and project-owned image tag. Follow `compose-disposable-recipe` exactly: remove only that project and exact local tag, verify project containers/volumes/networks and image tag are absent, and retain shared images. + +The empty and failed API states have focused bUnit coverage in `NotificationsPageTests`. Simulate the API error with an HTTP 500 response for `GET /api/notifications`; taking PostgreSQL offline also breaks server-side session validation before the page can handle the notification request. diff --git a/src/Heimdall.Web/Components/Pages/NotificationsPage.razor b/src/Heimdall.Web/Components/Pages/NotificationsPage.razor new file mode 100644 index 0000000..1ea34a7 --- /dev/null +++ b/src/Heimdall.Web/Components/Pages/NotificationsPage.razor @@ -0,0 +1,88 @@ +@page "/notifications" +@using Heimdall.Application.DTOs +@attribute [Authorize] +@layout MainLayout +@inject HeimdallApiClient Api +@inject NotificationsHubClient Hub +@inject UiText Text +@inject ISnackbar Snackbar + +@Text["NotificationsPage_Title"] +@if (_loading) +{ + +} +else if (_failed) +{ + @Text["NotificationsPage_Failed"] +} +else if (_items.Count == 0) +{ + @Text["NotificationsPage_Empty"] +} +else +{ +
+ + @Text["NotificationsPage_MarkAllRead"] + +
+ @foreach (var item in _items) + { + +
+
+ @item.Title + @item.Message + @item.CreatedAt.ToLocalTime().ToString("g") +
+ @if (!item.IsRead) + { + @Text["NotificationsPage_MarkRead"] + } +
+
+ } +} + +@code { + private List _items = []; + private bool _loading = true; + private bool _failed; + + protected override async Task OnInitializedAsync() => await LoadAsync(); + + private async Task LoadAsync() + { + _loading = true; + _failed = false; + try + { + var result = await Api.GetNotificationsAsync(); + _items = result?.Items.ToList() ?? []; + _failed = result is null; + } + catch (HttpRequestException) { _failed = true; } + finally { _loading = false; } + } + + private async Task MarkReadAsync(Guid id) + { + if (await Api.MarkNotificationAsReadAsync(id)) + { + await LoadAsync(); + await Hub.RefreshUnreadCountAsync(); + } + else Snackbar.Add(Text["NotificationsPage_UpdateFailed"], Severity.Error); + } + + private async Task MarkAllReadAsync() + { + if (await Api.MarkAllNotificationsAsReadAsync()) + { + await LoadAsync(); + await Hub.RefreshUnreadCountAsync(); + } + else Snackbar.Add(Text["NotificationsPage_UpdateFailed"], Severity.Error); + } +} diff --git a/src/Heimdall.Web/Components/Shared/NotificationBadge.razor b/src/Heimdall.Web/Components/Shared/NotificationBadge.razor index c38e9fa..2e3f787 100644 --- a/src/Heimdall.Web/Components/Shared/NotificationBadge.razor +++ b/src/Heimdall.Web/Components/Shared/NotificationBadge.razor @@ -10,7 +10,7 @@ Visible="@(Hub.UnreadCount > 0)"> + OnClick="@(() => Navigation.NavigateTo("/notifications"))" /> diff --git a/src/Heimdall.Web/Localization/SharedResources.fr.resx b/src/Heimdall.Web/Localization/SharedResources.fr.resx index a5572f2..9dcebe2 100644 --- a/src/Heimdall.Web/Localization/SharedResources.fr.resx +++ b/src/Heimdall.Web/Localization/SharedResources.fr.resx @@ -4133,4 +4133,10 @@ Confirmer le lien L’observation de plaque et son événement sont maintenant liés au véhicule sélectionné. Aucun véhicule correspondant trouvé. L’observation reste sans lien. + Notifications + Impossible de charger les notifications. Réessayez. + Vous n’avez aucune notification. + Tout marquer comme lu + Marquer comme lu + Impossible de mettre à jour la notification. diff --git a/src/Heimdall.Web/Localization/SharedResources.resx b/src/Heimdall.Web/Localization/SharedResources.resx index 7260461..bf1fd06 100644 --- a/src/Heimdall.Web/Localization/SharedResources.resx +++ b/src/Heimdall.Web/Localization/SharedResources.resx @@ -4133,4 +4133,10 @@ Confirmer le lien L’observation de plaque et son événement sont maintenant liés au véhicule sélectionné. Aucun véhicule correspondant trouvé. L’observation reste sans lien. + Notifications + Notifications could not be loaded. Try again. + You have no notifications. + Mark all as read + Mark as read + The notification could not be updated. diff --git a/src/Heimdall.Web/Services/HeimdallApiClient.cs b/src/Heimdall.Web/Services/HeimdallApiClient.cs index 0b65b7c..99a4e16 100644 --- a/src/Heimdall.Web/Services/HeimdallApiClient.cs +++ b/src/Heimdall.Web/Services/HeimdallApiClient.cs @@ -1037,6 +1037,25 @@ public class HeimdallApiClient : IDisposable // ─── Notification Config ───────────────────────────────────────── + public async Task?> GetNotificationsAsync(int page = 1, int pageSize = 20) + { + var client = CreateClient(); + return await client.GetFromJsonAsync>( + $"api/notifications?page={page}&pageSize={pageSize}", Json); + } + + public async Task MarkNotificationAsReadAsync(Guid id) + { + using var response = await CreateClient().PostAsync($"api/notifications/{id}/read", content: null); + return response.IsSuccessStatusCode; + } + + public async Task MarkAllNotificationsAsReadAsync() + { + using var response = await CreateClient().PostAsync("api/notifications/mark-all-read", content: null); + return response.IsSuccessStatusCode; + } + public async Task GetNotificationConfigAsync() { var client = CreateClient(); diff --git a/tests/Heimdall.Web.Tests/Components/Pages/NotificationsPageTests.cs b/tests/Heimdall.Web.Tests/Components/Pages/NotificationsPageTests.cs new file mode 100644 index 0000000..3e8e947 --- /dev/null +++ b/tests/Heimdall.Web.Tests/Components/Pages/NotificationsPageTests.cs @@ -0,0 +1,32 @@ +using Bunit; +using Bunit.TestDoubles; +using Heimdall.Application.DTOs; +using Heimdall.Web.Components.Pages; +using Shouldly; + +namespace Heimdall.Web.Tests.Components.Pages; + +public sealed class NotificationsPageTests : PageTestBase +{ + [Fact] + public void NotificationsPage_EmptyList_ShowsEmptyState() + { + Handler.OnGet("api/notifications", new PagedResult([], 0, 1, 20)); + LoginAs(); + + var cut = RenderPage(); + + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Vous n’avez aucune notification.")); + } + + [Fact] + public void NotificationsPage_ApiFailure_ShowsErrorState() + { + Handler.OnGet("api/notifications", System.Net.HttpStatusCode.InternalServerError); + LoginAs(); + + var cut = RenderPage(); + + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Impossible de charger les notifications. Réessayez.")); + } +}