diff --git a/migrator/migrations/1530859118-DOWN-add-indexes.sql b/migrator/migrations/1530859118-DOWN-add-indexes.sql new file mode 100644 index 00000000..f09d95aa --- /dev/null +++ b/migrator/migrations/1530859118-DOWN-add-indexes.sql @@ -0,0 +1,4 @@ +-- 1530859118 DOWN add-indexes + +drop index IX_invoices_expense_accounts__invoice_id ; +drop index IX_invoices_checks__invoice_id ; diff --git a/migrator/migrations/1530859118-UP-add-indexes.sql b/migrator/migrations/1530859118-UP-add-indexes.sql new file mode 100644 index 00000000..05ae605f --- /dev/null +++ b/migrator/migrations/1530859118-UP-add-indexes.sql @@ -0,0 +1,3 @@ +-- 1530859118 UP add-indexes +create index IX_invoices_expense_accounts__invoice_id ON invoices_expense_accounts (invoice_id); +create index IX_invoices_checks__invoice_id ON invoices_checks (invoice_id); diff --git a/src/clj/auto_ap/db/invoices_checks.clj b/src/clj/auto_ap/db/invoices_checks.clj index e91e50a4..3698097f 100644 --- a/src/clj/auto_ap/db/invoices_checks.clj +++ b/src/clj/auto_ap/db/invoices_checks.clj @@ -19,6 +19,11 @@ (query (-> base-query (helpers/merge-where [:= :invoice-id id])))) +(defn get-for-check-id [id] + (query (-> base-query + (helpers/merge-where [:= :check-id id])))) + + (defn get-sum-by-invoice [id] (:sum (first (query (-> (helpers/select :%sum.amount) (helpers/from :invoices-checks) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index e8580607..030f8192 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -73,6 +73,8 @@ } } + + :check {:fields {:id {:type 'Int} :amount {:type 'String} :vendor {:type :vendor @@ -80,9 +82,15 @@ :company {:type :company :resolve :get-company-for-check} :date {:type 'String} + :bank_account {:type :bank_account + :resolve :bank-account-for-check} + :memo {:type 'String} :s3_url {:type 'String} :check_number {:type 'Int} - :status {:type 'String}}} + :status {:type 'String} + :invoices {:type '(list :invoice_check) + :resolve :get-checks-invoices} + }} :transaction {:fields {:id {:type 'Int} :amount {:type 'String} @@ -98,6 +106,7 @@ :invoice_check {:fields {:id {:type 'Int} :amount {:type 'String} + :invoice_id {:type 'Int} :check_id {:type 'Int} :check {:type :check :resolve :get-check-by-id}}} @@ -183,6 +192,14 @@ :resolve :get-invoice-page} + :all_invoices {:type '(list :invoice) + :args {:company_id {:type 'Int}} + :resolve :get-all-invoices} + + :all_checks {:type '(list :check) + :args {:company_id {:type 'Int}} + :resolve :get-all-checks} + :transaction_page {:type '(list :transaction_page) :args {:company_id {:type 'Int} :start {:type 'Int} @@ -318,6 +335,35 @@ :start (:start args 0) :end (+ (:start args 0) (count invoices))}] extra-context))) +(defn get-all-invoices [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)))) + + + invoices (map + ->graphql + (invoices/get-graphql (assoc (<-graphql args) + :limit Integer/MAX_VALUE)))] + (resolve/with-context + invoices extra-context))) + +(defn get-all-checks [context args value] + (let [extra-context + (cond-> {} + (executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by :id (vendors/get-all))) + (or (executor/selects-field? context :check/company) + (executor/selects-field? context :check/bank_account)) (assoc :company-cache (by :id (companies/get-all)))) + + + checks (map + ->graphql + (checks/get-graphql (assoc (<-graphql args) + :limit Integer/MAX_VALUE)))] + (resolve/with-context + checks extra-context))) + (defn get-reminder-page [context args value] (let [extra-context (cond-> {} @@ -348,12 +394,23 @@ (->graphql (invoices-checks/get-for-invoice-id (:id value)))) +(defn get-checks-invoices [context args value] + (->graphql + (invoices-checks/get-for-check-id (:id value)))) + (defn get-company-for-invoice [context args value] (->graphql (if-let [company-cache (:company-cache context)] (company-cache (:company_id value)) (companies/get-by-id (:company_id value))))) +(defn bank-account-for-check [context args value] + (->graphql + (let [company (if-let [company-cache (:company-cache context)] + (company-cache (:company_id value)) + (companies/get-by-id (:company_id value)))] + (first (filter #(= (:id %) (:bank_account_id value)) (:bank-accounts company))) ))) + (defn get-user-companies [context args value] (->graphql (if-let [company-cache (:company-cache context)] @@ -401,6 +458,10 @@ (def schema (-> integreat-schema (attach-resolvers {:get-invoice-page get-invoice-page + :get-all-invoices get-all-invoices + :get-all-checks get-all-checks + :bank-account-for-check bank-account-for-check + :get-check-page gq-checks/get-check-page :get-transaction-page gq-transactions/get-transaction-page :get-reminder-page get-reminder-page @@ -411,6 +472,7 @@ :get-check-for-transaction gq-transactions/get-check-for-transaction :get-company-for-invoice get-company-for-invoice :get-invoices-checks get-invoices-checks + :get-checks-invoices get-checks-invoices :get-check-by-id get-check-by-id :get-invoices-expense-accounts gq-invoices/get-invoices-expense-accounts :get-company get-company @@ -452,5 +514,5 @@ ([id q] (query id q nil )) ([id q v] - (println id q v) + (println "executing graphql query" id q v) (simplify (execute schema q v {:identity id})))) diff --git a/src/clj/auto_ap/routes/exports.clj b/src/clj/auto_ap/routes/exports.clj index 8fb32e82..c3a97794 100644 --- a/src/clj/auto_ap/routes/exports.clj +++ b/src/clj/auto_ap/routes/exports.clj @@ -8,27 +8,42 @@ [auto-ap.db.utils :refer [query]] [auto-ap.utils :refer [by]] [auto-ap.parse :as parse] + [auto-ap.graphql :as graphql] + [auto-ap.graphql.utils :refer [->graphql]] [auto-ap.routes.utils :refer [wrap-secure]] [clj-time.coerce :refer [to-date]] [auto-ap.db.invoices-expense-accounts :as expense-accounts] [ring.middleware.json :refer [wrap-json-response]] [compojure.core :refer [GET POST context defroutes wrap-routes]] - [clojure.string :as str])) + [clojure.string :as str] + [venia.core :as venia])) (defroutes routes (wrap-routes (wrap-routes (context "/" [] - (GET "/invoices/export" {:keys [query-params]} - (let [invoices (invoices/get-graphql {:company (query-params "company")})] - (map (fn [i] - (update i :date to-date)) - invoices))) + (GET "/invoices/export" {:keys [query-params identity] :as request} + (let [query [[:all_invoices + {:company-id (query-params "company")} + [:id :total :outstanding-balance :invoice-number :date + [:checks [:amount [:check [:check-number]]]] + [:vendor [:name :id]] + [:expense_accounts [:amount :id :expense_account_id :location + [:expense_account [:id :name [:parent [:id :name]]]]]] + [:company [:name :id :locations]]]]] + invoices (graphql/query identity (venia/graphql-query {:venia/queries (->graphql query)}))] + (list (:all-invoices (:data invoices))))) (GET "/checks/export" {:keys [query-params]} - (let [checks (checks/get-graphql {:company (query-params "company")})] - (map (fn [i] - (update i :date to-date)) - checks))) + (let [query [[:all_checks + {:company-id (query-params "company")} + [:id :check-number :amount :memo :date + [:invoices [:invoice-id :amount]] + [:bank-account [:number :bank-name :bank-code]] + [:vendor [:name :id :primary-contact :primary-email :primary-phone :default-expense-account]] + [:company [:id :name]] + ]]] + checks (graphql/query identity (venia/graphql-query {:venia/queries (->graphql query)}))] + (list (:all-checks (:data checks))))) (GET "/companies/export" {:keys [query-params]} (companies/get-all))