Adds ability for users to configure basic ezcater integration.
This commit is contained in:
62
src/clj/auto_ap/ezcater/core.clj
Normal file
62
src/clj/auto_ap/ezcater/core.clj
Normal file
@@ -0,0 +1,62 @@
|
||||
(ns auto-ap.ezcater.core
|
||||
(:require
|
||||
[auto-ap.datomic :refer [conn]]
|
||||
[datomic.api :as d]
|
||||
[clj-http.client :as client]
|
||||
[venia.core :as v]
|
||||
[clojure.data.json :as json]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clj-time.core :as time]))
|
||||
|
||||
(defn get-caterers [{:ezcater-integration/keys [api-key]}]
|
||||
(-> (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})
|
||||
:body
|
||||
:data
|
||||
:caterers))
|
||||
|
||||
(defn get-integrations []
|
||||
(d/q '[:find [(pull ?i [:ezcater-integration/api-key
|
||||
:db/id
|
||||
:ezcater-integration/integration-status [:db/id]]) ...]
|
||||
:in $
|
||||
:where [?i :ezcater-integration/api-key]]
|
||||
(d/db conn)))
|
||||
|
||||
(defn mark-integration-status [integration integration-status]
|
||||
@(d/transact conn
|
||||
[{:db/id (:db/id integration)
|
||||
:ezcater-integration/integration-status (assoc integration-status
|
||||
:db/id (or (-> integration :ezcater-integration/integration-status :db/id)
|
||||
#db/id [:db.part/user]))}]))
|
||||
|
||||
(defn upsert-caterers
|
||||
([integration]
|
||||
@(d/transact
|
||||
conn
|
||||
(for [caterer (get-caterers integration)]
|
||||
{:db/id (:db/id integration)
|
||||
:ezcater-integration/caterers [{:ezcater-caterer/name (str (:name caterer) " (" (:street (:address caterer)) ")")
|
||||
:ezcater-caterer/search-terms (str (:name caterer) " " (:street (:address caterer)))
|
||||
:ezcater-caterer/uuid (:uuid caterer)}]}))))
|
||||
|
||||
(defn upsert-ezcater
|
||||
([] (upsert-ezcater (get-integrations)))
|
||||
([integrations]
|
||||
(doseq [integration integrations]
|
||||
(mark-integration-status integration {:integration-status/last-attempt (coerce/to-date (time/now))})
|
||||
(try
|
||||
(upsert-caterers integration)
|
||||
(mark-integration-status integration {:integration-status/state :integration-state/success
|
||||
:integration-status/last-updated (coerce/to-date (time/now))})
|
||||
|
||||
(catch Exception e
|
||||
(log/warn e)
|
||||
(mark-integration-status integration {:integration-status/state :integration-state/failed
|
||||
:integration-status/message (.getMessage e)}))))))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user