everything is now looked up through datomic
This commit is contained in:
60
src/clj/auto_ap/datomic/invoices.clj
Normal file
60
src/clj/auto_ap/datomic/invoices.clj
Normal file
@@ -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)))
|
||||||
Reference in New Issue
Block a user