supports grouping rules by priority.

This commit is contained in:
Bryce Covert
2019-05-16 19:01:33 -07:00
parent 21f7ac32b9
commit dde757e8fc
2 changed files with 79 additions and 32 deletions

View File

@@ -51,7 +51,7 @@
nil))
nil))
(defn transactions->txs [transactions transaction->client transaction->bank-account-id apply-rules]
(defn transactions->txs [transactions transaction->client transaction->bank-account-id apply-rules existing]
(into []
(for [transaction transactions
@@ -75,7 +75,8 @@
client-id (transaction->client transaction)
bank-account-id (transaction->bank-account-id transaction)
check (transaction->payment transaction check-number client-id bank-account-id amount id)]
:when client-id]
:when (and client-id
(not (existing (sha-256 (str id)))))]
(->
#:transaction
{:post-date (coerce/to-date (time/parse post-date "YYYY-MM-dd"))
@@ -174,31 +175,43 @@
:else
nil))
(defn prioritize-rule [left right]
(first (filter #(not (nil? %))
[(more-specific? left right :transaction-rule/bank-account)
(more-specific? left right :transaction-rule/client)
(more-specific? left right :transaction-rule/dom-lte)
(more-specific? left right :transaction-rule/dom-gte)
(more-specific? left right :transaction-rule/amount-lte)
(more-specific? left right :transaction-rule/amount-gte)
(more-specific? left right :transaction-rule/description)
(more-specific? left right :transaction-rule/yodlee-merchant)
true])))
(defn rule-priority [rule]
(or
(->> [[:transaction-rule/bank-account 0]
[:transaction-rule/client 1]
[:transaction-rule/dom-lte 2]
[:transaction-rule/dom-gte 2]
[:transaction-rule/amount-lte 3]
[:transaction-rule/amount-gte 3]
[:transaction-rule/description 4]
[:transaction-rule/yodlee-merchant 5]]
(filter (fn [[key]]
(get rule key)))
(map second)
first)
6))
(defn get-matching-rules-by-priority [rules-by-priority transaction]
(loop [[rule-set & rules] rules-by-priority]
(if rule-set
(let [matching-rules (into [] (filter #(rule-applies? transaction %) rule-set))]
(if (seq matching-rules)
matching-rules
(recur rules)))
[])))
(defn group-rules-by-priority [rules]
(->> rules
(map (fn [r] (update r :transaction-rule/description #(some-> % re-pattern))))
(group-by rule-priority)
(sort-by first)
(map second)))
(defn rule-applying-fn [rules]
(let [rules (transduce (map (fn [r]
(update r :transaction-rule/description #(some-> % re-pattern))))
conj
[]
(sort prioritize-rule rules))]
(let [rules-by-priority (group-rules-by-priority rules)]
(fn [transaction]
(let [matching-rules (->> rules
(filter #(rule-applies? transaction %) ))]
(if-let [top-match (first matching-rules)]
(let [matching-rules (get-matching-rules-by-priority rules-by-priority transaction )]
(if-let [top-match (and (= (count matching-rules) 1) (first matching-rules))]
(assoc transaction
:transaction/matched-rule (:db/id top-match)
:transaction/approval-status (:transaction-rule/transaction-approval-status top-match)
@@ -212,6 +225,13 @@
:transaction/vendor (:db/id (:transaction-rule/vendor top-match)))
transaction)))))
(defn get-existing []
(transduce (map first) conj #{}
(d/query {:query {:find ['?tid]
:in ['$]
:where ['[_ :transaction/id ?tid]]}
:args [(d/db (d/connect uri))]})))
(defn manual-import [manual-transactions]
(let [transformed-transactions (->> manual-transactions
(filter #(= "posted" (:status %)))
@@ -229,10 +249,11 @@
:simple high-level-category}
:status "POSTED"})
(range)
transaction-group))))]
transaction-group))))
all-rules (tr/get-all)]
(println "importing manual transactions" transformed-transactions)
(batch-transact
(transactions->txs transformed-transactions :client-id :bank-account-id (rule-applying-fn [])))))
(transactions->txs transformed-transactions :client-id :bank-account-id (rule-applying-fn all-rules) (get-existing)))))
@@ -251,5 +272,5 @@
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)
all-rules (tr/get-all)]
(batch-transact (transactions->txs transactions transaction->client transaction->bank-account-id (rule-applying-fn all-rules))))))
(batch-transact (transactions->txs transactions transaction->client transaction->bank-account-id (rule-applying-fn all-rules) (get-existing))))))