minor tweaks.
This commit is contained in:
108
scratch-sessions/payouts.repl
Normal file
108
scratch-sessions/payouts.repl
Normal file
@@ -0,0 +1,108 @@
|
||||
;; This buffer is for Clojure experiments and evaluation.
|
||||
|
||||
;; Press C-j to evaluate the last expression.
|
||||
|
||||
;; You can also press C-u C-j to evaluate the expression and pretty-print its result.
|
||||
|
||||
|
||||
(user/start-db)
|
||||
|
||||
(def c
|
||||
(first (get-square-clients "NGOP")))
|
||||
|
||||
(def l {:db/id 17592186051554,
|
||||
:square-location/name "Almaden",
|
||||
:square-location/square-id "68MJ8J2RX7KE1"})
|
||||
|
||||
(def base @(settlements c l))
|
||||
(def base-result @(daily-settlements c l))
|
||||
|
||||
base-result
|
||||
|
||||
|
||||
(def new
|
||||
@(de/chain (manifold-api-call
|
||||
{:url (str "https://connect.squareup.com/v2/payouts/?"
|
||||
(url/map->query
|
||||
{:location_id (:square-location/square-id l)}) )
|
||||
:method :get
|
||||
:headers (client-base-headers c "2023-04-19")
|
||||
})
|
||||
:body
|
||||
:payouts
|
||||
(fn [payouts]
|
||||
(->> payouts
|
||||
(s/->source)
|
||||
(s/map (fn [payout]
|
||||
(mu/with-context {}
|
||||
(log/info ::looking-up-payout
|
||||
:payout-id (:id payout))
|
||||
(de/chain
|
||||
(manifold-api-call
|
||||
{:url (str "https://connect.squareup.com/v2/payouts/" (:id payout) "/payout-entries")
|
||||
:method :get
|
||||
:headers (client-base-headers c "2023-04-19")
|
||||
:as :json})
|
||||
:body
|
||||
:payout_entries
|
||||
(fn [b]
|
||||
(assoc payout :payout_entries b))))))
|
||||
(s/buffer 10)
|
||||
(s/realize-each)
|
||||
(s/reduce conj [])))
|
||||
#_(fn [settlements]
|
||||
(log/info ::transforming-settlements)
|
||||
(try
|
||||
(->> (for [settlement settlements
|
||||
:let [best-sales-date (some->> (dc/q '[:find ?s4 (count ?s)
|
||||
:in $ ?settlement-id
|
||||
:where
|
||||
[?settlement :expected-deposit/external-id ?settlement-id]
|
||||
[?settlement :expected-deposit/charges ?c]
|
||||
[?s :sales-order/charges ?c]
|
||||
[?s :sales-order/date ?sales-date]
|
||||
[(clj-time.coerce/to-date-time ?sales-date) ?s2]
|
||||
[(auto-ap.time/localize ?s2) ?s3]
|
||||
[(clj-time.coerce/to-local-date ?s3) ?s4]]
|
||||
(dc/db conn)
|
||||
(str "square/payout/" (:id settlement)))
|
||||
(sort-by last)
|
||||
last
|
||||
first
|
||||
coerce/to-date-time
|
||||
atime/as-local-time
|
||||
coerce/to-date)]]
|
||||
#:expected-deposit {:external-id (str "square/payout/" (:id settlement))
|
||||
:vendor :vendor/ccp-square
|
||||
:status :expected-deposit-status/pending
|
||||
:total (amount->money (:amount_money settlement))
|
||||
:client (:db/id c)
|
||||
:location (:square-location/client-location l)
|
||||
:fee (- (reduce + 0.0 (map (fn [entry]
|
||||
(if (= (:type entry) "REFUND")
|
||||
(- (amount->money (:fee_amount_money entry)))
|
||||
(amount->money (:fee_amount_money entry))))
|
||||
(:payout_entries settlement))))
|
||||
:date (-> (:created_at settlement)
|
||||
(coerce/to-date))
|
||||
:sales-date (or best-sales-date
|
||||
(-> (:initiated_at settlement)
|
||||
(coerce/to-date)))
|
||||
:charges (reverse (->> (:payout_entries settlement)
|
||||
(filter (comp :payment_id :type_charge_details))
|
||||
(map (fn [p] {:charge/external-id (str "square/charge/" (:payment_id (:type_charge_details p)))}))))})
|
||||
(filter :expected-deposit/date)
|
||||
(into []))
|
||||
(catch Throwable e
|
||||
(log/error ::transform-settlement-failed
|
||||
:exception e))))
|
||||
))
|
||||
|
||||
(clojure.data/diff
|
||||
(update (first (drop 2 new)) :expected-deposit/charges #(into #{} %))
|
||||
|
||||
(update (first (reverse base-result)) :expected-deposit/charges #(into #{} %)))
|
||||
|
||||
|
||||
|
||||
@(get-payment c "lo1H33efDPosiQBNSni0fFcGEXGZY")
|
||||
@@ -59,7 +59,7 @@
|
||||
(fn [e]
|
||||
(if (>= attempt 5)
|
||||
(throw e)
|
||||
(de/chain
|
||||
(de/chain
|
||||
(mt/in 1000 (fn [] 1))
|
||||
(fn [_] (de/recur (inc attempt)))))))
|
||||
(de/chain identity)))
|
||||
|
||||
@@ -77,6 +77,8 @@ Set prod web workers to 0
|
||||
administer-system, upgrade schema
|
||||
Merge branch into master
|
||||
git push deploy master
|
||||
(auto-ap.datomic/transact-schema auto-ap.datomic/conn)
|
||||
(auto-ap.datomic/install-functions)
|
||||
regenerate all sales queries (clients.clj)
|
||||
(auto-ap.ledger/reset-client+account+location+date)
|
||||
(auto-ap.ledger/rebuild-running-balance-cache)
|
||||
@@ -102,7 +104,7 @@ regenerate all sales queries (clients.clj)
|
||||
(dc/transact conn {:tx-data batch})
|
||||
|
||||
)
|
||||
(user/index-solr)
|
||||
auser/index-solr)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user