graphql used for invoices
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
[cljs-time.coerce :as c]
|
||||
[cljs-time.core :as time]
|
||||
[cljs.core.async :refer [<!]]
|
||||
[clojure.string :as str]
|
||||
[clojure.walk :as walk]
|
||||
[venia.core :as v]
|
||||
[auto-ap.history :as p]
|
||||
[pushy.core :as pushy]))
|
||||
|
||||
@@ -54,3 +57,50 @@
|
||||
(dates->date-times)
|
||||
(conj on-success)
|
||||
(re-frame/dispatch)))))))
|
||||
|
||||
(defn kebab->snake [s]
|
||||
(str/replace s #"-" "_"))
|
||||
|
||||
(defn snake [x]
|
||||
(if (namespace x)
|
||||
(keyword (namespace x) (kebab->snake (name x)))
|
||||
(keyword (kebab->snake (name x)))))
|
||||
|
||||
(defn ->graphql [m]
|
||||
(walk/postwalk
|
||||
(fn [node]
|
||||
(cond
|
||||
|
||||
(keyword? node)
|
||||
(snake node)
|
||||
|
||||
:else
|
||||
node))
|
||||
m))
|
||||
|
||||
(re-frame/reg-fx
|
||||
:graphql
|
||||
(fn [{:keys [query on-success on-error token variables query-obj]}]
|
||||
(go
|
||||
(let [headers (if token
|
||||
{"Authorization" (str "Token " token)}
|
||||
{})
|
||||
query (or query (v/graphql-query (->graphql query-obj)))
|
||||
response (<! (http/request {:method :get
|
||||
:headers headers
|
||||
:url (str "/api/graphql?query=" (js/encodeURIComponent query)
|
||||
"&variables=" (pr-str (or variables {})))}))]
|
||||
(if (>= (:status response) 400)
|
||||
(when on-error
|
||||
(->> response
|
||||
:body
|
||||
:data
|
||||
(dates->date-times)
|
||||
(conj on-error)
|
||||
(re-frame/dispatch)))
|
||||
(->> response
|
||||
:body
|
||||
:data
|
||||
(dates->date-times)
|
||||
(conj on-success)
|
||||
(re-frame/dispatch)))))))
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
[auto-ap.subs :as subs]
|
||||
[auto-ap.routes :as routes]
|
||||
[auto-ap.effects :as effects]
|
||||
[venia.core :as v]
|
||||
[bidi.bidi :as bidi]))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
@@ -62,7 +63,7 @@
|
||||
(re-frame/reg-event-db
|
||||
::imported-invoices
|
||||
(fn [db [_ new-invoices]]
|
||||
(assoc-in db [:invoices] new-invoices)))
|
||||
(assoc-in db [:invoices :pending] new-invoices)))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::approve-invoices
|
||||
@@ -79,23 +80,22 @@
|
||||
::view-pending-invoices
|
||||
(fn [cofx []]
|
||||
{:db (assoc-in (:db cofx) [:status :loading] true)
|
||||
:http {:method :get
|
||||
:token (-> cofx :db :user)
|
||||
:uri (str "/api/invoices/pending"
|
||||
(when-let [company-id (:id @(re-frame/subscribe [::subs/company]))]
|
||||
(str "?company=" company-id)))
|
||||
:on-success [::received-invoices :pending]}}))
|
||||
:graphql {:token (-> cofx :db :user)
|
||||
:query-obj {:venia/queries [[:invoice
|
||||
{:imported false :company_id (:id @(re-frame/subscribe [::subs/company]))}
|
||||
[:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]]]}
|
||||
|
||||
:on-success [::received-invoices :pending]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::view-unpaid-invoices
|
||||
(fn [cofx []]
|
||||
{:db (assoc-in (:db cofx) [:status :loading] true)
|
||||
:http {:method :get
|
||||
:token (-> cofx :db :user)
|
||||
:uri (str "/api/invoices/unpaid"
|
||||
(when-let [company-id (:id @(re-frame/subscribe [::subs/company]))]
|
||||
(str "?company=" company-id)))
|
||||
:on-success [::received-invoices :unpaid]}}))
|
||||
:graphql {:token (-> cofx :db :user)
|
||||
:query-obj {:venia/queries [[:invoice
|
||||
{:imported true :company_id (:id @(re-frame/subscribe [::subs/company]))}
|
||||
[:id :total :invoice-number :date [:vendor [:name :id]] [:company [:name :id]]]]]}
|
||||
:on-success [::received-invoices :unpaid]}}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::reject-invoices
|
||||
@@ -133,11 +133,13 @@
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::received-invoices
|
||||
(fn [db [_ type new-invoices]]
|
||||
(-> db
|
||||
(assoc-in [:invoices type] new-invoices)
|
||||
(assoc-in [:status :loading] false)
|
||||
)))
|
||||
(fn [db [_ type result]]
|
||||
(let [new-invoices (if (:invoice result)
|
||||
(:invoice result)
|
||||
result)]
|
||||
(-> db
|
||||
(assoc-in [:invoices type] new-invoices)
|
||||
(assoc-in [:status :loading] false)))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::change-form-state
|
||||
|
||||
@@ -58,11 +58,11 @@
|
||||
[:th "Date"]
|
||||
[:th "Amount"]
|
||||
[:th]]]
|
||||
[:tbody (for [{:keys [vendor vendor-id potential-duplicate company-id customer-identifier invoice-number date total id] :as i} @invoices]
|
||||
^{:key (str company-id "-" invoice-number "-" date "-" total "-" id)}
|
||||
[:tbody (for [{:keys [vendor vendor-id potential-duplicate company customer-identifier invoice-number date total id] :as i} @invoices]
|
||||
^{:key id}
|
||||
[:tr
|
||||
[:td (:name (:vendor i))]
|
||||
(if company-id
|
||||
(if company
|
||||
[:td (:name (:company i))]
|
||||
[:td [:i.icon.fa.fa-warning {:title "potential duplicate"}]
|
||||
(str "'" customer-identifier "' doesn't match any known company")])
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
[:th "Invoice #"]
|
||||
[:th "Date"]
|
||||
[:th "Amount"]]]
|
||||
[:tbody (for [{:keys [company vendor invoice-number date total id] :as i} @invoices]
|
||||
^{:key (str (:id company) "-" invoice-number "-" date "-" total "-" id)}
|
||||
[:tbody (for [{:keys [company invoice-number date total id vendor] :as i} @invoices]
|
||||
^{:key (str company "-" invoice-number "-" date "-" total "-" id)}
|
||||
[:tr
|
||||
[:td (:name vendor)]
|
||||
[:td (:name company)]
|
||||
|
||||
Reference in New Issue
Block a user