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,7 +1,7 @@
(ns user
(:require
[amazonica.aws.s3 :as s3]
[auto-ap.datomic :refer [uri]]
[auto-ap.datomic :refer [conn pull-attr random-tempid]]
[auto-ap.ledger :as l :refer [transact-with-ledger]]
[auto-ap.server]
[auto-ap.square.core :as square]
@@ -17,7 +17,7 @@
[clojure.pprint]
[clojure.string :as str]
[config.core :refer [env]]
[datomic.api :as d]
[datomic.client.api :as dc]
[mount.core :as mount]
[nrepl.middleware.print]
[unilog.context :as lc]
@@ -70,71 +70,71 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn mark-until-date [client end]
(let [conn (d/connect uri)]
(doseq [p (->>
(d/query {:query {:find '[?e]
:in '[$ ?client ?end ]
:where [
'[?e :invoice/client ?c]
'[?c :client/code ?client]
'[?e :invoice/date ?d ]
'[(<= ?d ?end) ]]}
:args [(d/db conn)
client
(c/to-date end)]})
(mapv first)
(mapv (fn [i]
{:db/id i
:invoice/exclude-from-ledger true}))
(partition-all 100))]
(doseq [p (->>
(dc/q {:query {:find '[?e]
:in '[$ ?client ?end ]
:where [
'[?e :invoice/client ?c]
'[?c :client/code ?client]
'[?e :invoice/date ?d ]
'[(<= ?d ?end) ]]}
:args [(dc/db conn)
client
(c/to-date end)]})
(mapv first)
(mapv (fn [i]
{:db/id i
:invoice/exclude-from-ledger true}))
(partition-all 100))]
(transact-with-ledger p {:user/name "mark-until-date" :user/role "admin"})
(println "process 100"))
(transact-with-ledger p {:user/name "mark-until-date" :user/role "admin"})
(println "process 100"))
(doseq [p (->>
(d/query {:query {:find '[?e]
:in '[$ ?client ?end ]
:where [
'[?e :transaction/client ?c]
'[?c :client/code ?client]
'[?e :transaction/date ?d ]
'[(<= ?d ?end) ]]}
:args [(d/db conn)
client
(c/to-date end)]})
(mapv first)
(mapv (fn [i]
{:db/id i
:transaction/approval-status :transaction-approval-status/excluded}))
(partition-all 100))]
(doseq [p (->>
(dc/q {:query {:find '[?e]
:in '[$ ?client ?end ]
:where [
'[?e :transaction/client ?c]
'[?c :client/code ?client]
'[?e :transaction/date ?d ]
'[(<= ?d ?end) ]]}
:args [(dc/db conn)
client
(c/to-date end)]})
(mapv first)
(mapv (fn [i]
{:db/id i
:transaction/approval-status :transaction-approval-status/excluded}))
(partition-all 100))]
(transact-with-ledger p {:user/name "mark-until-date" :user/role "admin"}) (println "process 100"))))
(transact-with-ledger p {:user/name "mark-until-date" :user/role "admin"})
(println "process 100")))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn load-accounts [conn]
(let [[header & rows] (-> "master-account-list.csv" (io/resource) io/input-stream (BOMInputStream.) (io/reader) csv/read-csv)
code->existing-account (by :account/numeric-code (map first (d/query {:query {:find ['(pull ?e [:account/numeric-code
code->existing-account (by :account/numeric-code (map first (dc/q {:query {:find ['(pull ?e [:account/numeric-code
:db/id])]
:in ['$]
:where ['[?e :account/name]]}
:args [(d/db conn)]})))
:args [(dc/db conn)]})))
also-merge-txes (fn [also-merge old-account-id]
(if old-account-id
(let [[sunset-account]
(first (d/query {:query {:find ['?a ]
:in ['$ '?ac ]
:where ['[?a :account/numeric-code ?ac]]}
:args [(d/db conn) also-merge ]}))]
(first (dc/q {:query {:find ['?a ]
:in ['$ '?ac ]
:where ['[?a :account/numeric-code ?ac]]}
:args [(dc/db conn) also-merge ]}))]
(into (mapv
(fn [[entity id _]]
[:db/add entity id old-account-id])
(d/query {:query {:find ['?e '?id '?a ]
(dc/q {:query {:find ['?e '?id '?a ]
:in ['$ '?ac ]
:where ['[?a :account/numeric-code ?ac]
'[?e ?at ?a]
'[?at :db/ident ?id]]}
:args [(d/db conn) also-merge ]}))
:args [(dc/db conn) also-merge ]}))
[[:db/retractEntity sunset-account]]))
[]))
@@ -186,32 +186,33 @@
conj
[]
rows)]
@(d/transact conn txes)))
(dc/transact conn {:tx-data txes})))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn find-bad-accounts []
(set (map second (d/query {:query {:find ['(pull ?x [*]) '?z]
(set (map second (dc/q {:query {:find ['(pull ?x [*]) '?z]
:in ['$]
:where ['[?e :account/numeric-code ?z]
'[(<= ?z 9999)]
'[?x ?a ?e]]}
:args [(d/db (d/connect uri))]}))))
:args [(dc/db conn)]}))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn delete-4-digit-accounts []
@(d/transact (d/connect uri)
(transduce
(comp
(map first)
(map (fn [old-account-id]
[:db/retractEntity old-account-id])))
conj
[]
(d/query {:query {:find ['?e]
:in ['$]
:where ['[?e :account/numeric-code ?z]
'[(<= ?z 9999)]]}
:args [(d/db (d/connect uri))]})))
(dc/transact conn
{:tx-data
(transduce
(comp
(map first)
(map (fn [old-account-id]
[:db/retractEntity old-account-id])))
conj
[]
(dc/q {:query {:find ['?e]
:in ['$]
:where ['[?e :account/numeric-code ?z]
'[(<= ?z 9999)]]}
:args [(dc/db conn)]}))})
)
@@ -224,31 +225,30 @@
(fn [acc [e z]]
(update acc z conj e))
{}
(d/query {:query {:find ['?e '?z]
:in ['$]
:where ['[?e :account/numeric-code ?z]]}
:args [(d/db (d/connect uri))]}))))
(dc/q {:query {:find ['?e '?z]
:in ['$]
:where ['[?e :account/numeric-code ?z]]}
:args [(dc/db conn)]}))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn customize-accounts [customer filename]
(let [conn (d/connect uri)
[_ & rows] (-> filename (io/resource) io/input-stream (BOMInputStream.) (io/reader) csv/read-csv)
[client-id] (first (d/query (-> {:query {:find ['?e]
(let [[_ & rows] (-> filename (io/resource) io/input-stream (BOMInputStream.) (io/reader) csv/read-csv)
[client-id] (first (dc/q (-> {:query {:find ['?e]
:in ['$ '?z]
:where [['?e :client/code '?z]]}
:args [(d/db (d/connect uri)) customer]})))
:args [(dc/db conn) customer]})))
_ (println client-id)
code->existing-account (by :account/numeric-code (map first (d/query {:query {:find ['(pull ?e [:account/numeric-code
code->existing-account (by :account/numeric-code (map first (dc/q {:query {:find ['(pull ?e [:account/numeric-code
{:account/applicability [:db/ident]}
:db/id])]
:in ['$]
:where ['[?e :account/name]]}
:args [(d/db conn)]})))
:args [(dc/db conn)]})))
existing-account-overrides (d/query (-> {:query {:find ['?e]
existing-account-overrides (dc/q (-> {:query {:find ['?e]
:in ['$ '?client-id]
:where [['?e :account-client-override/client '?client-id]]}
:args [(d/db (d/connect uri)) client-id]}))
:args [(dc/db conn) client-id]}))
@@ -315,16 +315,16 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn fix-transactions-without-locations [client-code location]
(->>
(d/query {:query {:find ['(pull ?e [*])]
:in ['$ '?client-code]
:where ['[?e :transaction/accounts ?ta]
'[?e :transaction/matched-rule]
'[?e :transaction/approval-status :transaction-approval-status/approved]
'(not [?ta :transaction-account/location])
'[?e :transaction/client ?c]
'[?c :client/code ?client-code]
]}
:args [(d/db (d/connect uri)) client-code]})
(dc/q {:query {:find ['(pull ?e [*])]
:in ['$ '?client-code]
:where ['[?e :transaction/accounts ?ta]
'[?e :transaction/matched-rule]
'[?e :transaction/approval-status :transaction-approval-status/approved]
'(not [?ta :transaction-account/location])
'[?e :transaction/client ?c]
'[?c :client/code ?client-code]
]}
:args [(dc/db conn) client-code]})
(mapcat
(fn [[{:transaction/keys [accounts]}]]
(mapv
@@ -405,44 +405,48 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn entity-history [i]
(vec (sort-by first (d/query
(vec (sort-by first (dc/q
{:query {:find ['?tx '?z '?v ]
:in ['?i '$]
:where ['[?i ?a ?v ?tx ?ad]
'[?a :db/ident ?z]
'[(= ?ad true)]]}
:args [i (d/history (d/db (d/connect uri)))]}))))
:args [i (dc/history (dc/db conn))]}))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn entity-history-with-revert [i]
(vec (sort-by first (d/query
(vec (sort-by first (dc/q
{:query {:find ['?tx '?z '?v '?ad ]
:in ['?i '$]
:where ['[?i ?a ?v ?tx ?ad]
'[?a :db/ident ?z]]}
:args [i (d/history (d/db (d/connect uri)))]}))))
:args [i (dc/history (dc/db conn))]}))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn tx-detail [i]
(map (juxt :e #(d/ident (d/db (d/connect uri)) (:a %)) :v :added)
(map (juxt :e #(pull-attr (dc/db conn) :db/ident (:a %)) :v)
(:data (first
(d/tx-range (d/log (d/connect uri))
i
(inc i))))))
(dc/tx-range conn
{:start i
:end (inc i)})))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn tx-range-detail [i]
(map (juxt :e #(d/ident (d/db (d/connect uri)) (:a %)) :v)
(map (juxt :e #(pull-attr (dc/db conn) :db/ident (:a %)) :v)
(mapcat :data (d/tx-range (d/log (d/connect uri))
(- i 100)
(+ i 100)))))
(mapcat :data (dc/tx-range conn
{:start (- i 100)
:end (+ i 100)}))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn start-db []
(mu/start-publisher! {:type :dev})
(mount.core/start (mount.core/only #{#'auto-ap.datomic/conn})))
(mount.core/start (mount.core/only #{#'auto-ap.datomic/conn #'auto-ap.datomic/client})))
(defn restart-db []
(datomic.dev-local/release-db {:system "dev" :db-name "prod-migration"})
(mount.core/stop (mount.core/only #{#'auto-ap.datomic/conn #'auto-ap.datomic/client}))
(start-db))
@@ -481,29 +485,15 @@
(async/<!! (async/into [] output-chan))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn load-sales-for-day [date]
(doseq [client (d/q [:find [(list 'pull '?e square/square-read ) '...]
:where ['?e :client/square-locations ]]
(d/db auto-ap.datomic/conn))
square-location (:client/square-locations client)
:when (:square-location/client-location square-location)]
(println client)
(println "orders")
(lc/with-context {:source "Historical loading data"}
(square/upsert client square-location (c/to-date-time date)
(t/plus (c/to-date-time date) (t/days 1))))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn upsert-invoice-amounts [tsv]
(let [data (with-open [reader (io/reader (char-array tsv))]
(doall (csv/read-csv reader :separator \tab)))
db (d/db auto-ap.datomic/conn)
db (dc/db conn)
i->invoice-id (fn [i]
(try (Long/parseLong i)
(catch Exception e
(:db/id (d/pull db '[:db/id]
(:db/id (dc/pull db '[:db/id]
[:invoice/original-id (Long/parseLong (first (str/split i #"-")))])))))
invoice-totals (->> data
(drop 1)
@@ -521,7 +511,7 @@
:let [
invoice-id (i->invoice-id i)
invoice (d/entity db invoice-id)
invoice (dc/pull db '[FILL_IN] invoice-id)
current-total (:invoice/total invoice)
target-total (invoice-totals invoice-id) ;; TODO should include expense accounts not visible
new-account? (not (boolean (or (some-> invoice-expense-account-id not-empty Long/parseLong)
@@ -529,10 +519,9 @@
invoice-expense-account-id (or (some-> invoice-expense-account-id not-empty Long/parseLong)
(:db/id (first (:invoice/expense-accounts invoice)))
(d/tempid :db.part/user))
(random-tempid))
invoice-expense-account (when-not new-account?
(or (d/entity db invoice-expense-account-id)
(d/entity db [:invoice-expense-account/original-id invoice-expense-account-id])))
(dc/pull db '[FILL_IN]invoice-expense-account-id))
current-account-id (:db/id (:invoice-expense-account/account invoice-expense-account))
target-account-id (Long/parseLong (str/trim target-account))
@@ -548,19 +537,19 @@
target-expense-account-location location
[[_ _ invoice-payment]] (vec (d/q
'[:find ?p ?a ?ip
:in $ ?i
:where [?ip :invoice-payment/invoice ?i]
[?ip :invoice-payment/amount ?a]
[?ip :invoice-payment/payment ?p]
]
db invoice-id))]
[[_ _ invoice-payment]] (vec (dc/q
'[:find ?p ?a ?ip
:in $ ?i
:where [?ip :invoice-payment/invoice ?i]
[?ip :invoice-payment/amount ?a]
[?ip :invoice-payment/payment ?p]
]
db invoice-id))]
:when current-total]
[
(when (not (auto-ap.utils/dollars= current-total target-total))
{:db/id invoice-id
{:db/id invoice-id
:invoice/total target-total})
(when new-account?
@@ -596,19 +585,19 @@
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn get-schema [prefix]
(->> (d/q '[:find ?i
(->> (dc/q '[:find ?i
:in $ ?p
:where [_ :db/ident ?i]
[(namespace ?i) ?p]] (d/db auto-ap.datomic/conn) prefix)
[(namespace ?i) ?p]] (dc/db auto-ap.datomic/conn) prefix)
(mapcat identity)
vec))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn get-idents []
(->> (d/q '[:find ?i
(->> (dc/q '[:find ?i
:in $
:where [_ :db/ident ?i]]
(d/db auto-ap.datomic/conn) )
(dc/db conn) )
(mapcat identity)
(map str)
(sort)