making everyone do queries the same way.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
(ns auto-ap.datomic.checks
|
(ns auto-ap.datomic.checks
|
||||||
(:require [datomic.api :as d]
|
(:require [datomic.api :as d]
|
||||||
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination]]
|
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination add-sorter-field]]
|
||||||
[auto-ap.graphql.utils :refer [limited-clients]]
|
[auto-ap.graphql.utils :refer [limited-clients]]
|
||||||
[clojure.set :refer [rename-keys]]
|
[clojure.set :refer [rename-keys]]
|
||||||
[clj-time.coerce :as c]))
|
[clj-time.coerce :as c]))
|
||||||
@@ -12,13 +12,6 @@
|
|||||||
(update :payment/type :db/ident)
|
(update :payment/type :db/ident)
|
||||||
(rename-keys {:invoice-payment/_payment :payment/invoices})))
|
(rename-keys {:invoice-payment/_payment :payment/invoices})))
|
||||||
|
|
||||||
(defn add-arg [query name value where & rest]
|
|
||||||
(let [query (-> query
|
|
||||||
(update :args conj value)
|
|
||||||
(update-in [:query :in] conj name)
|
|
||||||
(update-in [:query :where] conj where))]
|
|
||||||
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
|
||||||
|
|
||||||
(def default-read '[*
|
(def default-read '[*
|
||||||
{:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]}
|
{:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]}
|
||||||
{:payment/client [:client/name :db/id :client/code]}
|
{:payment/client [:client/name :db/id :client/code]}
|
||||||
@@ -34,6 +27,19 @@
|
|||||||
:in ['$]
|
:in ['$]
|
||||||
:where []}
|
:where []}
|
||||||
:args [db]}
|
:args [db]}
|
||||||
|
(:sort-by args) (add-sorter-field {"client" ['[?e :payment/client ?c]
|
||||||
|
'[?c :client/name ?sorter]]
|
||||||
|
"vendor" ['[?e :payment/vendor ?v]
|
||||||
|
'[?v :vendor/name ?sorter]]
|
||||||
|
"bank-account" ['[?e :payment/bank-account ?c]
|
||||||
|
'[?c :bank-account/name ?sorter]]
|
||||||
|
"check-number" ['(or-join [?e ?sorter]
|
||||||
|
[?e :payment/check-number ?sorter]
|
||||||
|
[(ground "" ?sorter)])]
|
||||||
|
"date" ['[?e :payment/date ?sorter]]
|
||||||
|
"amount" ['[?e :payment/amount ?sorter]]
|
||||||
|
"status" ['[?e :payment/status ?sorter]]}
|
||||||
|
args)
|
||||||
|
|
||||||
(limited-clients (:id args))
|
(limited-clients (:id args))
|
||||||
(merge-query {:query {:in ['[?xx ...]]
|
(merge-query {:query {:in ['[?xx ...]]
|
||||||
@@ -110,17 +116,6 @@
|
|||||||
true (apply-sort-2 args)
|
true (apply-sort-2 args)
|
||||||
true (apply-pagination args))))
|
true (apply-pagination args))))
|
||||||
|
|
||||||
(defn sort-fn [args]
|
|
||||||
(cond
|
|
||||||
(= "client" (:sort-by args))
|
|
||||||
#(-> % :payment/client :client/name)
|
|
||||||
|
|
||||||
(= "vendor" (:sort-by args))
|
|
||||||
#(-> % :payment/vendor :vendor/name (or "") (.toLowerCase ))
|
|
||||||
|
|
||||||
:else
|
|
||||||
(keyword "payment" (:sort-by args))))
|
|
||||||
|
|
||||||
(defn graphql-results [ids db args]
|
(defn graphql-results [ids db args]
|
||||||
(let [results (->> (d/pull-many db default-read ids)
|
(let [results (->> (d/pull-many db default-read ids)
|
||||||
(group-by :db/id))
|
(group-by :db/id))
|
||||||
|
|||||||
@@ -1,44 +1,42 @@
|
|||||||
(ns auto-ap.datomic.invoices
|
(ns auto-ap.datomic.invoices
|
||||||
(:require [datomic.api :as d]
|
(:require [datomic.api :as d]
|
||||||
[auto-ap.datomic :refer [uri remove-nils merge-query]]
|
[auto-ap.datomic :refer [uri remove-nils merge-query apply-pagination apply-sort-2 add-sorter-field]]
|
||||||
[auto-ap.graphql.utils :refer [limited-clients]]
|
[auto-ap.graphql.utils :refer [limited-clients]]
|
||||||
[auto-ap.parse :as parse]
|
[auto-ap.parse :as parse]
|
||||||
[clj-time.coerce :as c]
|
[clj-time.coerce :as c]
|
||||||
[clojure.set :refer [rename-keys]]
|
[clojure.set :refer [rename-keys]]
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
|
(def default-read '[*
|
||||||
(defn add-arg [query name value where & rest]
|
{:invoice/client [:client/name :db/id :client/locations :client/code]}
|
||||||
(let [query (-> query
|
{:invoice/vendor [* {:vendor/address [*]}]}
|
||||||
(update :args conj value)
|
{:invoice/status [:db/ident]}
|
||||||
(update-in [:query :in] conj name)
|
{:invoice/expense-accounts [* {:invoice-expense-account/account [*]}]}
|
||||||
(update-in [:query :where] conj where))]
|
{:invoice-payment/_invoice [* {:invoice-payment/payment [* {:payment/status [*]}
|
||||||
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
|
{:payment/bank-account [*]}
|
||||||
|
{:transaction/_payment [*]}]}]}])
|
||||||
(def default-read '(pull ?e [*
|
|
||||||
{:invoice/client [:client/name :db/id :client/locations :client/code]}
|
|
||||||
{:invoice/vendor [* {:vendor/address [*]}]}
|
|
||||||
{:invoice/status [:db/ident]}
|
|
||||||
{:invoice/expense-accounts [* {:invoice-expense-account/account [*]}]}
|
|
||||||
{:invoice-payment/_invoice [* {:invoice-payment/payment [* {:payment/status [*]}
|
|
||||||
{:payment/bank-account [*]}
|
|
||||||
{:transaction/_payment [*]}]}]}]))
|
|
||||||
|
|
||||||
(defn <-datomic [x]
|
(defn <-datomic [x]
|
||||||
(->> x
|
(-> x
|
||||||
|
(update :invoice/date c/from-date)
|
||||||
|
(update :invoice/status :db/ident)
|
||||||
|
(rename-keys {:invoice-payment/_invoice :invoice/payments})))
|
||||||
|
|
||||||
|
(defn raw-graphql-ids [db args]
|
||||||
(map #(update % :invoice/date c/from-date))
|
(->> (cond-> {:query {:find []
|
||||||
(map #(update % :invoice/status :db/ident))
|
|
||||||
(map #(rename-keys % {:invoice-payment/_invoice :invoice/payments}))))
|
|
||||||
|
|
||||||
(defn raw-graphql [args]
|
|
||||||
(->> (d/query
|
|
||||||
(cond-> {:query {:find [default-read]
|
|
||||||
:in ['$]
|
:in ['$]
|
||||||
:where ['[?e :invoice/invoice-number]]}
|
:where ['[?e :invoice/invoice-number]]}
|
||||||
:args [(d/db (d/connect uri))]}
|
:args [(d/db (d/connect uri))]}
|
||||||
|
(:sort-by args) (add-sorter-field {"client" ['[?e :invoice/client ?c]
|
||||||
|
'[?c :client/name ?sorter]]
|
||||||
|
"vendor" ['[?e :invoice/vendor ?v]
|
||||||
|
'[?v :vendor/name ?sorter]]
|
||||||
|
"description-original" ['[?e :transaction/description-original ?sorter]]
|
||||||
|
"date" ['[?e :invoice/date ?sorter]]
|
||||||
|
"invoice-number" ['[?e :invoice/invoice-number ?sorter]]
|
||||||
|
"total" ['[?e :invoice/total ?sorter]]
|
||||||
|
"outstanding" ['[?e :invoice/outstanding-balance ?sorter]]}
|
||||||
|
args)
|
||||||
(limited-clients (:id args))
|
(limited-clients (:id args))
|
||||||
(merge-query {:query {:in ['[?xx ...]]
|
(merge-query {:query {:in ['[?xx ...]]
|
||||||
:where ['[?e :invoice/client ?xx]]}
|
:where ['[?e :invoice/client ?xx]]}
|
||||||
@@ -89,67 +87,53 @@
|
|||||||
(merge-query {:query {:in ['?invoice-number-like]
|
(merge-query {:query {:in ['?invoice-number-like]
|
||||||
:where ['[?e :invoice/invoice-number ?invoice-number]
|
:where ['[?e :invoice/invoice-number ?invoice-number]
|
||||||
'[(.contains ^String ?invoice-number ?invoice-number-like)]]}
|
'[(.contains ^String ?invoice-number ?invoice-number-like)]]}
|
||||||
:args [(:invoice-number-like args)]})))
|
:args [(:invoice-number-like args)]})
|
||||||
(map first)
|
true
|
||||||
(<-datomic)))
|
(merge-query {:query {:find ['?base-date '?e]
|
||||||
|
:where ['[?e :invoice/date ?base-date]]}}) )
|
||||||
|
(d/query)
|
||||||
|
(apply-sort-2 args)
|
||||||
|
(apply-pagination args)))
|
||||||
|
|
||||||
(defn sort-fn [args]
|
|
||||||
(cond
|
|
||||||
(= "client" (:sort-by args))
|
|
||||||
#(-> % :invoice/client :client/name .toLowerCase)
|
|
||||||
|
|
||||||
(= "vendor" (:sort-by args))
|
(defn graphql-results [ids db args]
|
||||||
#(-> % :invoice/vendor :vendor/name .toLowerCase)
|
(let [results (->> (d/pull-many db default-read ids)
|
||||||
|
(group-by :db/id))
|
||||||
|
|
||||||
:else
|
invoices (->> ids
|
||||||
(keyword "invoice" (:sort-by args))))
|
(map results)
|
||||||
|
(map first)
|
||||||
|
(mapv <-datomic))]
|
||||||
|
invoices))
|
||||||
|
|
||||||
(defn get-graphql [args]
|
(defn get-graphql [args]
|
||||||
(let [results (raw-graphql args)]
|
(let [db (d/db (d/connect uri))
|
||||||
(cond->> results
|
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
|
||||||
(:sort-by args) (sort-by (sort-fn args))
|
|
||||||
(= (:asc args) false) (reverse)
|
|
||||||
true (drop (:start args 0))
|
|
||||||
true (take (:count args 100)))))
|
|
||||||
|
|
||||||
(defn count-graphql [args]
|
|
||||||
|
|
||||||
(->> (raw-graphql args)
|
[(->> (graphql-results ids-to-retrieve db args))
|
||||||
(count)))
|
matching-count]))
|
||||||
|
|
||||||
(defn get-by-id [id]
|
(defn get-by-id [id]
|
||||||
(->>
|
(-> (d/db (d/connect uri))
|
||||||
(d/query (-> {:query {:find [default-read]
|
(d/pull default-read id)
|
||||||
:in ['$]
|
(<-datomic)))
|
||||||
:where []}
|
|
||||||
:args [(d/db (d/connect uri))]}
|
|
||||||
(add-arg '?e id ['?e])))
|
|
||||||
(map first)
|
|
||||||
(<-datomic)
|
|
||||||
(first)))
|
|
||||||
|
|
||||||
(defn update [update]
|
(defn update [update]
|
||||||
@(d/transact (d/connect uri) [update])
|
@(d/transact (d/connect uri) [update])
|
||||||
(get-by-id (:db/id update) ))
|
(get-by-id (:db/id update) ))
|
||||||
|
|
||||||
(defn get-multi [ids]
|
(defn get-multi [ids]
|
||||||
(->>
|
(map <-datomic
|
||||||
(d/query {:query {:find [[default-read '...]]
|
(-> (d/db (d/connect uri))
|
||||||
:in ['$ ['?e '...]]
|
(d/pull-many default-read ids))))
|
||||||
:where [['?e]]}
|
|
||||||
:args [(d/db (d/connect uri))
|
|
||||||
ids] }
|
|
||||||
|
|
||||||
|
|
||||||
)
|
|
||||||
(<-datomic)))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defn find-conflicting [{:keys [:invoice/invoice-number :invoice/vendor :invoice/client :db/id]}]
|
(defn find-conflicting [{:keys [:invoice/invoice-number :invoice/vendor :invoice/client :db/id]}]
|
||||||
|
|
||||||
(->> (d/query
|
(->> (d/query
|
||||||
(cond-> {:query {:find [default-read]
|
(cond-> {:query {:find [(list 'pull '?e default-read)]
|
||||||
:in ['$ '?invoice-number '?vendor '?client '?invoice-id]
|
:in ['$ '?invoice-number '?vendor '?client '?invoice-id]
|
||||||
:where '[[?e :invoice/invoice-number ?invoice-number]
|
:where '[[?e :invoice/invoice-number ?invoice-number]
|
||||||
[?e :invoice/vendor ?vendor]
|
[?e :invoice/vendor ?vendor]
|
||||||
@@ -157,9 +141,10 @@
|
|||||||
[(not= ?e ?invoice-id)]
|
[(not= ?e ?invoice-id)]
|
||||||
]}
|
]}
|
||||||
|
|
||||||
:args [(d/db (d/connect uri)) invoice-number vendor client (or id 0)]}))
|
:args [(d/db (d/connect uri)) invoice-number vendor client (or id 0)]}
|
||||||
|
true (doto println)))
|
||||||
(map first)
|
(map first)
|
||||||
|
|
||||||
(<-datomic)))
|
(map <-datomic)))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,12 +14,11 @@
|
|||||||
[clojure.set :as set]))
|
[clojure.set :as set]))
|
||||||
|
|
||||||
(defn get-invoice-page [context args value]
|
(defn get-invoice-page [context args value]
|
||||||
|
(println "HI")
|
||||||
(let [args (assoc args :id (:id context))
|
(let [args (assoc args :id (:id context))
|
||||||
invoices (map
|
[invoices invoice-count] (d-invoices/get-graphql (<-graphql (assoc args :id (:id context))))]
|
||||||
->graphql
|
(println "HELLO" (take 1 invoices ) invoice-count)
|
||||||
(d-invoices/get-graphql (<-graphql (assoc args :id (:id context)))))
|
[{:invoices (map ->graphql invoices)
|
||||||
invoice-count (d-invoices/count-graphql (<-graphql args))]
|
|
||||||
[{:invoices invoices
|
|
||||||
:total invoice-count
|
:total invoice-count
|
||||||
:count (count invoices)
|
:count (count invoices)
|
||||||
:start (:start args 0)
|
:start (:start args 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user