diff --git a/packages/state-machine/src/machine.ts b/packages/state-machine/src/machine.ts index 45b72ce..c2cc434 100644 --- a/packages/state-machine/src/machine.ts +++ b/packages/state-machine/src/machine.ts @@ -431,6 +431,7 @@ export function createATMMachine( }, }, generatingNdebit: { + // No CANCEL here — bills are already in the cash box invoke: { src: 'generateNdebit', input: ({ context }) => context, @@ -457,8 +458,15 @@ export function createATMMachine( target: 'error', actions: ['setError', 'setPaymentFailed'], }, - TIMEOUT: '#atm.idle', - CANCEL: '#atm.idle', + CANCEL: 'confirmAbandon', + TIMEOUT: 'confirmAbandon', + }, + }, + confirmAbandon: { + // Warning: cash is in the machine, abandoning forfeits it + on: { + CANCEL: '#atm.idle', // User confirms they want to leave + RETRY: 'generatingNdebit', // Go back and try again }, }, askForReceipt: { @@ -489,10 +497,20 @@ export function createATMMachine( on: { RETRY: { guard: 'canRetry', - target: 'fetchingRate', + target: 'generatingNdebit', actions: 'incrementRetry', }, - CANCEL: '#atm.idle', + CANCEL: [ + { + // No bills inserted: safe to cancel + guard: ({ context }) => context.billsInserted.length === 0, + target: '#atm.idle', + }, + { + // Bills inserted: show abandon warning first + target: 'confirmAbandon', + }, + ], }, }, },