From 85dec7a40761d67ce34b4aaf810088ef54464bbb Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 4 Sep 2020 19:53:54 -0700 Subject: [PATCH] everything is audited, and we have expected deposits --- src/clj/auto_ap/datomic/expected_deposit.clj | 105 +++++++++++++++++++ src/clj/auto_ap/graphql/expected_deposit.clj | 16 +++ 2 files changed, 121 insertions(+) create mode 100644 src/clj/auto_ap/datomic/expected_deposit.clj create mode 100644 src/clj/auto_ap/graphql/expected_deposit.clj diff --git a/src/clj/auto_ap/datomic/expected_deposit.clj b/src/clj/auto_ap/datomic/expected_deposit.clj new file mode 100644 index 00000000..6c50e5e3 --- /dev/null +++ b/src/clj/auto_ap/datomic/expected_deposit.clj @@ -0,0 +1,105 @@ +(ns auto-ap.datomic.expected-deposit + (:require [auto-ap.datomic :refer [add-sorter-fields apply-pagination apply-sort-3 merge-query conn]] + [auto-ap.graphql.utils :refer [limited-clients]] + [auto-ap.utils :refer [dollars=]] + [clj-time.coerce :as c] + [datomic.api :as d] + [clojure.tools.logging :as log])) + + +(defn <-datomic [result] + (-> result + (update :expected-deposit/date c/from-date))) + +(def default-read '[* + {:expected-deposit/client [:client/name :db/id :client/code]}]) + +(defn raw-graphql-ids [db args] + (let [query (cond-> {:query {:find [] + :in ['$] + :where []} + :args [db]} + (:sort args) (add-sorter-fields {"client" ['[?e :expected-deposit/client ?c] + '[?c :client/name ?sort-client]] + "location" ['[?e :expected-deposit/location ?sort-location]] + "date" ['[?e :expected-deposit/date ?sort-date]] + "total" ['[?e :expected-deposit/total ?sort-total]] + "fee" ['[?e :expected-deposit/fee ?sort-fee]]} + args) + + (limited-clients (:id args)) + (merge-query {:query {:in ['[?xx ...]] + :where ['[?e :expected-deposit/client ?xx]]} + :args [(set (map :db/id (limited-clients (:id args))))]}) + + (:client-id args) + (merge-query {:query {:in ['?client-id] + :where ['[?e :expected-deposit/client ?client-id]]} + :args [(:client-id args)]}) + (:client-code args) + (merge-query {:query {:in ['?client-code] + :where ['[?e :expected-deposit/client ?client-id] + '[?client-id :client/code ?client-code]]} + :args [(:client-code args)]}) + + (:total-gte args) + (merge-query {:query {:in ['?total-gte] + :where ['[?e :expected-deposit/total ?a] + '[(>= ?a ?total-gte)]]} + :args [(:total-gte args)]}) + + (:total-lte args) + (merge-query {:query {:in ['?total-lte] + :where ['[?e :expected-deposit/total ?a] + '[(<= ?a ?total-lte)]]} + :args [(:total-lte args)]}) + + (:total args) + (merge-query {:query {:in ['?total] + :where ['[?e :expected-deposit/total ?expected-deposit-total] + '[(auto-ap.utils/dollars= ?expected-deposit-total ?total)]]} + :args [(:total args)]}) + + + (:start (:date-range args)) + (merge-query {:query {:in '[?start-date] + :where ['[?e :expected-deposit/date ?date] + '[(>= ?date ?start-date)]]} + :args [(c/to-date (:start (:date-range args)))]}) + + (:end (:date-range args)) + (merge-query {:query {:in '[?end-date] + :where ['[?e :expected-deposit/date ?date] + '[(<= ?date ?end-date)]]} + :args [(c/to-date (:end (:date-range args)))]}) + + true + (merge-query {:query {:find ['?sort-default '?e] + :where ['[?e :expected-deposit/date ?sort-default]]}}))] + + (cond->> query + true (d/query) + true (apply-sort-3 args) + true (apply-pagination args)))) + +(defn graphql-results [ids db args] + (let [results (->> (d/pull-many db default-read ids) + (group-by :db/id)) + payments (->> ids + (map results) + (map first) + (mapv <-datomic))] + payments)) + +(defn get-graphql [args] + (log/info "ARGS" args) + (let [db (d/db conn) + {ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)] + + [(->> (graphql-results ids-to-retrieve db args)) + matching-count])) + +(defn get-by-id [id] + (->> + (d/pull (d/db conn) default-read id) + (<-datomic))) diff --git a/src/clj/auto_ap/graphql/expected_deposit.clj b/src/clj/auto_ap/graphql/expected_deposit.clj new file mode 100644 index 00000000..e2f45ac1 --- /dev/null +++ b/src/clj/auto_ap/graphql/expected_deposit.clj @@ -0,0 +1,16 @@ +(ns auto-ap.graphql.expected-deposit + (:require [auto-ap.datomic.expected-deposit :as d-expected-deposit] + [auto-ap.graphql.utils + :refer + [->graphql <-graphql assert-admin result->page]])) + +(defn get-expected-deposit [context args value] + (let [args (assoc args :id (:id context)) + [sales-orders sales-orders-count] (d-expected-deposit/get-graphql (<-graphql args))] + (result->page sales-orders sales-orders-count :data args ))) + +(defn get-all-expected-deposits [context args value] + (assert-admin (:id context)) + (map + ->graphql + (first (d-expected-deposit/get-graphql (assoc (<-graphql args) :count Integer/MAX_VALUE)))))