Adds the ability to sync ezcater orders every few hours
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
[datomic.api :as d]
|
||||
[clj-http.client :as client]
|
||||
[venia.core :as v]
|
||||
[clojure.string :as str]
|
||||
[clojure.data.json :as json]
|
||||
[clj-time.coerce :as coerce]
|
||||
[clojure.tools.logging :as log]
|
||||
@@ -272,3 +273,60 @@
|
||||
(order->sales-order)
|
||||
(update :sales-order/date coerce/to-date)
|
||||
(update-in [:sales-order/charges 0 :charge/date] coerce/to-date))]))
|
||||
|
||||
(defn upsert-recent []
|
||||
(let [last-monday (coerce/to-date (time/plus (first (->> (time/today)
|
||||
(iterate #(time/plus % (time/days -1)))
|
||||
(filter #(= 1 (time/day-of-week %)))))
|
||||
(time/days 1)))
|
||||
orders-to-update (doall (for [[order uuid] (d/q '[:find ?eid ?uuid
|
||||
:in $ ?start
|
||||
:where [?e :sales-order/vendor :vendor/ccp-ezcater]
|
||||
[?e :sales-order/date ?d]
|
||||
[(>= ?d ?start)]
|
||||
[?e :sales-order/external-id ?eid]
|
||||
[?e :sales-order/client ?c]
|
||||
[?c :client/ezcater-locations ?l]
|
||||
[?l :ezcater-location/caterer ?c2]
|
||||
[?c2 :ezcater-caterer/uuid ?uuid]]
|
||||
(d/db conn)
|
||||
last-monday)
|
||||
:let [_ (log/info "Considering updating " order)
|
||||
id (last (str/split order #"/"))
|
||||
id (str/join "-" (drop 2 (str/split order #"-")))
|
||||
lookup-map {"id" "bf3dcf5c-a68f-42d9-9084-049133e03d3d",
|
||||
"parent_type" "Caterer",
|
||||
"parent_id" uuid,
|
||||
"entity_type" "Order",
|
||||
"entity_id" id,
|
||||
"key" "accepted",
|
||||
"occurred_at" "2022-07-21T19:21:07.549Z"}
|
||||
ezcater-order (lookup-order lookup-map)
|
||||
extant-order (d/pull (d/db conn) '[:sales-order/total
|
||||
:sales-order/tax
|
||||
:sales-order/tip
|
||||
:sales-order/discount
|
||||
:sales-order/external-id
|
||||
{:sales-order/charges [:charge/tax
|
||||
:charge/tip
|
||||
:charge/total
|
||||
:charge/external-id]}]
|
||||
[:sales-order/external-id order])
|
||||
|
||||
updated-order (-> (order->sales-order ezcater-order)
|
||||
(select-keys
|
||||
#{:sales-order/total
|
||||
:sales-order/tax
|
||||
:sales-order/tip
|
||||
:sales-order/discount
|
||||
:sales-order/charges
|
||||
:sales-order/external-id})
|
||||
(update :sales-order/charges (fn [c]
|
||||
(map #(select-keys % #{:charge/tax :charge/tip :charge/total
|
||||
:charge/external-id}) c))))]
|
||||
|
||||
:when (not= updated-order extant-order)]
|
||||
|
||||
updated-order))]
|
||||
(log/info "Found these orders to update:" orders-to-update)
|
||||
@(d/transact conn orders-to-update)))
|
||||
|
||||
9
src/clj/auto_ap/jobs/ezcater_upsert.clj
Normal file
9
src/clj/auto_ap/jobs/ezcater_upsert.clj
Normal file
@@ -0,0 +1,9 @@
|
||||
(ns auto-ap.jobs.ezcater-upsert
|
||||
(:gen-class)
|
||||
(:require
|
||||
[auto-ap.jobs.core :refer [execute]]
|
||||
[auto-ap.ezcater.core :as ezcater]))
|
||||
|
||||
(defn -main [& _]
|
||||
(execute "ezcater-upsert" ezcater/upsert-recent))
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
[auto-ap.jobs.intuit :as job-intuit]
|
||||
[auto-ap.jobs.ledger-reconcile :as job-reconcile-ledger]
|
||||
[auto-ap.jobs.plaid :as job-plaid]
|
||||
[auto-ap.jobs.ezcater-upsert :as job-ezcater-upsert]
|
||||
[auto-ap.jobs.square :as job-square]
|
||||
[auto-ap.jobs.square2 :as job-square2]
|
||||
[auto-ap.jobs.bulk-journal-import :as job-bulk-journal-import]
|
||||
@@ -121,6 +122,9 @@
|
||||
(= job "close-auto-invoices")
|
||||
(job-close-auto-invoices/-main)
|
||||
|
||||
(= job "ezcater-upsert")
|
||||
(job-ezcater-upsert/-main)
|
||||
|
||||
(= job "bulk-journal-import")
|
||||
(job-bulk-journal-import/-main)
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
|
||||
(def default-read [:name :start-date :end-date :status])
|
||||
(def job-types [:yodlee2 :yodlee2-accounts :intuit :plaid :bulk-journal-import])
|
||||
(def job-types [:yodlee2 :yodlee2-accounts :intuit :plaid :bulk-journal-import :ezcater-upsert])
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::params-change
|
||||
@@ -146,6 +146,7 @@
|
||||
[job-button {:which :yodlee2-accounts} "Start Yodlee Account Import"]
|
||||
[job-button {:which :intuit} "Start Intuit"]
|
||||
[job-button {:which :plaid} "Start Plaid"]
|
||||
[job-button {:which :ezcater-upsert} "EZCater Sync"]
|
||||
[bulk-journal-import-button]]]
|
||||
[table/table {:id :jobs
|
||||
:data-page ::page}]])]))
|
||||
|
||||
Reference in New Issue
Block a user