refactor: delete shadow contracts DTOs, parse the generated shape for real (Step 2/8)

dashboard-view.dto.ts and brp-address.dto.ts each shadowed a generated
type: DashboardViewDto was declared twice (hand-written with required
fields, generated with everything optional), reconciled only by
structural typing. Both are gone.

dashboard-view.adapter.ts now imports the generated DashboardViewDto/
RegistrationDto/PersonDto/RegistrationStatusDto directly. Its parse
does real work now instead of an identity copy: parseRegistrationStatus
validates each status variant's required fields per-tag (the generated
type flattens the union, so a Geregistreerd row missing
herregistratieDatum previously passed the boundary unnoticed — it no
longer does). HerregistratieDecisions moves from contracts/ to
domain/registration.ts, so no contracts-typed value reaches a page.

brp.adapter.ts drops its own BrpAddressDto shadow the same way.

Hand-written contracts/*.dto.ts count: 4 -> 2 (duo-diplomas.dto.ts and
stamdata.dto.ts remain — both parse fields codegen can't type at all).

Part of the dashboard-readability refactor (see the approved plan).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
eho
2026-09-04 14:36:30 +02:00
co-authored by Claude Opus 5
parent 194cccfd02
commit 42e7a1e927
7 changed files with 134 additions and 118 deletions
@@ -1,6 +1,6 @@
import { Injectable, inject, resource } from '@angular/core';
import { Result, ok, err } from '@shared/kernel/fp';
import { BrpAddressDto } from '@registratie/contracts/brp-address.dto';
import { BrpAddressDto } from '@shared/infrastructure/api-client';
import { ApiClient } from '@shared/infrastructure/api-client';
/**
@@ -35,4 +35,43 @@ describe('parseDashboardView (trust boundary)', () => {
parseDashboardView({ ...valid, decisions: { eligibleForHerregistratie: 'yes' } }).ok,
).toBe(false);
});
it('rejects a status whose tag is present but its required fields are missing', () => {
// The generated RegistrationStatusDto flattens the union — every field is
// optional, so a wire bug (a Geregistreerd row with no herregistratieDatum)
// must be caught here, not by the compiler.
expect(
parseDashboardView({
...valid,
registration: { ...valid.registration, status: { tag: 'Geregistreerd' } },
}).ok,
).toBe(false);
expect(
parseDashboardView({
...valid,
registration: {
...valid.registration,
status: { tag: 'Geschorst', geschorstTot: '2027-01-01' }, // missing reden
},
}).ok,
).toBe(false);
});
it('rejects an unknown status tag', () => {
expect(
parseDashboardView({
...valid,
registration: { ...valid.registration, status: { tag: 'Ingetrokken' } },
}).ok,
).toBe(false);
});
it('rejects a person with an incomplete adres', () => {
expect(
parseDashboardView({
...valid,
person: { ...valid.person, adres: { straat: 'X 1' } },
}).ok,
).toBe(false);
});
});
@@ -1,18 +1,24 @@
import { Injectable, inject, resource } from '@angular/core';
import { Result, ok, err } from '@shared/kernel/fp';
import {
ApiClient,
DashboardViewDto,
RegistrationDto,
RegistrationStatusDto,
PersonDto,
} from '@shared/infrastructure/api-client';
import {
Registration,
RegistrationStatus,
HerregistratieDecisions,
} from '@registratie/contracts/dashboard-view.dto';
import { Registration } from '@registratie/domain/registration';
} from '@registratie/domain/registration';
import { Person } from '@registratie/domain/person';
import { BigProfile } from '@registratie/domain/big-profile';
import { ApiClient } from '@shared/infrastructure/api-client';
/**
* The parsed, frontend-side view: the wire DTO mapped onto our own domain model.
* Lives HERE, not in contracts/, because it references domain types — contracts
* stays import-free. This split is the decoupling seam (CLAUDE.md §1, ADR-0001).
* Lives HERE, not in a `contracts/*.dto.ts` file, because it references domain
* types — that split is the decoupling seam (CLAUDE.md §1, ADR-0001).
*/
export interface DashboardView {
profile: BigProfile;
@@ -31,17 +37,80 @@ export class DashboardViewAdapter {
// The value is still untrusted JSON — parseDashboardView validates it at the
// boundary and maps DTO → domain before the app uses it.
//
// SEAM (G5): retry-with-backoff for non-mutating reads wraps the loader here —
// e.g. `loader: () => withBackoff(() => this.client.dashboardView())` — since the
// adapter is the single place HTTP lives. Reads are safe to retry; MUTATING calls
// (the submit-* commands) must NEVER auto-retry — and don't. Manual retry
// (resource.reload via <app-async>) covers the UX today, so backoff stays unbuilt.
dashboardViewResource() {
return resource({ loader: () => this.client.dashboardView() });
}
}
/** Trust-boundary parse of the status union — the generated `RegistrationStatusDto`
flattens all three variants into one object with every field optional (NSwag
can't express a discriminated union), so the tag drives which fields must
actually be present. Mirrors `parseAanvraagStatus` in `aanvragen.adapter.ts`. */
export function parseRegistrationStatus(
s: RegistrationStatusDto | undefined,
): Result<string, RegistrationStatus> {
if (!s || typeof s.tag !== 'string') return err('registration: missing status');
switch (s.tag) {
case 'Geregistreerd':
if (typeof s.herregistratieDatum !== 'string')
return err('registration: bad Geregistreerd status');
return ok({ tag: 'Geregistreerd', herregistratieDatum: s.herregistratieDatum });
case 'Geschorst':
if (typeof s.geschorstTot !== 'string' || typeof s.reden !== 'string')
return err('registration: bad Geschorst status');
return ok({ tag: 'Geschorst', geschorstTot: s.geschorstTot, reden: s.reden });
case 'Doorgehaald':
if (typeof s.doorgehaaldOp !== 'string' || typeof s.reden !== 'string')
return err('registration: bad Doorgehaald status');
return ok({ tag: 'Doorgehaald', doorgehaaldOp: s.doorgehaaldOp, reden: s.reden });
default:
return err(`registration: unknown status tag ${s.tag}`);
}
}
function parseRegistration(dto: RegistrationDto | undefined): Result<string, Registration> {
if (
!dto ||
typeof dto.bigNummer !== 'string' ||
typeof dto.naam !== 'string' ||
typeof dto.beroep !== 'string' ||
typeof dto.registratiedatum !== 'string' ||
typeof dto.geboortedatum !== 'string'
) {
return err('dashboard-view: missing/invalid registration');
}
const status = parseRegistrationStatus(dto.status);
if (!status.ok) return status;
return ok({
bigNummer: dto.bigNummer,
naam: dto.naam,
beroep: dto.beroep,
registratiedatum: dto.registratiedatum,
geboortedatum: dto.geboortedatum,
status: status.value,
});
}
function parsePerson(dto: PersonDto | undefined): Result<string, Person> {
const a = dto?.adres;
if (
!dto ||
typeof dto.naam !== 'string' ||
typeof dto.geboortedatum !== 'string' ||
!a ||
typeof a.straat !== 'string' ||
typeof a.postcode !== 'string' ||
typeof a.woonplaats !== 'string'
) {
return err('dashboard-view: missing/invalid person');
}
return ok({
naam: dto.naam,
geboortedatum: dto.geboortedatum,
adres: { straat: a.straat, postcode: a.postcode, woonplaats: a.woonplaats },
});
}
/**
* Trust-boundary parse: validate the untrusted response shape and map the DTO
* onto our own domain model. Hand-written on purpose — no Zod for a single
@@ -51,43 +120,17 @@ export function parseDashboardView(json: unknown): Result<string, DashboardView>
if (typeof json !== 'object' || json === null) return err('dashboard-view: not an object');
const dto = json as Partial<DashboardViewDto>;
const reg = dto.registration;
if (
!reg ||
typeof reg.bigNummer !== 'string' ||
!reg.status ||
typeof reg.status.tag !== 'string'
) {
return err('dashboard-view: missing/invalid registration');
}
const person = dto.person;
if (!person || !person.adres || typeof person.adres.postcode !== 'string') {
return err('dashboard-view: missing/invalid person');
}
const registration = parseRegistration(dto.registration);
if (!registration.ok) return registration;
const person = parsePerson(dto.person);
if (!person.ok) return person;
const d = dto.decisions;
if (!d || typeof d.eligibleForHerregistratie !== 'boolean') {
return err('dashboard-view: missing/invalid decisions');
}
// Map wire → domain. The shapes are identical today, so this reads as an
// identity copy — but the TYPES differ (wire DTO vs domain), so the moment the
// wire diverges the compiler forces a real mapping here. That's the seam.
const registration: Registration = {
bigNummer: reg.bigNummer,
naam: reg.naam,
beroep: reg.beroep,
registratiedatum: reg.registratiedatum,
geboortedatum: reg.geboortedatum,
status: reg.status,
};
const persoon: Person = {
naam: person.naam,
geboortedatum: person.geboortedatum,
adres: person.adres,
};
return ok({
profile: { registration, person: persoon },
profile: { registration: registration.value, person: person.value },
decisions: {
eligibleForHerregistratie: d.eligibleForHerregistratie,
herregistratieReason: d.herregistratieReason,