edit prices

This commit is contained in:
Daniel O'Connell 2021-03-26 22:58:46 +01:00
parent 62a260f3ee
commit 9bee3a66f9
11 changed files with 174 additions and 80 deletions

View File

@ -9,7 +9,9 @@
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES products(id)
)"
"ALTER TABLE products ADD price BIGINT"
"ALTER TABLE order_products ADD price BIGINT"]
"ALTER TABLE order_products ADD price BIGINT"
"ALTER TABLE customer_group_products ADD price BIGINT"]
:down ["ALTER TABLE products DROP COLUMN price"
"ALTER TABLE order_products DROP COLUMN price"
"ALTER TABLE customer_group_products DROP COLUMN price"
"DROP TABLE customer_products"]}

View File

@ -8,7 +8,9 @@
(defn insert-products [coll {:keys [id name]} products]
(->> products
(reduce #(assoc %1 (-> %2 :products/name keyword) (:customer_group_products/amount %2)) {})
(reduce #(assoc %1 (-> %2 :products/name keyword)
{:amount (:customer_group_products/amount %2)
:price (:customer_group_products/price %2)}) {})
(assoc {:id id} :products)
(assoc coll name)))

View File

@ -26,11 +26,11 @@
:name (-> items first :customers/name)}
:products (->> items
(filter :products/name)
(reduce (fn [coll {:keys [order_products/amount products/name]}]
(assoc coll (keyword name) amount)) {}))})
(reduce (fn [coll {:keys [order_products/amount order_products/price products/name]}]
(assoc coll (keyword name) {:amount amount :price price})) {}))})
(def orders-query
"SELECT o.id, o.notes, o.status, o.order_date, c.id, c.name, p.name, op.amount
"SELECT o.id, o.notes, o.status, o.order_date, c.id, c.name, p.name, op.amount, op.price
FROM orders o JOIN customers c ON o.customer_id = c.id
LEFT OUTER JOIN order_products op ON o.id = op.order_id
LEFT OUTER JOIN products p on p.id = op.product_id ")
@ -84,7 +84,7 @@
"fulfilled" "-"
"waiting" "+")]
(when (not= (:state order) (keyword state))
(doseq [[prod amount] (:products order)]
(doseq [[prod {:keys [amount]}] (:products order)]
(jdbc/execute-one! tx
[(str "UPDATE products SET amount = amount " operator " ? WHERE name = ?")
amount (name prod)]))

View File

@ -48,8 +48,8 @@
products-map (products-map tx user-id products)]
(sql/delete! tx table {id-key id})
(sql/insert-multi! tx table
[id-key :product_id :amount]
(for [[n amount] products
[id-key :product_id :amount :price]
(for [[n {:keys [amount price]}] products
:let [product-id (-> n name products-map)]
:when product-id]
[id product-id amount]))))
[id product-id amount price]))))

View File

@ -9,27 +9,33 @@
(def sample-customers
[{:customers/name "klient 1", :customers/id 1
:customer_groups/name "group1", :customer_groups/id 1,
:customer_group_products/amount 2, :products/name "eggs"}
:customer_group_products/price 43 :customer_group_products/amount 2,
:products/name "eggs"}
{:customers/name "klient 1", :customers/id 1
:customer_groups/name "group1", :customer_groups/id 1,
:customer_group_products/amount 32, :products/name "milk"}
:customer_group_products/price nil :customer_group_products/amount 32, :products/name "milk"}
{:customers/name "klient 1", :customers/id 1
:customer_groups/name "group 2", :customer_groups/id 2,
:customer_group_products/amount 1, :products/name "milk"}
:customer_group_products/price 91 :customer_group_products/amount 1,
:products/name "milk"}
{:customers/name "klient 1", :customers/id 1
:customer_groups/name "group 2", :customer_groups/id 2,
:customer_group_products/price 23
:customer_group_products/amount 6, :products/name "eggs"}
{:customers/name "klient 1", :customers/id 1
:customer_groups/name "group 2", :customer_groups/id 2,
:customer_group_products/amount 89, :products/name "carrots"}
:customer_group_products/price nil :customer_group_products/amount 89,
:products/name "carrots"}
{:customers/name "klient 2", :customers/id 2
:customer_groups/name "group 3", :customer_groups/id 3,
:customer_group_products/amount 41, :products/name "milk"}
:customer_group_products/price 12 :customer_group_products/amount 41,
:products/name "milk"}
{:customers/name "klient 2", :customers/id 2
:customer_groups/name "group 3", :customer_groups/id 3,
:customer_group_products/amount 6, :products/name "eggs"}])
:customer_group_products/price nil :customer_group_products/amount 6,
:products/name "eggs"}])
(deftest format-products-tests
(testing "products amounts get formatted properly"
@ -40,17 +46,21 @@
{"ble" {:id 32} "bla" {:id 1 :products {}}}))
(is (= (sut/insert-products {} {:id 1 :name "bla"}
[{:products/name "milk" :customer_group_products/amount 1}
{:products/name "carrots" :customer_group_products/amount 12}
{:products/name "eggs" :customer_group_products/amount 3}])
{"bla" {:id 1 :products {:eggs 3 :carrots 12 :milk 1}}})))
[{:products/name "milk" :customer_group_products/amount 1 :customer_group_products/price nil}
{:products/name "carrots" :customer_group_products/amount 12 :customer_group_products/price nil}
{:products/name "eggs" :customer_group_products/amount 3 :customer_group_products/price 31}])
{"bla" {:id 1 :products {:eggs {:amount 3 :price 31} :carrots {:amount 12 :price nil} :milk {:amount 1 :price nil}}}})))
(testing "extracting product groups works"
(is (= (sut/extract-product-groups {:ble "ble"} sample-customers)
{:ble "ble"
:product-groups {"group1" {:id 1, :products {:eggs 2, :milk 32}},
"group 2" {:id 2, :products {:milk 1, :eggs 6, :carrots 89}},
"group 3" {:id 3, :products {:milk 41, :eggs 6}}}})))
{:ble "ble",
:product-groups {"group1" {:id 1, :products {:eggs {:amount 2, :price 43},
:milk {:amount 32, :price nil}}},
"group 2" {:id 2, :products {:milk {:amount 1, :price 91},
:eggs {:amount 6, :price 23},
:carrots {:amount 89, :price nil}}},
"group 3" {:id 3, :products {:milk {:amount 41, :price 12},
:eggs {:amount 6, :price nil}}}}})))
(testing "extracting product groups stops if no values"
(is (= (sut/extract-product-groups {:ble "ble"} nil)
@ -74,9 +84,15 @@
(testing "customer groups are mapped correctly"
(with-redefs [sql/query (constantly sample-customers)]
(is (= (sut/get-all "1")
[{:id 1 :name "klient 1" :product-groups {"group1" {:id 1 :products {:eggs 2 :milk 32}}
"group 2" {:id 2 :products {:milk 1 :eggs 6 :carrots 89}}}}
{:id 2 :name "klient 2" :product-groups {"group 3" {:id 3 :products {:milk 41 :eggs 6}}}}])))))
[{:id 1, :name "klient 1",
:product-groups {"group1" {:id 1, :products {:eggs {:amount 2, :price 43},
:milk {:amount 32, :price nil}}},
"group 2" {:id 2, :products {:milk {:amount 1, :price 91},
:eggs {:amount 6, :price 23},
:carrots {:amount 89, :price nil}}}}}
{:id 2, :name "klient 2",
:product-groups {"group 3" {:id 3, :products {:milk {:amount 41, :price 12},
:eggs {:amount 6, :price nil}}}}}])))))
(deftest test-create!
(testing "correct format is returned"
@ -100,10 +116,10 @@
sql/insert-multi! (fn [_tx table cols products]
(is (= table :customer_group_products))
(is (= cols [:customer_group_id :product_id :amount]))
(is (= products [[group-id 1 34]
[group-id 2 25]
[group-id 3 13]]))
(is (= cols [:customer_group_id :product_id :amount :price]))
(is (= products [[group-id 1 34 21]
[group-id 2 25 43]
[group-id 3 13 nil]]))
:ok)]
(testing "the correct query is used to check if group exists"
(with-redefs [jdbc/execute-one!
@ -111,7 +127,10 @@
(is (= query ["SELECT * FROM customer_groups WHERE user_id = ? AND customer_id = ? AND id =?"
user-id customer-id nil]))
nil)]
(sut/save-product-group user-id customer-id {:name "bla" :products {:eggs 34 :milk 25 :carrots 13}})))
(sut/save-product-group
user-id customer-id {:name "bla" :products {:eggs {:amount 34 :price 21}
:milk {:amount 25 :price 43}
:carrots {:amount 13 :price nil}}})))
(testing "product groups get created"
(with-redefs [jdbc/execute-one! (constantly nil) ; the group doesn't yet exist
@ -119,7 +138,10 @@
(is (= group {:name "bla" :customer_id customer-id :user_id user-id}))
{:customer_groups/id group-id}) ; create a new group
sql/update! (fn [&args] (is nil "The group shouldn't be updated"))]
(sut/save-product-group user-id customer-id {:name "bla" :products {:eggs 34 :milk 25 :carrots 13}})))
(sut/save-product-group
user-id customer-id {:name "bla" :products {:eggs {:amount 34 :price 21}
:milk {:amount 25 :price 43}
:carrots {:amount 13 :price nil}}})))
(testing "existing product groups get updated"
(with-redefs [jdbc/execute-one! (constantly true) ; the group should exist
@ -129,4 +151,6 @@
(is (= query {:id group-id})))
sql/insert! (fn [&args] (is nil "The group shouldn't be created"))]
(sut/save-product-group user-id customer-id
{:id group-id :name "bla" :products {:eggs 34 :milk 25 :carrots 13}}))))))
{:id group-id :name "bla" :products {:eggs {:amount 34 :price 21}
:milk {:amount 25 :price 43}
:carrots {:amount 13 :price nil}}}))))))

View File

@ -9,22 +9,23 @@
(defn raw-order-row [& {:keys [id notes status date user_id user_name products]
:or {id 1 notes "note" status "pending" date #inst "2020-01-01"
user_id 2 user_name "mr blobby" products {:eggs 12 :milk 3}}}]
user_id 2 user_name "mr blobby"
products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}}]
(if products
(for [[product amount] products]
(for [[product {:keys [amount price]}] products]
(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}))
{:products/name (name product) :order_products/price price :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})]))
{:products/name nil :order_products/price 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}})))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}})))
(testing "missing products"
(is (= (sut/structure-order (raw-order-row :products nil))
@ -41,7 +42,7 @@
(is (= (sut/get-order :tx "1" 123)
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}))))
(testing "Only 1 item returned"
(with-redefs [sql/query (fn [_ [query & params]]
@ -52,7 +53,7 @@
(is (= (sut/get-order :tx "1" 123)
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}})))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}})))))
(deftest test-get-all
(testing "correct values returned"
@ -67,22 +68,22 @@
(is (= (sut/get-all "1")
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}
{:id 3, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 43, :name "John"},
:products {:eggs 12 :milk 3}}
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}
{:id 4, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]
"2020-01-03" [{:id 2, :notes "note", :state :pending, :day "2020-01-03",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]})))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]})))))
(deftest test-replace!
(testing "basic replace order"
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]
:products {:eggs {:amount 12 :price 43} :milk {:amount 3 :price nil}}}]
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
jdbc/execute-one! (constantly nil)
products/products-map (constantly {"eggs" 1 "milk" 2})
@ -91,23 +92,23 @@
(is (= table :order_products))
(is (= by {:order_id (:id order)})))
sql/insert-multi! (fn [_ _ cols values]
(is (= cols [:order_id :product_id :amount]))
(is (= values [[1 1 12] [1 2 3]])))
(is (= cols [:order_id :product_id :amount :price]))
(is (= values [[1 1 12 43] [1 2 3 nil]])))
sql/query (constantly (concat
(raw-order-row :id 1 :status "waiting")
(raw-order-row :id 4)))]
(is (= (sut/replace! :user-id order)
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}
{:id 4, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]})))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]})))))
(testing "replace order from different day"
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-02",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]
:products {:eggs {:amount 12 :price 65} :milk {:amount 3 :price nil}}}]
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
jdbc/execute-one! (constantly {:orders/order_date #inst "2020-01-01"})
products/products-map (constantly {"eggs" 1 "milk" 2})
@ -116,23 +117,23 @@
(is (= table :order_products))
(is (= by {:order_id (:id order)})))
sql/insert-multi! (fn [_ _ cols values]
(is (= cols [:order_id :product_id :amount]))
(is (= values [[1 1 12] [1 2 3]])))
(is (= cols [:order_id :product_id :amount :price]))
(is (= values [[1 1 12 65] [1 2 3 nil]])))
sql/query (constantly (concat
(raw-order-row :id 1 :status "waiting" :date #inst "2020-01-02")
(raw-order-row :id 4)))]
(is (= (sut/replace! :user-id order)
{"2020-01-01" [{:id 4, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]
"2020-01-02" [{:id 1, :notes "note", :state :waiting, :day "2020-01-02",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]})))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]})))))
(testing "unknown products are ignored"
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]
:products {:eggs {:amount 12 :price 89} :milk {:amount 3 :price nil}}}]
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
jdbc/execute-one! (constantly nil)
products/products-map (constantly {"eggs" 1 "candles" 2})
@ -141,18 +142,18 @@
(is (= table :order_products))
(is (= by {:order_id (:id order)})))
sql/insert-multi! (fn [_ _ cols values]
(is (= cols [:order_id :product_id :amount]))
(is (= values [[1 1 12]])))
(is (= cols [:order_id :product_id :amount :price]))
(is (= values [[1 1 12 89]])))
sql/query (constantly (concat
(raw-order-row :id 1 :status "waiting")
(raw-order-row :id 4)))]
(is (= (sut/replace! :user-id order)
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}
{:id 4, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]}))))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]}))))))
(deftest test-delete!
(testing "non deleted items from day are returned"
@ -165,7 +166,7 @@
(is (= (sut/delete! :user-id 1)
{"2020-01-01" [{:id 4, :notes "note", :state :pending, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]}))))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]}))))
(testing "nothing returned if no date set for the given order"
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
@ -188,7 +189,7 @@
(is (= (sut/change-state! :user-id 1 "fulfilled")
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]}))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]}))
(is (= @updates [["UPDATE products SET amount = amount - ? WHERE name = ?" 12 "eggs"]
["UPDATE products SET amount = amount - ? WHERE name = ?" 3 "milk"]])))))
@ -200,7 +201,7 @@
(is (= (sut/change-state! :user-id 1 "waiting")
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
:who {:id 2, :name "mr blobby"},
:products {:eggs 12 :milk 3}}]}))
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]}))
(is (= @updates [])))))
(testing "unknown states cause an exception"

View File

@ -109,8 +109,10 @@
sql/insert-multi! (fn [_tx table cols products]
(is (= table :bla_products))
(is (= cols [:bla_id :product_id :amount]))
(is (= products [[item-id 1 34]
[item-id 2 25]
[item-id 3 13]])))]
(sut/update-products-mapping! :tx 123 :bla item-id {:eggs 34 :milk 25 :carrots 13})))))
(is (= cols [:bla_id :product_id :amount :price]))
(is (= products [[item-id 1 34 nil]
[item-id 2 25 32]
[item-id 3 13 nil]])))]
(sut/update-products-mapping!
:tx 123 :bla item-id
{:eggs {:amount 34 :price nil} :milk {:amount 25 :price 32} :carrots {:amount 13}})))))

View File

@ -32,7 +32,7 @@
: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)
:backend-url (get-setting :backend-url
(if (= (.. js/window -location -href) "http://localhost:8280/")

View File

@ -4,6 +4,7 @@
[re-frame.core :as re-frame]
[reagent.core :as reagent]
[chicken-master.html :as html]
[chicken-master.config :as config]
[chicken-master.subs :as subs]))
(defn num-or-nil [val]
@ -31,11 +32,18 @@
(map (fn [[k v]] [(str/split k "-") v]))
(group-by (comp last first))
(map #(sort-by first (second %)))
(map (fn [[[_ amount] [_ product]]] [(keyword product) (num-or-nil amount)]))
(remove (comp nil? first))
(remove (comp zero? second))
(map (partial reduce (fn [col [[k _] val]] (assoc col (keyword k) val)) {}))
(filter :product)
(map (fn [{:keys [product amount price]}]
[product {:amount (num-or-nil amount)
:price (-> price num-or-nil normalise-price)}]))
(remove (comp zero? :amount second))
(group-by first)
(map (fn [[product items]] [product (->> items (map last) (reduce +))]))
(map (fn [[product items]]
[(keyword product) (->> items
(map second)
(map (partial reduce-kv #(if %3 (assoc %1 %2 %3) %1) {}))
(apply merge-with +))]))
(into {})))
(defn product-item [available state what]
@ -51,8 +59,14 @@
(for [product (->> available (concat [what]) (remove nil?) sort vec)]
[:option {:key (gensym) :value product} (name product)])
[:option {:key (gensym) :value nil} "-"]]]
(number-input (str "amount-" id) nil (@state what)
#(swap! state assoc what (-> % .-target .-value num-or-nil)))]))
(number-input (str "amount-" id) nil (get-in @state [what :amount])
#(swap! state assoc-in [what :amount] (-> % .-target .-value num-or-nil)))
(when (config/settings :prices)
[:div {:class :stock-product-price}
(number-input (str "price-" id) "cena" (format-price (get-in @state [what :price]))
#(swap! state assoc-in
[what :price]
(some-> % .-target .-value num-or-nil normalise-price)))])]))
(defn products-edit [state & {:keys [available-prods getter-fn]
:or {available-prods @(re-frame/subscribe [::subs/available-products])}}]
@ -72,7 +86,7 @@
:on-click #(getter-fn (dissoc @state nil))} "ok"])
products)))))
(defn format-product [settings [product amount]]
(defn format-product [settings [product {:keys [amount price]}]]
[:div {:key (gensym) :class :product}
[:span {:class :product-name} product]
(if (settings :editable-number-inputs)

View File

@ -15,10 +15,10 @@
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble"
"day" "2020-10-10"
"product-eggs" "eggs" "amount-eggs" "12"
"product-cows" "cows" "amount-cows" "22"
"product-cows" "cows" "amount-cows" "22" "price-cows" "2.32"
"product-milk" "milk" "amount-milk" "3.2"})
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble"
:products {:eggs 12 :cows 22 :milk 3.2}})))
:products {:eggs {:amount 12} :cows {:amount 22 :price 232} :milk {:amount 3.2}}})))
(testing "duplicate products"
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble"
@ -27,7 +27,8 @@
"product-cows1" "cows" "amount-cows1" "1"
"product-cows2" "cows" "amount-cows2" "2"
"product-milk" "milk" "amount-milk" "3.2"})
{:who {:name "bla" :id 123} :day nil :notes "ble" :products {:eggs 24 :cows 3 :milk 3.2}})))
{:who {:name "bla" :id 123} :day nil :notes "ble"
:products {:eggs {:amount 24} :cows {:amount 3} :milk {:amount 3.2}}})))
(testing "unselected are ignored"
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble" "day" "2020-10-10"
@ -36,7 +37,17 @@
"product-bad2" "" "amount-bad2" "1"
"product-milk" "milk" "amount-milk" "3.2"
"product-bad3" "" "amount-bad3" "2"})
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble" :products {:eggs 12 :milk 3.2}})))
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble"
:products {:eggs {:amount 12} :milk {:amount 3.2}}})))
(testing "prices are handled"
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble" "day" "2020-10-10"
"product-eggs" "eggs" "amount-eggs" "12" "price-eggs" "4.31"
"product-eggs1" "eggs" "amount-eggs1" "0" "price-eggs1" "1.0"
"product-cow" "cow" "amount-cow" "0"
"product-milk" "milk" "amount-milk" "3.2"})
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble"
:products {:eggs {:amount 12 :price 431} :milk {:amount 3.2}}})))
(testing "items with 0 are removed"
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble" "day" "2020-10-10"
@ -44,7 +55,8 @@
"product-eggs1" "eggs" "amount-eggs1" "0"
"product-cow" "cow" "amount-cow" "0"
"product-milk" "milk" "amount-milk" "3.2"})
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble" :products {:eggs 12 :milk 3.2}}))))
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble"
:products {:eggs {:amount 12} :milk {:amount 3.2}}}))))
(def customers
[{:id 1 :name "mr blobby" :product-groups {"group 1" {:products {:eggs 1 :carrots 2}}

View File

@ -44,3 +44,40 @@
(testing "nil prices are handled"
(is (nil? (sut/format-price nil)))
(is (nil? (sut/normalise-price nil)))))
(deftest collect-products-test
(testing "no values"
(is (= (sut/collect-products []) {})))
(testing "non product fields are ignored"
(is (= (sut/collect-products [["day" "2021-03-23"] ["who-id" ""]]) {})))
(testing "items with 0 are ignored"
(is (= (sut/collect-products [["amount-G__125" "0"] ["product-G__125" "-"]]) {})))
(testing "products get extracted"
(is (= (sut/collect-products
[["amount-G__122" "33"] ["amount-G__119" "23"]
["product-G__119" "cheese"] ["product-G__122" "eggs"]])
{:eggs {:amount 33} :cheese {:amount 23}})))
(testing "prices are handled"
(is (= (sut/collect-products
[["amount-G__122" "33"] ["amount-G__119" "23"] ["price-G__119" "51"]
["product-G__119" "cheese"] ["product-G__122" "eggs"]])
{:eggs {:amount 33} :cheese {:amount 23 :price 5100}})))
(testing "multiple items of the same type get summed"
(is (= (sut/collect-products
[["amount-G__122" "33"] ["amount-G__119" "23"]
["product-G__119" "cheese"] ["product-G__122" "cheese"]])
{:cheese {:amount 56}})))
(testing "all together"
(is (= (sut/collect-products
[["price-G__122" "19.99"] ["amount-G__122" "33"] ["amount-G__125" "0"]
["product-G__125" "-"] ["amount-G__119" "23"]
["product-G__119" "cheese"] ["product-G__122" "eggs"]
["day" "2021-03-23"]
["who-id" ""]])
{:eggs {:amount 33 :price 1999} :cheese {:amount 23}}))))