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

@@ -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 ))