From e8f1a0d73405584242147b5ea4e55bb62fa3ab97 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 10 May 2022 08:50:02 -0700 Subject: [PATCH] Fixes issues with square loading --- src/clj/auto_ap/square/core.clj | 72 +++++++++++++++++++++------------ src/clj/user.clj | 24 ++++++++--- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/clj/auto_ap/square/core.clj b/src/clj/auto_ap/square/core.clj index 02c0d9a2..da747f57 100644 --- a/src/clj/auto_ap/square/core.clj +++ b/src/clj/auto_ap/square/core.clj @@ -1,20 +1,20 @@ (ns auto-ap.square.core (:require [auto-ap.datomic :refer [conn remove-nils]] [auto-ap.time :as atime] - [auto-ap.utils :refer [allow-once]] - [clj-http.client :as client] + [c(daily-results )lj-http.client :as client] [clj-time.coerce :as coerce] [clj-time.core :as time] - [clj-time.format :as f] [clj-time.periodic :as periodic] - [clojure.core.async :as async] - [clojure.data.json :as json] + [clj-time.format :as f] [clojure.string :as str] + [clojure.data.json :as json] [clojure.tools.logging :as log] [datomic.api :as d] [mount.core :as mount] [unilog.context :as lc] - [yang.scheduler :as scheduler])) + #_{:clj-kondo/ignore [:unused-namespace]} + [yang.scheduler :as scheduler] + [clojure.core.async :as async])) (defn client-base-headers [client] {"Square-Version" "2021-08-18" @@ -79,20 +79,20 @@ "Uncategorized")))) (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"))) - } + { + "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"))) + } - + - }} + }} - "sort" { - "sort_field" "CREATED_AT" - "sort_order" "DESC" - }}}) + "sort" { + "sort_field" "CREATED_AT" + "sort_order" "DESC" + }}}) (defn get-order ([client location order-id] @@ -104,6 +104,21 @@ )] result))) +(defn continue-search [client location d cursor] + (log/info "Continuing search for" cursor) + (let [result (->> (client/post "https://connect.squareup.com/v2/orders/search" + {:headers (client-base-headers client) + :body (json/write-str (cond-> {"location_ids" [(:square-location/square-id location)] + "limit" 10000 + "cursor" cursor} + d (merge (pc d)))) + :as :json}) + :body)] + (log/info "found " (count (:orders result))) + (if (not-empty (:cursor result)) + (concat (:orders result) (continue-search client location cursor)) + (:orders result)))) + (defn search ([client location d] (log/info "Searching for" (:square-location/client-location location)) @@ -112,10 +127,11 @@ :body (json/write-str (cond-> {"location_ids" [(:square-location/square-id location)] "limit" 10000} d (merge (pc d)))) :as :json}) - :body - :orders)] - (log/info "found " (count result)) - result))) + :body)] + (log/info "found " (count (:orders result))) + (if (not-empty (:cursor result)) + (concat (:orders result) (continue-search client location d (:cursor result))) + (:orders result))))) @@ -210,9 +226,10 @@ (filter (fn has-linked-koala-production? [order] ;; if a POS order is linked (via note) to a koala-production order, we want ;; to keep the koala-production order, because it has taxes correct - (not (->> (:line_items order) - (map :note) - (every? koala-production-ids))))) + (not (and (:line_items order) ;; returns do not have line items, so they should be allowed + (->> (:line_items order) + (map :note) + (every? koala-production-ids)))))) (map #(order->sales-order client location %)))))) @@ -481,9 +498,12 @@ (log/info "Loading refunds") (upsert-refunds client)))) -(def upsert-all (allow-once upsert-all)) - (mount/defstate square-loader :start (scheduler/every (* 4 59 60 1000) upsert-all) :stop (scheduler/stop square-loader)) + + + + + diff --git a/src/clj/user.clj b/src/clj/user.clj index 3c2b0779..820439cf 100644 --- a/src/clj/user.clj +++ b/src/clj/user.clj @@ -485,8 +485,8 @@ (println "orders") (lc/with-context {:source "Historical loading data"} - (doseq [d (clj-time.periodic/periodic-seq (t/plus (t/now) (t/days (- days))) - (t/now) + (doseq [d (clj-time.periodic/periodic-seq (t/plus (t/today) (t/days (- days))) + (t/today) (t/days 1))] (println d) (square/upsert client square-location d))) @@ -497,8 +497,8 @@ (println "settlements") (with-redefs [square/lookup-dates (fn lookup-dates [] - (->> (clj-time.periodic/periodic-seq (t/plus (t/now) (t/days (- days))) - (t/now) + (->> (clj-time.periodic/periodic-seq (t/plus (t/today) (t/days (- days))) + (t/today) (t/days 2)) (map (fn [d] [(atime/unparse (t/plus d (t/days 1)) atime/iso-date) @@ -507,10 +507,22 @@ (square/upsert-settlements client square-location))))) +(defn load-sales-for-day [date] + (doseq [client (d/q [:find [(list 'pull '?e square/square-read ) '...] + :where ['?e :client/square-locations ]] + (d/db auto-ap.datomic/conn)) + square-location (:client/square-locations client) + :when (:square-location/client-location square-location)] + (println client) + + (println "orders") + (lc/with-context {:source "Historical loading data"} + (square/upsert client square-location (c/to-date-time date))))) + (defn upsert-invoice-amounts [tsv] - (let [data (with-open [reader (io/reader (char-array tsv))] + (let [data (with-open [reader (io/reader (char-array tsv))] (doall (csv/read-csv reader :separator \tab))) - db (d/db auto-ap.datomic/conn) + db (d/db auto-ap.datomic/conn) invoice-totals (->> data (drop 1) (group-by first)