From f4e5b4ebe030922e9b3a2e2163a84973bb4b2ff6 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 28 May 2021 13:34:18 -0700 Subject: [PATCH] missing file. --- src/clj/auto_ap/background/vendor.clj | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/clj/auto_ap/background/vendor.clj diff --git a/src/clj/auto_ap/background/vendor.clj b/src/clj/auto_ap/background/vendor.clj new file mode 100644 index 00000000..cb82ac49 --- /dev/null +++ b/src/clj/auto_ap/background/vendor.clj @@ -0,0 +1,44 @@ +(ns auto-ap.background.vendor + (:require [auto-ap.datomic.invoices :as d-invoices] + [auto-ap.datomic :refer [uri conn]] + [datomic.api :as d] + [auto-ap.time :as time] + [clj-time.coerce :as coerce] + [mount.core :as mount] + [yang.scheduler :as scheduler] + [unilog.context :as lc] + [clojure.tools.logging :as log])) + +(defn refresh-vendor-usages [] + (lc/with-context {:source "refreshing vendor-usages"} + + (try + (->> {:query {:find ['?v '?c '(count ?e)] + :in ['$] + :where ['[?v :vendor/name] + '(or-join [?v ?c ?e] + (and + [?e :invoice/vendor ?v] + [?e :invoice/client ?c]) + (and + [?e :transaction/vendor ?v] + [?e :transaction/client ?c]) + (and + [?e :journal-entry/vendor ?v] + [?e :journal-entry/client ?c]))]} + :args [(d/db conn)]} + (d/query) + (map (fn [[v c cnt]] + #:vendor-usage {:vendor v + :client c + :key (str v "-" c) + :count cnt})) + (d/transact conn) + deref) + (log/info "updated vendor usage") + (catch Exception e + (log/error e))))) + +(mount/defstate refresh-vendor-usages-worker + :start (scheduler/every (* 60 60 1000) refresh-vendor-usages) + :stop (scheduler/stop refresh-vendor-usages))