test(event-subscriber): project zaak-created notifications into the read projection (refs #7)
Failing unit + acceptance tests for the Event Subscriber's NotificationProjector: a zaken/zaak/create notification yields one INGEDIEND projection row, duplicate deliveries collapse to one row, non-zaak/non-create notifications are ignored, and a rebuild repopulates the projection from the durable notification log (PRD §8.4). The projector is a no-op stub so the tests compile and fail on the assertions; the implementation follows in the green commit. The notification log doubles as the idempotency guard and rebuild source so a rebuild needs no OpenZaak access (§8.1). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace EventSubscriber.Application;
|
||||
|
||||
/// <summary>
|
||||
/// An inbound NRC (Open Notificaties) notification, as Open Notificaties POSTs it to an
|
||||
/// abonnement callback. Only the fields the projection needs are modelled; the full ZGW
|
||||
/// "Notificatie" resource also carries <c>aanmaakdatum</c> and <c>kenmerken</c> which the
|
||||
/// minimal projection ignores (bsn is deferred — see ADR-0008). For a <c>zaken</c>/<c>zaak</c>/<c>create</c>
|
||||
/// notification <c>hoofdObject</c> and <c>resourceUrl</c> are both the created zaak's URL.
|
||||
/// </summary>
|
||||
public sealed record Notification(
|
||||
string Kanaal,
|
||||
string Resource,
|
||||
string Actie,
|
||||
Uri ResourceUrl)
|
||||
{
|
||||
/// <summary>The only event the walking-skeleton projection reacts to: a zaak being created.</summary>
|
||||
public bool IsZaakCreated =>
|
||||
Kanaal == "zaken" && Resource == "zaak" && Actie == "create";
|
||||
|
||||
/// <summary>The zaak UUID — the trailing path segment of the resource URL — used as the projection key.</summary>
|
||||
public string ZaakId => ResourceUrl.Segments[^1].Trim('/');
|
||||
|
||||
/// <summary>
|
||||
/// A deterministic dedup key. Open Notificaties carries no notification id and may
|
||||
/// redeliver, so the key is derived from the immutable notification content: two
|
||||
/// deliveries of the same zaak-create collapse to one. (NRC may also deliver
|
||||
/// out of order; the projector tolerates that — order does not change the outcome.)
|
||||
/// </summary>
|
||||
public string IdempotencyKey => $"{Kanaal}:{Resource}:{Actie}:{ResourceUrl}";
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace EventSubscriber.Application;
|
||||
|
||||
/// <summary>
|
||||
/// Projects inbound NRC notifications into the read projection. Tolerates duplicate and
|
||||
/// out-of-order deliveries (CLAUDE.md §8.6): the notification log dedups, and the projection
|
||||
/// upsert is idempotent on the zaak id. Rebuilds the projection by replaying the log.
|
||||
/// </summary>
|
||||
public sealed class NotificationProjector(INotificationLog log, IProjectionStore store)
|
||||
{
|
||||
/// <summary>Handle one inbound notification. Ignores anything that is not a zaak-created event.</summary>
|
||||
public Task HandleAsync(Notification notification, CancellationToken ct = default)
|
||||
// RED: not yet implemented — the smallest change to make the tests pass comes next.
|
||||
=> Task.CompletedTask;
|
||||
|
||||
/// <summary>Rebuild the projection from the durable notification log (PRD §8.4).</summary>
|
||||
public Task RebuildAsync(CancellationToken ct = default)
|
||||
// RED: not yet implemented.
|
||||
=> Task.CompletedTask;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace EventSubscriber.Application;
|
||||
|
||||
/// <summary>
|
||||
/// The durable log of notifications the subscriber has accepted. It is both the idempotency
|
||||
/// guard (a replayed notification is recognised and dropped) and the rebuild source: the
|
||||
/// projection is a derived artefact (PRD §8.4) regenerated by replaying this log, so a rebuild
|
||||
/// needs no access to OpenZaak (CLAUDE.md §8.1). Implemented in Infrastructure over Postgres.
|
||||
/// </summary>
|
||||
public interface INotificationLog
|
||||
{
|
||||
/// <summary>
|
||||
/// Record a notification. Returns <c>true</c> if it was newly recorded, <c>false</c> if an
|
||||
/// entry with the same key already existed (a duplicate delivery). Must be atomic so that
|
||||
/// concurrent duplicate deliveries cannot both observe <c>true</c>.
|
||||
/// </summary>
|
||||
Task<bool> TryRecordAsync(RecordedNotification notification, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Every accepted notification, for rebuilding the projection.</summary>
|
||||
Task<IReadOnlyList<RecordedNotification>> AllAsync(CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>A notification that has been accepted, retaining what a rebuild needs to recompute its projection row.</summary>
|
||||
public sealed record RecordedNotification(string Key, string Actie, string ZaakId);
|
||||
|
||||
/// <summary>The read projection store. Owned by the projection bounded context (ADR-0008); the
|
||||
/// subscriber writes to it and the projection-api reads it.</summary>
|
||||
public interface IProjectionStore
|
||||
{
|
||||
/// <summary>Insert or update the row for <see cref="RegisterEntry.Id"/>. Idempotent on the id.</summary>
|
||||
Task UpsertAsync(RegisterEntry entry, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Remove every row — the first step of a rebuild.</summary>
|
||||
Task ClearAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>Every projection row (used by tests and the rebuild verification).</summary>
|
||||
Task<IReadOnlyList<RegisterEntry>> AllAsync(CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace EventSubscriber.Application;
|
||||
|
||||
/// <summary>
|
||||
/// A row of the rebuildable read projection (PRD §8.4). For the minimal slice it carries
|
||||
/// the zaak id and a status; <c>Bsn</c> and <c>NaamPlaceholder</c> are part of the schema but
|
||||
/// are deferred — the NRC notification does not carry them and the subscriber may not read
|
||||
/// OpenZaak directly (CLAUDE.md §8.1). Populating them is a follow-up (ADR-0008).
|
||||
/// </summary>
|
||||
public sealed record RegisterEntry(
|
||||
string Id,
|
||||
string Status,
|
||||
string? Bsn = null,
|
||||
string? NaamPlaceholder = null);
|
||||
|
||||
/// <summary>The projection statuses the walking skeleton knows about.</summary>
|
||||
public static class RegistrationStatus
|
||||
{
|
||||
/// <summary>A zaak has been created; the registration is submitted.</summary>
|
||||
public const string Ingediend = "INGEDIEND";
|
||||
}
|
||||
Reference in New Issue
Block a user