diff --git a/src/clj/auto_ap/db/invoices.clj b/src/clj/auto_ap/db/invoices.clj index 39a2d3fe..7679dd2d 100644 --- a/src/clj/auto_ap/db/invoices.clj +++ b/src/clj/auto_ap/db/invoices.clj @@ -11,6 +11,8 @@ (map db->clj (j/query conn "SELECT * FROM invoices"))) +(defn get-unpaid [] + (map db->clj (j/query conn "SELECT * FROM invoices WHERE imported=true"))) + (defn get-pending [] - (println conn) - (map db->clj (j/query conn "SELECT * FROM invoices WHERE imported=0"))) + (map db->clj (j/query conn "SELECT * FROM invoices WHERE imported=false or imported is null"))) diff --git a/src/clj/auto_ap/db/utils.clj b/src/clj/auto_ap/db/utils.clj index 95c1398d..fa899c3e 100644 --- a/src/clj/auto_ap/db/utils.clj +++ b/src/clj/auto_ap/db/utils.clj @@ -21,14 +21,19 @@ [(keyword (kebab->snake (name k))) v]) x))) -(def conn {:classname "org.postgresql.Driver" ; must be in classpath - :dbtype "postgresql" - :ssl true - :sslfactory "org.postgresql.ssl.NonValidatingFactory" - :host "ec2-54-235-123-153.compute-1.amazonaws.com" - :port 5342 - :dbname "dbfemhppkdksfp" - ; Any additional keys are passed to the driver +(let [db-host "ec2-54-235-123-153.compute-1.amazonaws.com" + db-port 5432 + db-name "dbfemhppkdksfp"] + + (def conn {:classname "org.postgresql.Driver" ; must be in classpath + :ssl true + :sslfactory "org.postgresql.ssl.NonValidatingFactory" + :subprotocol "postgresql" + :subname (str "//" db-host ":" db-port "/" db-name) + ; Any additional + ; keys are passed + ; to the driver ; as driver-specific properties. - :user "tkilrhrhzlumol" - :password "de6117f8551364ac84ed31c1231941f53ab0b5470c9956f478b2025ab5a0fb8b"}) + :user "tkilrhrhzlumol" + :password "de6117f8551364ac84ed31c1231941f53ab0b5470c9956f478b2025ab5a0fb8b"})) + diff --git a/src/clj/auto_ap/handler.clj b/src/clj/auto_ap/handler.clj index d820826b..df56920d 100644 --- a/src/clj/auto_ap/handler.clj +++ b/src/clj/auto_ap/handler.clj @@ -20,6 +20,11 @@ :body (pr-str (invoices/get-all)) :headers {"Content-Type" "application/edn"}}) + (GET "/api/invoices/unpaid" [] + {:status 200 + :body (pr-str (invoices/get-unpaid)) + :headers {"Content-Type" "application/edn"}}) + (GET "/api/invoices/pending" [] {:status 200 :body (pr-str (invoices/get-pending)) @@ -33,12 +38,13 @@ (POST "/pdf-upload" {{ files "file"} :params :as params} (let [{:keys [filename tempfile]} files] - (for [{:keys [total date invoice-number customer-identifier]} - (parse/parse-file (.getPath tempfile))] - {"customer-identifier" customer-identifier - "invoice-number" invoice-number - "date" date - "total" total}))) + (invoices/insert-multi! + (for [{:keys [total date invoice-number customer-identifier] :as row} + (parse/parse-file (.getPath tempfile))] + (assoc row :imported false))) + {:status 200 + :body (pr-str (invoices/get-pending)) + :headers {"Content-Type" "application/edn"}})) (route/resources "/") (routes (ANY "*" [] (response/resource-response "index.html" {:root "public"}))) (route/not-found "Not Found")) diff --git a/src/cljs/auto_ap/db.cljs b/src/cljs/auto_ap/db.cljs index f52ae4d8..4cea68f8 100644 --- a/src/cljs/auto_ap/db.cljs +++ b/src/cljs/auto_ap/db.cljs @@ -2,6 +2,6 @@ (def default-db {:company {:name "Campbell brewery"} - :invoices #{} - :unpaid-invoices #{} + :invoices {:pending #{} + :unpaid #{}} }) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 1935927a..f77f6a08 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -18,9 +18,9 @@ (re-frame/reg-event-db ::imported-invoices (fn [db [_ new-invoices]] - (update-in db [:invoices] into new-invoices))) + (assoc-in db [:invoices] new-invoices))) (re-frame/reg-event-db ::received-invoices - (fn [db [_ new-invoices]] - (update-in db [:unpaid-invoices] into new-invoices))) + (fn [db [_ type new-invoices]] + (assoc-in db [:invoices type] new-invoices))) diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index c86916ab..4e384842 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -12,11 +12,11 @@ (:active-page db))) (re-frame/reg-sub - ::invoices + ::pending-invoices (fn [db] - (:invoices db))) + (:pending (:invoices db)))) (re-frame/reg-sub ::unpaid-invoices (fn [db] - (:unpaid-invoices db))) + (:unpaid (:invoices db)))) diff --git a/src/cljs/auto_ap/views.cljs b/src/cljs/auto_ap/views.cljs index be851089..8e793f32 100644 --- a/src/cljs/auto_ap/views.cljs +++ b/src/cljs/auto_ap/views.cljs @@ -6,6 +6,7 @@ [reagent.core :as reagent] [auto-ap.subs :as subs] [auto-ap.events :as events] + [cljs.reader :as edn] [auto-ap.routes :as routes] [bidi.bidi :as bidi] [cljs-http.client :as http] @@ -23,14 +24,12 @@ [:span [:span {:class "icon"} [:i {:class "fa fa-cloud-download"}]] - "Drop any invoices you want to process here"]]]]) {:component-did-mount (fn [this] (js/Dropzone. (reagent/dom-node this) (clj->js {:init (fn [] (.on (js-this) "success" (fn [_ files] - (re-frame/dispatch [::events/imported-invoices (js->clj (.parse js/JSON files))]) - ))) + (re-frame/dispatch [::events/received-invoices :pending (edn/read-string files)])))) :paramName "file" :url "/pdf-upload" :previewsContainer "#dz-hidden" @@ -71,9 +70,9 @@ [:td total]])]]])) {:component-did-mount (fn [] (go - (->> (> (> (