From bbd9f00f3024b26f5d9954ce55a0550f15de97d6 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 14 May 2018 09:30:01 -0700 Subject: [PATCH] switched to graphql to create a payment. --- src/clj/auto_ap/db/invoices_checks.clj | 2 +- src/clj/auto_ap/graphql.clj | 23 ++++++++++-- src/clj/auto_ap/routes/checks.clj | 28 +++++++++++++++ .../views/components/invoice_table.cljs | 10 +++++- .../auto_ap/views/pages/unpaid_invoices.cljs | 36 +++++++++++++------ 5 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/clj/auto_ap/db/invoices_checks.clj b/src/clj/auto_ap/db/invoices_checks.clj index 18b36e26..143b78c7 100644 --- a/src/clj/auto_ap/db/invoices_checks.clj +++ b/src/clj/auto_ap/db/invoices_checks.clj @@ -10,7 +10,7 @@ :from :invoices-checks)) (defn query [q] - (map data->fields (u/query q))) + (u/query q)) (defn get-all [] (query base-query)) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index cdfc7ea7..0a812a53 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -9,6 +9,7 @@ [auto-ap.db.vendors :as vendors] [auto-ap.db.companies :as companies] [auto-ap.db.checks :as checks] + [auto-ap.routes.checks :as rchecks] [auto-ap.db.reminders :as reminders] [auto-ap.db.invoices-checks :as invoices-checks] [auto-ap.db.utils :as utils] @@ -17,6 +18,7 @@ (:import (clojure.lang IPersistentMap))) + (def integreat-schema { :objects @@ -77,7 +79,13 @@ :count {:type 'Int} :total {:type 'Int} :start {:type 'Int} - :end {:type 'Int}}}} + :end {:type 'Int}}} + :check_result {:fields {:invoices {:type '(list :invoice)} + :pdf_url {:type 'String}}} + } + + + :queries {:invoice_page {:type '(list :invoice_page) :args {:imported {:type 'Boolean} @@ -96,7 +104,12 @@ :company {:type '(list :company) :resolve :get-company} :vendor {:type '(list :vendor) - :resolve :get-vendor}}}) + :resolve :get-vendor}} + + :mutations + {:print_checks {:type :check_result + :args {:invoice_ids {:type '(list Int)}} + :resolve :mutation/print-checks}}}) (defn by [x kf] (reduce @@ -202,6 +215,11 @@ (->graphql (vendors/get-all))) +(defn print-checks [context args value] + (->graphql + + (rchecks/print-checks (:invoice_ids args)))) + (def schema (-> integreat-schema (attach-resolvers {:get-invoice-page get-invoice-page @@ -211,6 +229,7 @@ :get-invoices-checks get-invoices-checks :get-check-by-id get-check-by-id :get-company get-company + :mutation/print-checks print-checks :get-vendor get-vendor}) schema/compile)) diff --git a/src/clj/auto_ap/routes/checks.clj b/src/clj/auto_ap/routes/checks.clj index b59b19dc..27c3bade 100644 --- a/src/clj/auto_ap/routes/checks.clj +++ b/src/clj/auto_ap/routes/checks.clj @@ -185,6 +185,34 @@ :invoices (map :id invoices)})) +(defn print-checks [invoice-ids] + (let [invoices (invoices/get-multi invoice-ids) + companies (into {} + (map (fn [c] [(:id c) c]) + (companies/get-all))) + vendors (into {} + (map (fn [v] [(:id v) v]) + (vendors/get-all))) + invoices-grouped-by-vendor (group-by :vendor-id invoices) + checks (-> (for [[vendor-id invoices] invoices-grouped-by-vendor] + [invoices (checks/insert! (check-for-invoices invoices vendor-id vendors companies))]) + doall) + invoice-checks (invoices-checks/insert-multi! + (mapcat + (fn [[invoices check]] + (map + (fn [i] + {:invoice-id (:id i) + :check-id (:id check) + :amount (:total i)}) + invoices)) + checks)) ] + + (make-pdfs (map second checks)) + {:invoices (mapcat first checks) + :pdf-url (merge-pdfs (map (comp :s3-key second) checks))})) + + (defroutes routes (wrap-routes (context "/checks" [] diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index ac10b137..d5a3dada 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -9,6 +9,14 @@ [cljs-time.format :as format] [goog.string :as gstring])) +;; TODO show busy +;; TODO only when one company selected +;; TODO partial payments +;; TODO invoice status = paid when complete +;; TODO performance +;; TODO check numbers +;; TODO psql transactions +;; TODO refactor graphql @@ -97,4 +105,4 @@ [:td (gstring/format "$%.2f" total )] [:td (for [check checks] ^{:key (:id check)} - [:a.tag {:href (:s3-url (:check check)) :target "_new"} (str "check " (:check-number (:check check)))])]]))]]])))) + [:a.tag {:href (:s3-url (:check check)) :target "_new"} [:i.fa.fa-money-check] (str " " (:check-number (:check check)) " (" (gstring/format "$%.2f" (:amount check) ) ")")])]]))]]])))) diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 0fe33c4d..af7cb1b4 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -49,20 +49,36 @@ (re-frame/reg-event-fx ::print-checks (fn [{:keys [db]} [_ data]] - {:db (assoc-in db [::invoice-page :checked] nil) - :http {:method :post - :token (:user db) - :body (pr-str {:invoice-ids (get-in db [::invoice-page :checked])}) - :headers {"Content-Type" "application/edn"} - :uri (str "/api/checks/") - :on-success [::checks-created] - :on-error [::save-error]}})) + {:graphql + {:token (-> db :user) + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "PrintChecks"} + + :venia/queries [[:print-checks + {:invoice_ids (vec (get-in db [::invoice-page :checked]))} + [[:invoices [:id [:checks [:amount [:check [:amount :s3_url :check_number ]]]]] + ] + :pdf_url]]]} + + :on-success [::checks-created]}})) (re-frame/reg-event-fx ::checks-created (fn [{:keys [db]} [_ data]] - {:new-window (:url data) - })) + (let [{{:keys [pdf-url invoices]} :print-checks} data + invoices-by-id (reduce + (fn [x a] + (assoc x (:id a) a)) + {} + invoices)] + {:new-window pdf-url + :db (-> db + (update-in [::invoice-page :invoices] + (fn [invoices] + (map (fn [i] + (merge i (invoices-by-id (:id i)))) + invoices))) + (assoc-in [::invoice-page :checked] nil))}))) (re-frame/reg-event-fx ::invalidated