(ns iol-ion.tx.pay
  (:require [datomic.api :as dc]))

(defn pay [db e amount]
  (let [current-outstanding-balance (-> (dc/pull db [:invoice/outstanding-balance] e) :invoice/outstanding-balance)
        new-outstanding-balance     (- current-outstanding-balance amount)]
    [[:upsert-invoice {:db/id                       e
                       :invoice/outstanding-balance new-outstanding-balance
                       :invoice/status              (if (< -0.0001 new-outstanding-balance 0.0001)
                                                      :invoice-status/paid
                                                      :invoice-status/unpaid)}]]))
