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}}
:resolve :mutation/match-transaction}
:match_transaction_rule {:type :transaction
:args {:transaction_id {:type :id}
:transaction_rule_id {:type :id}}
:resolve :mutation/match-transaction-rule}
:match_transaction_rules {:type '(list :transaction)
:args {:transaction_ids {:type '(list :id)}
:transaction_rule_id {:type :id}}
:resolve :mutation/match-transaction-rules}
:void_invoice {:type :invoice
:args {:invoice_id {:type :id}}
:resolve :mutation/void-invoice}
@@ -823,7 +823,7 @@
:test-transaction-rule gq-transaction-rules/test-transaction-rule
:run-transaction-rule gq-transaction-rules/run-transaction-rule
: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/upsert-vendor gq-vendors/upsert-vendor
:mutation/upsert-account gq-accounts/upsert-account

View File

@@ -92,7 +92,7 @@
(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]}]
(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
(cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name]
@@ -157,12 +157,18 @@
(merge-query {:query {:in ['?client-id]
:where ['[?e :transaction/client ?client-id]]}
:args [(:db/id client)]})
(not include-coded?)
(merge-query {:query {:where ['[?e :transaction/approval-status :transaction-approval-status/unapproved]]}})
true
(merge-query {:query {:where ['[?e :transaction/id]]}})))
(transduce (comp
(take 15)
(map first)
(map #(dissoc % :transaction/id))
(map (fn [x]
(update x :transaction/date c/from-date)))
(map ->graphql))
@@ -171,15 +177,16 @@
(defn test-transaction-rule [{:keys [id]} {{:keys [description note client_id bank_account_id amount_lte amount_gte dom_lte dom_gte yodlee_merchant_id]} :transaction_rule :as z} value]
(assert-admin id)
(-test-transaction-rule id #:transaction-rule {:description description
:client (when client_id {:db/id client_id})
:bank-account (when bank_account_id {:db/id bank_account_id})
:amount-lte amount_lte
:amount-gte amount_gte
:dom-lte dom_lte
:dom-gte dom_gte
:yodlee-merchant (when yodlee_merchant_id {:db/id yodlee_merchant_id})}))
:client (when client_id {:db/id client_id})
:bank-account (when bank_account_id {:db/id bank_account_id})
:amount-lte amount_lte
:amount-gte amount_gte
:dom-lte dom_lte
:dom-gte dom_gte
:yodlee-merchant (when yodlee_merchant_id {:db/id yodlee_merchant_id})}
true))
(defn run-transaction-rule [{:keys [id]} {:keys [transaction_rule_id]} value]
(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,26 +122,40 @@
approval-status->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))
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))]
(when (not (rm/rule-applies? transaction transaction-rule))
(throw (ex-info "Transaction rule does not apply" {:validation-error "Transaction rule does not apply"})))
(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"})))
(when (:transaction/payment transaction)
(throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"})))
(when (:transaction/payment transaction)
@(d/transact (d/connect uri)
(into
[(remove-nils (rm/apply-rule {:db/id (:db/id transaction)
:transaction/amount (:transaction/amount transaction)}
transaction-rule
(throw (ex-info "Transaction already associated with a payment" {:validation-error "Transaction already associated with a payment"})))
(or (-> transaction :transaction/bank-account :bank-account/locations)
(-> transaction :transaction/client :client/locations))))]
(map (fn [x] [:db/retractEntity (:db/id x)] )
(:transaction/accounts transaction)))))
(-> (d-transactions/get-by-id transaction_id)
approval-status->graphql
->graphql))
@(d/transact (d/connect uri)
(into
[(remove-nils (rm/apply-rule {:db/id (:db/id transaction)
:transaction/amount (:transaction/amount transaction)}
transaction-rule
(or (-> transaction :transaction/bank-account :bank-account/locations)
(-> transaction :transaction/client :client/locations))))]
(map (fn [x] [:db/retractEntity (:db/id x)] )
(:transaction/accounts transaction))))))
(transduce
(comp
(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
:date (coerce/to-date (time/parse date "YYYY-MM-dd"))
: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})
:amount (double amount)
:description-original description-original

View File

@@ -178,7 +178,7 @@
[(forms/triggers-stop-loading ::form)]
(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]
[auto-ap.subs :as subs]
[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]))
@@ -30,9 +31,10 @@
(re-frame/reg-event-fx
::opening
(fn [{:keys [db]} [_ results runnable?]]
(fn [{:keys [db]} [_ results transaction-rule-id runnable?]]
{:db (-> db
(assoc ::test-results results)
(assoc ::transaction-rule-id transaction-rule-id)
(assoc ::checked #{})
(assoc ::runnable? runnable?))
:dispatch [::events/modal-status ::test-results {:visible? true}]}))
@@ -46,8 +48,23 @@
(disj 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?])
checked @(re-frame/subscribe [::checked])
checked-count @(re-frame/subscribe [::checked-count])]
@@ -55,7 +72,8 @@
[modal {:title "Rule results"
:hide-event [::events/modal-status ::test-results {:visible? false}]
: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")])}
[:table.table.is-fullwidth.compact
[:tr

View File

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

View File

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