specing.
This commit is contained in:
@@ -1,29 +1,47 @@
|
||||
(ns auto-ap.db.vendors
|
||||
(:require [clojure.java.jdbc :as j]
|
||||
[auto-ap.db.utils :refer [clj->db db->clj get-conn]]
|
||||
[auto-ap.entities.vendors :as entities]
|
||||
[clojure.edn :as edn]))
|
||||
|
||||
(defn merge-data [{:keys [data] :as x}]
|
||||
(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]
|
||||
(-> x
|
||||
(db->clj)
|
||||
merge-data
|
||||
(assign-namespace "auto-ap.entities.vendors")
|
||||
))
|
||||
|
||||
(defn unparse [x]
|
||||
(-> x
|
||||
(select-keys entities/all-keys)
|
||||
clj->db))
|
||||
|
||||
(defn get-all []
|
||||
(->> (j/query (get-conn) "SELECT * FROM vendors")
|
||||
(map parse)))
|
||||
|
||||
(defn upsert [id data]
|
||||
(j/update! (get-conn) :vendors (clj->db data) ["id = ?" (Integer/parseInt id)] )
|
||||
(merge-data (db->clj (first (j/query (get-conn) ["SELECT * FROM vendors WHERE id = ?" (Integer/parseInt id)])))))
|
||||
(j/update! (get-conn) :vendors (unparse data) ["id = ?" (Integer/parseInt id)] )
|
||||
(parse (first (j/query (get-conn) ["SELECT * FROM vendors WHERE id = ?" (Integer/parseInt id)]))))
|
||||
|
||||
(defn insert [data]
|
||||
(parse (first (j/insert! (get-conn)
|
||||
:vendors
|
||||
(clj->db data)))))
|
||||
(unparse data)))))
|
||||
|
||||
(defn find-with-reminders []
|
||||
(map parse (j/query (get-conn) ["SELECT * FROM vendors WHERE invoice_reminder_schedule = ?" "Weekly"])))
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
(ns auto-ap.routes.vendors
|
||||
(:require [compojure.core :refer [context GET PUT POST defroutes wrap-routes]]
|
||||
[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
|
||||
(wrap-routes
|
||||
@@ -11,10 +13,14 @@
|
||||
:body (pr-str (vendors/get-all))
|
||||
:headers {"Content-Type" "application/edn"}})
|
||||
(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
|
||||
:body (pr-str (vendors/upsert id edn-params))
|
||||
:headers {"Content-Type" "application/edn"}})
|
||||
(POST "/" {:keys [edn-params] :as r}
|
||||
(println (s/valid? ::entity/vendor edn-params))
|
||||
{:status 200
|
||||
:body (pr-str (vendors/insert edn-params))
|
||||
:headers {"Content-Type" "application/edn"}}))
|
||||
|
||||
46
src/cljc/auto_ap/entities/vendors.cljc
Normal file
46
src/cljc/auto_ap/entities/vendors.cljc
Normal 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)))
|
||||
@@ -1,8 +1,11 @@
|
||||
(ns auto-ap.events.admin.vendors
|
||||
(:require [re-frame.core :as re-frame]
|
||||
[clojure.spec.alpha :as s]
|
||||
[auto-ap.db :as db]
|
||||
[auto-ap.routes :as routes]
|
||||
[auto-ap.effects :as effects]
|
||||
[auto-ap.entities.vendors :as entity]
|
||||
|
||||
[bidi.bidi :as bidi]))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
@@ -21,21 +24,21 @@
|
||||
::save
|
||||
(fn [{:keys [db]} _]
|
||||
(let [edited-vendor (get-in db [:admin :vendor])
|
||||
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]]
|
||||
(if (:id edited-vendor)
|
||||
(assoc fx :http {:method :put
|
||||
:token (:user db)
|
||||
:body (pr-str (select-keys edited-vendor all-fields))
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:uri (str "/api/vendors/" (:id edited-vendor))
|
||||
:on-success [::save-complete]})
|
||||
(assoc fx :http {:method :post
|
||||
:token (:user db)
|
||||
:body (pr-str (select-keys edited-vendor all-fields))
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:uri (str "/api/vendors")
|
||||
:on-success [::save-complete]})))))
|
||||
fx {:db (assoc-in db [:admin :vendor :saving?] true)}]
|
||||
(when (s/valid? ::entity/vendor edited-vendor)
|
||||
(if (::entity/id edited-vendor)
|
||||
(assoc fx :http {:method :put
|
||||
:token (:user db)
|
||||
:body (pr-str edited-vendor)
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:uri (str "/api/vendors/" (::entity/id edited-vendor))
|
||||
:on-success [::save-complete]})
|
||||
(assoc fx :http {:method :post
|
||||
:token (:user db)
|
||||
:body (pr-str edited-vendor)
|
||||
:headers {"Content-Type" "application/edn"}
|
||||
:uri (str "/api/vendors")
|
||||
:on-success [::save-complete]}))))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::save-complete
|
||||
@@ -43,7 +46,7 @@
|
||||
(-> db
|
||||
|
||||
(assoc-in [:admin :vendor] nil)
|
||||
(assoc-in [:vendors (:id vendor)] vendor))))
|
||||
(assoc-in [:vendors (::entity/id vendor)] vendor))))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::change
|
||||
@@ -64,6 +67,6 @@
|
||||
(fn [db [_ vendors]]
|
||||
|
||||
(assoc db :vendors (reduce (fn [vendors vendor]
|
||||
(assoc vendors (:id vendor) vendor))
|
||||
(assoc vendors (::entity/id vendor) vendor))
|
||||
{}
|
||||
vendors))))
|
||||
|
||||
@@ -2,40 +2,49 @@
|
||||
(:require-macros [cljs.core.async.macros :refer [go]])
|
||||
(:require [re-frame.core :as re-frame]
|
||||
[reagent.core :as reagent]
|
||||
[clojure.string :as str]
|
||||
[auto-ap.subs :as subs]
|
||||
[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]]
|
||||
[cljs.reader :as edn]
|
||||
[auto-ap.routes :as routes]
|
||||
[bidi.bidi :as bidi]))
|
||||
|
||||
(defn invalid-fields [spec v]
|
||||
(set (mapcat :in (::s/problems (s/explain-data spec v)))))
|
||||
|
||||
(defn vendors-table []
|
||||
(let [vendors (re-frame/subscribe [::subs/vendors])
|
||||
editing-vendor (:editing @(re-frame/subscribe [::subs/admin]))]
|
||||
|
||||
[:table {:class "table", :style {:width "100%"}}
|
||||
[:thead
|
||||
[:tr
|
||||
[:th "Name"]
|
||||
[:th "Email"]
|
||||
[:th "Invoice Reminders"]]]
|
||||
[:tbody (for [{:keys [id name primary-email data invoice-reminder-schedule]} @vendors]
|
||||
^{:key (str name "-" id )}
|
||||
[:tr {:on-click (fn [] (re-frame/dispatch [::events/edit id]))
|
||||
[:tbody (for [v @vendors]
|
||||
^{:key (str (::entity/id v))}
|
||||
[:tr {:on-click (fn [] (re-frame/dispatch [::events/edit (::entity/id v)]))
|
||||
:style {"cursor" "pointer"}}
|
||||
[:td name]
|
||||
[:td primary-email]
|
||||
[:td invoice-reminder-schedule]])]]))
|
||||
[:td (::entity/name v)]
|
||||
[:td (::entity/primary-email v)]
|
||||
[:td (::entity/invoice-reminder-schedule v)]])]]))
|
||||
|
||||
(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-background {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]
|
||||
|
||||
[:div.modal-card
|
||||
[:header.modal-card-head
|
||||
[:p.modal-card-title
|
||||
(if id
|
||||
(str "Edit " (or name "<vendor>"))
|
||||
(str "Add " (or name "<new vendor>")))]
|
||||
(if (::entity/id editing-vendor)
|
||||
(str "Edit " (or (::entity/name editing-vendor) "<vendor>"))
|
||||
(str "Add " (or (::entity/name editing-vendor) "<new vendor>")))]
|
||||
[:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]]
|
||||
[:section.modal-card-body
|
||||
[:div.field.is-horizontal
|
||||
@@ -44,16 +53,18 @@
|
||||
[:div.field-body
|
||||
[:div.field
|
||||
[:div.control
|
||||
[:input.input {:type "text" :value name
|
||||
:on-change (dispatch-value-change [::events/change [:name]])}]]]]]
|
||||
[:input.input {:type "text" :value (::entity/name editing-vendor)
|
||||
: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-label
|
||||
[:label.label "Code"]]
|
||||
[:div.field-body
|
||||
[:div.field
|
||||
[:div.control
|
||||
[:input.input.is-expanded {:type "text" :value code
|
||||
:on-change (dispatch-value-change [::events/change [:code]])}]
|
||||
[:input.input.is-expanded {:type "text" :value (::entity/code editing-vendor)
|
||||
: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"]]]]
|
||||
]
|
||||
|
||||
@@ -66,8 +77,8 @@
|
||||
[:p.help "Address"]
|
||||
[:input.input.is-expanded {:type "text"
|
||||
:placeholder "1700 Pennsylvania Ave"
|
||||
:value address1
|
||||
:on-change (dispatch-value-change [::events/change [:address1]])}]]]]]
|
||||
:value (::entity/address1 editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/address1]])}]]]]]
|
||||
[:div.field.is-horizontal
|
||||
[:div.field-label]
|
||||
[:div.field-body
|
||||
@@ -76,8 +87,8 @@
|
||||
|
||||
[:input.input.is-expanded {:type "text"
|
||||
:placeholder "Suite 400"
|
||||
:value address2
|
||||
:on-change (dispatch-value-change [::events/change [:address2]])}]]]]]
|
||||
:value (::entity/address2 editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/address2]])}]]]]]
|
||||
[:div.field.is-horizontal
|
||||
[:div.field-label]
|
||||
[:div.field-body
|
||||
@@ -85,26 +96,26 @@
|
||||
[:p.help "City"]
|
||||
[:div.control
|
||||
[:input.input.is-expanded {:type "text"
|
||||
:value city
|
||||
:value (::entity/city editing-vendor)
|
||||
:placeholder "Cupertino"
|
||||
:on-change (dispatch-value-change [::events/change [:city]])}]]]
|
||||
:on-change (dispatch-value-change [::events/change [::entity/city]])}]]]
|
||||
[:div.field
|
||||
[:div.control
|
||||
|
||||
[:p.help "State"]
|
||||
[:input.input {:type "email"
|
||||
:value state
|
||||
:value (::entity/state editing-vendor)
|
||||
:placeholder "CA"
|
||||
:on-change (dispatch-value-change [::events/change [:state]])}]
|
||||
:on-change (dispatch-value-change [::events/change [::entity/state]])}]
|
||||
]]
|
||||
|
||||
[:div.field
|
||||
[:div.control
|
||||
[:p.help "Zip"]
|
||||
[:input.input {:type "phone"
|
||||
:value zip
|
||||
:value (::entity/zip editing-vendor)
|
||||
:placeholder "95014"
|
||||
:on-change (dispatch-value-change [::events/change [:zip]])}]
|
||||
:on-change (dispatch-value-change [::events/change [::entity/zip]])}]
|
||||
]]]]
|
||||
[:h2.subtitle "Contact"]
|
||||
|
||||
@@ -116,8 +127,8 @@
|
||||
[:div.field
|
||||
[:div.control.has-icons-left
|
||||
[:input.input.is-expanded {:type "text"
|
||||
:value primary-contact
|
||||
:on-change (dispatch-value-change [::events/change [:primary-contact]])}]
|
||||
:value (::entity/primary-contact editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/primary-contact]])}]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-user]]]]
|
||||
[:div.field
|
||||
@@ -125,14 +136,16 @@
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-envelope]]
|
||||
[:input.input {:type "email"
|
||||
:value primary-email
|
||||
:on-change (dispatch-value-change [::events/change [:primary-email]])}]]]
|
||||
:value (::entity/primary-email editing-vendor)
|
||||
: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.control.has-icons-left
|
||||
[:input.input {:type "phone"
|
||||
:value primary-phone
|
||||
:on-change (dispatch-value-change [::events/change [:primary-phone]])}]
|
||||
:value (::entity/primary-phone editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/primary-phone]])}]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-phone]]]]]]
|
||||
[:div.field.is-horizontal
|
||||
@@ -142,8 +155,8 @@
|
||||
[:div.field
|
||||
[:div.control.has-icons-left
|
||||
[:input.input.is-expanded {:type "text"
|
||||
:value secondary-contact
|
||||
:on-change (dispatch-value-change [::events/change [:secondary-contact]])}]
|
||||
:value (::entity/secondary-contact editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/secondary-contact]])}]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-user]]]]
|
||||
[:div.field
|
||||
@@ -151,14 +164,14 @@
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-envelope]]
|
||||
[:input.input {:type "email"
|
||||
:value secondary-email
|
||||
:on-change (dispatch-value-change [::events/change [:secondary-email]])}]]]
|
||||
:value (::entity/secondary-email editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/secondary-email]])}]]]
|
||||
|
||||
[:div.field
|
||||
[:div.control.has-icons-left
|
||||
[:input.input {:type "phone"
|
||||
:value secondary-phone
|
||||
:on-change (dispatch-value-change [::events/change [:secondary-phone]])}]
|
||||
:value (::entity/secondary-phone editing-vendor)
|
||||
:on-change (dispatch-value-change [::events/change [::entity/secondary-phone]])}]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-phone]]]]]]
|
||||
|
||||
@@ -172,27 +185,29 @@
|
||||
[:input {:type "radio"
|
||||
:name "schedule"
|
||||
:value "Weekly"
|
||||
:checked (if (= "Weekly" invoice-reminder-schedule)
|
||||
:checked (if (= "Weekly" (::entity/invoice-reminder-schedule editing-vendor))
|
||||
"checked"
|
||||
"")
|
||||
:on-change (dispatch-value-change [::events/change [:invoice-reminder-schedule]])}]
|
||||
:on-change (dispatch-value-change [::events/change [::entity/invoice-reminder-schedule]])}]
|
||||
|
||||
" Send weekly"]
|
||||
[:label.radio
|
||||
[:input {:type "radio"
|
||||
:name "schedule"
|
||||
:value "Never"
|
||||
:checked (if (= "Never" invoice-reminder-schedule)
|
||||
:checked (if (= "Never" (::entity/invoice-reminder-schedule editing-vendor))
|
||||
"checked"
|
||||
"")
|
||||
:on-change (dispatch-value-change [::events/change [:invoice-reminder-schedule]])}]
|
||||
:on-change (dispatch-value-change [::events/change [::entity/invoice-reminder-schedule]])}]
|
||||
" Never"]]]]
|
||||
]
|
||||
|
||||
(when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]
|
||||
|
||||
[: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"]
|
||||
(when (:saving? editing-vendor)
|
||||
[:span.icon
|
||||
|
||||
Reference in New Issue
Block a user