From ccf3214abaf76a3d7f3e689c906dc3debc35fca6 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Sat, 27 Mar 2021 18:10:05 +0100 Subject: [PATCH] empty price marker --- backend/src/chicken_master/products.clj | 2 +- backend/test/chicken_master/products_test.clj | 19 ++++++++++-- frontend/src/chicken_master/calendar.cljs | 7 ++++- frontend/src/chicken_master/config.cljs | 4 ++- frontend/src/chicken_master/products.cljs | 7 +++-- frontend/src/chicken_master/stock.cljs | 4 +-- .../test/chicken_master/calendar_test.cljs | 29 +++++++++++++++++++ 7 files changed, 62 insertions(+), 10 deletions(-) diff --git a/backend/src/chicken_master/products.clj b/backend/src/chicken_master/products.clj index c8ff8a5..71e36d0 100644 --- a/backend/src/chicken_master/products.clj +++ b/backend/src/chicken_master/products.clj @@ -18,7 +18,7 @@ (into {})))) (defn- update-product [tx user-id prod values] - (let [to-update (seq (filter values [:amount :price])) + (let [to-update (seq (filter (partial contains? values) [:amount :price])) cols (->> to-update (map name) (str/join ", ")) params (concat [(name prod) user-id] (map values to-update)) updates (->> to-update diff --git a/backend/test/chicken_master/products_test.clj b/backend/test/chicken_master/products_test.clj index bfca7cd..a835b7d 100644 --- a/backend/test/chicken_master/products_test.clj +++ b/backend/test/chicken_master/products_test.clj @@ -73,6 +73,21 @@ "ON CONFLICT (name, user_id) DO UPDATE SET " "deleted = NULL, amount = EXCLUDED.amount") "milk" :user-id 3]]))))) + (testing "empty fields aren't ignored" + (let [inserts (atom [])] + (with-redefs [jdbc/transact (fn [_ f & args] (apply f args)) + jdbc/execute! #(swap! inserts conj %2) + sql/update! (constantly nil) + sql/query (constantly [])] + (sut/update! :user-id {:eggs {:amount 2} :milk {:amount 3 :price nil} :cows {}}) + (is (= (sort-by second @inserts) + [[(str "INSERT INTO products (name, user_id, amount) VALUES(?, ?, ?) " + "ON CONFLICT (name, user_id) DO UPDATE " + "SET deleted = NULL, amount = EXCLUDED.amount") "eggs" :user-id 2] + [(str "INSERT INTO products (name, user_id, amount, price) VALUES(?, ?, ?, ?) " + "ON CONFLICT (name, user_id) DO UPDATE SET " + "deleted = NULL, amount = EXCLUDED.amount, price = EXCLUDED.price") "milk" :user-id 3 nil]]))))) + (testing "non selected items get removed" (let [updates (atom [])] (with-redefs [jdbc/transact (fn [_ f & args] (apply f args)) @@ -82,7 +97,7 @@ (sut/update! :user-id {:eggs {:amount 2} :milk {:amount 3} :cows {:amount 2}}) (is (= @updates [{} :products {:deleted true} ["name NOT IN (?, ?, ?)" "eggs" "milk" "cows"]]))))) - (testing "non selected items get removed" + (testing "products get returned" (with-redefs [jdbc/transact (fn [_ f & args] (apply f args)) jdbc/execute! (constantly nil) sql/update! (constantly nil) @@ -102,7 +117,7 @@ (is (= id {:bla_id item-id})))] (sut/update-products-mapping! :tx 123 :bla item-id {:eggs 34 :milk 25 :carrots 13})))) - (testing "items get removed" + (testing "items get added" (let [item-id 123] (with-redefs [sut/products-map (constantly {"eggs" 1 "milk" 2 "carrots" 3}) sql/delete! (constantly :ok) diff --git a/frontend/src/chicken_master/calendar.cljs b/frontend/src/chicken_master/calendar.cljs index 2349ad8..fb76810 100644 --- a/frontend/src/chicken_master/calendar.cljs +++ b/frontend/src/chicken_master/calendar.cljs @@ -34,6 +34,11 @@ (prod/calc-price (:id who) prod price amount))) products) (assoc order :products))) +(defn merge-product-values [& products] + (apply merge-with + (fn [& values] (some->> values (remove nil?) seq (reduce +))) + products)) + (defn order-form ([order] (order-form order #{:who :day :notes :products :group-products})) ([order fields] @@ -126,7 +131,7 @@ [:div {:class :header} "w sumie:"] (->> orders (map :products) - (apply merge-with (partial merge-with +)) + (apply merge-with merge-product-values) (sort-by first) (map (partial prod/format-product settings)) (into [:div {:class :products-sum}]))])]]])) diff --git a/frontend/src/chicken_master/config.cljs b/frontend/src/chicken_master/config.cljs index 6086945..5724840 100644 --- a/frontend/src/chicken_master/config.cljs +++ b/frontend/src/chicken_master/config.cljs @@ -32,7 +32,8 @@ :editable-number-inputs (get-setting :editable-number-inputs false) ; only allow number modifications in the edit modal :hide-fulfilled-orders (get-setting :hide-fulfilled-orders false) - :prices (get-setting :prices true) + :prices (get-setting :prices false) + :empty-price-marker (get-setting :empty-price-marker "-") :backend-url (get-setting :backend-url (if (= (.. js/window -location -href) "http://localhost:8280/") @@ -97,6 +98,7 @@ [:h3 "Ustawienia magazynu"] (input :prices "pokaż ceny" {:type :checkbox}) + (input :empty-price-marker "co wyświetlić jezeli nie ma ceny" {}) [:h3 "Ustawienia tyłu"] (input :backend-url "backend URL" {}) diff --git a/frontend/src/chicken_master/products.cljs b/frontend/src/chicken_master/products.cljs index db06e87..1b5f9ee 100644 --- a/frontend/src/chicken_master/products.cljs +++ b/frontend/src/chicken_master/products.cljs @@ -11,8 +11,9 @@ (when-not (js/isNaN i) i))) (defn round [num digits] - (let [div (js/Math.pow 10 digits)] - (/ (js/Math.round (* num div)) div))) + (when num + (let [div (js/Math.pow 10 digits)] + (/ (js/Math.round (* num div)) div)))) (defn format-price [price] (when price (round (/ price 100) 2))) (defn normalise-price [price] (when price (round (* price 100) 0))) @@ -100,7 +101,7 @@ [:span {:class :product-amount} amount]) (when (settings :prices) [:span {:class :product-price} - (format-price final-price)])]) + (or (format-price final-price) (settings :empty-price-marker))])]) (defn item-adder [& {:keys [type value callback button class] :or {type :text value "" button nil}}] diff --git a/frontend/src/chicken_master/stock.cljs b/frontend/src/chicken_master/stock.cljs index 2435b8e..c2199ba 100644 --- a/frontend/src/chicken_master/stock.cljs +++ b/frontend/src/chicken_master/stock.cljs @@ -29,7 +29,7 @@ #(swap! state assoc-in [product :price] (some-> % .-target .-value prod/num-or-nil prod/normalise-price)))])])) - [prod/item-adder :callback #(swap! state assoc (keyword %) 0) :button "+"]]))) + [prod/item-adder :callback #(swap! state assoc (keyword %) {:amount 0}) :button "+"]]))) (defn process-form [form] (->> form @@ -47,4 +47,4 @@ [:h2 "Magazyn"] [stock-form @(re-frame/subscribe [::subs/available-products])]] ;; On success - :on-submit (fn [form] (re-frame/dispatch [::event/save-stock (process-form form)])))) + :on-submit (fn [form] (prn form) (prn (process-form form)) (re-frame/dispatch [::event/save-stock (process-form form)])))) diff --git a/frontend/test/chicken_master/calendar_test.cljs b/frontend/test/chicken_master/calendar_test.cljs index fd09cfd..9b515db 100644 --- a/frontend/test/chicken_master/calendar_test.cljs +++ b/frontend/test/chicken_master/calendar_test.cljs @@ -52,6 +52,35 @@ :milk {:amount 3 :price 5 :final-price 15} :carrots {:amount 6 :final-price nil}}))))))) +(deftest merge-product-values-test + (testing "single item" + (is (= (sut/merge-product-values {:amount 23, :price 32, :final-price 1}) + {:amount 23, :price 32, :final-price 1}))) + + (testing "items with price" + (is (= (sut/merge-product-values {:amount 23, :price 2, :final-price 3} + {:amount 45, :price 1, :final-price 4}) + {:amount 68, :price 3, :final-price 7}))) + + (testing "items without prices" + (is (= (sut/merge-product-values {:amount 23, :price nil, :final-price nil} + {:amount 45, :price nil, :final-price nil}) + {:amount 68, :price nil, :final-price nil}))) + + (testing "items with mixed prices" + (is (= (sut/merge-product-values {:amount 23, :price 2, :final-price 3} + {:amount 45, :price nil, :final-price nil}) + {:amount 68, :price 2, :final-price 3}))) + + (testing "multiple items" + (is (= (sut/merge-product-values {:amount 6, :price 7, :final-price 3} + {:amount 5, :price 2, :final-price 4} + {:amount 4, :price nil, :final-price 5} + {:amount 3, :price 4, :final-price nil} + {:amount 2, :price 5, :final-price 7} + {:amount 1}) + {:amount 21, :price 18, :final-price 19})))) + (deftest format-raw-order-test (testing "no products" (is (= (sut/format-raw-order {}) {:who {:name nil :id nil} :day nil :notes nil :products {}}))