Makes integreat run on datomic cloud
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
(ns auto-ap.square.core
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn remove-nils]]
|
||||
[auto-ap.datomic :refer [conn random-tempid remove-nils]]
|
||||
[auto-ap.time :as atime]
|
||||
[clj-http.client :as client]
|
||||
[clj-time.coerce :as coerce]
|
||||
@@ -12,7 +12,7 @@
|
||||
[clojure.set :as set]
|
||||
[clojure.string :as str]
|
||||
[clojure.tools.logging :as log]
|
||||
[datomic.api :as d]
|
||||
[datomic.client.api :as dc]
|
||||
[slingshot.slingshot :refer [try+]]
|
||||
[unilog.context :as lc]))
|
||||
|
||||
@@ -395,11 +395,11 @@
|
||||
(upsert client square-location (time/plus (time/now) (time/days -3)) (time/now))))
|
||||
([client location start end]
|
||||
(lc/with-context {:source "Square loading"}
|
||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||
(let [existing (->> (dc/q {:query {:find ['?external-id]
|
||||
:in ['$ '?client]
|
||||
:where ['[?o :sales-order/client ?client]
|
||||
'[?o :sales-order/external-id ?external-id]]}
|
||||
:args [(d/db conn) (:db/id client)]})
|
||||
:args [(dc/db conn) (:db/id client)]})
|
||||
(map first)
|
||||
set)
|
||||
_ (log/info (count existing) "Sales orders already exist")
|
||||
@@ -407,7 +407,7 @@
|
||||
(daily-results client location start end))]
|
||||
(doseq [x (partition-all 20 to-create)]
|
||||
(log/info "Loading " (count x))
|
||||
@(d/transact conn x))))))
|
||||
(dc/transact conn {:tx-data x}))))))
|
||||
|
||||
(defn upsert-settlements
|
||||
([client]
|
||||
@@ -419,7 +419,7 @@
|
||||
:client (:client/code client)}
|
||||
(doseq [x (partition-all 20 (daily-settlements client location))]
|
||||
(log/info "Loading expected deposit" (count x))
|
||||
@(d/transact conn x))
|
||||
(dc/transact conn {:tx-data x}))
|
||||
(log/info "Done loading settlements"))))
|
||||
|
||||
(defn upsert-refunds
|
||||
@@ -433,7 +433,7 @@
|
||||
:location (:square-location/client-location client)}
|
||||
(doseq [x (partition-all 20 (refunds client location))]
|
||||
(log/info "Loading refund" (count x))
|
||||
@(d/transact conn x))
|
||||
(dc/transact conn {:tx-data x}))
|
||||
(log/info "Done loading refunds"))))
|
||||
|
||||
(def square-read [:db/id
|
||||
@@ -443,26 +443,26 @@
|
||||
|
||||
(defn get-square-clients
|
||||
([]
|
||||
(d/q '[:find [(pull ?c [:db/id
|
||||
:client/square-integration-status
|
||||
:client/code
|
||||
:client/square-auth-token
|
||||
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}]) ...]
|
||||
:in $
|
||||
:where [?c :client/square-auth-token]
|
||||
(not [?c :client/feature-flags "new-square"])]
|
||||
(d/db conn)))
|
||||
(map first (dc/q '[:find (pull ?c [:db/id
|
||||
:client/square-integration-status
|
||||
:client/code
|
||||
:client/square-auth-token
|
||||
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}])
|
||||
:in $
|
||||
:where [?c :client/square-auth-token]
|
||||
(not [?c :client/feature-flags "new-square"])]
|
||||
(dc/db conn))))
|
||||
([ & codes]
|
||||
(d/q '[:find [(pull ?c [:db/id
|
||||
:client/code
|
||||
:client/square-auth-token
|
||||
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}]) ...]
|
||||
:in $ [?code ...]
|
||||
:where [?c :client/square-auth-token]
|
||||
(not [?c :client/feature-flags "new-square"])
|
||||
[?c :client/code ?code]]
|
||||
(d/db conn)
|
||||
codes)))
|
||||
(map first (dc/q '[:find (pull ?c [:db/id
|
||||
:client/code
|
||||
:client/square-auth-token
|
||||
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}])
|
||||
:in $ [?code ...]
|
||||
:where [?c :client/square-auth-token]
|
||||
(not [?c :client/feature-flags "new-square"])
|
||||
[?c :client/code ?code]]
|
||||
(dc/db conn)
|
||||
codes))))
|
||||
|
||||
(defn upsert-locations
|
||||
([] (doseq [client (get-square-clients)]
|
||||
@@ -474,31 +474,29 @@
|
||||
[(:square-location/square-id sl)
|
||||
(:db/id sl)])
|
||||
(:client/square-locations client)))]
|
||||
(->> (for [square-location (client-locations client)]
|
||||
{:db/id (or (square-id->id (:id square-location)) (d/tempid :db.part/user))
|
||||
:client/_square-locations (:db/id client)
|
||||
:square-location/name (:name square-location)
|
||||
:square-location/square-id (:id square-location)})
|
||||
(d/transact conn)
|
||||
deref))))
|
||||
(dc/transact conn {:tx-data (for [square-location (client-locations client)]
|
||||
{:db/id (or (square-id->id (:id square-location)) (random-tempid))
|
||||
:client/_square-locations (:db/id client)
|
||||
:square-location/name (:name square-location)
|
||||
:square-location/square-id (:id square-location)})}))))
|
||||
|
||||
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
|
||||
(defn reset []
|
||||
(->>
|
||||
(d/query {:query {:find ['?e]
|
||||
(dc/q {:query {:find ['?e]
|
||||
:in ['$]
|
||||
:where ['(or [?e :sales-order/date]
|
||||
[?e :expected-deposit/date])]}
|
||||
:args [(d/db conn)]})
|
||||
:args [(dc/db conn)]})
|
||||
(map first)
|
||||
(map (fn [x] [:db/retractEntity x]))))
|
||||
|
||||
(defn mark-integration-status [client integration-status]
|
||||
@(d/transact conn
|
||||
[{:db/id (:db/id client)
|
||||
:client/square-integration-status (assoc integration-status
|
||||
:db/id (or (-> client :client/square-integration-status :db/id)
|
||||
#db/id [:db.part/user]))}]))
|
||||
(dc/transact conn
|
||||
{:tx-data [{:db/id (:db/id client)
|
||||
:client/square-integration-status (assoc integration-status
|
||||
:db/id (or (-> client :client/square-integration-status :db/id)
|
||||
#db/id [:db.part/user]))}]}))
|
||||
|
||||
(defn upsert-all [ & clients]
|
||||
(doseq [client (apply get-square-clients clients)
|
||||
|
||||
Reference in New Issue
Block a user