From 54fc606cf0d25316d5ba98f66c4b5bba1b30d546 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 24 Jan 2020 19:28:52 -0800 Subject: [PATCH] added apply all rule. --- resources/public/css/main.css | 3 + src/clj/auto_ap/graphql.clj | 1 + src/clj/auto_ap/graphql/transaction_rules.clj | 13 ++-- src/clj/auto_ap/graphql/transactions.clj | 16 ++++- src/clj/auto_ap/graphql/utils.clj | 1 + src/clj/auto_ap/rule_matching.clj | 2 +- src/cljs/auto_ap/views/components/modal.cljs | 5 +- .../pages/admin/rules/results_modal.cljs | 63 ++++++++++++++++--- 8 files changed, 85 insertions(+), 19 deletions(-) diff --git a/resources/public/css/main.css b/resources/public/css/main.css index 2a49d742..5c010de3 100644 --- a/resources/public/css/main.css +++ b/resources/public/css/main.css @@ -114,6 +114,9 @@ .modal { animation: appear .7s ease both; } + .modal.wide .modal-card { + width: 1024px; + } @keyframes grow-width { from { width: 0px; diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 27fbbec2..06bd213d 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -670,6 +670,7 @@ :match_transaction_rules {:type '(list :transaction) :args {:transaction_ids {:type '(list :id)} + :all {:type 'Boolean} :transaction_rule_id {:type :id}} :resolve :mutation/match-transaction-rules} :void_invoice {:type :invoice diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index a8390540..b2a723eb 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -92,11 +92,12 @@ (defn tr [z x] (re-find (re-pattern z) x)) -(defn -test-transaction-rule [id {:keys [:transaction-rule/description :transaction-rule/note :transaction-rule/client :transaction-rule/bank-account :transaction-rule/amount-lte :transaction-rule/amount-gte :transaction-rule/dom-lte :transaction-rule/dom-gte :transaction-rule/yodlee-merchant]} include-coded?] +(defn -test-transaction-rule [id {:keys [:transaction-rule/description :transaction-rule/note :transaction-rule/client :transaction-rule/bank-account :transaction-rule/amount-lte :transaction-rule/amount-gte :transaction-rule/dom-lte :transaction-rule/dom-gte :transaction-rule/yodlee-merchant]} include-coded? count] (->> (d/query (cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name] - :transaction/bank-account [:bank-account/name]} + :transaction/bank-account [:bank-account/name] + :transaction/payment [:db/id]} ])] :in ['$ ] :where []} @@ -167,7 +168,7 @@ (merge-query {:query {:where ['[?e :transaction/id]]}}))) (transduce (comp - (take 15) + (take (or count 15)) (map first) (map #(dissoc % :transaction/id)) (map (fn [x] @@ -185,9 +186,9 @@ :dom-lte dom_lte :dom-gte dom_gte :yodlee-merchant (when yodlee_merchant_id {:db/id yodlee_merchant_id})} - true)) + true 15)) -(defn run-transaction-rule [{:keys [id]} {:keys [transaction_rule_id]} value] +(defn run-transaction-rule [{:keys [id]} {:keys [transaction_rule_id count]} value] (assert-admin id) - (-test-transaction-rule id (tr/get-by-id transaction_rule_id) false)) + (-test-transaction-rule id (tr/get-by-id transaction_rule_id) false count)) diff --git a/src/clj/auto_ap/graphql/transactions.clj b/src/clj/auto_ap/graphql/transactions.clj index 06a92846..a35e06d2 100644 --- a/src/clj/auto_ap/graphql/transactions.clj +++ b/src/clj/auto_ap/graphql/transactions.clj @@ -3,6 +3,7 @@ [auto-ap.datomic.transactions :as d-transactions] [auto-ap.datomic.vendors :as d-vendors] [auto-ap.datomic.checks :as d-checks] + [auto-ap.graphql.transaction-rules :as g-tr] [datomic.api :as d] [auto-ap.datomic :refer [uri remove-nils]] [com.walmartlabs.lacinia :refer [execute]] @@ -122,8 +123,17 @@ approval-status->graphql ->graphql)) -(defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id]} value] +(defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id all]} value] (let [_ (assert-admin (:id context)) + transaction_ids (if all + (->> (g-tr/run-transaction-rule context {:transaction_rule_id transaction_rule_id + :count Integer/MAX_VALUE} nil) + + (filter #(not (:transaction/payment %))) + (map :id )) + + + transaction_ids) transactions (transduce (comp (map d-transactions/get-by-id) @@ -134,7 +144,9 @@ transaction-rule (update (tr/get-by-id transaction_rule_id) :transaction-rule/description #(some-> % re-pattern))] (doseq [transaction transactions] (when (not (rm/rule-applies? transaction transaction-rule)) - (throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply"}))) + (throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply" + :transaction-rule transaction-rule + :transaction transaction}))) (when (:transaction/payment transaction) diff --git a/src/clj/auto_ap/graphql/utils.clj b/src/clj/auto_ap/graphql/utils.clj index b730cf66..8480fd15 100644 --- a/src/clj/auto_ap/graphql/utils.clj +++ b/src/clj/auto_ap/graphql/utils.clj @@ -44,6 +44,7 @@ (defn assert-admin [id] + (println "role" id) (when-not (= "admin" (:user/role id)) (throw-unauthorized))) diff --git a/src/clj/auto_ap/rule_matching.clj b/src/clj/auto_ap/rule_matching.clj index 17022aff..ef3353c1 100644 --- a/src/clj/auto_ap/rule_matching.clj +++ b/src/clj/auto_ap/rule_matching.clj @@ -24,7 +24,7 @@ (.get java.time.temporal.ChronoField/DAY_OF_MONTH))] (and (if description - (re-find description (:transaction/description-original transaction)) + (re-find description (or (:transaction/description-original transaction) "")) true) (if dom-gte (>= transaction-dom dom-gte) diff --git a/src/cljs/auto_ap/views/components/modal.cljs b/src/cljs/auto_ap/views/components/modal.cljs index a5155e33..1868e073 100644 --- a/src/cljs/auto_ap/views/components/modal.cljs +++ b/src/cljs/auto_ap/views/components/modal.cljs @@ -5,8 +5,9 @@ [auto-ap.subs :as subs] [auto-ap.views.utils :refer [with-keys]])) -(defn modal [{:keys [title foot hide-event]} & body] - [:div.modal.is-active +(defn modal [{:keys [title foot hide-event class]} & body] + [:div.modal.is-active (cond-> {} + class (assoc :class class)) [:div.modal-background {:on-click (fn [] (re-frame/dispatch hide-event ))}] [:div.modal-card diff --git a/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs b/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs index 780375df..c8d90522 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules/results_modal.cljs @@ -23,6 +23,11 @@ (fn [db] (::checked db))) +(re-frame/reg-sub + ::all-checked + (fn [db] + (::all-checked db nil))) + (re-frame/reg-sub ::checked-count :<- [::checked] @@ -35,11 +40,12 @@ ::opening (fn [{:keys [db]} [_ results transaction-rule-id runnable?]] {:db (-> db + (assoc ::all-checked false) (assoc ::test-results results) (assoc ::transaction-rule-id transaction-rule-id) (assoc ::checked #{}) (assoc ::runnable? runnable?)) - :dispatch [::events/modal-status ::test-results {:visible? true}]})) + :dispatch [::events/modal-status ::test-results {:visible? true :error-message nil}]})) (re-frame/reg-event-db ::toggle @@ -50,6 +56,32 @@ (disj checked which) (conj checked which))))) +(re-frame/reg-event-db + ::toggle-all + (fn [db [_ which]] + (let [{::keys [all-checked checked test-results]} db] + (assoc db + ::all-checked (not all-checked) + ::checked (if all-checked + #{} + (into #{} (map :id test-results))))))) + +(re-frame/reg-event-fx + ::apply-failed + [with-user] + (fn [{:keys [db user]} [_ params]] + {:graphql + {:token user + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "MatchTransactionRules"} + :venia/queries [{:query/data [:match-transaction-rules + {:transaction-ids (seq @(re-frame/subscribe [::checked])) + :all (boolean (:all? params)) + :transaction-rule-id (::transaction-rule-id db)} + transaction-read]}]} + :on-success [::events/modal-status ::test-results {:visible? false}] + :on-error [:events/modal-failed ::test-results]}})) + (re-frame/reg-event-fx ::apply-rule [with-user] @@ -60,27 +92,42 @@ :operation/name "MatchTransactionRules"} :venia/queries [{:query/data [:match-transaction-rules {:transaction-ids (seq @(re-frame/subscribe [::checked])) + :all (boolean (:all? params)) :transaction-rule-id (::transaction-rule-id db)} transaction-read]}]} :on-success [::events/modal-status ::test-results {:visible? false}] - #_#_:on-error [:forms/save-error ::form]}})) + :on-error [::events/modal-failed ::test-results]}})) (defn test-results-modal [params] (let [runnable? @(re-frame/subscribe [::runnable?]) checked @(re-frame/subscribe [::checked]) - checked-count @(re-frame/subscribe [::checked-count])] - (when (:visible? @(re-frame/subscribe [::subs/modal-state ::test-results])) + all-checked @(re-frame/subscribe [::all-checked]) + checked-count @(re-frame/subscribe [::checked-count]) + modal-state @(re-frame/subscribe [::subs/modal-state ::test-results])] + (when (:visible? modal-state) [modal {:title "Rule results" + :class "wide" :hide-event [::events/modal-status ::test-results {:visible? false}] :foot (when runnable? - [:button.button.is-primary {:disabled (boolean (= checked-count 0)) - :on-click (dispatch-event [::apply-rule params])} - (str "Apply to " checked-count " transactions")])} + [:div + (when (:error-message modal-state) + [:div.notification.is-warning + (:error-message modal-state)]) + [:button.button.is-primary {:disabled (boolean (= checked-count 0)) + :on-click (dispatch-event [::apply-rule params])} + (str "Apply to " checked-count " transactions")] + + [:button.button.is-warning {:disabled (not all-checked) + :on-click (dispatch-event [::apply-rule (assoc params :all? true)])} + (str "Apply to all transactions")]])} [:table.table.is-fullwidth.compact [:tr (when runnable? - [:th {:style {:width "2em"}}]) + [:th {:style {:width "2em"}} + [:input.checkbox {:type "checkbox" + :checked all-checked + :on-change (dispatch-event [::toggle-all])}]]) [:th "Client"] [:th "Bank Account"] [:th "Description"]