adds integration statuses to bank account pages.

This commit is contained in:
2022-07-08 11:59:59 -07:00
parent 2f2d582a4b
commit 789914b3f7
13 changed files with 234 additions and 58 deletions

View File

@@ -14,7 +14,8 @@
[mount.core :as mount]
[unilog.context :as lc]
[yang.scheduler :as scheduler]
[clojure.core.async :as async]))
[clojure.core.async :as async]
[slingshot.slingshot :refer [try+]]))
(defn client-base-headers [client]
{"Square-Version" "2021-08-18"
@@ -385,22 +386,19 @@
(upsert client square-location (time/plus (time/now) (time/days -3)) (time/now))))
([client location start end]
(lc/with-context {:source "Square loading"}
(try
(let [existing (->> (d/query {: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)]})
(map first)
set)
_ (log/info (count existing) "Sales orders already exist")
to-create (filter #(not (existing (:sales-order/external-id %)))
(daily-results client location start end))]
(doseq [x (partition-all 20 to-create)]
(log/info "Loading " (count x))
@(d/transact conn x)))
(catch Exception e
(log/error e))))))
(let [existing (->> (d/query {: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)]})
(map first)
set)
_ (log/info (count existing) "Sales orders already exist")
to-create (filter #(not (existing (:sales-order/external-id %)))
(daily-results client location start end))]
(doseq [x (partition-all 20 to-create)]
(log/info "Loading " (count x))
@(d/transact conn x))))))
(defn upsert-settlements
([client]
@@ -409,12 +407,9 @@
(upsert-settlements client square-location)))
([client location]
(lc/with-context {:source "Square settlements loading"}
(try
(doseq [x (partition-all 20 (daily-settlements client location))]
(log/info "Loading expected deposit" (count x))
@(d/transact conn x))
(catch Exception e
(log/error e)))
(doseq [x (partition-all 20 (daily-settlements client location))]
(log/info "Loading expected deposit" (count x))
@(d/transact conn x))
(log/info "Done loading settlements"))))
(defn upsert-refunds
@@ -426,12 +421,9 @@
(lc/with-context {:source "Loading Square Settlements"
:client (:client/code client)
:location (:square-location/client-location client)}
(try
(doseq [x (partition-all 20 (refunds client location))]
(log/info "Loading refund" (count x))
@(d/transact conn x))
(catch Exception e
(log/error e)))
(doseq [x (partition-all 20 (refunds client location))]
(log/info "Loading refund" (count x))
@(d/transact conn x))
(log/info "Done loading refunds"))))
(def square-read [:db/id
@@ -442,6 +434,7 @@
(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]}]) ...]
@@ -487,17 +480,33 @@
(map first)
(map (fn [x] [:db/retractEntity x]))))
(defn upsert-all []
(doseq [client (get-square-clients)
(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]))}]))
(defn upsert-all [ & clients]
(doseq [client (apply get-square-clients clients)
:when (seq (filter :square-location/client-location (:client/square-locations client)))]
(lc/with-context {:client (:client/code client)}
(upsert-locations client)
(log/info "Loading Orders")
(upsert client)
(log/info "Loading Settlements")
(upsert-settlements client)
(log/info "Loading refunds")
(upsert-refunds client))))
(mark-integration-status client {:integration-status/last-attempt (coerce/to-date (time/now))})
(try+
(upsert-locations client)
(upsert client)
(upsert-settlements client)
(upsert-refunds client)
(mark-integration-status client {:integration-status/state :integration-state/success
:integration-status/last-updated (coerce/to-date (time/now))})
(catch [:status 401] data
(mark-integration-status client {:integration-status/state :integration-state/unauthorized
:integration-status/message (-> data :body )}))
(catch Exception e
(log/warn e)
(mark-integration-status client {:integration-status/state :integration-state/failed
:integration-status/message (.getMessage e)}))))))
(mount/defstate square-loader
:start (scheduler/every (* 4 59 60 1000) (heartbeat upsert-all "square-loading"))
@@ -505,6 +514,3 @@