83 lines
3.1 KiB
Clojure
83 lines
3.1 KiB
Clojure
(ns auto-ap.datomic.ledger
|
|
(:require [datomic.api :as d]
|
|
[auto-ap.graphql.utils :refer [->graphql limited-clients]]
|
|
[auto-ap.datomic :refer [merge-query apply-sort-2 apply-sort apply-pagination add-sorter-field]]
|
|
[auto-ap.datomic :refer [uri]]
|
|
[clj-time.coerce :as c]))
|
|
|
|
(defn sort-fn [sort-by]
|
|
(cond
|
|
(= "client" sort-by)
|
|
#(-> % :transaction/client :client/name)
|
|
|
|
(= "account" sort-by)
|
|
#(vector (-> % :transaction/account :account/name) (-> % :db/id))
|
|
|
|
:else
|
|
(keyword "transaction" sort-by)))
|
|
|
|
(defn raw-graphql-ids [db args]
|
|
(println args)
|
|
(let [query (cond-> {:query {:find []
|
|
:in ['$ ]
|
|
:where []}
|
|
:args [db]}
|
|
|
|
(:sort-by args) (add-sorter-field {"client" ['[?e :journal-entry/client ?c]
|
|
'[?c :client/name ?sorter]]
|
|
"date" ['[?e :journal-entry/date ?sorter]]
|
|
"amount" ['[?e :journal-entry/amount ?sorter]]}
|
|
args)
|
|
|
|
(limited-clients (:id args))
|
|
(merge-query {:query {:in ['[?xx ...]]
|
|
:where ['[?e :journal-entry/client ?xx]]}
|
|
:args [(set (map :db/id (limited-clients (:id args))))]})
|
|
|
|
(:client-id args)
|
|
(merge-query {:query {:in ['?client-id]
|
|
:where ['[?e :journal-entry/client ?client-id]]}
|
|
:args [(:client-id args)]})
|
|
|
|
(:date-before args)
|
|
(merge-query {:query {:in ['?date-before]
|
|
:where ['[?e :journal-entry/date ?d]
|
|
'[(<= ?d ?date-before)]]}
|
|
:args [(:date-before args)]})
|
|
|
|
true
|
|
(merge-query {:query {:find ['?e] :where ['[?e :journal-entry/original-entity]]}}))]
|
|
(cond->> query
|
|
true (d/query)
|
|
(:sort-by args) (apply-sort-2 args)
|
|
true (apply-pagination args))))
|
|
|
|
(defn graphql-results [ids db args]
|
|
(->> (d/pull-many db '[* {:journal-entry/client [:client/name :client/code :db/id]
|
|
:journal-entry/vendor [:vendor/name :db/id]
|
|
:journal-entry/line-items [* {:journal-entry-line/account [*
|
|
{:account/type [*]}
|
|
{:bank-account/type [*]}]}]
|
|
|
|
}]
|
|
ids)
|
|
(map #(update % :journal-entry/date c/from-date))
|
|
(apply-sort args (some-> (:sort-by args) sort-fn))))
|
|
|
|
(defn get-graphql [args]
|
|
(let [db (d/db (d/connect uri))
|
|
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
|
|
|
|
[(->> (graphql-results ids-to-retrieve db args))
|
|
matching-count]))
|
|
|
|
|
|
|
|
|
|
|
|
#_(get-graphql {})
|
|
|
|
|
|
|
|
|