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