lots of fixes.

This commit is contained in:
Bryce Covert
2020-09-08 18:00:07 -07:00
parent 74e34e3592
commit 179c2d13c6
6 changed files with 182 additions and 41 deletions

View File

@@ -1,13 +1,15 @@
(ns auto-ap.square.core
(:require [auto-ap.datomic :refer [conn remove-nils]]
[auto-ap.utils :refer [by]]
[clj-http.client :as client]
[clj-time.coerce :as coerce]
[clj-time.core :as time]
[clj-time.format :as f]
[clojure.data.json :as json]
[datomic.api :as d]
[unilog.context :as lc]
[clojure.tools.logging :as log]
[datomic.api :as d]
[mount.core :as mount]
[unilog.context :as lc]
[yang.scheduler :as scheduler]))
(defn locations []
@@ -19,16 +21,97 @@
:body
:locations))
(defn search [l]
(log/info "Searching for" l)
(->> (client/post "https://connect.squareup.com/v2/orders/search"
{:headers {"Square-Version" "2020-08-12"
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
"Content-Type" "application/json"}
:body (json/write-str {"location_ids" [l] "limit" 4000})
:as :json})
:body
:orders))
(defn fetch-catalog [i]
(if i
(do
(log/info "looking up catalog for" (str "https://connect.squareup.com/v2/catalog/object/" i))
(->> (client/get (str "https://connect.squareup.com/v2/catalog/object/" i)
{:headers {"Square-Version" "2020-08-12"
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
"Content-Type" "application/json"}
:query-params {"include_related_items" "true"}
:as :json})
:body
:object))
(log/warn "Trying to look up non existant ")))
(def fetch-catalog-fast (memoize fetch-catalog))
(defn item-id->category-name [i]
(let [item (fetch-catalog-fast i)]
(cond (:item_variation_data item)
(item-id->category-name (:item_id (:item_variation_data item)))
(:category_id (:item_data item))
(:name (:category_data (fetch-catalog-fast (:category_id (:item_data item)))))
(:item_data item)
"Uncategorized"
:else
(do
(log/error "couldn't look up" i)
"Uncategorized"))))
(defn categories []
(by :id (comp :name :category_data) ))
(def potential-query
{"query" {"filter" {"date_time_filter"
{
"created_at" {
"start_at" "2020-08-28T00:00:00-07:00",
"end_at" "2020-08-28T23:59:59-07:00"
}
}
"state_filter" {"states" ["COMPLETED"]}}
"sort" {
"sort_field" "CREATED_AT"
"sort_order" "DESC"
}}})
(defn pc [d]
{"query" {"filter" {"date_time_filter"
{
"created_at" {
"start_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (coerce/to-date-time d) (time/time-zone-for-id "America/Los_Angeles")))
"end_at" (f/unparse (f/formatter "YYYY-MM-dd'T'HH:mm:ssZZ") (time/to-time-zone (time/plus (coerce/to-date-time d) (time/days 1)) (time/time-zone-for-id "America/Los_Angeles")))
}
}
"state_filter" {"states" ["COMPLETED"]}}
"sort" {
"sort_field" "CREATED_AT"
"sort_order" "DESC"
}}})
(defn search
([l]
(search l nil))
([l d]
(log/info "Searching for" l)
(let [result (->> (client/post "https://connect.squareup.com/v2/orders/search"
{:headers {"Square-Version" "2020-08-12"
"Authorization" "Bearer EAAAEO2xSqesDutZz71hz3eulKmrlKTiEqG3uZ4j25x5GYlOluQ2cj2JxNUXqXD7"
"Content-Type" "application/json"}
:body (json/write-str (cond-> {"location_ids" [l] "limit" 4000}
d (merge (pc d))))
:as :json})
:body
:orders)]
(println (cond-> {"location_ids" [l] "limit" 4000}
d (merge (pc d))))
(log/info "found " (count result))
result)))
@@ -44,11 +127,11 @@
"4X8T65741AEPS" ["NGVZ" "NP"]
"KMVFQ9CRCXJ10" ["NGZO" "VT"]} location))
(defn daily-results []
(defn daily-results [d]
(->> (locations)
(map :id)
(filter location_id->client-location)
(mapcat search)
(mapcat #(search % d))
(map (fn [order]
(let [[client loc] (location_id->client-location (:location_id order))]
(remove-nils
@@ -56,10 +139,11 @@
{:date (coerce/to-date (time/to-time-zone (coerce/to-date-time (:created_at order)) (time/time-zone-for-id "America/Los_Angeles")))
:client [:client/code client]
:location loc
:external-id (str "square-" client "-" loc "-" (:id order))
:external-id (str "square/order/" client "-" loc "-" (:id order))
:total (-> order :net_amounts :total_money amount->money)
:tax (-> order :net_amounts :tax_money amount->money)
:tip (-> order :net_amounts :tip_money amount->money)
:discount (-> order :net_amounts :discount_money amount->money)
:charges (->> (:tenders order)
(map (fn [t]
(remove-nils
@@ -72,8 +156,10 @@
(remove-nils
#:order-line-item
{:item-name (:name li)
:category (item-id->category-name (:catalog_object_id li))
:total (amount->money (:total_money li))
:tax (amount->money (:total_tax_money li))}))))}))))))
:tax (amount->money (:total_tax_money li))
:discount (amount->money (:total_discount_money li))}))))}))))))
#_(daily-results)
@@ -126,23 +212,26 @@
:date (-> (:initiated_at settlement)
(coerce/to-date))})))))
(defn upsert []
(lc/with-context {:source "Square loading"}
(try
(let [existing (->> (d/query {:query {:find ['?external-id]
:in ['$]
:where ['[_ :sales-order/external-id ?external-id]]}
:args [(d/db conn)]})
(map first)
set)
_ (log/info (count existing) "Sales orders already exist")
to-create (filter #(not (existing (:sales-order/external-id %)))
(daily-results))]
(doseq [x (partition-all 20 to-create)]
(log/info "Loading " (count x))
@(d/transact conn x)))
(catch Exception e
(log/error e)))))
(defn upsert
([]
(upsert nil))
([d]
(lc/with-context {:source "Square loading"}
(try
(let [existing (->> (d/query {:query {:find ['?external-id]
:in ['$]
:where ['[_ :sales-order/external-id ?external-id]]}
:args [(d/db conn)]})
(map first)
set)
_ (log/info (count existing) "Sales orders already exist")
to-create (filter #(not (existing (:sales-order/external-id %)))
(daily-results d))]
(doseq [x (partition-all 20 to-create)]
(log/info "Loading " (count x))
@(d/transact conn x)))
(catch Exception e
(log/error e))))))
(defn upsert-settlements []
(lc/with-context {:source "Square settlements loading "}