From 0741b272390b85e5fff4ba97b47a8ba7d690f0b9 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 16 Aug 2018 07:13:38 -0700 Subject: [PATCH] everything is now looked up through datomic --- src/clj/auto_ap/datomic/invoices.clj | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/clj/auto_ap/datomic/invoices.clj diff --git a/src/clj/auto_ap/datomic/invoices.clj b/src/clj/auto_ap/datomic/invoices.clj new file mode 100644 index 00000000..114c7c7e --- /dev/null +++ b/src/clj/auto_ap/datomic/invoices.clj @@ -0,0 +1,60 @@ +(ns auto-ap.datomic.invoices + (:require [datomic.api :as d] + [auto-ap.datomic :refer [uri]] + [clj-time.coerce :as c] + [clojure.set :refer [rename-keys]])) + +(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-> (doto {:query {:find ['(pull ?e [* + {:invoice/client [:client/name :db/id]} + {:invoice/vendor [:vendor/name :db/id]} + {:invoice/status [:db/ident]} + {:invoice-payment/_invoice [* {:invoice-payment/payment [*]}]}])] + :in ['$] + :where ['[?e :invoice/original-id] + ]} + :args [(d/db (d/connect uri))]} + println) + + (:client-id args) (add-arg '?client-id (Long/parseLong (:client-id args)) + '[?e :invoice/client ?client-id]) + (:status args) (add-arg '?status (keyword "invoice-status" (:status args)) + '[?e :invoice/status ?status]))) + (map first) + + (map #(update % :invoice/date c/from-date)) + (map #(update % :invoice/status :db/ident)) + (map #(rename-keys % {:invoice-payment/_invoice :invoice/payments})) + + #_(map #(update % :transaction/post-date c/from-date)))) + +(defn sort-fn [args] + (cond + (= "client" (:sort-by args)) + #(-> % :invoice/client :client/name .toLowerCase) + + (= "vendor" (:sort-by args)) + #(-> % :invoice/vendor :vendor/name .toLowerCase) + + :else + (keyword "invoice" (: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)))