You can now run the rule on a bunch of transactions.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 ))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user