import { AanvraagType } from '@registratie/domain/aanvraag'; import { AanvragenAdapter, parseAanvragen } from '@registratie/infrastructure/aanvragen.adapter'; /** * Read half of the Concept lookup that `createDraftSync` (`draft-sync.ts`) needs * before it can start writing (CQ-001). Free functions that take the adapter * as a parameter, not `inject()`, so they get a direct spec without Angular TestBed. * `createDraftSync` keeps the closure state (`id`, `resumeGate`) and the write path; * these two functions only read. */ /** Find the user's existing Concept of a given type (at most one), if any. */ export async function findConcept( adapter: AanvragenAdapter, type: AanvraagType, ): Promise { try { const parsed = parseAanvragen(await adapter.list()); return parsed.ok ? parsed.value.find((a) => a.type === type && a.status.tag === 'Concept')?.id : undefined; } catch { return undefined; } } /** Outcome of loading one Concept by id: its draft (or null when it has none), or `not-concept` when the id is not an editable Concept (submitted/gone) or the lookup failed (unknown/deleted id) — the caller treats both the same way, as "start fresh". */ export type LoadedConcept = { tag: 'concept'; draft: unknown | null } | { tag: 'not-concept' }; /** Load a specific Concept by id and report whether it is still editable. */ export async function loadConcept(adapter: AanvragenAdapter, id: string): Promise { try { const dto = await adapter.detail(id); if (dto.status && dto.status.tag !== 'Concept') return { tag: 'not-concept' }; return { tag: 'concept', draft: dto.draft ?? null }; } catch { return { tag: 'not-concept' }; } }