Adding refunds
This commit is contained in:
@@ -78,8 +78,7 @@
|
||||
}}})
|
||||
|
||||
|
||||
(defn pc [d]
|
||||
{"query" {"filter" {"date_time_filter"
|
||||
(defn pc [d] {"query" {"filter" {"date_time_filter"
|
||||
{
|
||||
"created_at" {
|
||||
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles")))
|
||||
@@ -127,7 +126,7 @@
|
||||
"SCX0Y8CTGM1S0" ["NGE1" "UC"]
|
||||
"FNH5VRT890WK8" ["NGMJ" "SC"]
|
||||
"AMQ0NPA8FGDEF" ["NGPG" "SZ"]
|
||||
"4X8T65741AEPS" ["NGVZ" "NP"]
|
||||
"ACNTYY8WVZ6DV" ["NGVZ" "NP"]
|
||||
"KMVFQ9CRCXJ10" ["NGZO" "VT"]} location))
|
||||
|
||||
;; to get totals:
|
||||
@@ -243,6 +242,30 @@
|
||||
:date (-> (:initiated_at settlement)
|
||||
(coerce/to-date))})))))
|
||||
|
||||
(defn refunds [l]
|
||||
(let [refunds (:refunds (:body (client/get (str "https://connect.squareup.com/v2/refunds?location_id=" l)
|
||||
{:headers {"Square-Version" "2021-05-13"
|
||||
"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))})))]
|
||||
(->> refunds
|
||||
(map (fn [r]
|
||||
(let [[client location] (location_id->client-location l)]
|
||||
#:sales-refund {:external-id (str "square/refund/" (:id r))
|
||||
:total (amount->money (:amount_money r))
|
||||
:fee (transduce
|
||||
(comp (map :amount_money) (map amount->money))
|
||||
+
|
||||
0.0
|
||||
(:processing_fee r))
|
||||
:client [:client/code client]
|
||||
:location location
|
||||
:date (coerce/to-date (:created_at r))
|
||||
}))))))
|
||||
|
||||
(defn upsert
|
||||
([]
|
||||
(upsert nil))
|
||||
@@ -283,6 +306,27 @@
|
||||
(log/error e)))
|
||||
(log/info "Done loading settlements")))
|
||||
|
||||
(defn upsert-refunds []
|
||||
(doseq [{location :id} (locations)]
|
||||
(when (location_id->client-location location)
|
||||
(lc/with-context {:source (str "Square refunds loading for " location)}
|
||||
(try
|
||||
(let [existing (->> (d/query {:query {:find ['?external-id]
|
||||
:in ['$]
|
||||
:where ['[_ :sales-refund/external-id ?external-id]]}
|
||||
:args [(d/db conn)]})
|
||||
(map first)
|
||||
set)
|
||||
_ (log/info (count existing) "refunds already exist")
|
||||
to-create (filter #(not (existing (:sales-refund/external-id %)))
|
||||
(refunds location))]
|
||||
(doseq [x (partition-all 20 to-create)]
|
||||
(log/info "Loading refund" (count x))
|
||||
@(d/transact conn x)))
|
||||
(catch Exception e
|
||||
(log/error e)))
|
||||
(log/info "Done loading refunds")))))
|
||||
|
||||
(defn reset []
|
||||
(->>
|
||||
(d/query {:query {:find ['?e]
|
||||
@@ -300,6 +344,10 @@
|
||||
:start (scheduler/every (* 14 60 1000) upsert-settlements)
|
||||
:stop (scheduler/stop square-settlement-loader))
|
||||
|
||||
(mount/defstate square-refund-loader
|
||||
:start (scheduler/every (* 30 60 1000) upsert-refunds)
|
||||
:stop (scheduler/stop square-refund-loader))
|
||||
|
||||
(comment
|
||||
(daily-results)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user