diff --git a/src/clj/auto_ap/datomic/transactions.clj b/src/clj/auto_ap/datomic/transactions.clj new file mode 100644 index 00000000..7fbf75c3 --- /dev/null +++ b/src/clj/auto_ap/datomic/transactions.clj @@ -0,0 +1,44 @@ +(ns auto-ap.datomic.transactions + (:require [datomic.api :as d] + [auto-ap.datomic :refer [uri]] + [clj-time.coerce :as c])) + +(defn add-arg [query name value where] + (-> query + (update :args conj value) + (update-in [:query :in] conj name) + (update-in [:query :where] conj where))) + +(defn raw-graphql [args] + (->> (d/query + (cond-> {:query {:find ['(pull ?e [* {:transaction/client [:client/name :db/id]}])] + :in ['$] + :where ['[?e :transaction/original-id]]} + :args [(d/db (d/connect uri))]} + + (:company-id args) (add-arg '?company-id (Long/parseLong (:company-id args)) + '[?e :transaction/client ?company-id]))) + (map first) + + (map #(update % :transaction/date c/from-date)) + (map #(update % :transaction/post-date c/from-date)))) + +(defn sort-fn [args] + (cond + (= "client" (:sort-by args)) + #(-> % :transaction/client :client/name) + + :else + (keyword "transaction" (:sort-by args)))) +(defn get-graphql [args] + (let [results (raw-graphql args)] + (cond->> results + (:sort-by args) (sort-by (sort-fn args)) + (= (:asc args) false) (reverse) + true (drop (:start args 0)) + true (take (:count args 20))))) + +(defn count-graphql [args] + + (->> (raw-graphql args) + (count)))