everything is audited.
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
:orders))
|
||||
|
||||
|
||||
|
||||
(defn amount->money [amt]
|
||||
(* 0.01 (or (:amount amt) 0.0)))
|
||||
|
||||
@@ -77,6 +78,53 @@
|
||||
#_(daily-results)
|
||||
|
||||
|
||||
(defn retry
|
||||
([f] (retry f 0))
|
||||
([f i]
|
||||
(if (< i 5)
|
||||
(try
|
||||
(f)
|
||||
(catch Exception e
|
||||
(log/warn "error pulling http " e)
|
||||
(retry f (inc i))))
|
||||
(log/error "Too many failures"))))
|
||||
|
||||
(defn settlements [l]
|
||||
(log/info "Searching for" l)
|
||||
(let [settlements (->> (client/get (str "https://connect.squareup.com/v1/" l "/settlements")
|
||||
{:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
:as :json})
|
||||
:body
|
||||
(map :id))]
|
||||
(loop [[s & xs] (take 5 settlements)
|
||||
result []]
|
||||
(log/info "Looking up settlement " s " for location " l)
|
||||
(let [n (:body (retry #(client/get (str "https://connect.squareup.com/v1/" l "/settlements/" s)
|
||||
{:headers {"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
|
||||
"Content-Type" "application/json"}
|
||||
:as :json
|
||||
:retry-handler (fn [ex try-count http-context]
|
||||
(log/warn "Retrying after failure " ex)
|
||||
(if (> try-count 4) false true))})))]
|
||||
(if (seq xs)
|
||||
(recur xs (conj result n))
|
||||
(conj result n))))))
|
||||
|
||||
(defn daily-settlements []
|
||||
(->> (locations)
|
||||
(map :id)
|
||||
(filter location_id->client-location)
|
||||
(mapcat (fn [l]
|
||||
(for [settlement (settlements l)
|
||||
:let [[client loc] (location_id->client-location l)]]
|
||||
#:expected-deposit {:external-id (str "square/settlement/" (:id settlement))
|
||||
:total (amount->money (:total_money settlement))
|
||||
:client [:client/code client]
|
||||
:location loc
|
||||
:fee (- (reduce + 0 (map (comp amount->money :fee_money) (:entries settlement))))
|
||||
:date (-> (:initiated_at settlement)
|
||||
(coerce/to-date))})))))
|
||||
|
||||
(defn upsert []
|
||||
(lc/with-context {:source "Square loading"}
|
||||
@@ -96,6 +144,25 @@
|
||||
(catch Exception e
|
||||
(log/error e)))))
|
||||
|
||||
(defn upsert-settlements []
|
||||
(lc/with-context {:source "Square settlements loading "}
|
||||
(try
|
||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||
:in ['$]
|
||||
:where ['[_ :expected-deposit/external-id ?external-id]]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)
|
||||
set)
|
||||
_ (log/info (count existing) "settlements already exist")
|
||||
to-create (filter #(not (existing (:expected-deposit/external-id %)))
|
||||
(daily-settlements))]
|
||||
(doseq [x (partition-all 20 to-create)]
|
||||
(log/info "Loading expected deposit" (count x))
|
||||
@(d/transact conn x)))
|
||||
(catch Exception e
|
||||
(log/error e)))
|
||||
(log/info "Done loading settlements")))
|
||||
|
||||
(defn reset []
|
||||
(->>
|
||||
(d/query {:query {:find ['?e]
|
||||
@@ -109,6 +176,10 @@
|
||||
:start (scheduler/every (* 15 60 1000) upsert)
|
||||
:stop (scheduler/stop square-loader))
|
||||
|
||||
(mount/defstate square-settlement-loader
|
||||
:start (scheduler/every (* 15 60 1000) upsert-settlements)
|
||||
:stop (scheduler/stop square-settlement-loader))
|
||||
|
||||
(comment
|
||||
(daily-results)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user