diff --git a/src/clj/auto_ap/datomic/transactions.clj b/src/clj/auto_ap/datomic/transactions.clj index 10c8cb95..5228bb27 100644 --- a/src/clj/auto_ap/datomic/transactions.clj +++ b/src/clj/auto_ap/datomic/transactions.clj @@ -12,13 +12,16 @@ :else (keyword "transaction" sort-by))) +;; TODO - unneeeded with merge-query (defn add-where [query & wheres] (reduce #(update-in %1 [:query :where] conj %2) query wheres)) +;; TODO - unneeeded with merge-query (defn add-wheres [query wheres] (apply add-where query wheres)) +;; TODO - unneeeded with merge-query (defn add-arg [query name value where & rest] (let [query (-> query (update :args conj value) @@ -26,12 +29,19 @@ (update-in [:query :where] conj where))] (reduce #(update-in %1 [:query :where] conj %2) query rest))) +(defn merge-query [query-part-1 query-part-2] + (-> query-part-1 + (update-in [:query :find] into (get-in query-part-2 [:query :find])) + (update-in [:query :in] into (get-in query-part-2 [:query :in])) + (update-in [:query :where] into (get-in query-part-2 [:query :where])) + (update-in [:args] into (get-in query-part-2 [:args])))) + (defn add-sorter-field [q sort-map args] - (-> q - (update-in [:query :find] conj '?sorter) - (add-wheres (sort-map - (:sort-by args) - (println "Warning, trying to sort by unsupported field" (:sort-by args)))))) + (merge-query q + {:query {:find ['?sorter] + :where (sort-map + (:sort-by args) + (println "Warning, trying to sort by unsupported field" (:sort-by args)))}})) (defn apply-sort [args sort-fn results ] (cond->> results @@ -51,8 +61,7 @@ :where ['[?e :transaction/id]]} :args [db]} - (:sort-by args) (add-sorter-field {"client" ['[?e :transaction/id] - '[?e :transaction/client ?c] + (:sort-by args) (add-sorter-field {"client" ['[?e :transaction/client ?c] '[?c :client/name ?sorter]] "description-original" ['[?e :transaction/description-original ?sorter]] "date" ['[?e :transaction/date ?sorter]] @@ -60,13 +69,20 @@ "status" ['[?e :transaction/status ?sorter]]} args) - (limited-clients (:id args)) (add-arg '[?xx ...] (set (map :db/id (limited-clients (:id args)))) - '[?e :transaction/client ?xx]) - (:client-id args) (add-arg '?client-id (:client-id args) - '[?e :transaction/client ?client-id]) - (:original-id args) (add-arg '?original-id (cond-> (:original-id args) (string? (:original-id args)) Long/parseLong ) - '[?e :transaction/client ?c] - '[?c :client/original-id ?original-id]))] + (limited-clients (:id args)) + (merge-query {:query {:in ['[?xx ...]] + :where ['[?e :transaction/client ?xx]]} + :args [(set (map :db/id (limited-clients (:id args))))]}) + + (:client-id args) + (merge-query {:query {:in ['?client-id] + :where ['[?e :transaction/client ?client-id]]} + :args [(:client-id args)]}) + (:original-id args) + (merge-query {:query {:in ['?original-id] + :where ['[?e :transaction/client ?c] + '[?c :client/original-id ?original-id]]} + :args [(:original-id args)]}))] (cond->> query true (d/query) (:sort-by args) (apply-sort args second)