Migrates back to datomic on-prem
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn pull-ref random-tempid]]
|
||||
[clojure.tools.logging :as log]
|
||||
[datomic.client.api :as dc]))
|
||||
[datomic.api :as dc]))
|
||||
|
||||
(defn bank-account->integration-id [bank-account]
|
||||
(or (pull-ref (dc/db conn) :bank-account/integration-status bank-account)
|
||||
@@ -11,19 +11,19 @@
|
||||
(defn wrap-integration [f bank-account]
|
||||
(try
|
||||
(let [result (f)]
|
||||
(dc/transact conn {:tx-data [{:db/id bank-account
|
||||
:bank-account/integration-status
|
||||
{:db/id (bank-account->integration-id bank-account)
|
||||
:integration-status/state :integration-state/success
|
||||
:integration-status/last-attempt (java.util.Date.)
|
||||
:integration-status/last-updated (java.util.Date.)}}]})
|
||||
@(dc/transact conn [{:db/id bank-account
|
||||
:bank-account/integration-status
|
||||
{:db/id (bank-account->integration-id bank-account)
|
||||
:integration-status/state :integration-state/success
|
||||
:integration-status/last-attempt (java.util.Date.)
|
||||
:integration-status/last-updated (java.util.Date.)}}])
|
||||
result)
|
||||
(catch Exception e
|
||||
(dc/transact conn {:tx-data [{:db/id bank-account
|
||||
:bank-account/integration-status
|
||||
{:db/id (bank-account->integration-id bank-account)
|
||||
:integration-status/state :integration-state/failed
|
||||
:integration-status/last-attempt (java.util.Date.)
|
||||
:integration-status/message (.getMessage e)}}]})
|
||||
@(dc/transact conn [{:db/id bank-account
|
||||
:bank-account/integration-status
|
||||
{:db/id (bank-account->integration-id bank-account)
|
||||
:integration-status/state :integration-state/failed
|
||||
:integration-status/last-attempt (java.util.Date.)
|
||||
:integration-status/message (.getMessage e)}}])
|
||||
(log/warn e)
|
||||
nil)))
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
[clojure.string :as str]
|
||||
[clojure.tools.logging :as log]
|
||||
[com.unbounce.dogstatsd.core :as statsd]
|
||||
[datomic.client.api :as dc]
|
||||
[datomic.api :as dc]
|
||||
[mount.core :as mount]
|
||||
[yang.scheduler :as scheduler]))
|
||||
|
||||
@@ -78,8 +78,8 @@
|
||||
(defn upsert-accounts []
|
||||
(let [token (i/get-fresh-access-token)
|
||||
bank-accounts (i/get-bank-accounts token)]
|
||||
(dc/transact conn {:tx-data (mapv
|
||||
(fn [ba]
|
||||
{:intuit-bank-account/external-id (:name ba)
|
||||
:intuit-bank-account/name (:name ba)})
|
||||
bank-accounts)})))
|
||||
@(dc/transact conn (mapv
|
||||
(fn [ba]
|
||||
{:intuit-bank-account/external-id (:name ba)
|
||||
:intuit-bank-account/name (:name ba)})
|
||||
bank-accounts))))
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
[auto-ap.import.transactions :as t]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.data.csv :as csv]
|
||||
[datomic.client.api :as dc]
|
||||
[datomic.api :as dc]
|
||||
[unilog.context :as lc]))
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
[auto-ap.utils :refer [allow-once by]]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]
|
||||
[datomic.client.api :as dc]
|
||||
[datomic.api :as dc]
|
||||
[digest :as di]
|
||||
[unilog.context :as lc]))
|
||||
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
[clj-time.core :as t]
|
||||
[clojure.core.cache :as cache]
|
||||
[clojure.tools.logging :as log]
|
||||
[datomic.client.api :as dc]
|
||||
[digest :as di]
|
||||
[iol-ion.tx :refer [upsert-invoice upsert-transaction]]))
|
||||
[datomic.api :as dc]
|
||||
[digest :as di]))
|
||||
|
||||
(defn rough-match [client-id bank-account-id amount]
|
||||
(if (and client-id bank-account-id amount)
|
||||
@@ -56,15 +55,15 @@
|
||||
|
||||
(defn match-transaction-to-unfulfilled-autopayments [amount client-id]
|
||||
(log/info "trying to find uncleared autopay invoices")
|
||||
(let [candidate-invoices-vendor-groups (->> (dc/q {:query {:find ['?vendor-id '?e '?total '?sd]
|
||||
:in ['$ '?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?e :invoice/scheduled-payment ?sd]
|
||||
'[?e :invoice/status :invoice-status/paid]
|
||||
'(not [_ :invoice-payment/invoice ?e])
|
||||
'[?e :invoice/vendor ?vendor-id]
|
||||
'[?e :invoice/total ?total]]}
|
||||
:args [(dc/db conn) client-id]})
|
||||
(let [candidate-invoices-vendor-groups (->> (dc/q {:find ['?vendor-id '?e '?total '?sd]
|
||||
:in ['$ '?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?e :invoice/scheduled-payment ?sd]
|
||||
'[?e :invoice/status :invoice-status/paid]
|
||||
'(not [_ :invoice-payment/invoice ?e])
|
||||
'[?e :invoice/vendor ?vendor-id]
|
||||
'[?e :invoice/total ?total]]}
|
||||
(dc/db conn) client-id)
|
||||
(sort-by last) ;; sort by scheduled payment date
|
||||
(group-by first) ;; group by vendors
|
||||
vals)
|
||||
@@ -81,15 +80,15 @@
|
||||
|
||||
(defn match-transaction-to-unpaid-invoices [amount client-id]
|
||||
(log/info "trying to find unpaid invoices for " client-id amount)
|
||||
(let [candidate-invoices-vendor-groups (->> (dc/q {:query {:find ['?vendor-id '?e '?outstanding-balance '?d]
|
||||
:in ['$ '?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?e :invoice/status :invoice-status/unpaid]
|
||||
'(not [_ :invoice-payment/invoice ?e])
|
||||
'[?e :invoice/vendor ?vendor-id]
|
||||
'[?e :invoice/outstanding-balance ?outstanding-balance]
|
||||
'[?e :invoice/date ?d]]}
|
||||
:args [(dc/db conn) client-id]})
|
||||
(let [candidate-invoices-vendor-groups (->> (dc/q {:find ['?vendor-id '?e '?outstanding-balance '?d]
|
||||
:in ['$ '?client-id]
|
||||
:where ['[?e :invoice/client ?client-id]
|
||||
'[?e :invoice/status :invoice-status/unpaid]
|
||||
'(not [_ :invoice-payment/invoice ?e])
|
||||
'[?e :invoice/vendor ?vendor-id]
|
||||
'[?e :invoice/outstanding-balance ?outstanding-balance]
|
||||
'[?e :invoice/date ?d]]}
|
||||
(dc/db conn) client-id)
|
||||
(sort-by last) ;; sort by scheduled payment date
|
||||
(group-by first) ;; group by vendors
|
||||
vals)
|
||||
@@ -112,33 +111,34 @@
|
||||
(defn add-new-payment [transaction [[vendor] :as invoice-payments] bank-account-id client-id]
|
||||
(log/info "Adding a new payment for transaction " (:transaction/id transaction) " and invoices " invoice-payments)
|
||||
(let [payment-id (random-tempid)]
|
||||
(-> [`(upsert-transaction ~(assoc transaction
|
||||
:transaction/payment payment-id
|
||||
:transaction/approval-status :transaction-approval-status/approved
|
||||
:transaction/vendor vendor
|
||||
:transaction/location "A"
|
||||
:transaction/accounts
|
||||
[#:transaction-account
|
||||
{:db/id (random-tempid)
|
||||
:account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
|
||||
:location "A"
|
||||
:amount (Math/abs (:transaction/amount transaction))}]))]
|
||||
(-> [[:upsert-transaction
|
||||
(assoc transaction
|
||||
:transaction/payment payment-id
|
||||
:transaction/approval-status :transaction-approval-status/approved
|
||||
:transaction/vendor vendor
|
||||
:transaction/location "A"
|
||||
:transaction/accounts
|
||||
[#:transaction-account
|
||||
{:db/id (random-tempid)
|
||||
:account (:db/id (a/get-account-by-numeric-code-and-sets 21000 ["default"]))
|
||||
:location "A"
|
||||
:amount (Math/abs (:transaction/amount transaction))}])]]
|
||||
|
||||
(conj {:payment/bank-account bank-account-id
|
||||
:payment/client client-id
|
||||
:payment/amount (- (:transaction/amount transaction))
|
||||
:payment/vendor vendor
|
||||
:payment/date (:transaction/date transaction)
|
||||
:payment/type :payment-type/debit
|
||||
:payment/status :payment-status/cleared
|
||||
:db/id payment-id})
|
||||
:payment/client client-id
|
||||
:payment/amount (- (:transaction/amount transaction))
|
||||
:payment/vendor vendor
|
||||
:payment/date (:transaction/date transaction)
|
||||
:payment/type :payment-type/debit
|
||||
:payment/status :payment-status/cleared
|
||||
:db/id payment-id})
|
||||
(into (mapcat (fn [[_ invoice-id invoice-amount]]
|
||||
[{:invoice-payment/invoice invoice-id
|
||||
:invoice-payment/payment payment-id
|
||||
:invoice-payment/amount invoice-amount}
|
||||
`(upsert-invoice ~{:db/id invoice-id
|
||||
:invoice/outstanding-balance 0.0
|
||||
:invoice/status :invoice-status/paid})])
|
||||
[:upsert-invoice {:db/id invoice-id
|
||||
:invoice/outstanding-balance 0.0
|
||||
:invoice/status :invoice-status/paid}]])
|
||||
invoice-payments)))))
|
||||
|
||||
(defn extract-check-number [{:transaction/keys [description-original]}]
|
||||
@@ -293,11 +293,11 @@
|
||||
:import-batch/not-ready 0
|
||||
:import-batch/extant 0})
|
||||
extant-cache (atom (cache/ttl-cache-factory {} :ttl 60000 ))
|
||||
import-id (get (:tempids (dc/transact conn {:tx-data [{:db/id "import-batch"
|
||||
:import-batch/date (coerce/to-date (t/now))
|
||||
:import-batch/source source
|
||||
:import-batch/status :import-status/started
|
||||
:import-batch/user-name user}]})) "import-batch")
|
||||
import-id (get (:tempids @(dc/transact conn [{:db/id "import-batch"
|
||||
:import-batch/date (coerce/to-date (t/now))
|
||||
:import-batch/source source
|
||||
:import-batch/status :import-status/started
|
||||
:import-batch/user-name user}])) "import-batch")
|
||||
rule-applying-function (rm/rule-applying-fn (tr/get-all))]
|
||||
(log/info "Importing transactions from " source)
|
||||
|
||||
@@ -317,7 +317,7 @@
|
||||
:error :import-batch/error
|
||||
:not-ready :import-batch/not-ready) inc))
|
||||
(when (= :import action)
|
||||
(audit-transact [`(upsert-transaction ~(transaction->txs transaction bank-account rule-applying-function))
|
||||
(audit-transact [[:upsert-transaction (transaction->txs transaction bank-account rule-applying-function)]
|
||||
{:db/id import-id
|
||||
:import-batch/entry (:db/id transaction)}]
|
||||
{:user/name user
|
||||
@@ -329,14 +329,14 @@
|
||||
(fail! [_ error]
|
||||
(log/errorf "Couldn't complete import %d with error." import-id)
|
||||
(log/error error)
|
||||
(dc/transact conn {:tx-data [(merge {:db/id import-id
|
||||
:import-batch/status :import-status/completed
|
||||
:import-batch/error-message (str error)}
|
||||
@stats)]}))
|
||||
@(dc/transact conn [(merge {:db/id import-id
|
||||
:import-batch/status :import-status/completed
|
||||
:import-batch/error-message (str error)}
|
||||
@stats)]))
|
||||
|
||||
(finish! [_]
|
||||
(log/infof "Finishing import batch %d for %s with stats %s " import-id (name source) (pr-str @stats))
|
||||
(dc/transact conn [(merge {:db/id import-id
|
||||
@(dc/transact conn [(merge {:db/id import-id
|
||||
|
||||
:import-batch/status :import-status/completed}
|
||||
@stats)])))))
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.string :as str]
|
||||
[com.unbounce.dogstatsd.core :as statsd]
|
||||
[datomic.client.api :as dc]
|
||||
[datomic.api :as dc]
|
||||
[digest :as di]
|
||||
[unilog.context :as lc]))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user