making it possible to see a list of checks

This commit is contained in:
Bryce Covert
2018-06-07 15:17:07 -07:00
parent fbccc0b209
commit 970a27da56
7 changed files with 256 additions and 3 deletions

View File

@@ -49,3 +49,36 @@
(map clj->db)))
(map db->clj)
(map data->fields)))
#_(defn add-sort-by [q sort-by asc]
(let [sort-by-key (keyword sort-by)]
(cond (all-keys sort-by-key)
(helpers/merge-order-by q [sort-by-key (when-not asc :desc)])
(= :vendor sort-by-key)
(-> q
(helpers/merge-left-join [:vendors :v] [:= :v.id :invoices.vendor-id] )
(helpers/merge-order-by [:v.name (when-not asc :desc)]))
(= :company sort-by-key)
(-> q
(helpers/merge-left-join [:companies :c] [:= :c.id :invoices.company-id] )
(helpers/merge-order-by [:c.name (when-not asc :desc)]))
:else
q)))
(defn base-graphql [{:keys [company-id]}]
(cond-> base-query
(not (nil? company-id)) (helpers/merge-where [:= :company-id company-id])))
(defn get-graphql [{:keys [start sort-by asc] :as args}]
(query
(cond-> (base-graphql args)
#_#_(not (nil? sort-by) ) (add-sort-by sort-by asc)
true (assoc :limit 20)
start (assoc :offset start))))
(defn count-graphql [args]
(:count (first (query
(assoc (base-graphql args) :select [:%count.*])))))

View File

@@ -13,6 +13,7 @@
[auto-ap.db.checks :as checks]
[auto-ap.routes.checks :as rchecks]
[auto-ap.graphql.users :as gq-users]
[auto-ap.graphql.checks :as gq-checks]
[auto-ap.graphql.expense-accounts :as expense-accounts]
[auto-ap.graphql.invoices :as gq-invoices]
[auto-ap.db.reminders :as reminders]
@@ -54,11 +55,17 @@
:scheduled {:type 'String}
:sent {:type 'String}
:vendor {:type :vendor
:resolve :get-vendor-for-invoice}}
:resolve :get-vendor-for-invoice}
}
}
:check {:fields {:id {:type 'Int}
:amount {:type 'String}
:vendor {:type :vendor
:resolve :get-vendor-for-check}
:company {:type :company
:resolve :get-company-for-check}
:date {:type 'String}
:s3_url {:type 'String}
:check_number {:type 'Int}}}
@@ -113,6 +120,12 @@
:start {:type 'Int}
:end {:type 'Int}}}
:check_page {:fields {:checks {:type '(list :check)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}
:reminder_page {:fields {:reminders {:type '(list :reminder)}
:count {:type 'Int}
:total {:type 'Int}
@@ -136,6 +149,14 @@
:asc {:type 'Boolean}}
:resolve :get-invoice-page}
:check_page {:type '(list :check_page)
:args {:company_id {:type 'Int}
:start {:type 'Int}
:sort_by {:type 'String}
:asc {:type 'Boolean}}
:resolve :get-check-page}
:reminder_page {:type '(list :reminder_page)
:args {:start {:type 'Int}
:sort_by {:type 'String}
@@ -329,8 +350,11 @@
(def schema
(-> integreat-schema
(attach-resolvers {:get-invoice-page get-invoice-page
:get-check-page gq-checks/get-check-page
:get-reminder-page get-reminder-page
:get-vendor-for-invoice get-vendor-for-invoice
:get-vendor-for-check gq-checks/get-vendor-for-check
:get-company-for-check gq-checks/get-company-for-check
:get-company-for-invoice get-company-for-invoice
:get-invoices-checks get-invoices-checks
:get-check-by-id get-check-by-id

View File

@@ -0,0 +1,44 @@
(ns auto-ap.graphql.checks
(:require [auto-ap.graphql.utils :refer [->graphql <-graphql]]
[com.walmartlabs.lacinia :refer [execute]]
[com.walmartlabs.lacinia.executor :as executor]
[com.walmartlabs.lacinia.resolve :as resolve]
[auto-ap.db.checks :as checks]
[auto-ap.db.vendors :as vendors]
[auto-ap.utils :refer [by]]
[auto-ap.db.companies :as companies]
[auto-ap.time :refer [parse normal-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 [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)))