Really fixes issue with square and deleted categories.

This commit is contained in:
2023-01-21 22:18:45 -08:00
parent 01af0b7b3c
commit f5ef0e9b64

View File

@@ -43,7 +43,7 @@
(client/request (assoc request (client/request (assoc request
:socket-timeout 5000 :socket-timeout 5000
:connection-timeout 5000 :connection-timeout 5000
:connection-request-timeout 5000 #_#_:connection-request-timeout 5000
:as :json)) :as :json))
(catch Throwable e (catch Throwable e
(log/warn ::raw-request-failed (log/warn ::raw-request-failed
@@ -107,7 +107,7 @@
(def item-cache (atom {})) (def item-cache (atom {}))
(defn fetch-catalog [client i] (defn fetch-catalog [client i v]
(capture-context->lc (capture-context->lc
(de/chain (de/chain
[client i] [client i]
@@ -116,7 +116,8 @@
(manifold-api-call {:url (str "https://connect.squareup.com/v2/catalog/object/" i) (manifold-api-call {:url (str "https://connect.squareup.com/v2/catalog/object/" i)
:method :get :method :get
:headers (client-base-headers client) :headers (client-base-headers client)
:query-params {"include_related_items" "true"} :query-params {"include_related_items" "true"
"catalog_version" v}
:as :json}))) :as :json})))
:body :body
:object :object
@@ -124,26 +125,26 @@
%)))) %))))
(defn fetch-catalog-cache [client i] (defn fetch-catalog-cache [client i version]
(if (get @item-cache i) (if (get @item-cache i)
(de/success-deferred (get @item-cache i)) (de/success-deferred (get @item-cache i))
(fetch-catalog client i))) (fetch-catalog client i version)))
(defn item->category-name-impl [client item ] (defn item->category-name-impl [client item version]
(capture-context->lc (capture-context->lc
(cond (:item_id (:item_variation_data item)) (cond (:item_id (:item_variation_data item))
(de/catch (de/chain (fetch-catalog-cache client (:item_id (:item_variation_data item))) (de/catch (de/chain (fetch-catalog-cache client (:item_id (:item_variation_data item)) version)
(fn [item] (fn [item]
(mu/with-context lc (mu/with-context lc
(item->category-name-impl client item)))) (item->category-name-impl client item version))))
(fn [e] (fn [e]
(log/warn ::couldnt-fetch-variation (log/warn ::couldnt-fetch-variation
:exception e) :exception e)
"Uncategorized")) "Uncategorized"))
(:category_id (:item_data item)) (:category_id (:item_data item))
(de/catch (de/chain (fetch-catalog-cache client (:category_id (:item_data item))) (de/catch (de/chain (fetch-catalog-cache client (:category_id (:item_data item)) version)
:category_data :category_data
:name) :name)
(fn [e] (fn [e]
@@ -161,16 +162,21 @@
"Uncategorized")))) "Uncategorized"))))
(defn item-id->category-name [client i] (defn item-id->category-name [client i version]
(capture-context->lc (capture-context->lc
(-> [client i] (-> [client i]
(de/chain (de/chain
(fn [[client i]] (fn [[client i]]
(if (str/blank? i) (if (str/blank? i)
"Uncategorized" "Uncategorized"
(de/chain (fetch-catalog-cache client i) (de/catch (de/chain (fetch-catalog-cache client i version)
#(mu/with-context lc #(mu/with-context lc
(item->category-name-impl client %))))))))) (item->category-name-impl client % version)))
(fn [error]
(log/warn ::couldnt-fetch-item
:exception error)
"Uncategorized"
(throw error)))))))))
(defn pc [start end] (defn pc [start end]
{"query" {"filter" {"date_time_filter" {"query" {"filter" {"date_time_filter"
@@ -326,17 +332,22 @@
(s/transform (s/transform
(map-indexed (fn [i li] (map-indexed (fn [i li]
(mu/with-context lc (mu/with-context lc
(de/let-flow [category (item-id->category-name client (:catalog_object_id li))] (->
(remove-nils (de/let-flow [category (item-id->category-name client (:catalog_object_id li) (:catalog_version li))]
#:order-line-item (remove-nils
{:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order) "-" i) #:order-line-item
:item-name (:name li) {:external-id (str "square/order/" (:client/code client) "-" (:square-location/client-location location) "-" (:id order) "-" i)
:category (if (= "GIFT_CARD" (:item_type li)) :item-name (:name li)
"Gift Card" :category (if (= "GIFT_CARD" (:item_type li))
category) "Gift Card"
:total (amount->money (:total_money li)) category)
:tax (amount->money (:total_tax_money li)) :total (amount->money (:total_money li))
:discount (amount->money (:total_discount_money li))}))))) :tax (amount->money (:total_tax_money li))
:discount (amount->money (:total_discount_money li))}))
(de/catch (fn [e]
(log/error ::cant-transform
:exception e
:line-item li)))))))
) )
(s/buffer 5) (s/buffer 5)
(s/realize-each) (s/realize-each)
@@ -374,24 +385,28 @@
(daily-results client location (time/plus (time/now) (time/days -7)) (time/now))) (daily-results client location (time/plus (time/now) (time/days -7)) (time/now)))
([client location start end] ([client location start end]
(capture-context->lc (capture-context->lc
(de/chain (search client location start end) (->
(fn [search-results] (de/chain (search client location start end)
(->> search-results (fn [search-results]
(s/->source) (->> search-results
(s/filter (fn [order] (s/->source)
;; sometimes orders stay open in square. At least one payment (s/filter (fn [order]
;; is needed to import, in order to avoid importing orders in-progress. ;; sometimes orders stay open in square. At least one payment
(and ;; is needed to import, in order to avoid importing orders in-progress.
(or (> (count (:tenders order)) 0) (and
(seq (:returns order))) (or (> (count (:tenders order)) 0)
(or (= #{} (set (map #(:status (:card_details %)) (:tenders order)))) (seq (:returns order)))
(not= #{} (set/difference (or (= #{} (set (map #(:status (:card_details %)) (:tenders order))))
(set (map #(:status (:card_details %)) (:tenders order))) (not= #{} (set/difference
#{"FAILED" "VOIDED"})))))) (set (map #(:status (:card_details %)) (:tenders order)))
(s/map #(mu/with-context lc (order->sales-order client location %))) #{"FAILED" "VOIDED"}))))))
(s/buffer 10) (s/map #(mu/with-context lc (order->sales-order client location %)))
(s/realize-each) (s/buffer 10)
(s/reduce into []))))))) (s/realize-each)
(s/reduce into []))))
(de/catch (fn [e]
(log/error ::cant-create-results
:exception e)))))))
(defn get-payment [client p] (defn get-payment [client p]