This commit is contained in:
2024-05-26 20:22:13 -07:00
111 changed files with 10708 additions and 5015 deletions

View File

@@ -41,7 +41,7 @@
(de/chain
(de/loop [attempt 0]
(-> (de/chain (de/future-with (ex/execute-pool)
(log/info ::request-started
#_(log/info ::request-started
:url (:url request)
:attempt attempt
:source "Square 3"
@@ -53,6 +53,7 @@
#_#_:connection-request-timeout 5000
:as :json))
(catch Throwable e
(println e)
(log/warn ::raw-request-failed
:exception e)
(throw e)))))
@@ -313,7 +314,8 @@
(capture-context->lc
(let [is-order-only-for-charge? (= ["CUSTOM_AMOUNT"]
(mapv :item_type (:line_items order)))]
(if is-order-only-for-charge?
(if (and is-order-only-for-charge?
(not ((set (:client/feature-flags client)) "import-custom-amount")))
(de/success-deferred
(->> (:tenders order)
(map #(tender->charge order client location %))))
@@ -371,6 +373,7 @@
(fn [e]
(log/error ::failed-to-transform-order
:exception e)))))))
(defn should-import-order? [order]
;; sometimes orders stay open in square. At least one payment
;; is needed to import, in order to avoid importing orders in-progress.
@@ -731,6 +734,7 @@
(def square-read [:db/id
:client/code
:client/square-auth-token
:client/feature-flags
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}])
(defn get-square-clients
@@ -739,6 +743,7 @@
:client/square-integration-status
:client/code
:client/square-auth-token
:client/feature-flags
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}])
:in $
:where [?c :client/square-auth-token]]
@@ -747,6 +752,7 @@
(map first (dc/q '[:find (pull ?c [:db/id
:client/code
:client/square-auth-token
:client/feature-flags
{:client/square-locations [:db/id :square-location/name :square-location/square-id :square-location/client-location]}])
:in $ [?code ...]
:where [?c :client/square-auth-token]
@@ -784,13 +790,17 @@
:square-location/square-id (:id square-location)})))))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn reset []
(defn reset [client]
(->>
(dc/q {:find ['?e]
:in ['$]
:where ['(or [?e :sales-order/date]
[?e :expected-deposit/date])]}
(dc/db conn))
:in ['$ '?c]
:where ['(or [?e :sales-order/client ?c]
[?e :expected-deposit/client ?c]
[?e :sales-refund/client ?c]
[?e :charge/client ?c]
[?e :cash-drawer-shift/client ?c])]}
(dc/db conn)
client)
(map first)
(map (fn [x] [:db/retractEntity x]))))
@@ -800,6 +810,69 @@
:client/square-integration-status (assoc integration-status
:db/id (or (-> client :client/square-integration-status :db/id)
(str (java.util.UUID/randomUUID))))}]))
(defn max-date [d1 d2]
(if (time/after? d1 d2)
d1
d2))
(defn remove-voided-orders
([client]
(apply de/zip
(for [square-location (:client/square-locations client)
:when (:square-location/client-location square-location)]
(remove-voided-orders client square-location (time/plus (time/now) (time/days -14)) (time/now)))))
([client location start end]
(let [start (max-date start (coerce/to-date-time #inst "2024-04-15T00:00:00-08:00"))]
(capture-context->lc
(-> (de/chain (search client location start end)
(fn [search-results]
(->> (or search-results [])
(s/->source)
(s/filter #(not (should-import-order? %)))
(s/map #(mu/with-context lc (order->sales-order client location %)))
(s/buffer 10)
(s/realize-each)
(s/filter (fn already-exists [[o]]
(when (:sales-order/external-id o)
(seq (dc/q '[:find ?i
:in $ ?ei
:where [?i :sales-order/external-id ?ei]]
(dc/db conn)
(:sales-order/external-id o))))))
(s/map (fn [[o]]
[[:db/retractEntity [:sales-order/external-id (:sales-order/external-id o)]]]))
(s/reduce into [])))
(fn [results]
(mu/with-context lc
(doseq [x (partition-all 100 results)]
(log/info ::removing-orders
:count (count x))
@(dc/transact-async conn x)))))
(de/catch (fn [e]
(log/warn ::couldnt-remove :error e)
nil) ))))))
#_(comment
(require 'auto-ap.time-reader)
@(let [[c [l]] (get-square-client-and-location "NGAK") ]
(log/peek :x [ c l])
(remove-voided-orders c l #clj-time/date-time "2024-04-11" #clj-time/date-time "2024-04-15"))
(doseq [c (get-square-clients)]
(try
@(remove-voided-orders c)
(catch Exception e
nil)))
)
(defn upsert-all [& clients]
(capture-context->lc
@@ -819,6 +892,10 @@
(mu/with-context lc
(log/info ::upsert-orders-started)
(upsert client)))
(fn [_]
(mu/with-context lc
(log/info ::remove-voided-orders-started)
(remove-voided-orders client)))
(fn [_]
(mu/with-context lc
(log/info ::upsert-payouts-started)
@@ -936,7 +1013,25 @@
(clojure.pprint/pprint (let [[c [l]] (get-square-client-and-location "NGWC")]
(require 'auto-ap.time-reader)
@(upsert-all "NGPG")
(clojure.pprint/pprint (let [[c [l]] (get-square-client-and-location "NGVT")]
l
(def z @(search c l #clj-time/date-time "2024-04-25T00:00:00-08:00"
#clj-time/date-time "2024-04-28T00:00:00-08:00"))))
(->> z
(filter (fn [o]
(seq (filter (comp #{"OTHER"} :type) (:tenders o)))))
(filter #(not (:name (:source %))))
(count)
)
#_(filter (comp #{"OTHER"} :type) (mapcat :tenders z))
(get-order c l "yzmLBYVGhKXUPwGXm482GJb2VX9YY")))
)