Starts deleting voided orders after they've been imported

This commit is contained in:
2024-04-18 00:33:54 -07:00
parent 61338dd4fc
commit 76c0ccb4a7

View File

@@ -800,6 +800,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 +882,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)