Cash drawer setup
This commit is contained in:
@@ -19,10 +19,16 @@
|
||||
[manifold.stream :as s]
|
||||
[manifold.time :as mt]))
|
||||
|
||||
(defn client-base-headers [client]
|
||||
{"Square-Version" "2021-08-18"
|
||||
"Authorization" (str "Bearer " (:client/square-auth-token client))
|
||||
"Content-Type" "application/json"})
|
||||
(defn client-base-headers
|
||||
([client] (client-base-headers client "2021-08-18"))
|
||||
([client v]
|
||||
{"Square-Version" v
|
||||
"Authorization" (str "Bearer " (:client/square-auth-token client))
|
||||
"Content-Type" "application/json"}))
|
||||
|
||||
|
||||
(defn ->square-date [d]
|
||||
(f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") d))
|
||||
|
||||
|
||||
(def manifold-api-stream
|
||||
@@ -182,8 +188,8 @@
|
||||
{"query" {"filter" {"date_time_filter"
|
||||
{
|
||||
"created_at" {
|
||||
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") start)
|
||||
"end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") end)
|
||||
"start_at" (->square-date start)
|
||||
"end_at" (->square-date end)
|
||||
}}}
|
||||
|
||||
"sort" {
|
||||
@@ -611,6 +617,83 @@
|
||||
|
||||
(log/info ::done-loading-refunds)))))))
|
||||
|
||||
|
||||
(defn get-cash-shift [client id]
|
||||
(de/chain (manifold-api-call {:url (str (url/url "https://connect.squareup.com/v2/cash-drawers/shifts" id
|
||||
|
||||
))
|
||||
:method :get
|
||||
|
||||
:headers (client-base-headers client "2023-04-19")
|
||||
:as :json})
|
||||
:body
|
||||
:cash_drawer_shift))
|
||||
|
||||
(defn cash-drawer-shifts
|
||||
([client l]
|
||||
(cash-drawer-shifts client l (time/plus (time/now) (time/days -14)) (time/now)))
|
||||
([client l start end]
|
||||
(de/chain (manifold-api-call {:url (str "https://connect.squareup.com/v2/cash-drawers/shifts"
|
||||
"?"
|
||||
|
||||
(url/map->query
|
||||
{:location_id (:square-location/square-id l)
|
||||
:begin_time (->square-date start)
|
||||
:end_time (->square-date end)}))
|
||||
:method :get
|
||||
|
||||
:headers (client-base-headers client "2023-04-19")
|
||||
:as :json})
|
||||
:body
|
||||
:cash_drawer_shifts
|
||||
(fn [shifts]
|
||||
(->> shifts
|
||||
(filter (fn [r] (= "ENDED" (:state r))))
|
||||
(s/->source )
|
||||
(s/map (fn [s]
|
||||
(de/chain
|
||||
(get-cash-shift client (:id s))
|
||||
(fn [cash-drawer-shift]
|
||||
#:cash-drawer-shift {:external-id (str "square/cash-drawer-shift/" (:id cash-drawer-shift))
|
||||
:vendor :vendor/ccp-square
|
||||
:paid-in (amount->money (:cash_paid_in_money cash-drawer-shift))
|
||||
:paid-out (amount->money (:cash_paid_out_money cash-drawer-shift))
|
||||
:expected-cash (amount->money (:expected_cash_money cash-drawer-shift))
|
||||
:opened-cash (amount->money (:opened_cash_money cash-drawer-shift))
|
||||
:date (coerce/to-date (:opened_at cash-drawer-shift))
|
||||
:client (:db/id client)
|
||||
:location (:square-location/client-location l)
|
||||
}))))
|
||||
(s/buffer 5)
|
||||
(s/realize-each)
|
||||
(s/reduce conj []))))))
|
||||
|
||||
(defn upsert-cash-shifts
|
||||
([client]
|
||||
(apply de/zip
|
||||
(for [square-location (:client/square-locations client)
|
||||
:when (:square-location/client-location square-location)]
|
||||
(upsert-cash-shifts client square-location))))
|
||||
([client location]
|
||||
(with-context-as {:source "Square cash shift loading"
|
||||
:client (:client/code client)} lc
|
||||
|
||||
(de/chain (cash-drawer-shifts client location)
|
||||
(fn [cash-shifts]
|
||||
(mu/with-context lc
|
||||
(try
|
||||
(doseq [x (partition-all 100 cash-shifts)]
|
||||
(log/info ::loading-cash-shifts
|
||||
:count (count x)
|
||||
:sample (first x))
|
||||
@(d/transact conn x))
|
||||
|
||||
(catch Throwable e
|
||||
(log/error ::upsert-cash-shifts-failed
|
||||
:exception e)))
|
||||
|
||||
(log/info ::done-loading-cash-shifts)))))))
|
||||
|
||||
(def square-read [:db/id
|
||||
:client/code
|
||||
:client/square-auth-token
|
||||
@@ -704,6 +787,11 @@
|
||||
(mu/with-context lc
|
||||
(log/info ::upsert-refunds-started)
|
||||
(upsert-refunds client)))
|
||||
|
||||
(fn [_]
|
||||
(mu/with-context lc
|
||||
(log/info ::upsert-cash-shifts)
|
||||
(upsert-cash-shifts client)))
|
||||
(fn [_]
|
||||
(mu/with-context lc
|
||||
(log/info ::upsert-done))
|
||||
|
||||
Reference in New Issue
Block a user