context shop.orders

consumes shop.payment as Pay

exports transparent { OrderError }

type OrderError = enum { Rejected }

agent Ledger {
  key id: String

  store placed: Cell[Int]

  on call tally() -> Effect[Result[Int, OrderError]] {
    let n = placed + 1
    placed := n
    Ok(n)
  }
}

service place {
  on call(id: String, cents: Int) -> Effect[Result[Int, OrderError]] {
    let a <- Pay.authorise(cents)
    match a {
      Ok(_)  => {
        let ledger = Ledger(id)
        let count <- ledger.tally()
        count
      }
      Err(_) => Err(Rejected)
    }
  }
}
