You can now run the rule on a bunch of transactions.

This commit is contained in:
Bryce Covert
2019-06-05 20:55:32 -07:00
parent 18d8ece2f4
commit b01578b70a
8 changed files with 86 additions and 46 deletions

View File

@@ -648,10 +648,10 @@
:payment_id {:type :id}} :payment_id {:type :id}}
:resolve :mutation/match-transaction} :resolve :mutation/match-transaction}
:match_transaction_rule {:type :transaction :match_transaction_rules {:type '(list :transaction)
:args {:transaction_id {:type :id} :args {:transaction_ids {:type '(list :id)}
:transaction_rule_id {:type :id}} :transaction_rule_id {:type :id}}
:resolve :mutation/match-transaction-rule} :resolve :mutation/match-transaction-rules}
:void_invoice {:type :invoice :void_invoice {:type :invoice
:args {:invoice_id {:type :id}} :args {:invoice_id {:type :id}}
:resolve :mutation/void-invoice} :resolve :mutation/void-invoice}
@@ -823,7 +823,7 @@
:test-transaction-rule gq-transaction-rules/test-transaction-rule :test-transaction-rule gq-transaction-rules/test-transaction-rule
:run-transaction-rule gq-transaction-rules/run-transaction-rule :run-transaction-rule gq-transaction-rules/run-transaction-rule
:mutation/match-transaction gq-transactions/match-transaction :mutation/match-transaction gq-transactions/match-transaction
:mutation/match-transaction-rule gq-transactions/match-transaction-rule :mutation/match-transaction-rules gq-transactions/match-transaction-rules
:mutation/edit-client gq-clients/edit-client :mutation/edit-client gq-clients/edit-client
:mutation/upsert-vendor gq-vendors/upsert-vendor :mutation/upsert-vendor gq-vendors/upsert-vendor
:mutation/upsert-account gq-accounts/upsert-account :mutation/upsert-account gq-accounts/upsert-account

View File

@@ -92,7 +92,7 @@
(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]}] (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?]
(->> (->>
(d/query (d/query
(cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name] (cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name]
@@ -157,12 +157,18 @@
(merge-query {:query {:in ['?client-id] (merge-query {:query {:in ['?client-id]
:where ['[?e :transaction/client ?client-id]]} :where ['[?e :transaction/client ?client-id]]}
:args [(:db/id client)]}) :args [(:db/id client)]})
(not include-coded?)
(merge-query {:query {:where ['[?e :transaction/approval-status :transaction-approval-status/unapproved]]}})
true true
(merge-query {:query {:where ['[?e :transaction/id]]}}))) (merge-query {:query {:where ['[?e :transaction/id]]}})))
(transduce (comp (transduce (comp
(take 15) (take 15)
(map first) (map first)
(map #(dissoc % :transaction/id))
(map (fn [x] (map (fn [x]
(update x :transaction/date c/from-date))) (update x :transaction/date c/from-date)))
(map ->graphql)) (map ->graphql))
@@ -177,9 +183,10 @@
:amount-gte amount_gte :amount-gte amount_gte
: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))
(defn run-transaction-rule [{:keys [id]} {:keys [transaction_rule_id]} value] (defn run-transaction-rule [{:keys [id]} {:keys [transaction_rule_id]} value]
(assert-admin id) (assert-admin id)
(-test-transaction-rule id (tr/get-by-id transaction_rule_id))) (-test-transaction-rule id (tr/get-by-id transaction_rule_id) false))

View File

@@ -122,16 +122,25 @@
approval-status->graphql approval-status->graphql
->graphql)) ->graphql))
(defn match-transaction-rule [context {:keys [transaction_id transaction_rule_id]} value] (defn match-transaction-rules [context {:keys [transaction_ids transaction_rule_id]} value]
(let [_ (assert-admin (:id context)) (let [_ (assert-admin (:id context))
transaction (update (d-transactions/get-by-id transaction_id) :transaction/date coerce/to-date) transactions (transduce
(comp
(map d-transactions/get-by-id)
(map #(update % :transaction/date coerce/to-date)))
conj
[]
transaction_ids)
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]
(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"})))
(when (:transaction/payment transaction) (when (:transaction/payment transaction)
(throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"}))) (throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"})))
@(d/transact (d/connect uri) @(d/transact (d/connect uri)
(into (into
[(remove-nils (rm/apply-rule {:db/id (:db/id transaction) [(remove-nils (rm/apply-rule {:db/id (:db/id transaction)
@@ -141,7 +150,12 @@
(or (-> transaction :transaction/bank-account :bank-account/locations) (or (-> transaction :transaction/bank-account :bank-account/locations)
(-> transaction :transaction/client :client/locations))))] (-> transaction :transaction/client :client/locations))))]
(map (fn [x] [:db/retractEntity (:db/id x)] ) (map (fn [x] [:db/retractEntity (:db/id x)] )
(:transaction/accounts transaction))))) (:transaction/accounts transaction))))))
(-> (d-transactions/get-by-id transaction_id) (transduce
approval-status->graphql (comp
->graphql)) (map d-transactions/get-by-id)
(map approval-status->graphql)
(map ->graphql))
conj
[]
transaction_ids ))

View File

@@ -89,8 +89,9 @@
:account-id account-id :account-id account-id
:date (coerce/to-date (time/parse date "YYYY-MM-dd")) :date (coerce/to-date (time/parse date "YYYY-MM-dd"))
:yodlee-merchant (when (and merchant-id merchant-name (not (str/blank? merchant-id))) :yodlee-merchant (when (and merchant-id merchant-name (not (str/blank? merchant-id)))
nil
{:yodlee-merchant/yodlee-id merchant-id #_{:yodlee-merchant/yodlee-id merchant-id
:yodlee-merchant/name merchant-name}) :yodlee-merchant/name merchant-name})
:amount (double amount) :amount (double amount)
:description-original description-original :description-original description-original

View File

@@ -178,7 +178,7 @@
[(forms/triggers-stop-loading ::form)] [(forms/triggers-stop-loading ::form)]
(fn [{:keys [db]} [_ result]] (fn [{:keys [db]} [_ result]]
{:dispatch [::results-modal/opening (:test-transaction-rule result) false]})) {:dispatch [::results-modal/opening (:test-transaction-rule result) nil false]}))

View File

@@ -2,7 +2,8 @@
(:require [auto-ap.events :as events] (:require [auto-ap.events :as events]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.views.components.modal :refer [modal]] [auto-ap.views.components.modal :refer [modal]]
[auto-ap.views.utils :refer [date->str dispatch-event]] [auto-ap.views.utils :refer [date->str dispatch-event with-user]]
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]))
@@ -30,9 +31,10 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::opening ::opening
(fn [{:keys [db]} [_ results runnable?]] (fn [{:keys [db]} [_ results transaction-rule-id runnable?]]
{:db (-> db {:db (-> db
(assoc ::test-results results) (assoc ::test-results results)
(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}]}))
@@ -46,8 +48,23 @@
(disj checked which) (disj checked which)
(conj checked which))))) (conj checked which)))))
(re-frame/reg-event-fx
::apply-rule
[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]))
:transaction-rule-id (::transaction-rule-id db)}
transaction-read]}]}
#_#_:on-success [:edited params]
#_#_:on-error [:forms/save-error ::form]}}))
(defn test-results-modal []
(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])] checked-count @(re-frame/subscribe [::checked-count])]
@@ -55,7 +72,8 @@
[modal {:title "Rule results" [modal {:title "Rule results"
: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))} [:button.button.is-primary {:disabled (boolean (= checked-count 0))
:on-click (dispatch-event [::apply-rule params])}
(str "Apply to " checked-count " transactions")])} (str "Apply to " checked-count " transactions")])}
[:table.table.is-fullwidth.compact [:table.table.is-fullwidth.compact
[:tr [:tr

View File

@@ -23,14 +23,14 @@
[:client [:name]] [:client [:name]]
[:bank-account [:name]] [:bank-account [:name]]
:description-original]]}]} :description-original]]}]}
:on-success [::succeeded-run] :on-success [::succeeded-run (:id which)]
#_#_:on-error [:forms/save-error ::form]}})) #_#_:on-error [:forms/save-error ::form]}}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::succeeded-run ::succeeded-run
(fn [{:keys [db]} [_ result]] (fn [{:keys [db]} [_ transaction-rule-id result]]
{:dispatch [::results-modal/opening (:run-transaction-rule result) true]})) {:dispatch [::results-modal/opening (:run-transaction-rule result) transaction-rule-id true]}))
(defn table [{:keys [id rule-page on-params-change params status]}] (defn table [{:keys [id rule-page on-params-change params status]}]
(let [opc (fn [p] (let [opc (fn [p]

View File

@@ -107,9 +107,9 @@
{:graphql {:graphql
{:token user {:token user
:query-obj {:venia/operation {:operation/type :mutation :query-obj {:venia/operation {:operation/type :mutation
:operation/name "MatchTransactionRule"} :operation/name "MatchTransactionRules"}
:venia/queries [{:query/data [:match-transaction-rule :venia/queries [{:query/data [:match-transaction-rules
{:transaction-id id {:transaction-ids [id]
:transaction-rule-id transaction-rule-id} :transaction-rule-id transaction-rule-id}
transaction-read]}]} transaction-read]}]}
:on-success [::edited params] :on-success [::edited params]
@@ -118,8 +118,8 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::edited ::edited
[(forms/triggers-stop ::form)] [(forms/triggers-stop ::form)]
(fn [{:keys [db]} [_ {:keys [edit-completed]} {:keys [edit-transaction match-transaction match-transaction-rule]}]] (fn [{:keys [db]} [_ {:keys [edit-completed]} {:keys [edit-transaction match-transaction match-transaction-rules]}]]
{:dispatch (conj edit-completed (or edit-transaction match-transaction match-transaction-rule))})) {:dispatch (conj edit-completed (or edit-transaction match-transaction (first match-transaction-rules)))}))
(re-frame/reg-event-db (re-frame/reg-event-db
::manual-match ::manual-match