This commit is contained in:
Daniel O'Connell 2021-03-13 16:49:20 +01:00
parent 10d9ebe046
commit a667957f1c
6 changed files with 36 additions and 21 deletions

View File

@ -17,22 +17,23 @@
(:orders/id (sql/insert! tx :orders (assoc order :user_id user-id))))))
(defn- structure-order [items]
(defn structure-order [items]
{:id (-> items first :orders/id)
:notes (-> items first :orders/notes)
:state (-> items first :orders/status keyword)
:day (-> items first :orders/order_date (.toInstant) str (subs 0 10))
:who {:id (-> items first :customers/id)
:name (-> items first :customers/name)}
:products (into {}
(for [{:keys [order_products/amount products/name]} items]
[(keyword name) amount]))})
:products (->> items
(filter :products/name)
(reduce (fn [coll {:keys [order_products/amount products/name]}]
(assoc coll (keyword name) amount)) {}))})
(def orders-query
"SELECT o.id, o.notes, o.status, o.order_date, c.id, c.name, p.name, op.amount
FROM orders o JOIN customers c ON o.customer_id = c.id
JOIN order_products op ON o.id = op.order_id
JOIN products p on p.id = op.product_id ")
LEFT OUTER JOIN order_products op ON o.id = op.order_id
LEFT OUTER JOIN products p on p.id = op.product_id ")
(defn- get-orders [tx where params]
(->> (into [(if where (str orders-query where) orders-query)] params)

View File

@ -15,9 +15,22 @@
(merge #:orders{:id id :notes notes :status status :order_date date}
#:customers{:id user_id :name user_name}
{:products/name (name product) :order_products/amount amount}))
(merge #:orders{:id id :notes notes :status status :order_date date}
#:customers{:id user_id :name user_name}
{:products/name nil :order_products/amount nil})))
[(merge #:orders{:id id :notes notes :status status :order_date date}
#:customers{:id user_id :name user_name}
{:products/name nil :order_products/amount nil})]))
(deftest structure-order-test
(testing "basic structure"
(is (= (sut/structure-order (raw-order-row))
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}})))
(testing "missing products"
(is (= (sut/structure-order (raw-order-row :products nil))
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {}}))))
(deftest test-get-order
(testing "correct values returned"

View File

@ -44,9 +44,9 @@
(html/input :who "kto" {:required true
:default (:name who)
:list :customers
:on-blur #(->> % .-target .-value
(get-group-products customers)
(swap! state assoc :group-products))})
:on-change (fn [e]
(if-let [products (->> e .-target .-value (get-group-products customers))]
(swap! state assoc :group-products products)))})
(into [:datalist {:id :customers}]
(for [cust customers] [:option {:value (:name cust) :id (:id cust)}]))
[:input {:id :who-id :name :who-id :type :hidden :value (or (:id who) "")}]]))

View File

@ -51,8 +51,7 @@
(re-frame/reg-event-db ::hide-modal (fn [db [_ modal]] (assoc-in db [modal :show] nil)))
(re-frame/reg-event-db ::start-loading (fn [db _] (update db :loading? inc)))
(re-frame/reg-event-db ::stop-loading (fn [db _] (update db :loading? dec)))
(re-frame/reg-event-db ::stop-loading (fn [db _] (update db :loading? #(-> % dec (max 0)))))
(re-frame/reg-event-fx
::confirm-action
(fn [_ [_ msg on-confirm-event & params]]
@ -190,7 +189,7 @@
(re-frame/reg-event-fx
::fetch-stock
(fn [_ _]
(fn [_ [_ ]]
{:dispatch [::start-loading]
:http-xhrio (http-get "stock" {} ::process-stock)}))

View File

@ -10,10 +10,11 @@
(re-frame/reg-sub ::available-customers (fn [db] (:customers db)))
(re-frame/reg-sub ::orders (fn [db] (:orders db)))
(re-frame/reg-sub ::show-edit-modal (fn [db] (-> db :order-edit :show)))
(re-frame/reg-sub ::show-stock-modal (fn [db] (-> db :stock :show)))
(defn- show-modal? [modal db] (and (-> modal db :show) (-> db :loading? zero?)))
(re-frame/reg-sub ::show-edit-modal (partial show-modal? :order-edit))
(re-frame/reg-sub ::show-stock-modal (partial show-modal? :stock))
(re-frame/reg-sub ::show-settings-modal (partial show-modal? :settings))
(re-frame/reg-sub ::show-customers-modal (fn [db] (-> db :clients :show)))
(re-frame/reg-sub ::show-settings-modal (fn [db] (-> db :settings :show)))
(re-frame/reg-sub ::editted-order (fn [db] (:order-edit db)))

View File

@ -33,7 +33,8 @@
(set-db {:order-edit {:show true}
:stock {:show true}
:clients {:show true}
:settings {:show true}})
:settings {:show true}
:loading? 0})
(is @(rf/subscribe [::subs/show-edit-modal]))
(is @(rf/subscribe [::subs/show-stock-modal]))
(is @(rf/subscribe [::subs/show-customers-modal]))
@ -382,7 +383,7 @@
(deftest stock-tests
(testing "stock fetched before showing"
(rf-test/run-test-sync
(set-db {:stock {}})
(set-db {:stock {} :loading? 0})
(let [called (atom false)]
(param-validator ::sut/fetch-stock #(reset! called true))
(rf/dispatch [::sut/show-stock])
@ -461,7 +462,7 @@
(deftest test-settings
(testing "settings get shown"
(rf-test/run-test-sync
(set-db {:settings {}})
(set-db {:settings {} :loading? 0})
(rf/dispatch [::sut/show-settings])
(is @(rf/subscribe [::subs/show-settings-modal]))))