adding transaction rules.

This commit is contained in:
Bryce Covert
2019-05-07 07:10:21 -07:00
parent 918f2e5be1
commit a1abb4b05e
15 changed files with 266 additions and 15 deletions

View File

@@ -131,9 +131,9 @@
:auto-ap/add-exclude-to-transaction {:txes add-general-ledger/add-exclude-to-transaction :requires [:auto-ap/add-external-id-to-ledger]}
:auto-ap/convert-transactions {:txes-fn `add-general-ledger/convert-transactions :requires [:auto-ap/add-external-id-to-ledger]}
:auto-ap/add-transaction-rules {:txes add-general-ledger/add-transaction-rules :requires [:auto-ap/convert-transactions]}
#_#_:auto-ap/bulk-load-invoice-ledger3 {:txes-fn `add-general-ledger/bulk-load-invoice-ledger :requires [:auto-ap/convert-transactions]}
#_#_:auto-ap/bulk-load-transaction-ledger3 {:txes-fn `add-general-ledger/bulk-load-transaction-ledger :requires [:auto-ap/convert-transactions]}
}]
(println "Conforming database...")

View File

@@ -305,6 +305,44 @@
:db/cardinality :db.cardinality/one
:db/doc "Whether to exclude from the ledger"}]])
(def add-transaction-rules
[[{:db/ident :transaction-rule/client
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "The specific client this rule is for"}
{:db/ident :transaction-rule/bank-account
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "The specific bank account this rule is for"}
{:db/ident :transaction-rule/yodlee-merchant
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Apply this rule if the yodlee merchant matches"}
{:db/ident :transaction-rule/description
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "A description to match this rule against"}
{:db/ident :transaction-rule/note
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "A friendly description for this rule (internal)"}
{:db/ident :transaction-rule/amount-lte
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one
:db/doc "Amount has to be less than or equal to this"}
{:db/ident :transaction-rule/amount-gte
:db/valueType :db.type/double
:db/cardinality :db.cardinality/one
:db/doc "Amount has to be greater than or equal to this"}
]])
(def add-credit-bank-account
[[{:db/ident :bank-account-type/credit}]])

View File

@@ -0,0 +1,71 @@
(ns auto-ap.datomic.transaction-rules
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination add-sorter-field]]
[auto-ap.graphql.utils :refer [limited-clients]]
[clojure.set :refer [rename-keys]]
[clj-time.coerce :as c]))
(defn <-datomic [result]
result)
(def default-read '[*
{:transaction-rule/client [:client/name :db/id :client/code]}
{:transaction-rule/bank-account [*]}
{:transaction-rule/yodlee-merchant [*]}])
(defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find []
:in ['$]
:where []}
:args [db]}
(:sort-by args) (add-sorter-field {"client" ['[?e :transaction-rule/client ?c]
'[?c :client/name ?sorter]]
"yodlee-merchant" ['[?e :transaction-rule/yodlee-merchant ?c]
'[?c :yodlee-merchant/name ?sorter]]
"bank-account" ['[?e :transaction-rule/bank-account ?c]
'[?c :bank-account/name ?sorter]]
"amount_lte" ['[?e :transaction-rule/amount-lte ?sorter]]
"amount_gte" ['[?e :transaction-rule/amount-gte ?sorter]]
}
args)
(limited-clients (:id args))
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :transaction-rule/client ?xx]]}
:args [(set (map :db/id (limited-clients (:id args))))]})
(:client-id args)
(merge-query {:query {:in ['?client-id]
:where ['[?e :transaction-rule/client ?client-id]]}
:args [(:client-id args)]})
true
(merge-query {:query {:find ['?e]
:where ['[?e :transaction-rule/description]]}}))]
(cond->> query
true (d/query)
true (apply-sort-2 args [:asc])
true (apply-pagination args))))
(defn graphql-results [ids db args]
(let [results (->> (d/pull-many db default-read ids)
(group-by :db/id))
transaction-rules (->> ids
(map results)
(map first)
(mapv <-datomic))]
transaction-rules))
(defn get-graphql [args]
(let [db (d/db (d/connect uri))
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))
(defn get-by-id [id]
(->>
(d/pull (d/db (d/connect uri)) default-read id)
(<-datomic)))