setup for ezcater.
This commit is contained in:
@@ -6,20 +6,34 @@
|
||||
[venia.core :as v]
|
||||
[clojure.data.json :as json]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]))
|
||||
[clojure.tools.logging :as log]
|
||||
[clj-time.core :as time]
|
||||
[clojure.set :as set]))
|
||||
|
||||
(defn get-caterers [{:ezcater-integration/keys [api-key]}]
|
||||
(defn query [{:ezcater-integration/keys [api-key]} q]
|
||||
(-> (client/post "https://api.ezcater.com/graphql/"
|
||||
{:headers {"Authorization" api-key
|
||||
"Content-Type" "application/json"}
|
||||
:body (json/write-str {"query" (v/graphql-query {:venia/queries [{:query/data [:caterers [:name :uuid [:address [:name :street]]]]}]})})
|
||||
:as :json})
|
||||
{:headers {"Authorization" api-key
|
||||
"Content-Type" "application/json"}
|
||||
:body (json/write-str {"query" (v/graphql-query q)})
|
||||
:as :json})
|
||||
:body
|
||||
:data
|
||||
:caterers))
|
||||
))
|
||||
|
||||
(defn get-caterers [integration]
|
||||
(:caterers (query integration {:venia/queries [{:query/data
|
||||
[:caterers [:name :uuid [:address [:name :street]]]]}]} )))
|
||||
|
||||
(defn get-subscriptions [integration]
|
||||
(->> (query integration {:venia/queries [{:query/data
|
||||
[:subscribers [:id [:subscriptions [:parentId :parentEntity :eventEntity :eventKey]] ]]}]} )
|
||||
:subscribers
|
||||
first
|
||||
:subscriptions))
|
||||
|
||||
(defn get-integrations []
|
||||
(d/q '[:find [(pull ?i [:ezcater-integration/api-key
|
||||
:ezcater-integration/subscriber-uuid
|
||||
:db/id
|
||||
:ezcater-integration/integration-status [:db/id]]) ...]
|
||||
:in $
|
||||
@@ -43,6 +57,41 @@
|
||||
:ezcater-caterer/search-terms (str (:name caterer) " " (:street (:address caterer)))
|
||||
:ezcater-caterer/uuid (:uuid caterer)}]}))))
|
||||
|
||||
(defn upsert-used-subscriptions
|
||||
([integration]
|
||||
(let [extant (get-subscriptions integration)
|
||||
to-ensure (set (d/q '[:find [?cu ...]
|
||||
:in $
|
||||
:where [_ :client/ezcater-locations ?el]
|
||||
[?el :ezcater-location/caterer ?c]
|
||||
[?c :ezcater-caterer/uuid ?cu]]
|
||||
(d/db conn)))
|
||||
to-create (set/difference
|
||||
to-ensure
|
||||
(set (map :parentId extant)))]
|
||||
(doseq [parentId to-create]
|
||||
(query integration
|
||||
{:venia/operation {:operation/type :mutation
|
||||
:operation/name "createSubscription"}
|
||||
:venia/queries [[:createSubscription
|
||||
{:subscriptionParams {:subscriberId (:ezcater-integration/subscriber-uuid integration)
|
||||
:parentEntity 'Caterer
|
||||
:parentId parentId
|
||||
:eventEntity 'Order
|
||||
:eventKey 'accepted}}
|
||||
[[:subscription [:parentId :parentEntity :eventEntity :eventKey]]]]]})
|
||||
(query integration
|
||||
{:venia/operation {:operation/type :mutation
|
||||
:operation/name "createSubscription"}
|
||||
:venia/queries [[:createSubscription
|
||||
{:subscriptionParams {:subscriberId (:ezcater-integration/subscriber-uuid integration)
|
||||
:parentEntity 'Caterer
|
||||
:parentId parentId
|
||||
:eventEntity 'Order
|
||||
:eventKey 'cancelled}}
|
||||
[[:subscription [:parentId :parentEntity :eventEntity :eventKey]]]]]}))
|
||||
)))
|
||||
|
||||
(defn upsert-ezcater
|
||||
([] (upsert-ezcater (get-integrations)))
|
||||
([integrations]
|
||||
@@ -50,6 +99,7 @@
|
||||
(mark-integration-status integration {:integration-status/last-attempt (coerce/to-date (time/now))})
|
||||
(try
|
||||
(upsert-caterers integration)
|
||||
(upsert-used-subscriptions integration)
|
||||
(mark-integration-status integration {:integration-status/state :integration-state/success
|
||||
:integration-status/last-updated (coerce/to-date (time/now))})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user