makes POS page much less likely to break everything

This commit is contained in:
2023-04-05 22:50:38 -07:00
parent e7ae418614
commit 2a0f736af7
6 changed files with 441 additions and 501 deletions

View File

@@ -19,8 +19,11 @@
(localize (time/now))) (localize (time/now)))
(defn recent-date [] (defn recent-date
(coerce/to-date (time/minus (local-now) (time/days 90)))) ([]
(recent-date 90))
([n]
(coerce/to-date (time/minus (local-now) (time/days n)))))
(def excel-formatter (f/with-zone (f/formatter "MM/dd/yyyy") (time/time-zone-for-id "America/Los_Angeles"))) (def excel-formatter (f/with-zone (f/formatter "MM/dd/yyyy") (time/time-zone-for-id "America/Los_Angeles")))
(defn excel-date [d] (defn excel-date [d]

View File

@@ -4,380 +4,381 @@
;; You can also press C-u C-j to evaluate the expression and pretty-print its result. ;; You can also press C-u C-j to evaluate the expression and pretty-print its result.
(ns auto-ap.backup (comment
(:require [datomic.api :as d] (ns auto-ap.backup
[manifold.deferred :as de] (:require [datomic.api :as d]
[manifold.executor :as ex] [manifold.deferred :as de]
[manifold.stream :as s] [manifold.executor :as ex]
[manifold.time :as mt] [manifold.stream :as s]
[auto-ap.jobs.core :refer [execute]] [manifold.time :as mt]
[clojure.java.io :as io] [auto-ap.jobs.core :refer [execute]]
[amazonica.aws.s3 :as s3] [clojure.java.io :as io]
[config.core :refer [env]] [amazonica.aws.s3 :as s3]
[clojure.core.async :as a] [config.core :refer [env]]
[lambdaisland.edn-lines :as ednl] [clojure.core.async :as a]
[clojure.set :as set] [lambdaisland.edn-lines :as ednl]
[com.brunobonacci.mulog :as mu])) [clojure.set :as set]
[com.brunobonacci.mulog :as mu]))
(def request-pool (ex/fixed-thread-executor 30)) (def request-pool (ex/fixed-thread-executor 30))
(def buffered (ex/fixed-thread-executor 30)) (def buffered (ex/fixed-thread-executor 30))
(defn get-schema [remote-db] (defn get-schema [remote-db]
(let [everything (->> (d/q '[:find [(pull ?e [:db/ident (let [everything (->> (d/q '[:find [(pull ?e [:db/ident
{:db/valueType [:db/ident]} {:db/valueType [:db/ident]}
{:db/cardinality [:db/ident]} {:db/cardinality [:db/ident]}
:db.attr/preds :db.attr/preds
{:db/unique [:db/ident]} {:db/unique [:db/ident]}
:db/isComponent :db/isComponent
:db/id :db/id
:db/noHistory :db/noHistory
:db/tupleAttrs :db/tupleAttrs
:db.entity/attrs :db.entity/attrs
:db.entity/preds :db.entity/preds
:db/doc]) ...] :db/doc]) ...]
:where [?e :db/ident]] :where [?e :db/ident]]
remote-db)) remote-db))
schema-attrs (->> everything schema-attrs (->> everything
(filter :db/ident) (filter :db/ident)
(filter (fn [{:db/keys [ident]}] (filter (fn [{:db/keys [ident]}]
(if (namespace ident) (if (namespace ident)
(re-matches #"^(?!cartographer)(?!db)(?!fressian).+" (namespace ident)) (re-matches #"^(?!cartographer)(?!db)(?!fressian).+" (namespace ident))
true true
)))) ))))
meta-schema-schema (filter #(-> % :db/ident not) everything)] meta-schema-schema (filter #(-> % :db/ident not) everything)]
schema-attrs)) schema-attrs))
(def entity->best-key (def entity->best-key
{"transaction-rule" {"transaction-rule"
[:transaction-rule/description, :transaction-rule/note :transaction-rule/vendor] [:transaction-rule/description, :transaction-rule/note :transaction-rule/vendor]
"square-location" "square-location"
:square-location/square-id, :square-location/square-id,
"expected-deposit" "expected-deposit"
:expected-deposit/date, :expected-deposit/date,
"journal-entry-line" "journal-entry-line"
[:journal-entry-line/account, :journal-entry-line/debit :journal-entry-line/credit] [:journal-entry-line/account, :journal-entry-line/debit :journal-entry-line/credit]
"vendor" "vendor"
[:vendor/name, :vendor/default-account, :vendor/hidden] [:vendor/name, :vendor/default-account, :vendor/hidden]
"transaction" "transaction"
:transaction/amount, :transaction/amount,
"yodlee-provider-account" "yodlee-provider-account"
:yodlee-provider-account/id, :yodlee-provider-account/id,
"journal-entry" "journal-entry"
:journal-entry/source, :journal-entry/source,
"yodlee-merchant" :yodlee-merchant/yodlee-id, "yodlee-merchant" :yodlee-merchant/yodlee-id,
"invoice" "invoice"
:invoice/invoice-number, :invoice/invoice-number,
"vendor-terms-override" "vendor-terms-override"
:vendor-terms-override/client, :vendor-terms-override/client,
"integration-status" "integration-status"
:integration-status/state, :integration-status/state,
"conformity" :conformity/conformed-norms-index, "conformity" :conformity/conformed-norms-index,
"user" "user"
:user/provider-id, :user/provider-id,
"sales-refund" "sales-refund"
:sales-refund/total, :sales-refund/total,
"plaid-account" "plaid-account"
:plaid-account/name, :plaid-account/name,
"charge" "charge"
[:charge/total, :charge/external-id] [:charge/total, :charge/external-id]
"location-match" :location-match/location, "location-match" :location-match/location,
"vendor-schedule-payment-dom" "vendor-schedule-payment-dom"
:vendor-schedule-payment-dom/dom, :vendor-schedule-payment-dom/dom,
"account-client-override" "account-client-override"
:account-client-override/client, :account-client-override/client,
"plaid-item" "plaid-item"
:plaid-item/client, :plaid-item/client,
"transaction-account" "transaction-account"
:transaction-account/account, :transaction-account/account,
"address" "address"
[:address/street1, :address/city :address/state :address/zip] [:address/street1, :address/city :address/state :address/zip]
"order-line-item" "order-line-item"
:order-line-item/total, :order-line-item/total,
"ezcater-location" [:ezcater-location/location, :ezcater-location/caterer] "ezcater-location" [:ezcater-location/location, :ezcater-location/caterer]
"account" "account"
[:account/numeric-code, :account/code :account/name :account/type] [:account/numeric-code, :account/code :account/name :account/type]
"intuit-bank-account" "intuit-bank-account"
:intuit-bank-account/name, :intuit-bank-account/name,
"saved-query" "saved-query"
:saved-query/guid, :saved-query/guid,
"ezcater-caterer" "ezcater-caterer"
:ezcater-caterer/uuid, :ezcater-caterer/uuid,
"forecasted-transaction" "forecasted-transaction"
:forecasted-transaction/day-of-month, :forecasted-transaction/day-of-month,
"audit" :audit/user, "audit" :audit/user,
"yodlee-account" "yodlee-account"
:yodlee-account/id, :yodlee-account/id,
"transaction-rule-account" "transaction-rule-account"
[:transaction-rule-account/account, :transaction-rule-account/location] [:transaction-rule-account/account, :transaction-rule-account/location]
"ezcater-integration" "ezcater-integration"
:ezcater-integration/subscriber-uuid, :ezcater-integration/subscriber-uuid,
"report" "report"
:report/created, :report/created,
"bank-account" "bank-account"
:bank-account/code, :bank-account/code,
"vendor-usage" "vendor-usage"
:vendor-usage/key, :vendor-usage/key,
"invoice-expense-account" "invoice-expense-account"
[:invoice-expense-account/expense-account-id, :invoice-expense-account/account :invoice-expense-account/location :invoice-expense-account/amount] [:invoice-expense-account/expense-account-id, :invoice-expense-account/account :invoice-expense-account/location :invoice-expense-account/amount]
"sales-order" "sales-order"
:sales-order/date, :sales-order/date,
"client" "client"
:client/code, :client/code,
"email-contact" :email-contact/email, "email-contact" :email-contact/email,
"invoice-payment" "invoice-payment"
:invoice-payment/amount, :invoice-payment/amount,
"contact" "contact"
[:contact/name, :contact/phone :contact/email] [:contact/name, :contact/phone :contact/email]
"import-batch" "import-batch"
:import-batch/date, :import-batch/date,
"payment" "payment"
[:payment/date, :payment/bank-account] [:payment/date, :payment/bank-account]
"vendor-account-override" "vendor-account-override"
:vendor-account-override/client}) :vendor-account-override/client})
#_(defn references [schema] #_(defn references [schema]
(filter (comp #{:db.type/ref} :db/ident :db/valueType) schema )) (filter (comp #{:db.type/ref} :db/ident :db/valueType) schema ))
#_(defn reference->entity [remote-db] #_(defn reference->entity [remote-db]
(->> (d/q '[:find ?a ?v3 (->> (d/q '[:find ?a ?v3
:in $ $$ [?a ...] :in $ $$ [?a ...]
:where [$$ _ ?a ?e] :where [$$ _ ?a ?e]
[$ ?e ?v _ _] [$ ?e ?v _ _]
[$ ?v :db/ident ?v2 _ _] [$ ?v :db/ident ?v2 _ _]
[(namespace ?v2) ?v3] [(namespace ?v2) ?v3]
[(namespace ?v2) ?v3]] [(namespace ?v2) ?v3]]
remote-db remote-db
(d/since remote-db #inst "2022-06-01") (d/since remote-db #inst "2022-06-01")
(map :db/ident references) (map :db/ident references)
) )
(group-by first) (group-by first)
(map (fn [[k v]] (map (fn [[k v]]
[k (disj (set (map second v)) "db")])) [k (disj (set (map second v)) "db")]))
(into {}))) (into {})))
#_(def manual-dependencies #_(def manual-dependencies
{:client/location-matches #{"location-match"} {:client/location-matches #{"location-match"}
:transaction/yodlee-merchant #{"yodlee-merchant"} :transaction/yodlee-merchant #{"yodlee-merchant"}
:vendor-account-override/account #{"account"} :vendor-account-override/account #{"account"}
:vendor-account-override/client #{"client"} :vendor-account-override/client #{"client"}
:vendor/secondary-contact #{"contact"} :vendor/secondary-contact #{"contact"}
:vendor/account-overrides #{"vendor-account-override"} :vendor/account-overrides #{"vendor-account-override"}
:client/bank-accounts #{"bank-account"} :client/bank-accounts #{"bank-account"}
:transaction-rule/yodlee-merchant #{"yodlee-merchant"} :transaction-rule/yodlee-merchant #{"yodlee-merchant"}
:client/forecasted-transactions #{"forecasted-transaction"} :client/forecasted-transactions #{"forecasted-transaction"}
:transaction/forecast-match #{"forecasted-transaction"} :transaction/forecast-match #{"forecasted-transaction"}
:vendor/automatically-paid-when-due #{"client"} :vendor/automatically-paid-when-due #{"client"}
:vendor/schedule-payment-dom #{"vendor-schedule-payment-dom"} :vendor/schedule-payment-dom #{"vendor-schedule-payment-dom"}
:vendor/terms-overrides #{"vendor-terms-override"} :vendor/terms-overrides #{"vendor-terms-override"}
:vendor-schedule-payment-dom/client #{"client"}}) :vendor-schedule-payment-dom/client #{"client"}})
#_(defn full-dependencies [remote-db] #_(defn full-dependencies [remote-db]
(update (merge-with into (reference->entity remote-db) manual-dependencies) (update (merge-with into (reference->entity remote-db) manual-dependencies)
:journal-entry/original-entity :journal-entry/original-entity
#(disj % "journal-entry"))) #(disj % "journal-entry")))
#_(defn entity-dependencies [schema] #_(defn entity-dependencies [schema]
(let [base-dependencies (let [base-dependencies
(into (into
{} {}
(map (fn [i] (map (fn [i]
[i #{}]) [i #{}])
(set (map (comp namespace :db/ident) (set (map (comp namespace :db/ident)
(filter :db/valueType (filter :db/valueType
schema)))) schema))))
) )
] ]
(into base-dependencies (reduce (into base-dependencies (reduce
(fn [acc [ref deps]] (fn [acc [ref deps]]
(update acc (namespace ref) (fnil #(into % deps) #{}))) (update acc (namespace ref) (fnil #(into % deps) #{})))
{} {}
(full-dependencies remote-db))))) (full-dependencies remote-db)))))
(def full-dependencies (def full-dependencies
{:invoice/client #{"client"}, {:invoice/client #{"client"},
:sales-order/client #{"client"}, :sales-order/client #{"client"},
:transaction-rule/transaction-approval-status #{}, :transaction-rule/transaction-approval-status #{},
:transaction/forecast-match #{"forecasted-transaction"}, :transaction/forecast-match #{"forecasted-transaction"},
:user/role #{}, :user/role #{},
:vendor-schedule-payment-dom/client #{"client"}, :vendor-schedule-payment-dom/client #{"client"},
:invoice-payment/payment #{"payment"}, :invoice-payment/payment #{"payment"},
:transaction-rule/client #{"client"}, :transaction-rule/client #{"client"},
:invoice/status #{}, :invoice/status #{},
:payment/type #{}, :payment/type #{},
:expected-deposit/client #{"client"}, :expected-deposit/client #{"client"},
:transaction/bank-account #{"bank-account"}, :transaction/bank-account #{"bank-account"},
:transaction-rule-account/account #{"account"}, :transaction-rule-account/account #{"account"},
:import-batch/status #{}, :import-batch/status #{},
:user/clients #{"client"}, :user/clients #{"client"},
:payment/client #{"client"}, :payment/client #{"client"},
:expected-deposit/charges #{"charge"}, :expected-deposit/charges #{"charge"},
:vendor/automatically-paid-when-due #{"client"}, :vendor/automatically-paid-when-due #{"client"},
:payment/invoices #{"invoice"}, :payment/invoices #{"invoice"},
:client/forecasted-transactions #{"forecasted-transaction"}, :client/forecasted-transactions #{"forecasted-transaction"},
:transaction/matched-rule #{"transaction-rule"}, :transaction/matched-rule #{"transaction-rule"},
:invoice/import-status #{}, :invoice/import-status #{},
:charge/processor #{}, :charge/processor #{},
:expected-deposit/vendor #{"vendor"}, :expected-deposit/vendor #{"vendor"},
:client/square-locations #{"square-location"}, :client/square-locations #{"square-location"},
:payment/status #{}, :payment/status #{},
:client/location-matches #{"location-match"}, :client/location-matches #{"location-match"},
:saved-query/client #{"client"}, :saved-query/client #{"client"},
:transaction/payment #{"payment"}, :transaction/payment #{"payment"},
:transaction-rule/vendor #{"vendor"}, :transaction-rule/vendor #{"vendor"},
:plaid-item/client #{"client"}, :plaid-item/client #{"client"},
:account/applicability #{}, :account/applicability #{},
:journal-entry-line/account #{"account" "bank-account"}, :journal-entry-line/account #{"account" "bank-account"},
:client/bank-accounts #{"bank-account"}, :client/bank-accounts #{"bank-account"},
:yodlee-provider-account/client #{"client"}, :yodlee-provider-account/client #{"client"},
:account/vendor-allowance #{}, :account/vendor-allowance #{},
:payment/bank-account #{"bank-account"}, :payment/bank-account #{"bank-account"},
:account/default-allowance #{}, :account/default-allowance #{},
:transaction-rule/yodlee-merchant #{"yodlee-merchant"}, :transaction-rule/yodlee-merchant #{"yodlee-merchant"},
:vendor/account-overrides #{"vendor-account-override"}, :vendor/account-overrides #{"vendor-account-override"},
:transaction/client #{"client"}, :transaction/client #{"client"},
:invoice/vendor #{"vendor"}, :invoice/vendor #{"vendor"},
:sales-order/vendor #{"vendor"}, :sales-order/vendor #{"vendor"},
:expected-deposit/status #{}, :expected-deposit/status #{},
:journal-entry/original-entity #{"transaction" "invoice"}, :journal-entry/original-entity #{"transaction" "invoice"},
:vendor-usage/client #{"client"}, :vendor-usage/client #{"client"},
:transaction/expected-deposit #{"expected-deposit"}, :transaction/expected-deposit #{"expected-deposit"},
:client/ezcater-locations #{"ezcater-location"}, :client/ezcater-locations #{"ezcater-location"},
:journal-entry/client #{"client"}, :journal-entry/client #{"client"},
:vendor/secondary-contact #{"contact"}, :vendor/secondary-contact #{"contact"},
:journal-entry/line-items #{"journal-entry-line"}, :journal-entry/line-items #{"journal-entry-line"},
:vendor/legal-entity-1099-type #{}, :vendor/legal-entity-1099-type #{},
:transaction-rule/bank-account #{"bank-account"}, :transaction-rule/bank-account #{"bank-account"},
:transaction-account/account #{"account"}, :transaction-account/account #{"account"},
:vendor/terms-overrides #{"vendor-terms-override"}, :vendor/terms-overrides #{"vendor-terms-override"},
:vendor/default-account #{"account"}, :vendor/default-account #{"account"},
:transaction/yodlee-merchant #{"yodlee-merchant"}, :transaction/yodlee-merchant #{"yodlee-merchant"},
:sales-refund/client #{"client"}, :sales-refund/client #{"client"},
:client/emails #{"email-contact"}, :client/emails #{"email-contact"},
:payment/vendor #{"vendor"}, :payment/vendor #{"vendor"},
:invoice-payment/invoice #{"invoice"}, :invoice-payment/invoice #{"invoice"},
:report/client #{"client"}, :report/client #{"client"},
:transaction-rule/accounts #{"transaction-rule-account"}, :transaction-rule/accounts #{"transaction-rule-account"},
:charge/client #{"client"}, :charge/client #{"client"},
:bank-account/type #{}, :bank-account/type #{},
:invoice-expense-account/account #{"account"}, :invoice-expense-account/account #{"account"},
:vendor/legal-entity-tin-type #{}, :vendor/legal-entity-tin-type #{},
:transaction/approval-status #{}, :transaction/approval-status #{},
:import-batch/entry #{"transaction"}, :import-batch/entry #{"transaction"},
:bank-account/intuit-bank-account #{"intuit-bank-account"}, :bank-account/intuit-bank-account #{"intuit-bank-account"},
:account/type #{}, :account/type #{},
:sales-refund/vendor #{"vendor"}, :sales-refund/vendor #{"vendor"},
:bank-account/yodlee-account #{"yodlee-account"}, :bank-account/yodlee-account #{"yodlee-account"},
:vendor/address #{"address"}, :vendor/address #{"address"},
:integration-status/state #{}, :integration-status/state #{},
:transaction/accounts #{"transaction-account"}, :transaction/accounts #{"transaction-account"},
:sales-order/charges #{"charge"}, :sales-order/charges #{"charge"},
:client/address #{"address"}, :client/address #{"address"},
:ezcater-location/caterer #{"ezcater-caterer"}, :ezcater-location/caterer #{"ezcater-caterer"},
:vendor-account-override/client #{"client"}, :vendor-account-override/client #{"client"},
:bank-account/integration-status #{"integration-status"}, :bank-account/integration-status #{"integration-status"},
:yodlee-provider-account/accounts #{"yodlee-account"}, :yodlee-provider-account/accounts #{"yodlee-account"},
:account/invoice-allowance #{}, :account/invoice-allowance #{},
:journal-entry/vendor #{"vendor"}, :journal-entry/vendor #{"vendor"},
:plaid-item/accounts #{"plaid-account"}, :plaid-item/accounts #{"plaid-account"},
:vendor-usage/vendor #{"vendor"}, :vendor-usage/vendor #{"vendor"},
:sales-order/line-items #{"order-line-item"}, :sales-order/line-items #{"order-line-item"},
:invoice/expense-accounts #{"invoice-expense-account"}, :invoice/expense-accounts #{"invoice-expense-account"},
:account-client-override/client #{"client"}, :account-client-override/client #{"client"},
:vendor/primary-contact #{"contact"}, :vendor/primary-contact #{"contact"},
:vendor/schedule-payment-dom #{"vendor-schedule-payment-dom"}, :vendor/schedule-payment-dom #{"vendor-schedule-payment-dom"},
:account/client-overrides #{"account-client-override"}, :account/client-overrides #{"account-client-override"},
:transaction/vendor #{"vendor"}, :transaction/vendor #{"vendor"},
:client/square-integration-status #{"integration-status"}, :client/square-integration-status #{"integration-status"},
:ezcater-integration/caterers #{"ezcater-caterer"}, :ezcater-integration/caterers #{"ezcater-caterer"},
:ezcater-integration/integration-status #{"integration-status"} :ezcater-integration/integration-status #{"integration-status"}
:vendor-account-override/account #{"account"}, :vendor-account-override/account #{"account"},
:import-batch/source #{}}) :import-batch/source #{}})
(def entity-dependencies (def entity-dependencies
{"transaction-rule" {"transaction-rule"
#{"vendor" "yodlee-merchant" "transaction-rule-account" "bank-account" #{"vendor" "yodlee-merchant" "transaction-rule-account" "bank-account"
"client"}, "client"},
"square-location" #{}, "square-location" #{},
"expected-deposit" #{"vendor" "charge" "client"}, "expected-deposit" #{"vendor" "charge" "client"},
"journal-entry-line" #{"account" "bank-account"}, "journal-entry-line" #{"account" "bank-account"},
"vendor" "vendor"
#{"vendor-schedule-payment-dom" "address" "account" "client" "contact" #{"vendor-schedule-payment-dom" "address" "account" "client" "contact"
"vendor-account-override"}, "vendor-account-override"},
"transaction" "transaction"
#{"transaction-rule" "expected-deposit" "vendor" "yodlee-merchant" #{"transaction-rule" "expected-deposit" "vendor" "yodlee-merchant"
"transaction-account" "forecasted-transaction" "bank-account" "client" "transaction-account" "forecasted-transaction" "bank-account" "client"
"payment"}, "payment"},
"yodlee-provider-account" #{"yodlee-account" "client"}, "yodlee-provider-account" #{"yodlee-account" "client"},
"journal-entry" "journal-entry"
#{"journal-entry-line" "vendor" "transaction" "invoice" "client"}, #{"journal-entry-line" "vendor" "transaction" "invoice" "client"},
"yodlee-merchant" #{}, "yodlee-merchant" #{},
"invoice" #{"vendor" "invoice-expense-account" "client"}, "invoice" #{"vendor" "invoice-expense-account" "client"},
"vendor-terms-override" #{}, "vendor-terms-override" #{},
"integration-status" #{}, "integration-status" #{},
"conformity" #{}, "conformity" #{},
"user" #{"client"}, "user" #{"client"},
"sales-refund" #{"vendor" "client"}, "sales-refund" #{"vendor" "client"},
"plaid-account" #{}, "plaid-account" #{},
"charge" #{"client"}, "charge" #{"client"},
"location-match" #{}, "location-match" #{},
"vendor-schedule-payment-dom" #{"client"}, "vendor-schedule-payment-dom" #{"client"},
"account-client-override" #{"client"}, "account-client-override" #{"client"},
"plaid-item" #{"plaid-account" "client"}, "plaid-item" #{"plaid-account" "client"},
"transaction-account" #{"account"}, "transaction-account" #{"account"},
"address" #{}, "address" #{},
"order-line-item" #{}, "order-line-item" #{},
"ezcater-location" #{"ezcater-caterer"}, "ezcater-location" #{"ezcater-caterer"},
"account" #{"account-client-override"}, "account" #{"account-client-override"},
"intuit-bank-account" #{}, "intuit-bank-account" #{},
"saved-query" #{"client"}, "saved-query" #{"client"},
"ezcater-caterer" #{}, "ezcater-caterer" #{},
"forecasted-transaction" #{}, "forecasted-transaction" #{},
"audit" #{}, "audit" #{},
"yodlee-account" #{}, "yodlee-account" #{},
"transaction-rule-account" #{"account"}, "transaction-rule-account" #{"account"},
"ezcater-integration" #{"ezcater-caterer" "integration-status"}, "ezcater-integration" #{"ezcater-caterer" "integration-status"},
"report" #{"client"}, "report" #{"client"},
"bank-account" #{"integration-status" "intuit-bank-account" "yodlee-account"}, "bank-account" #{"integration-status" "intuit-bank-account" "yodlee-account"},
"vendor-usage" #{"vendor" "client"}, "vendor-usage" #{"vendor" "client"},
"invoice-expense-account" #{"account"}, "invoice-expense-account" #{"account"},
"sales-order" #{"vendor" "charge" "order-line-item" "client"}, "sales-order" #{"vendor" "charge" "order-line-item" "client"},
"client" "client"
#{"square-location" "integration-status" "location-match" "address" #{"square-location" "integration-status" "location-match" "address"
"ezcater-location" "forecasted-transaction" "bank-account" "email-contact"}, "ezcater-location" "forecasted-transaction" "bank-account" "email-contact"},
"email-contact" #{}, "email-contact" #{},
"invoice-payment" #{"invoice" "payment"}, "invoice-payment" #{"invoice" "payment"},
"contact" #{}, "contact" #{},
"import-batch" #{"transaction"}, "import-batch" #{"transaction"},
"payment" #{"vendor" "invoice" "bank-account" "client"}, "payment" #{"vendor" "invoice" "bank-account" "client"},
"vendor-account-override" #{"account" "client"}}) "vendor-account-override" #{"account" "client"}})
(defn order-of-insert [entity-dependencies] (defn order-of-insert [entity-dependencies]
(loop [entity-dependencies entity-dependencies (loop [entity-dependencies entity-dependencies
order []] order []]
(let [next-order (for [[entity deps] entity-dependencies (let [next-order (for [[entity deps] entity-dependencies
:when (not (seq deps))] :when (not (seq deps))]
entity) entity)
next-deps (reduce next-deps (reduce
(fn [entity-dependencies next-entity] (fn [entity-dependencies next-entity]
(into {} (into {}
(map (map
(fn [[k v]] (fn [[k v]]
[k (disj v next-entity)]) [k (disj v next-entity)])
entity-dependencies))) entity-dependencies)))
(apply dissoc entity-dependencies next-order) (apply dissoc entity-dependencies next-order)
next-order)] next-order)]
(if (seq next-deps) (if (seq next-deps)
(recur next-deps (into order next-order)) (recur next-deps (into order next-order))
(into order next-order))))) (into order next-order)))))
(def loaded (atom #{})) (def loaded (atom #{}))
@@ -526,3 +527,4 @@
:service "export-backup") :service "export-backup")
(Thread/sleep 5000) (Thread/sleep 5000)
(throw e)))) (throw e))))
)

View File

@@ -853,3 +853,21 @@
:status "WARN") :status "WARN")
(transact-with-backoff tx (inc attempt))) (transact-with-backoff tx (inc attempt)))
(throw e)))))) (throw e))))))
(defn visible-clients [id]
(cond
(= (:user/role id) "none")
#{}
(= (:user/role id) "admin")
(->> (dc/q '[:find (pull ?e [:db/id :client/code])
:in $
:where [?e :client/code]]
(dc/db conn))
(map first)
(map :db/id)
(into #{}))
(#{"manager" "user" "power-user"} (:user/role id))
(into #{}
(map :db/id (:user/clients id [])))))

View File

@@ -4,11 +4,14 @@
:refer [add-sorter-fields-2 :refer [add-sorter-fields-2
apply-pagination apply-pagination
apply-sort-3 apply-sort-3
pull-id
merge-query merge-query
pull-many pull-many
visible-clients
conn]] conn]]
[auto-ap.graphql.utils :refer [limited-clients]] [auto-ap.graphql.utils :refer [limited-clients]]
[clj-time.coerce :as c] [clj-time.coerce :as c]
[clj-time.core :as time]
[clojure.set :as set] [clojure.set :as set]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[datomic.client.api :as dc] [datomic.client.api :as dc]
@@ -30,132 +33,45 @@
{:sales-order/client [:client/name :db/id :client/code] {:sales-order/client [:client/name :db/id :client/code]
:sales-order/charges [* {:charge/processor [:db/ident]} {:expected-deposit/_charges [:db/id]}]}]) :sales-order/charges [* {:charge/processor [:db/ident]} {:expected-deposit/_charges [:db/id]}]}])
#_(defn client-index-search [db attr client-ids [start end]]
(for [c client-ids
d
(dc/q '[:find ?e
:in $ ?start ?end
:where [?e :sales-order/client+date ?g]
[(>= ?g ?start)]
[(<= ?g ?end)]]
db [c start] [c end])
#_(dc/index-range db {:attrid attr
:start [c start]
:end [c end]})]
(first d)))
#_(comment
(do
(println "starting")
(doseq [n (partition-all 500 (dc/q '[:find ?s ?d
:where [?s :sales-order/date ?d]]
(dc/db conn)))]
(print ".")
(dc/transact conn {:tx-data (map (fn [[n d]] {:db/id n :sales-order/date d}) n)}))
(println "done"))
(time (count (client-index-search (dc/db conn)
:sales-order/client+date
[3575611821801781]
[#inst "2021-05-21T18:06:02.000-00:00"
#inst "2022-09-25T18:06:02.000-00:00"]
)))
(time (count (dc/q '[:find ?e
:in $ ?client ?start ?end
:where [?e :sales-order/client ?client]
[?e :sales-order/date ?date]
[(>= ?date ?start)]
[(<= ?date ?end)]]
(dc/db conn)
3575611821801781
#inst "2021-05-21T18:06:02.000-00:00"
#inst "2022-09-25T18:06:02.000-00:00"
)))
(time (do (count (dc/q '[:find ?e,
:in $ [[?s1 ?e1]]
:where
#_[(untuple ?period) [?s1 ?e1]]
[(q '[:find ?e
:in $ ?s1 ?e1
:where [?e :sales-order/client+date ?g]
[(>= ?g ?s1)]
[(<= ?g ?e1)]]
$ ?s1 ?e1) [?e ...]]
]
(dc/db conn) [[[3575611821801781 #inst "2022-08-21T18:06:02.000-00:00" ] [3575611821801781 #inst "2022-09-25T18:06:02.000-00:00"]]
[[3575611821801766 #inst "2022-08-21T18:06:02.000-00:00" ] [3575611821801766 #inst "2022-09-25T18:06:02.000-00:00"]]]))
(println "done")))
(dc/index-range (dc/db conn) {:attrid :sales-order/date+client3
:start [#inst "2022-08-25T18:06:02.000-00:00" ]
:end [#inst "2022-08-25T20:06:02.000-00:00" ]})
(dc/pull (dc/db conn) '[*] :sales-order/date+client3)
(dc/pull (dc/db conn) '[*] 5040161313201309)
#_(time (do (dc/q {:query '{:find [(count ?x)],
:in [$ ?client-id],
:where [[?e :sales-order/date+client3 ?x]]},
:args [(dc/db conn) 3575611821801781]})
(println "done")))
#_(datomic.dev-local.async/return-1)
(clojure.core.async/<!! (dca/transact conn {:tx-data [{:db/ident :sales-order/client+date
:db/valueType :db.type/tuple
:db/tupleAttrs [:sales-order/client :sales-order/date]
:db/cardinality :db.cardinality/one}]
:timeout 600000}))
)
(defn raw-graphql-ids [db args] (defn raw-graphql-ids [db args]
(let [query (cond-> {:query {:find [] (let [visible-clients (visible-clients (:id args))
:in ['$] selected-clients (->> (cond
(:client-id args)
(set/intersection #{(:client-id args)}
visible-clients)
(:client-code args)
(set/intersection #{(pull-id db [:client/code (:client-code args)])}
visible-clients)
:else
visible-clients)
(take 3)
set)
_ (mu/log ::selected-clients
:selected-clients selected-clients)
query (cond-> {:query {:find []
:in ['$ '[?c ...]]
:where []} :where []}
:args [db]} :args [db selected-clients]}
(:sort args) (add-sorter-fields-2 {"client" ['[?e :sales-order/client ?c]
'[?c :client/name ?sort-client]]
"location" ['[?e :sales-order/location ?sort-location]] true
"source" ['[?e :sales-order/source ?sort-source]] (merge-query {:query {:in ['?start-date '?end-date]
"date" ['[?e :sales-order/date ?sort-date]] :where '[[(iol-ion.query/sales-orders-in-range $ ?c ?start-date ?end-date) [?e ...]]]}
"total" ['[?e :sales-order/total ?sort-total]] :args [(or (some-> (:start (:date-range args)) c/to-date) (iol-ion.query/recent-date 5))
"tax" ['[?e :sales-order/tax ?sort-tax]] (or (some-> (:end (:date-range args)) c/to-date )
"tip" ['[?e :sales-order/tip ?sort-tip]]} (c/to-date (time/now)))]})
(:sort args) (add-sorter-fields-2 {"client" ['[?e :sales-order/client ?c]
'[?c :client/name ?sort-client]]
"location" ['[?e :sales-order/location ?sort-location]]
"source" ['[?e :sales-order/source ?sort-source]]
"date" ['[?e :sales-order/date ?sort-date]]
"total" ['[?e :sales-order/total ?sort-total]]
"tax" ['[?e :sales-order/tax ?sort-tax]]
"tip" ['[?e :sales-order/tip ?sort-tip]]}
args) args)
(:start (:date-range args))
(merge-query {:query {:in '[?start-date]
:where ['[?e :sales-order/date ?date]
'[(>= ?date ?start-date)]]}
:args [(c/to-date (:start (:date-range args)))]})
(:end (:date-range args))
(merge-query {:query {:in '[?end-date]
:where ['[?e :sales-order/date ?date]
'[(<= ?date ?end-date)]]}
:args [(c/to-date (:end (:date-range args)))]})
(limited-clients (:id args))
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :sales-order/client ?xx]]}
:args [(set (map :db/id (limited-clients (:id args))))]})
(:client-id args)
(merge-query {:query {:in ['?client-id]
:where ['[?e :sales-order/client ?client-id]]}
:args [(:client-id args)]})
(:client-code args)
(merge-query {:query {:in ['?client-code]
:where ['[?e :sales-order/client ?client-id]
'[?client-id :client/code ?client-code]]}
:args [(:client-code args)]})
(:category args) (:category args)
(merge-query {:query {:in ['?category] (merge-query {:query {:in ['?category]
:where ['[?e :sales-order/line-items ?li] :where ['[?e :sales-order/line-items ?li]
@@ -163,11 +79,11 @@
:args [(:category args)]}) :args [(:category args)]})
(:processor args) (:processor args)
(merge-query {:query {:in ['?processor] (merge-query {:query {:in ['?processor]
:where ['[?e :sales-order/charges ?chg] :where ['[?e :sales-order/charges ?chg]
'[?chg :charge/processor ?processor]]} '[?chg :charge/processor ?processor]]}
:args [(keyword "ccp-processor" :args [(keyword "ccp-processor"
(name (:processor args)))]}) (name (:processor args)))]})
(:type-name args) (:type-name args)
(merge-query {:query {:in ['?type-name] (merge-query {:query {:in ['?type-name]
:where ['[?e :sales-order/charges ?chg] :where ['[?e :sales-order/charges ?chg]
@@ -175,28 +91,27 @@
:args [(:type-name args)]}) :args [(:type-name args)]})
(:total-gte args) (:total-gte args)
(merge-query {:query {:in ['?total-gte] (merge-query {:query {:in ['?total-gte]
:where ['[?e :sales-order/total ?a] :where ['[?e :sales-order/total ?a]
'[(>= ?a ?total-gte)]]} '[(>= ?a ?total-gte)]]}
:args [(:total-gte args)]}) :args [(:total-gte args)]})
(:total-lte args) (:total-lte args)
(merge-query {:query {:in ['?total-lte] (merge-query {:query {:in ['?total-lte]
:where ['[?e :sales-order/total ?a] :where ['[?e :sales-order/total ?a]
'[(<= ?a ?total-lte)]]} '[(<= ?a ?total-lte)]]}
:args [(:total-lte args)]}) :args [(:total-lte args)]})
(:total args) (:total args)
(merge-query {:query {:in ['?total] (merge-query {:query {:in ['?total]
:where ['[?e :sales-order/total ?sales-order-total] :where ['[?e :sales-order/total ?sales-order-total]
'[(iol-ion.query/dollars= ?sales-order-total ?total)]]} '[(iol-ion.query/dollars= ?sales-order-total ?total)]]}
:args [(:total args)]}) :args [(:total args)]})
true true
(merge-query {:query {:find ['?date '?e] (merge-query {:query {:find ['?date '?e]
:where ['[?e :sales-order/date ?date]]}}))] :where ['[?e :sales-order/date ?date]]}}))]
(println query)
(mu/log ::query-stats (mu/log ::query-stats
:stats (:query-stats (dc/q (assoc query :query-stats true))) :stats (:query-stats (dc/q (assoc query :query-stats true)))
:q (str query)) :q (str query))
@@ -207,12 +122,12 @@
true (apply-pagination args)))) true (apply-pagination args))))
(defn graphql-results [ids db _] (defn graphql-results [ids db _]
(let [results (->> (pull-many db default-read ids) (let [results (->> (pull-many db default-read ids)
(group-by :db/id)) (group-by :db/id))
payments (->> ids payments (->> ids
(map results) (map results)
(map first) (map first)
(mapv <-datomic))] (mapv <-datomic))]
payments)) payments))
(defn summarize-orders [ids] (defn summarize-orders [ids]

View File

@@ -98,6 +98,8 @@
(#{"manager" "user" "power-user"} (:user/role id)) (#{"manager" "user" "power-user"} (:user/role id))
(:user/clients id []))) (:user/clients id [])))
(defn result->page [results result-count key args] (defn result->page [results result-count key args]
{key (map ->graphql results) {key (map ->graphql results)
:total result-count :total result-count

View File

@@ -363,13 +363,13 @@
:input-stream (io/make-input-stream pdf-data {}) :input-stream (io/make-input-stream pdf-data {})
:metadata {:content-length (count pdf-data) :metadata {:content-length (count pdf-data)
:content-type "application/pdf"}) :content-type "application/pdf"})
@(d/transact conn (dc/transact conn
[{:report/name name {:tx-data [{:report/name name
:report/client (:client_ids args) :report/client (:client_ids args)
:report/key key :report/key key
:report/url url :report/url url
:report/creator (:user user) :report/creator (:user user)
:report/created (java.util.Date.)}]) :report/created (java.util.Date.)}]})
{:report/name name {:report/name name
:report/url url })) :report/url url }))