Makes integreat run on datomic cloud

This commit is contained in:
2022-08-23 12:13:12 -07:00
parent 58b9dcf272
commit d02fba2b44
58 changed files with 2163 additions and 1257 deletions

View File

@@ -1,6 +1,7 @@
(ns auto-ap.graphql.ledger
(:require
[auto-ap.datomic :refer [audit-transact-batch conn remove-nils uri]]
[auto-ap.datomic
:refer [audit-transact-batch conn pull-attr pull-many remove-nils]]
[auto-ap.datomic.accounts :as a]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.ledger :as l]
@@ -17,7 +18,7 @@
[clojure.tools.logging :as log]
[clojure.data.csv :as csv]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[datomic.api :as d]
[datomic.client.api :as dc]
[mount.core :as mount]
[com.brunobonacci.mulog :as mu]
[unilog.context :as lc]
@@ -135,19 +136,19 @@
[]))))
(defn build-account-lookup [client-id]
(let [accounts (by :db/id (map first (d/query {:query {:find ['(pull ?e [:db/id :account/name
(let [accounts (by :db/id (map first (dc/q {:query {:find ['(pull ?e [:db/id :account/name
:account/numeric-code
{:account/type [:db/ident]
:account/client-overrides [:account-client-override/client :account-client-override/name]}
])]
:in ['$]
:where ['[?e :account/name]]}
:args [(d/db (d/connect uri) )]})))
:args [(dc/db conn )]})))
bank-accounts (by :db/id (map first (d/query {:query {:find ['(pull ?e [:db/id :bank-account/name :bank-account/numeric-code {:bank-account/type [:db/ident]}])]
bank-accounts (by :db/id (map first (dc/q {:query {:find ['(pull ?e [:db/id :bank-account/name :bank-account/numeric-code {:bank-account/type [:db/ident]}])]
:in ['$]
:where ['[?e :bank-account/name]]}
:args [(d/db (d/connect uri))]})))
:args [(dc/db conn)]})))
overrides-by-client (->> accounts
vals
(mapcat (fn [a]
@@ -171,7 +172,7 @@
:client_id client-id})))
(defn full-ledger-for-client [client-id]
(->> (d/query
(->> (dc/q
{:query {:find ['?d '?jel '?account '?location '?debit '?credit]
:in ['$ '?client-id]
:where '[[?e :journal-entry/client ?client-id]
@@ -191,7 +192,7 @@
[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
[(get-else $ ?jel :journal-entry-line/location "") ?location]]
}
:args [(d/db (d/connect uri)) client-id]})
:args [(dc/db conn) client-id]})
(sort-by first)))
(defn get-balance-sheet [context args _]
@@ -259,14 +260,15 @@
(defn all-ids-not-locked [all-ids]
(->> all-ids
(d/q '[:find [?t ...]
(dc/q '[:find ?t
:in $ [?t ...]
:where
[?t :journal-entry/client ?c]
[(get-else $ ?c :client/locked-until #inst "2000-01-01") ?lu]
[?t :journal-entry/date ?d]
[(>= ?d ?lu)]]
(d/db conn))))
(dc/db conn))
(map first)))
(defn delete-external-ledger [context args _]
(let [_ (assert-admin (:id context))
@@ -275,7 +277,7 @@
(assoc :only-external true)
(<-graphql)
(assoc :per-page Integer/MAX_VALUE)
(#(l/raw-graphql-ids (d/db conn) %))
(#(l/raw-graphql-ids (dc/db conn) %))
:ids)
specific-ids (l/filter-ids (:ids args))
all-ids (all-ids-not-locked (into (set ids) specific-ids))]
@@ -426,7 +428,6 @@
(:location ea)
"'")
{:status :error})))
(when (and matching-account
(not (:account/location matching-account))
(= "A" (:location ea)))
@@ -513,7 +514,7 @@
(defn running-balance-for [client-id]
(let [lookup-account (build-account-lookup client-id)]
(->> (d/query
(->> (dc/q
{:query {:find ['?d '?e '?jel '?account '?location '?debit '?credit]
:in ['$ '?client-id]
:where '[[?e :journal-entry/client ?client-id]
@@ -533,7 +534,7 @@
[(get-else $ ?jel :journal-entry-line/credit 0.0) ?credit]
[(get-else $ ?jel :journal-entry-line/location "") ?location]]
}
:args [(d/db conn) client-id]})
:args [(dc/db conn) client-id]})
(sort-by (juxt first second))
(build-running-balance lookup-account))))
@@ -541,25 +542,23 @@
(defn build-running-balance-cache []
(let [clients-needing-refresh (if-let [last-run @last-run-running-balance]
(->> (d/query
(->> (dc/q
{:query {:find ['?v]
:in ['$ '?log '?since '?till]
:where ['[(tx-ids ?log ?since ?till) [?tx ...]]
'[$ _ :journal-entry/client ?v ?tx]]}
:args [(d/history (d/db conn))
(d/log conn)
last-run
(java.util.Date.)]})
:in ['$ '[?tx ...]]
:where ['[$ _ :journal-entry/client ?v ?tx]]}
:args [(dc/history (dc/db conn))
(map :t (mapcat :data (dc/tx-range conn {:start last-run
:end (java.util.Date.)})))]})
(map first)
(into #{}))
(into #{} (map :db/id (d-clients/get-all))))
(into #{}))
(into #{} (map :db/id (d-clients/get-all))))
starting (java.util.Date.)]
(log/info (count clients-needing-refresh) "Clients need their balance cache refreshed.")
(swap! running-balance-cache
merge
(reduce
(fn [acc client]
(log/info "Computing running balance cache for " (:client/code (d/entity (d/db conn) client)))
(log/info "Computing running balance cache for " (pull-attr (dc/db conn) :client/code client ))
(assoc acc client (running-balance-for client)))
{}
clients-needing-refresh))