setup for ezcater.
This commit is contained in:
@@ -6,20 +6,34 @@
|
|||||||
[venia.core :as v]
|
[venia.core :as v]
|
||||||
[clojure.data.json :as json]
|
[clojure.data.json :as json]
|
||||||
[clj-time.coerce :as coerce]
|
[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/"
|
(-> (client/post "https://api.ezcater.com/graphql/"
|
||||||
{:headers {"Authorization" api-key
|
{:headers {"Authorization" api-key
|
||||||
"Content-Type" "application/json"}
|
"Content-Type" "application/json"}
|
||||||
:body (json/write-str {"query" (v/graphql-query {:venia/queries [{:query/data [:caterers [:name :uuid [:address [:name :street]]]]}]})})
|
:body (json/write-str {"query" (v/graphql-query q)})
|
||||||
:as :json})
|
:as :json})
|
||||||
:body
|
:body
|
||||||
:data
|
: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 []
|
(defn get-integrations []
|
||||||
(d/q '[:find [(pull ?i [:ezcater-integration/api-key
|
(d/q '[:find [(pull ?i [:ezcater-integration/api-key
|
||||||
|
:ezcater-integration/subscriber-uuid
|
||||||
:db/id
|
:db/id
|
||||||
:ezcater-integration/integration-status [:db/id]]) ...]
|
:ezcater-integration/integration-status [:db/id]]) ...]
|
||||||
:in $
|
:in $
|
||||||
@@ -43,6 +57,41 @@
|
|||||||
:ezcater-caterer/search-terms (str (:name caterer) " " (:street (:address caterer)))
|
:ezcater-caterer/search-terms (str (:name caterer) " " (:street (:address caterer)))
|
||||||
:ezcater-caterer/uuid (:uuid 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
|
(defn upsert-ezcater
|
||||||
([] (upsert-ezcater (get-integrations)))
|
([] (upsert-ezcater (get-integrations)))
|
||||||
([integrations]
|
([integrations]
|
||||||
@@ -50,6 +99,7 @@
|
|||||||
(mark-integration-status integration {:integration-status/last-attempt (coerce/to-date (time/now))})
|
(mark-integration-status integration {:integration-status/last-attempt (coerce/to-date (time/now))})
|
||||||
(try
|
(try
|
||||||
(upsert-caterers integration)
|
(upsert-caterers integration)
|
||||||
|
(upsert-used-subscriptions integration)
|
||||||
(mark-integration-status integration {:integration-status/state :integration-state/success
|
(mark-integration-status integration {:integration-status/state :integration-state/success
|
||||||
:integration-status/last-updated (coerce/to-date (time/now))})
|
:integration-status/last-updated (coerce/to-date (time/now))})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user