This commit is contained in:
Bryce Covert
2018-04-09 12:35:13 -07:00
parent 87b94f0f3f
commit c82f61bdb4
6 changed files with 154 additions and 69 deletions

View File

@@ -2,10 +2,7 @@
:description "FIXME: write description" :description "FIXME: write description"
:url "http://example.com/FIXME" :url "http://example.com/FIXME"
:min-lein-version "2.0.0" :min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.8.0"] :dependencies [[org.clojure/clojure "1.9.0"]
[compojure "1.6.0"] [compojure "1.6.0"]
[kibu/pushy "0.3.8"] [kibu/pushy "0.3.8"]
[bidi "2.1.2"] [bidi "2.1.2"]
@@ -40,7 +37,7 @@
[lein-cljsbuild "1.1.5"]] [lein-cljsbuild "1.1.5"]]
:clean-targets ^{:protect false} ["resources/public/js/compiled" "target"] :clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]
:ring {:handler auto-ap.handler/app} :ring {:handler auto-ap.handler/app}
:source-paths ["src/clj"] :source-paths ["src/clj" "src/cljc"]
:resource-paths ["resources"] :resource-paths ["resources"]
:figwheel {:css-dirs ["resources/public/css"] :figwheel {:css-dirs ["resources/public/css"]
@@ -75,7 +72,7 @@
:cljsbuild :cljsbuild
{:builds {:builds
[{:id "dev" [{:id "dev"
:source-paths ["src/cljs"] :source-paths ["src/cljs" "src/cljc"]
:figwheel {:on-jsload "auto-ap.core/mount-root"} :figwheel {:on-jsload "auto-ap.core/mount-root"}
:compiler {:main auto-ap.core :compiler {:main auto-ap.core
:output-to "resources/public/js/compiled/app.js" :output-to "resources/public/js/compiled/app.js"
@@ -87,7 +84,7 @@
}} }}
{:id "min" {:id "min"
:source-paths ["src/cljs"] :source-paths ["src/cljs" "src/cljc"]
:jar true :jar true
:compiler {:main auto-ap.core :compiler {:main auto-ap.core
:output-to "resources/public/js/compiled/app.js" :output-to "resources/public/js/compiled/app.js"

View File

@@ -1,29 +1,47 @@
(ns auto-ap.db.vendors (ns auto-ap.db.vendors
(:require [clojure.java.jdbc :as j] (:require [clojure.java.jdbc :as j]
[auto-ap.db.utils :refer [clj->db db->clj get-conn]] [auto-ap.db.utils :refer [clj->db db->clj get-conn]]
[auto-ap.entities.vendors :as entities]
[clojure.edn :as edn])) [clojure.edn :as edn]))
(defn merge-data [{:keys [data] :as x}] (defn merge-data [{:keys [data] :as x}]
(merge x (edn/read-string data))) (merge x (edn/read-string data)))
(defn assign-namespace [x n]
(reduce-kv
(fn [x k v]
(assoc x (if (and (keyword? k)
(not (qualified-keyword? k)))
(keyword n (name k))
k)
v))
{}
x))
(defn parse [x] (defn parse [x]
(-> x (-> x
(db->clj) (db->clj)
merge-data merge-data
(assign-namespace "auto-ap.entities.vendors")
)) ))
(defn unparse [x]
(-> x
(select-keys entities/all-keys)
clj->db))
(defn get-all [] (defn get-all []
(->> (j/query (get-conn) "SELECT * FROM vendors") (->> (j/query (get-conn) "SELECT * FROM vendors")
(map parse))) (map parse)))
(defn upsert [id data] (defn upsert [id data]
(j/update! (get-conn) :vendors (clj->db data) ["id = ?" (Integer/parseInt id)] ) (j/update! (get-conn) :vendors (unparse data) ["id = ?" (Integer/parseInt id)] )
(merge-data (db->clj (first (j/query (get-conn) ["SELECT * FROM vendors WHERE id = ?" (Integer/parseInt id)]))))) (parse (first (j/query (get-conn) ["SELECT * FROM vendors WHERE id = ?" (Integer/parseInt id)]))))
(defn insert [data] (defn insert [data]
(parse (first (j/insert! (get-conn) (parse (first (j/insert! (get-conn)
:vendors :vendors
(clj->db data))))) (unparse data)))))
(defn find-with-reminders [] (defn find-with-reminders []
(map parse (j/query (get-conn) ["SELECT * FROM vendors WHERE invoice_reminder_schedule = ?" "Weekly"]))) (map parse (j/query (get-conn) ["SELECT * FROM vendors WHERE invoice_reminder_schedule = ?" "Weekly"])))

View File

@@ -1,7 +1,9 @@
(ns auto-ap.routes.vendors (ns auto-ap.routes.vendors
(:require [compojure.core :refer [context GET PUT POST defroutes wrap-routes]] (:require [compojure.core :refer [context GET PUT POST defroutes wrap-routes]]
[auto-ap.db.vendors :as vendors] [auto-ap.db.vendors :as vendors]
[auto-ap.routes.utils :refer [wrap-secure]])) [auto-ap.routes.utils :refer [wrap-secure]]
[auto-ap.entities.vendors :as entity]
[clojure.spec.alpha :as s]))
(defroutes routes (defroutes routes
(wrap-routes (wrap-routes
@@ -11,10 +13,14 @@
:body (pr-str (vendors/get-all)) :body (pr-str (vendors/get-all))
:headers {"Content-Type" "application/edn"}}) :headers {"Content-Type" "application/edn"}})
(PUT "/:id" {:keys [edn-params] {:keys [id]} :route-params :as r} (PUT "/:id" {:keys [edn-params] {:keys [id]} :route-params :as r}
(println edn-params)
(println (s/valid? ::entity/vendor edn-params))
(println (s/explain ::entity/vendor edn-params))
{:status 200 {:status 200
:body (pr-str (vendors/upsert id edn-params)) :body (pr-str (vendors/upsert id edn-params))
:headers {"Content-Type" "application/edn"}}) :headers {"Content-Type" "application/edn"}})
(POST "/" {:keys [edn-params] :as r} (POST "/" {:keys [edn-params] :as r}
(println (s/valid? ::entity/vendor edn-params))
{:status 200 {:status 200
:body (pr-str (vendors/insert edn-params)) :body (pr-str (vendors/insert edn-params))
:headers {"Content-Type" "application/edn"}})) :headers {"Content-Type" "application/edn"}}))

View File

@@ -0,0 +1,46 @@
(ns auto-ap.entities.vendors
(:require [clojure.spec.alpha :as s]))
(def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$")
(s/def ::id int)
(s/def ::name (s/nilable string?))
(s/def ::email (s/nilable (s/and string? (s/or :is-email #(re-matches email-regex %)
:is-empty #(= % "")))))
(s/def ::phone (s/nilable string?))
(s/def ::data (s/nilable string?))
(s/def ::invoice-reminder-schedule (s/nilable string?))
(s/def ::primary-contact ::name)
(s/def ::primary-email ::email)
(s/def ::primary-phone ::phone)
(s/def ::secondary-contact ::name)
(s/def ::secondary-email ::email)
(s/def ::secondary-phone ::phone)
(s/def ::code (s/nilable string?))
(s/def ::address1 (s/nilable string?))
(s/def ::address2 (s/nilable string?))
(s/def ::city (s/nilable string?))
(s/def ::state (s/nilable string?))
(s/def ::zip (s/nilable string?))
(s/def ::vendor (s/keys :req [::name]
:opt [::code
::id
::invoice-reminder-schedule
::primary-contact
::primary-email
::primary-phone
::secondary-contact
::secondary-email
::secondary-phone
::address1
::address2
::city
::state
::zip]))
(def vendor-spec (apply hash-map (drop 1 (s/form ::vendor))))
(def all-keys (concat (:req vendor-spec) (:opt vendor-spec)))

View File

@@ -1,8 +1,11 @@
(ns auto-ap.events.admin.vendors (ns auto-ap.events.admin.vendors
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[clojure.spec.alpha :as s]
[auto-ap.db :as db] [auto-ap.db :as db]
[auto-ap.routes :as routes] [auto-ap.routes :as routes]
[auto-ap.effects :as effects] [auto-ap.effects :as effects]
[auto-ap.entities.vendors :as entity]
[bidi.bidi :as bidi])) [bidi.bidi :as bidi]))
(re-frame/reg-event-db (re-frame/reg-event-db
@@ -21,21 +24,21 @@
::save ::save
(fn [{:keys [db]} _] (fn [{:keys [db]} _]
(let [edited-vendor (get-in db [:admin :vendor]) (let [edited-vendor (get-in db [:admin :vendor])
fx {:db (assoc-in db [:admin :vendor :saving?] true)} fx {:db (assoc-in db [:admin :vendor :saving?] true)}]
all-fields [:name :email :data :invoice-reminder-schedule :primary-contact :primary-email :primary-phone :secondary-contact :secondary-email :secondary-phone :code :address1 :address2 :city :state :zip]] (when (s/valid? ::entity/vendor edited-vendor)
(if (:id edited-vendor) (if (::entity/id edited-vendor)
(assoc fx :http {:method :put (assoc fx :http {:method :put
:token (:user db) :token (:user db)
:body (pr-str (select-keys edited-vendor all-fields)) :body (pr-str edited-vendor)
:headers {"Content-Type" "application/edn"} :headers {"Content-Type" "application/edn"}
:uri (str "/api/vendors/" (:id edited-vendor)) :uri (str "/api/vendors/" (::entity/id edited-vendor))
:on-success [::save-complete]}) :on-success [::save-complete]})
(assoc fx :http {:method :post (assoc fx :http {:method :post
:token (:user db) :token (:user db)
:body (pr-str (select-keys edited-vendor all-fields)) :body (pr-str edited-vendor)
:headers {"Content-Type" "application/edn"} :headers {"Content-Type" "application/edn"}
:uri (str "/api/vendors") :uri (str "/api/vendors")
:on-success [::save-complete]}))))) :on-success [::save-complete]}))))))
(re-frame/reg-event-db (re-frame/reg-event-db
::save-complete ::save-complete
@@ -43,7 +46,7 @@
(-> db (-> db
(assoc-in [:admin :vendor] nil) (assoc-in [:admin :vendor] nil)
(assoc-in [:vendors (:id vendor)] vendor)))) (assoc-in [:vendors (::entity/id vendor)] vendor))))
(re-frame/reg-event-db (re-frame/reg-event-db
::change ::change
@@ -64,6 +67,6 @@
(fn [db [_ vendors]] (fn [db [_ vendors]]
(assoc db :vendors (reduce (fn [vendors vendor] (assoc db :vendors (reduce (fn [vendors vendor]
(assoc vendors (:id vendor) vendor)) (assoc vendors (::entity/id vendor) vendor))
{} {}
vendors)))) vendors))))

View File

@@ -2,40 +2,49 @@
(:require-macros [cljs.core.async.macros :refer [go]]) (:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[reagent.core :as reagent] [reagent.core :as reagent]
[clojure.string :as str]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.events.admin.vendors :as events] [auto-ap.events.admin.vendors :as events]
[auto-ap.entities.vendors :as entity]
[clojure.spec.alpha :as s]
[auto-ap.views.utils :refer [login-url dispatch-value-change dispatch-event]] [auto-ap.views.utils :refer [login-url dispatch-value-change dispatch-event]]
[cljs.reader :as edn] [cljs.reader :as edn]
[auto-ap.routes :as routes] [auto-ap.routes :as routes]
[bidi.bidi :as bidi])) [bidi.bidi :as bidi]))
(defn invalid-fields [spec v]
(set (mapcat :in (::s/problems (s/explain-data spec v)))))
(defn vendors-table [] (defn vendors-table []
(let [vendors (re-frame/subscribe [::subs/vendors]) (let [vendors (re-frame/subscribe [::subs/vendors])
editing-vendor (:editing @(re-frame/subscribe [::subs/admin]))] editing-vendor (:editing @(re-frame/subscribe [::subs/admin]))]
[:table {:class "table", :style {:width "100%"}} [:table {:class "table", :style {:width "100%"}}
[:thead [:thead
[:tr [:tr
[:th "Name"] [:th "Name"]
[:th "Email"] [:th "Email"]
[:th "Invoice Reminders"]]] [:th "Invoice Reminders"]]]
[:tbody (for [{:keys [id name primary-email data invoice-reminder-schedule]} @vendors] [:tbody (for [v @vendors]
^{:key (str name "-" id )} ^{:key (str (::entity/id v))}
[:tr {:on-click (fn [] (re-frame/dispatch [::events/edit id])) [:tr {:on-click (fn [] (re-frame/dispatch [::events/edit (::entity/id v)]))
:style {"cursor" "pointer"}} :style {"cursor" "pointer"}}
[:td name] [:td (::entity/name v)]
[:td primary-email] [:td (::entity/primary-email v)]
[:td invoice-reminder-schedule]])]])) [:td (::entity/invoice-reminder-schedule v)]])]]))
(defn edit-dialog [] (defn edit-dialog []
(let [{:keys [name city state zip address1 address2 primary-contact primary-email primary-phone secondary-contact secondary-email secondary-phone invoice-reminder-schedule id code] :as editing-vendor} (:vendor @(re-frame/subscribe [::subs/admin]))] (let [editing-vendor (:vendor @(re-frame/subscribe [::subs/admin]))]
(println editing-vendor)
[:div.modal.is-active [:div.modal.is-active
[:div.modal-background {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}] [:div.modal-background {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]
[:div.modal-card [:div.modal-card
[:header.modal-card-head [:header.modal-card-head
[:p.modal-card-title [:p.modal-card-title
(if id (if (::entity/id editing-vendor)
(str "Edit " (or name "<vendor>")) (str "Edit " (or (::entity/name editing-vendor) "<vendor>"))
(str "Add " (or name "<new vendor>")))] (str "Add " (or (::entity/name editing-vendor) "<new vendor>")))]
[:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]] [:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]]
[:section.modal-card-body [:section.modal-card-body
[:div.field.is-horizontal [:div.field.is-horizontal
@@ -44,16 +53,18 @@
[:div.field-body [:div.field-body
[:div.field [:div.field
[:div.control [:div.control
[:input.input {:type "text" :value name [:input.input {:type "text" :value (::entity/name editing-vendor)
:on-change (dispatch-value-change [::events/change [:name]])}]]]]] :class (str/join " " (when (not (s/valid? ::entity/name (::entity/name editing-vendor)))
"is-danger"))
:on-change (dispatch-value-change [::events/change [::entity/name]])}]]]]]
[:div.field.is-horizontal [:div.field.is-horizontal
[:div.field-label [:div.field-label
[:label.label "Code"]] [:label.label "Code"]]
[:div.field-body [:div.field-body
[:div.field [:div.field
[:div.control [:div.control
[:input.input.is-expanded {:type "text" :value code [:input.input.is-expanded {:type "text" :value (::entity/code editing-vendor)
:on-change (dispatch-value-change [::events/change [:code]])}] :on-change (dispatch-value-change [::events/change [::entity/code]])}]
[:p.help "The vendor code is used for invoice parsing. Only one vendor at a time can use a code"]]]] [:p.help "The vendor code is used for invoice parsing. Only one vendor at a time can use a code"]]]]
] ]
@@ -66,8 +77,8 @@
[:p.help "Address"] [:p.help "Address"]
[:input.input.is-expanded {:type "text" [:input.input.is-expanded {:type "text"
:placeholder "1700 Pennsylvania Ave" :placeholder "1700 Pennsylvania Ave"
:value address1 :value (::entity/address1 editing-vendor)
:on-change (dispatch-value-change [::events/change [:address1]])}]]]]] :on-change (dispatch-value-change [::events/change [::entity/address1]])}]]]]]
[:div.field.is-horizontal [:div.field.is-horizontal
[:div.field-label] [:div.field-label]
[:div.field-body [:div.field-body
@@ -76,8 +87,8 @@
[:input.input.is-expanded {:type "text" [:input.input.is-expanded {:type "text"
:placeholder "Suite 400" :placeholder "Suite 400"
:value address2 :value (::entity/address2 editing-vendor)
:on-change (dispatch-value-change [::events/change [:address2]])}]]]]] :on-change (dispatch-value-change [::events/change [::entity/address2]])}]]]]]
[:div.field.is-horizontal [:div.field.is-horizontal
[:div.field-label] [:div.field-label]
[:div.field-body [:div.field-body
@@ -85,26 +96,26 @@
[:p.help "City"] [:p.help "City"]
[:div.control [:div.control
[:input.input.is-expanded {:type "text" [:input.input.is-expanded {:type "text"
:value city :value (::entity/city editing-vendor)
:placeholder "Cupertino" :placeholder "Cupertino"
:on-change (dispatch-value-change [::events/change [:city]])}]]] :on-change (dispatch-value-change [::events/change [::entity/city]])}]]]
[:div.field [:div.field
[:div.control [:div.control
[:p.help "State"] [:p.help "State"]
[:input.input {:type "email" [:input.input {:type "email"
:value state :value (::entity/state editing-vendor)
:placeholder "CA" :placeholder "CA"
:on-change (dispatch-value-change [::events/change [:state]])}] :on-change (dispatch-value-change [::events/change [::entity/state]])}]
]] ]]
[:div.field [:div.field
[:div.control [:div.control
[:p.help "Zip"] [:p.help "Zip"]
[:input.input {:type "phone" [:input.input {:type "phone"
:value zip :value (::entity/zip editing-vendor)
:placeholder "95014" :placeholder "95014"
:on-change (dispatch-value-change [::events/change [:zip]])}] :on-change (dispatch-value-change [::events/change [::entity/zip]])}]
]]]] ]]]]
[:h2.subtitle "Contact"] [:h2.subtitle "Contact"]
@@ -116,8 +127,8 @@
[:div.field [:div.field
[:div.control.has-icons-left [:div.control.has-icons-left
[:input.input.is-expanded {:type "text" [:input.input.is-expanded {:type "text"
:value primary-contact :value (::entity/primary-contact editing-vendor)
:on-change (dispatch-value-change [::events/change [:primary-contact]])}] :on-change (dispatch-value-change [::events/change [::entity/primary-contact]])}]
[:span.icon.is-small.is-left [:span.icon.is-small.is-left
[:i.fa.fa-user]]]] [:i.fa.fa-user]]]]
[:div.field [:div.field
@@ -125,14 +136,16 @@
[:span.icon.is-small.is-left [:span.icon.is-small.is-left
[:i.fa.fa-envelope]] [:i.fa.fa-envelope]]
[:input.input {:type "email" [:input.input {:type "email"
:value primary-email :value (::entity/primary-email editing-vendor)
:on-change (dispatch-value-change [::events/change [:primary-email]])}]]] :class (str/join " " [(when (::entity/primary-email (invalid-fields ::entity/vendor editing-vendor ))
"is-danger")])
:on-change (dispatch-value-change [::events/change [::entity/primary-email]])}]]]
[:div.field [:div.field
[:div.control.has-icons-left [:div.control.has-icons-left
[:input.input {:type "phone" [:input.input {:type "phone"
:value primary-phone :value (::entity/primary-phone editing-vendor)
:on-change (dispatch-value-change [::events/change [:primary-phone]])}] :on-change (dispatch-value-change [::events/change [::entity/primary-phone]])}]
[:span.icon.is-small.is-left [:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]]] [:i.fa.fa-phone]]]]]]
[:div.field.is-horizontal [:div.field.is-horizontal
@@ -142,8 +155,8 @@
[:div.field [:div.field
[:div.control.has-icons-left [:div.control.has-icons-left
[:input.input.is-expanded {:type "text" [:input.input.is-expanded {:type "text"
:value secondary-contact :value (::entity/secondary-contact editing-vendor)
:on-change (dispatch-value-change [::events/change [:secondary-contact]])}] :on-change (dispatch-value-change [::events/change [::entity/secondary-contact]])}]
[:span.icon.is-small.is-left [:span.icon.is-small.is-left
[:i.fa.fa-user]]]] [:i.fa.fa-user]]]]
[:div.field [:div.field
@@ -151,14 +164,14 @@
[:span.icon.is-small.is-left [:span.icon.is-small.is-left
[:i.fa.fa-envelope]] [:i.fa.fa-envelope]]
[:input.input {:type "email" [:input.input {:type "email"
:value secondary-email :value (::entity/secondary-email editing-vendor)
:on-change (dispatch-value-change [::events/change [:secondary-email]])}]]] :on-change (dispatch-value-change [::events/change [::entity/secondary-email]])}]]]
[:div.field [:div.field
[:div.control.has-icons-left [:div.control.has-icons-left
[:input.input {:type "phone" [:input.input {:type "phone"
:value secondary-phone :value (::entity/secondary-phone editing-vendor)
:on-change (dispatch-value-change [::events/change [:secondary-phone]])}] :on-change (dispatch-value-change [::events/change [::entity/secondary-phone]])}]
[:span.icon.is-small.is-left [:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]]] [:i.fa.fa-phone]]]]]]
@@ -172,27 +185,29 @@
[:input {:type "radio" [:input {:type "radio"
:name "schedule" :name "schedule"
:value "Weekly" :value "Weekly"
:checked (if (= "Weekly" invoice-reminder-schedule) :checked (if (= "Weekly" (::entity/invoice-reminder-schedule editing-vendor))
"checked" "checked"
"") "")
:on-change (dispatch-value-change [::events/change [:invoice-reminder-schedule]])}] :on-change (dispatch-value-change [::events/change [::entity/invoice-reminder-schedule]])}]
" Send weekly"] " Send weekly"]
[:label.radio [:label.radio
[:input {:type "radio" [:input {:type "radio"
:name "schedule" :name "schedule"
:value "Never" :value "Never"
:checked (if (= "Never" invoice-reminder-schedule) :checked (if (= "Never" (::entity/invoice-reminder-schedule editing-vendor))
"checked" "checked"
"") "")
:on-change (dispatch-value-change [::events/change [:invoice-reminder-schedule]])}] :on-change (dispatch-value-change [::events/change [::entity/invoice-reminder-schedule]])}]
" Never"]]]] " Never"]]]]
] ]
(when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])] (when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]
[:footer.modal-card-foot [:footer.modal-card-foot
[:a.button.is-primary {:on-click (fn [] (re-frame/dispatch [::events/save]))} [:a.button.is-primary {:on-click (fn [] (re-frame/dispatch [::events/save]))
:disabled (when (not (s/valid? ::entity/vendor editing-vendor ))
"disabled")}
[:span "Save"] [:span "Save"]
(when (:saving? editing-vendor) (when (:saving? editing-vendor)
[:span.icon [:span.icon