From d833a8558131dd91928b3968a758230ae57b82ea Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 20 Mar 2019 21:23:25 -0700 Subject: [PATCH] Fixed issue with paying negative invoices. --- src/clj/auto_ap/datomic/migrate.clj | 13 +++++++++++++ src/cljs/auto_ap/effects.cljs | 3 +-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index fb0cfe97..be6a96ca 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -34,6 +34,18 @@ :code [[:db/add e a (-> (d/entity db e) a (+ amount))]] })}]] ) +(defn fix-pay-function [conn] + [[{:db/ident :pay + :db/doc "Data function that increments value of attribute a by amount." + :db/fn (d/function '{:lang "clojure" + :params [db e amount] + :code (let [current-outstanding-balance (-> (d/entity db e) :invoice/outstanding-balance) + new-outstanding-balance (- current-outstanding-balance amount)] + [[:db/add e :invoice/outstanding-balance new-outstanding-balance] + [:db/add e :invoice/status (if (> -0.001 new-outstanding-balance 0.001) + :invoice-status/paid + :invoice-status/unpaid)]])})}]] ) + (defn -main [& args] (println "Creating database...") (d/create-database uri) @@ -42,6 +54,7 @@ conn (d/connect uri) norms-map {:auto-ap/base-schema {:txes auto-ap.datomic/base-schema} :auto-ap/functions {:txes-fn 'auto-ap.datomic.migrate/functions :requires [:auto-ap/base-schema]} + :auto-ap/fx-pay-function {:txes-fn 'auto-ap.datomic.migrate/fix-pay-function :requires [:auto-ap/functions]} :auto-ap/migrate-vendors {:txes-fn 'auto-ap.datomic/migrate-vendors :requires [:auto-ap/base-schema]} :auto-ap/migrate-clients {:txes-fn 'auto-ap.datomic/migrate-clients :requires [:auto-ap/migrate-vendors]} :auto-ap/migrate-users {:txes-fn 'auto-ap.datomic/migrate-users :requires [:auto-ap/migrate-clients]} diff --git a/src/cljs/auto_ap/effects.cljs b/src/cljs/auto_ap/effects.cljs index 27c90edf..3601dbc8 100644 --- a/src/cljs/auto_ap/effects.cljs +++ b/src/cljs/auto_ap/effects.cljs @@ -10,7 +10,6 @@ [clojure.walk :as walk] [venia.core :as v] [auto-ap.history :as p] - [auto-ap.events :as events] [pushy.core :as pushy])) (re-frame/reg-fx @@ -158,7 +157,7 @@ "&variables=" (pr-str (or variables {})))}))] (cond (= (:status response) 401) - (re-frame/dispatch [::events/logout "Your session has expired. Please log in again."]) + (re-frame/dispatch [:auto-ap.events/logout "Your session has expired. Please log in again."])