prioritizes rules.

This commit is contained in:
Bryce Covert
2019-05-15 18:57:42 -07:00
parent 79500ba157
commit 5f810ac2c4
2 changed files with 202 additions and 24 deletions

View File

@@ -121,22 +121,79 @@
[]
(partition-all 100 transactions)))
(defn rule-applies? [transaction rule]
(re-find (:transaction-rule/description rule) (:transaction/description-original transaction)))
(defn rule-applies? [transaction {:keys [:transaction-rule/description
:transaction-rule/dom-gte :transaction-rule/dom-lte
:transaction-rule/amount-gte :transaction-rule/amount-lte
:transaction-rule/client :transaction-rule/bank-account]} ]
(let [transaction-dom (some-> transaction
:transaction/date
.toInstant
(.atZone (java.time.ZoneId/of "US/Pacific"))
(.get java.time.temporal.ChronoField/DAY_OF_MONTH))]
(and
(if description
(re-find description (:transaction/description-original transaction))
true)
(if dom-gte
(>= transaction-dom dom-gte)
true)
(if dom-lte
(<= transaction-dom dom-lte)
true)
(if amount-gte
(>= (:transaction/amount transaction) amount-gte)
true)
(if amount-lte
(<= (:transaction/amount transaction) amount-lte)
true)
(if client
(= (:transaction/client transaction)
(:db/id client))
true)
(if bank-account
(= (:transaction/bank-account transaction)
(:db/id bank-account))
true))))
(defn more-specific? [left right key]
(cond (and (get left key)
(get right key))
nil
(get left key)
true
(get right key)
false
: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)
true])))
(defn rule-applying-fn [rules]
(let [rules (transduce (map (fn [r] (update r :transaction-rule/description #(some-> % re-pattern))))
conj
[]
rules)]
(sort prioritize-rule rules))]
(println rules)
(fn [transaction]
(let [matching-rules (filter #(rule-applies? transaction %) rules)]
(let [matching-rules (->> rules
(filter #(rule-applies? transaction %) ))]
(if-let [top-match (first matching-rules)]
(assoc transaction
:transaction/matched-rule (:db/id top-match)
:transaction/approval-status (:transaction-rule/transaction-approval-status top-match)
:transaction/vendor (:db/id (:transaction-rule/vendor top-match))
)
:transaction/vendor (:db/id (:transaction-rule/vendor top-match)))
transaction)))))
(defn manual-import [manual-transactions]