kind of applies rules.

This commit is contained in:
Bryce Covert
2019-05-15 07:12:56 -07:00
parent d5c3a73bef
commit 70714a2de1
2 changed files with 114 additions and 37 deletions

View File

@@ -50,7 +50,7 @@
nil))
nil))
(defn transactions->txs [transactions transaction->client transaction->bank-account-id]
(defn transactions->txs [transactions transaction->client transaction->bank-account-id apply-rules]
(into []
(for [transaction transactions
@@ -75,36 +75,40 @@
bank-account-id (transaction->bank-account-id transaction)
check (transaction->payment transaction check-number client-id bank-account-id amount id)]
:when client-id]
(->> (remove-nils #:transaction
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
:id (sha-256 (str id))
:account-id account-id
:date (coerce/to-date (time/parse date "YYYY-MM-dd"))
:yodlee-merchant (when (and merchant-id merchant-name)
{:yodlee-merchant/yodlee-id merchant-id
:yodlee-merchant/name merchant-name})
:amount (double amount)
:description-original description-original
:description-simple description-simple
:exclude-from-ledger false
:type type
:status status
:client client-id
:check-number check-number
:bank-account (transaction->bank-account-id transaction)
:payment (when check
{:db/id (:db/id check)
:payment/status :payment-status/cleared}
)
(->
#:transaction
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
:id (sha-256 (str id))
:account-id account-id
:date (coerce/to-date (time/parse date "YYYY-MM-dd"))
:yodlee-merchant (when (and merchant-id merchant-name)
{:yodlee-merchant/yodlee-id merchant-id
:yodlee-merchant/name merchant-name})
:amount (double amount)
:description-original description-original
:description-simple description-simple
:exclude-from-ledger false
:type type
:status status
:client client-id
:check-number check-number
:bank-account (transaction->bank-account-id transaction)
:payment (when check
{:db/id (:db/id check)
:payment/status :payment-status/cleared}
)
:vendor (when check
(:db/id (:payment/vendor check)))
:location (when check
"A")
:accounts (when check
[#:transaction-account {:account (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"]))
:location "A"
:amount (Math/abs (double amount))}])})))))
:vendor (when check
(:db/id (:payment/vendor check)))
:location (when check
"A")
:accounts (when check
[#:transaction-account {:account (:db/id (a/get-account-by-numeric-code-and-sets 2110 ["default"]))
:location "A"
:amount (Math/abs (double amount))}])}
remove-nils
apply-rules))))
(defn batch-transact [transactions]
@@ -117,6 +121,21 @@
[]
(partition-all 100 transactions)))
(defn rule-applies? [transaction rule]
(re-find (:transaction-rule/description rule) (:transaction/description-original transaction)))
(defn rule-applying-fn [rules]
(let [rules (transduce (map (fn [r] (update r :transaction-rule/description #(some-> % re-pattern))))
conj
[]
rules)]
(fn [transaction]
(let [matching-rules (filter #(rule-applies? transaction %) rules)]
(if-let [top-match (first matching-rules)]
(assoc transaction :transaction/approval-status (:transaction-rule/transaction-approval-status (first matching-rules)))
transaction)))))
(defn manual-import [manual-transactions]
(let [transformed-transactions (->> manual-transactions
(filter #(= "posted" (:status %)))
@@ -137,7 +156,7 @@
transaction-group))))]
(println "importing manual transactions" transformed-transactions)
(batch-transact
(transactions->txs transformed-transactions :client-id :bank-account-id))))
(transactions->txs transformed-transactions :client-id :bank-account-id (rule-applying-fn [])))))
@@ -155,5 +174,5 @@
(d-clients/get-all))
transaction->client (comp (by :yodlee-account-id :client-id all-bank-accounts) :accountId)
transaction->bank-account-id (comp (by :yodlee-account-id :bank-account-id all-bank-accounts) :accountId)]
(batch-transact (transactions->txs transactions transaction->client transaction->bank-account-id)))))
(batch-transact (transactions->txs transactions transaction->client transaction->bank-account-id (rule-applying-fn []))))))