From a25232a1954ac5bc734c0ad8a1a89a9fa1adbb69 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Tue, 14 Aug 2018 08:41:11 -0700 Subject: [PATCH] payments work through the ui in datomic --- src/clj/auto_ap/datomic/checks.clj | 49 ++++++++++++++++++++++++ src/clj/auto_ap/datomic/transactions.clj | 1 + src/clj/auto_ap/graphql.clj | 10 ++--- src/clj/auto_ap/graphql/checks.clj | 32 ++++------------ src/cljs/auto_ap/views/pages/checks.cljs | 10 ++--- 5 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 src/clj/auto_ap/datomic/checks.clj diff --git a/src/clj/auto_ap/datomic/checks.clj b/src/clj/auto_ap/datomic/checks.clj new file mode 100644 index 00000000..5d149878 --- /dev/null +++ b/src/clj/auto_ap/datomic/checks.clj @@ -0,0 +1,49 @@ +(ns auto-ap.datomic.checks + (:require [datomic.api :as d] + [auto-ap.datomic :refer [uri]] + [clj-time.coerce :as c])) + +(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-> {:query {:find ['(pull ?e [* + {:payment/client [:client/name :db/id]} + {:payment/vendor [:vendor/name :db/id]} + {:payment/status [:db/ident]}])] + :in ['$] + :where ['[?e :payment/original-id]]} + :args [(d/db (d/connect uri))]} + + (:company-id args) (add-arg '?company-id (Long/parseLong (:company-id args)) + '[?e :payment/client ?company-id]))) + (map first) + + (map #(update % :payment/date c/from-date)) + (map #(update % :payment/status :db/ident)) + #_(map #(update % :transaction/post-date c/from-date)))) + +(defn sort-fn [args] + (cond + (= "client" (:sort-by args)) + #(-> % :payment/client :client/name) + + :else + (keyword "payment" (: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))) diff --git a/src/clj/auto_ap/datomic/transactions.clj b/src/clj/auto_ap/datomic/transactions.clj index 7fbf75c3..3bd952e4 100644 --- a/src/clj/auto_ap/datomic/transactions.clj +++ b/src/clj/auto_ap/datomic/transactions.clj @@ -30,6 +30,7 @@ :else (keyword "transaction" (:sort-by args)))) + (defn get-graphql [args] (let [results (raw-graphql args)] (cond->> results diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index e29f4839..03fa3f0c 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -112,13 +112,11 @@ :resolve :get-checks-invoices} }} - :payment {:fields {:id {:type 'Int} + :payment {:fields {:id {:type 'String} :type {:type 'String} :amount {:type 'String} - :vendor {:type :vendor - :resolve :get-vendor-for-check} - :company {:type :company - :resolve :get-company-for-check} + :vendor {:type :vendor} + :client {:type :client} :date {:type 'String} :bank_account {:type :bank_account :resolve :bank-account-for-check} @@ -193,7 +191,7 @@ :start {:type 'Int} :end {:type 'Int}}} - :check_page {:fields {:checks {:type '(list :check)} + :check_page {:fields {:checks {:type '(list :payment)} :count {:type 'Int} :total {:type 'Int} :start {:type 'Int} diff --git a/src/clj/auto_ap/graphql/checks.clj b/src/clj/auto_ap/graphql/checks.clj index 2f6df9d9..a21c3b08 100644 --- a/src/clj/auto_ap/graphql/checks.clj +++ b/src/clj/auto_ap/graphql/checks.clj @@ -6,41 +6,25 @@ [com.walmartlabs.lacinia.resolve :as resolve] [auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.checks :as checks] + [auto-ap.datomic.checks :as d-checks] [auto-ap.db.vendors :as vendors] [auto-ap.db.invoices :as invoices] [auto-ap.utils :refer [by]] [auto-ap.db.companies :as companies] [auto-ap.time :refer [parse normal-date iso-date]])) -(defn get-vendor-for-check [context args value] - (->graphql - (if-let [vendor-cache (:vendor-cache context)] - (vendor-cache (:vendor_id value)) - (vendors/get-by-id (:vendor_id value))))) - -(defn get-company-for-check [context args value] - (->graphql - (if-let [company-cache (:company-cache context)] - (company-cache (:company_id value)) - (companies/get-by-id (:company_id value))))) (defn get-check-page [context args value] (let [args (assoc args :id (:id context)) - extra-context - (cond-> {} - (executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by :id (vendors/get-all))) - (executor/selects-field? context :invoice/company) (assoc :company-cache (by :id (companies/get-all)))) - checks (map ->graphql - (checks/get-graphql (<-graphql args))) - checks-count (checks/count-graphql (<-graphql args))] - (resolve/with-context - [{:checks checks - :total checks-count - :count (count checks) - :start (:start args 0) - :end (+ (:start args 0) (count checks))}] extra-context))) + (d-checks/get-graphql (<-graphql args))) + checks-count (d-checks/count-graphql (<-graphql args))] + [{:checks checks + :total checks-count + :count (count checks) + :start (:start args 0) + :end (+ (:start args 0) (count checks))}])) (defn add-handwritten-check [context args value] (let [invoice (invoices/get-by-id (:invoice_id args)) diff --git a/src/cljs/auto_ap/views/pages/checks.cljs b/src/cljs/auto_ap/views/pages/checks.cljs index 11b1e306..3e666df1 100644 --- a/src/cljs/auto_ap/views/pages/checks.cljs +++ b/src/cljs/auto_ap/views/pages/checks.cljs @@ -36,7 +36,7 @@ :graphql {:token (-> cofx :db :user) :query-obj {:venia/queries [[:check_page (assoc params :company-id (:id @(re-frame/subscribe [::subs/company]))) - [[:checks [:id :status :amount :type :check_number :s3_url :date [:vendor [:name :id]] [:company [:name :id]]]] + [[:checks [:id :status :amount :type :check_number :s3_url :date [:vendor [:name :id]] [:client [:name :id]]]] :total :start :end]]]} @@ -103,10 +103,10 @@ (when-not selected-company [sorted-column {:on-sort opc :style {:width percentage-size :cursor "pointer"} - :sort-key "company" + :sort-key "client" :sort-by sort-by :asc asc} - "Company"]) + "Client"]) [sorted-column {:on-sort opc :style {:width percentage-size :cursor "pointer"} @@ -147,12 +147,12 @@ [:tr [:td {:col-span 5} [:i.fa.fa-spin.fa-spinner]]] - (for [{:keys [company s3-url checks type check-number date amount id vendor status] :as i} (:checks @check-page)] + (for [{:keys [client s3-url checks type check-number date amount id vendor status] :as i} (:checks @check-page)] ^{:key id} [:tr {:class (:class i)} (when-not selected-company - [:td (:name company)]) + [:td (:name client)]) [:td (:name vendor)] [:td (cond (= "cash" type) "Cash"