added apply all rule.

This commit is contained in:
Bryce Covert
2020-01-24 19:28:52 -08:00
parent bb40bff203
commit 54fc606cf0
8 changed files with 85 additions and 19 deletions

View File

@@ -114,6 +114,9 @@
.modal { .modal {
animation: appear .7s ease both; animation: appear .7s ease both;
} }
.modal.wide .modal-card {
width: 1024px;
}
@keyframes grow-width { @keyframes grow-width {
from { from {
width: 0px; width: 0px;

View File

@@ -670,6 +670,7 @@
:match_transaction_rules {:type '(list :transaction) :match_transaction_rules {:type '(list :transaction)
:args {:transaction_ids {:type '(list :id)} :args {:transaction_ids {:type '(list :id)}
:all {:type 'Boolean}
:transaction_rule_id {:type :id}} :transaction_rule_id {:type :id}}
:resolve :mutation/match-transaction-rules} :resolve :mutation/match-transaction-rules}
:void_invoice {:type :invoice :void_invoice {:type :invoice

View File

@@ -92,11 +92,12 @@
(defn tr [z x] (defn tr [z x]
(re-find (re-pattern 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 (d/query
(cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name] (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 ['$ ] :in ['$ ]
:where []} :where []}
@@ -167,7 +168,7 @@
(merge-query {:query {:where ['[?e :transaction/id]]}}))) (merge-query {:query {:where ['[?e :transaction/id]]}})))
(transduce (comp (transduce (comp
(take 15) (take (or count 15))
(map first) (map first)
(map #(dissoc % :transaction/id)) (map #(dissoc % :transaction/id))
(map (fn [x] (map (fn [x]
@@ -185,9 +186,9 @@
:dom-lte dom_lte :dom-lte dom_lte
:dom-gte dom_gte :dom-gte dom_gte
:yodlee-merchant (when yodlee_merchant_id {:db/id yodlee_merchant_id})} :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) (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))

View File

@@ -3,6 +3,7 @@
[auto-ap.datomic.transactions :as d-transactions] [auto-ap.datomic.transactions :as d-transactions]
[auto-ap.datomic.vendors :as d-vendors] [auto-ap.datomic.vendors :as d-vendors]
[auto-ap.datomic.checks :as d-checks] [auto-ap.datomic.checks :as d-checks]
[auto-ap.graphql.transaction-rules :as g-tr]
[datomic.api :as d] [datomic.api :as d]
[auto-ap.datomic :refer [uri remove-nils]] [auto-ap.datomic :refer [uri remove-nils]]
[com.walmartlabs.lacinia :refer [execute]] [com.walmartlabs.lacinia :refer [execute]]
@@ -122,8 +123,17 @@
approval-status->graphql approval-status->graphql
->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)) (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 transactions (transduce
(comp (comp
(map d-transactions/get-by-id) (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))] transaction-rule (update (tr/get-by-id transaction_rule_id) :transaction-rule/description #(some-> % re-pattern))]
(doseq [transaction transactions] (doseq [transaction transactions]
(when (not (rm/rule-applies? transaction transaction-rule)) (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) (when (:transaction/payment transaction)

View File

@@ -44,6 +44,7 @@
(defn assert-admin [id] (defn assert-admin [id]
(println "role" id)
(when-not (= "admin" (:user/role id)) (when-not (= "admin" (:user/role id))
(throw-unauthorized))) (throw-unauthorized)))

View File

@@ -24,7 +24,7 @@
(.get java.time.temporal.ChronoField/DAY_OF_MONTH))] (.get java.time.temporal.ChronoField/DAY_OF_MONTH))]
(and (and
(if description (if description
(re-find description (:transaction/description-original transaction)) (re-find description (or (:transaction/description-original transaction) ""))
true) true)
(if dom-gte (if dom-gte
(>= transaction-dom dom-gte) (>= transaction-dom dom-gte)

View File

@@ -5,8 +5,9 @@
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.views.utils :refer [with-keys]])) [auto-ap.views.utils :refer [with-keys]]))
(defn modal [{:keys [title foot hide-event]} & body] (defn modal [{:keys [title foot hide-event class]} & body]
[:div.modal.is-active [:div.modal.is-active (cond-> {}
class (assoc :class class))
[:div.modal-background {:on-click (fn [] (re-frame/dispatch hide-event ))}] [:div.modal-background {:on-click (fn [] (re-frame/dispatch hide-event ))}]
[:div.modal-card [:div.modal-card

View File

@@ -23,6 +23,11 @@
(fn [db] (fn [db]
(::checked db))) (::checked db)))
(re-frame/reg-sub
::all-checked
(fn [db]
(::all-checked db nil)))
(re-frame/reg-sub (re-frame/reg-sub
::checked-count ::checked-count
:<- [::checked] :<- [::checked]
@@ -35,11 +40,12 @@
::opening ::opening
(fn [{:keys [db]} [_ results transaction-rule-id runnable?]] (fn [{:keys [db]} [_ results transaction-rule-id runnable?]]
{:db (-> db {:db (-> db
(assoc ::all-checked false)
(assoc ::test-results results) (assoc ::test-results results)
(assoc ::transaction-rule-id transaction-rule-id) (assoc ::transaction-rule-id transaction-rule-id)
(assoc ::checked #{}) (assoc ::checked #{})
(assoc ::runnable? runnable?)) (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 (re-frame/reg-event-db
::toggle ::toggle
@@ -50,6 +56,32 @@
(disj checked which) (disj checked which)
(conj 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 (re-frame/reg-event-fx
::apply-rule ::apply-rule
[with-user] [with-user]
@@ -60,27 +92,42 @@
:operation/name "MatchTransactionRules"} :operation/name "MatchTransactionRules"}
:venia/queries [{:query/data [:match-transaction-rules :venia/queries [{:query/data [:match-transaction-rules
{:transaction-ids (seq @(re-frame/subscribe [::checked])) {:transaction-ids (seq @(re-frame/subscribe [::checked]))
:all (boolean (:all? params))
:transaction-rule-id (::transaction-rule-id db)} :transaction-rule-id (::transaction-rule-id db)}
transaction-read]}]} transaction-read]}]}
:on-success [::events/modal-status ::test-results {:visible? false}] :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] (defn test-results-modal [params]
(let [runnable? @(re-frame/subscribe [::runnable?]) (let [runnable? @(re-frame/subscribe [::runnable?])
checked @(re-frame/subscribe [::checked]) checked @(re-frame/subscribe [::checked])
checked-count @(re-frame/subscribe [::checked-count])] all-checked @(re-frame/subscribe [::all-checked])
(when (:visible? @(re-frame/subscribe [::subs/modal-state ::test-results])) 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" [modal {:title "Rule results"
:class "wide"
:hide-event [::events/modal-status ::test-results {:visible? false}] :hide-event [::events/modal-status ::test-results {:visible? false}]
:foot (when runnable? :foot (when runnable?
[:button.button.is-primary {:disabled (boolean (= checked-count 0)) [:div
:on-click (dispatch-event [::apply-rule params])} (when (:error-message modal-state)
(str "Apply to " checked-count " transactions")])} [: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 [:table.table.is-fullwidth.compact
[:tr [:tr
(when runnable? (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 "Client"]
[:th "Bank Account"] [:th "Bank Account"]
[:th "Description"] [:th "Description"]