---
Orders context. Places orders, calling payment for authorisation.
---
context commerce.orders

uses commerce.money
consumes commerce.payment as Payment

exports transparent { OrderError }

type OrderError = enum {
  PaymentDeclined,
  PaymentInsufficientFunds,
  PaymentInfrastructureError,
}

service placeOrder {
  ---
  Place an order by authorising payment for the total amount. Maps
  payment's error variants to orders' own OrderError shape (the
  anti-corruption-layer pattern).
  ---
  on call(total: Money) -> Effect[Result[(), OrderError]] {
    let auth <- Payment.authorise(total)
    match auth {
      Ok(_) => Ok(())
      Err(error) => match error {
        Declined          => Err(PaymentDeclined)
        InsufficientFunds => Err(PaymentInsufficientFunds)
        GatewayDown       => Err(PaymentInfrastructureError)
      }
    }
  }
}
