EZ cater is available now.

This commit is contained in:
2022-07-30 16:25:21 -07:00
parent e38388b980
commit 46cfb440ef
4 changed files with 315 additions and 5 deletions

View File

@@ -8,7 +8,8 @@
[clj-time.coerce :as coerce]
[clojure.tools.logging :as log]
[clj-time.core :as time]
[clojure.set :as set]))
[clojure.set :as set]
[auto-ap.time :as atime]))
(defn query [{:ezcater-integration/keys [api-key]} q]
(-> (client/post "https://api.ezcater.com/graphql/"
@@ -89,8 +90,7 @@
:parentId parentId
:eventEntity 'Order
:eventKey 'cancelled}}
[[:subscription [:parentId :parentEntity :eventEntity :eventKey]]]]]}))
)))
[[:subscription [:parentId :parentEntity :eventEntity :eventKey]]]]]})))))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn upsert-ezcater
@@ -109,5 +109,127 @@
(mark-integration-status integration {:integration-status/state :integration-state/failed
:integration-status/message (.getMessage e)}))))))
(defn get-caterer [caterer-uuid]
(d/pull (d/db conn)
'[:ezcater-caterer/name
{:ezcater-integration/_caterers [:ezcater-integration/api-key]}
{:ezcater-location/_caterer [:ezcater-location/location
{:client/_ezcater-locations [:client/code]}]}]
[:ezcater-caterer/uuid caterer-uuid]))
(defn commision [order]
(let [commision% (if (= "MARKETPLACE" (:orderSourceType order))
0.15
0.07)]
(* commision%
0.01
(+
(-> order :totals :subTotal :subunits )
(reduce +
0
(map :subunits (:feesAndDiscounts (:catererCart order))))))))
(defn ccp-fee [order]
(* 0.000275
(+
(-> order :totals :subTotal :subunits )
(-> order :totals :salesTax :subunits )
(reduce +
0
(map :subunits (:feesAndDiscounts (:catererCart order)))))))
(defn order->sales-order [{{:keys [timestamp]} :event {:keys [orderItems]} :catererCart :keys [client-code client-location uuid] :as order}]
#:sales-order
{:date (atime/localize (coerce/to-date-time timestamp))
:external-id (str "ezcater/order/" client-code "-" client-location "-" uuid)
:client [:client/code client-code]
:location client-location
:line-items (->> orderItems
(map-indexed (fn [i li]
#:order-line-item
{:external-id (str "ezcater/order/" client-code "-" client-location "-" uuid "-" i)
:item-name (:name li)
:category "External Catering"
:total (* 0.01 (:subunits (:totalInSubunits li)))})))
:total (-> order :catererCart :totals :catererTotalDue )
:discount (- (+ (-> order :totals :subTotal :subunits (* 0.01))
(-> order :totals :salesTax :subunits (* 0.01)))
(-> order :catererCart :totals :catererTotalDue )
(commision order)
(ccp-fee order))
:service-charge (+ (commision order) (ccp-fee order))
:tax (-> order :totals :salesTax :subunits (* 0.01))
:tip (-> order :totals :tip :subunits (* 0.01))
:vendor :vendor/ccp-ezcater})
(defn lookup-order [json]
(let [caterer (get-caterer (get json "parent_id"))
integration (:ezcater-integration/_caterers caterer)
client (-> caterer :ezcater-location/_caterer first :client/_ezcater-locations :client/code)
location (-> caterer :ezcater-location/_caterer first :ezcater-location/location)]
(-> (query
integration
{:venia/queries [[:order {:id (get json "entity_id")}
[:uuid
:orderSourceType
[:caterer
[:name
:uuid
[:address [:street]]]]
[:event
[:timestamp
:catererHandoffFoodTime
:orderType]]
[:catererCart [[:orderItems
[:name
:quantity
:posItemId
[:totalInSubunits
[:currency
:subunits]]]]
[:totals
[:catererTotalDue]]
[:feesAndDiscounts
{:type 'DELIVERY_FEE}
[[:cost
[:currency
:subunits]]]]]]
[:totals [[:customerTotalDue
[
:currency
:subunits
]]
[:pointOfSaleIntegrationFee
[
:currency
:subunits
]]
[:tip
[:currency
:subunits]]
[:salesTax
[
:currency
:subunits
]]
[:salesTaxRemittance
[:currency
:subunits
]]
[:subTotal
[:currency
:subunits]]]]]]]})
(:order)
(assoc :client-code client
:client-location location))))
(defn import-order [json]
;; {"id" "bf3dcf5c-a68f-42d9-9084-049133e03d3d", "parent_type" "Caterer", "parent_id" "91541331-d7ae-4634-9e8b-ccbbcfb2ce70", "entity_type" "Order", "entity_id" "9ab05fee-a9c5-483b-a7f2-14debde4b7a8", "key" "accepted", "occurred_at" "2022-07-21T19:21:07.549Z"}
@(d/transact conn [(-> json
(lookup-order)
(order->sales-order)
(update :sales-order/date coerce/to-date))])
)