From 5f82871ca5ea38098b6dcc6fa8218febc5dd66c3 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 28 Apr 2022 08:48:29 -0700 Subject: [PATCH] prevents external ledger imports before the client was locked. --- src/clj/auto_ap/graphql/invoices.clj | 6 ++++-- src/clj/auto_ap/graphql/ledger.clj | 23 +++++++++++++++++++++-- src/clj/auto_ap/square/core.clj | 9 +++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/clj/auto_ap/graphql/invoices.clj b/src/clj/auto_ap/graphql/invoices.clj index 623107ec..899b981f 100644 --- a/src/clj/auto_ap/graphql/invoices.clj +++ b/src/clj/auto_ap/graphql/invoices.clj @@ -65,8 +65,10 @@ (defn approve-invoices [context {:keys [invoices]} _] (assert-power-user (:id context)) - (doseq [i invoices] - (assert-can-see-client (:id context) (:db/id (:invoice/client (d/entity (d/db conn) i))))) + (doseq [i invoices + :let [invoice (d/entity (d/db conn) i)]] + (assert-can-see-client (:id context) (-> invoice :invoice/client :db/id)) + (assert-not-locked (-> invoice :invoice/client :db/id) (-> invoice :invoice/date))) (let [transactions (map (fn [i] {:db/id i :invoice/import-status :import-status/imported}) invoices)] (audit-transact transactions (:id context)) invoices)) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index 87397ace..dd2bbdf5 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -16,7 +16,8 @@ [datomic.api :as d] [mount.core :as mount] [unilog.context :as lc] - [yang.scheduler :as scheduler])) + [yang.scheduler :as scheduler] + [clj-time.core :as t])) (mount/defstate running-balance-cache :start (atom {})) @@ -234,6 +235,17 @@ :status (or (:status (ex-data e)) :error)))))) +(defn all-ids-not-locked [all-ids] + (->> all-ids + (d/q '[:find [?t ...] + :in $ [?t ...] + :where + [?t :journal-entry/client ?c] + [(get-else $ ?c :client/locked-until #inst "2000-01-01") ?lu] + [?t :journal-entry/date ?d] + [(>= ?d ?lu)]] + (d/db conn)))) + (defn delete-external-ledger [context args _] (let [_ (assert-admin (:id context)) args (assoc args :id (:id context)) @@ -244,7 +256,7 @@ (#(l/raw-graphql-ids (d/db conn) %)) :ids) specific-ids (l/filter-ids (:ids args)) - all-ids (into (set ids) specific-ids)] + all-ids (all-ids-not-locked (into (set ids) specific-ids))] (if (> (count all-ids) 1000) {:message (str "You can only delete 1000 ledger entries at a time.")} (do @@ -310,6 +322,13 @@ (throw (ex-info (str "Vendor '" (:vendor_name entry) "' not found.") {:status :error}))) (when-not (re-find #"\d{1,2}/\d{1,2}/\d{4}" (:date entry)) (throw (ex-info (str "Date must be MM/dd/yyyy") {:status :error}))) + (when-let [locked-until (:client/locked-until (all-clients (:client_code entry)))] + (when (and (not (t/after? (coerce/to-date-time (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))) + (coerce/to-date-time locked-until))) + (not (t/equal? (coerce/to-date-time (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry)))) + (coerce/to-date-time locked-until)))) + (throw (ex-info (str "Client's data is locked until " locked-until) {:status :error})))) + (when-not (dollars= (reduce (fnil + 0.0 0.0) 0.0 (map :debit (:line_items entry))) (reduce (fnil + 0.0 0.0) 0.0 (map :credit (:line_items entry)))) (throw (ex-info (str "Debits '" diff --git a/src/clj/auto_ap/square/core.clj b/src/clj/auto_ap/square/core.clj index 17a24111..a6080485 100644 --- a/src/clj/auto_ap/square/core.clj +++ b/src/clj/auto_ap/square/core.clj @@ -94,6 +94,15 @@ "sort_order" "DESC" }}}) +(defn get-order + ([client location order-id] + (log/info "Searching for" (:square-location/client-location location)) + (let [result (->> (client/get (str "https://connect.squareup.com/v2/orders/" order-id) + {:headers (client-base-headers client) + :as :json}) + :body + )] + result))) (defn search ([client location d]