using Big.Domain;
namespace Big.Application;
/// A zorgprofessional's request to withdraw their own registration ("trek aanvraag in").
public sealed record WithdrawRegistrationCommand(RegistrationId RegistrationId);
///
/// The withdrawal use case (S-11): a zorgprofessional pulls a still-open registration back. It
/// advances the aggregate to INGETROKKEN and persists it. Idempotent — a repeated or redelivered
/// withdrawal of an already-withdrawn registration is a no-op (the aggregate is not persisted again).
/// Cancelling the running Flowable process (so the case leaves the behandelaar's werkbak) is a later
/// sub-slice (S-11b, via a BPMN message event); this sub-slice owns the domain transition only —
/// mirroring how the beoordeling's rejection deferred its zaak propagation.
///
public sealed class WithdrawRegistration(IRegistrationStore store)
{
public async Task HandleAsync(WithdrawRegistrationCommand command, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(command);
var registration = await store.GetAsync(command.RegistrationId, ct)
?? throw new InvalidOperationException($"No registration {command.RegistrationId} to withdraw.");
// A repeated withdrawal is a no-op: don't persist the already-withdrawn aggregate again.
if (registration.Status == RegistrationStatus.Ingetrokken)
return;
registration.Withdraw();
await store.SaveAsync(registration, ct);
}
}