mirror of
https://github.com/mruwnik/chicken-master.git
synced 2025-06-08 21:34:43 +02:00
edit prices
This commit is contained in:
parent
62a260f3ee
commit
9bee3a66f9
@ -9,7 +9,9 @@
|
|||||||
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES products(id)
|
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES products(id)
|
||||||
)"
|
)"
|
||||||
"ALTER TABLE products ADD price BIGINT"
|
"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"
|
:down ["ALTER TABLE products DROP COLUMN price"
|
||||||
"ALTER TABLE order_products DROP COLUMN price"
|
"ALTER TABLE order_products DROP COLUMN price"
|
||||||
|
"ALTER TABLE customer_group_products DROP COLUMN price"
|
||||||
"DROP TABLE customer_products"]}
|
"DROP TABLE customer_products"]}
|
||||||
|
@ -8,7 +8,9 @@
|
|||||||
|
|
||||||
(defn insert-products [coll {:keys [id name]} products]
|
(defn insert-products [coll {:keys [id name]} products]
|
||||||
(->> 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 {:id id} :products)
|
||||||
(assoc coll name)))
|
(assoc coll name)))
|
||||||
|
|
||||||
|
@ -26,11 +26,11 @@
|
|||||||
:name (-> items first :customers/name)}
|
:name (-> items first :customers/name)}
|
||||||
:products (->> items
|
:products (->> items
|
||||||
(filter :products/name)
|
(filter :products/name)
|
||||||
(reduce (fn [coll {:keys [order_products/amount products/name]}]
|
(reduce (fn [coll {:keys [order_products/amount order_products/price products/name]}]
|
||||||
(assoc coll (keyword name) amount)) {}))})
|
(assoc coll (keyword name) {:amount amount :price price})) {}))})
|
||||||
|
|
||||||
(def orders-query
|
(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
|
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 order_products op ON o.id = op.order_id
|
||||||
LEFT OUTER JOIN products p on p.id = op.product_id ")
|
LEFT OUTER JOIN products p on p.id = op.product_id ")
|
||||||
@ -84,7 +84,7 @@
|
|||||||
"fulfilled" "-"
|
"fulfilled" "-"
|
||||||
"waiting" "+")]
|
"waiting" "+")]
|
||||||
(when (not= (:state order) (keyword state))
|
(when (not= (:state order) (keyword state))
|
||||||
(doseq [[prod amount] (:products order)]
|
(doseq [[prod {:keys [amount]}] (:products order)]
|
||||||
(jdbc/execute-one! tx
|
(jdbc/execute-one! tx
|
||||||
[(str "UPDATE products SET amount = amount " operator " ? WHERE name = ?")
|
[(str "UPDATE products SET amount = amount " operator " ? WHERE name = ?")
|
||||||
amount (name prod)]))
|
amount (name prod)]))
|
||||||
|
@ -48,8 +48,8 @@
|
|||||||
products-map (products-map tx user-id products)]
|
products-map (products-map tx user-id products)]
|
||||||
(sql/delete! tx table {id-key id})
|
(sql/delete! tx table {id-key id})
|
||||||
(sql/insert-multi! tx table
|
(sql/insert-multi! tx table
|
||||||
[id-key :product_id :amount]
|
[id-key :product_id :amount :price]
|
||||||
(for [[n amount] products
|
(for [[n {:keys [amount price]}] products
|
||||||
:let [product-id (-> n name products-map)]
|
:let [product-id (-> n name products-map)]
|
||||||
:when product-id]
|
:when product-id]
|
||||||
[id product-id amount]))))
|
[id product-id amount price]))))
|
||||||
|
@ -9,27 +9,33 @@
|
|||||||
(def sample-customers
|
(def sample-customers
|
||||||
[{:customers/name "klient 1", :customers/id 1
|
[{:customers/name "klient 1", :customers/id 1
|
||||||
:customer_groups/name "group1", :customer_groups/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
|
{:customers/name "klient 1", :customers/id 1
|
||||||
:customer_groups/name "group1", :customer_groups/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
|
{:customers/name "klient 1", :customers/id 1
|
||||||
:customer_groups/name "group 2", :customer_groups/id 2,
|
: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
|
{:customers/name "klient 1", :customers/id 1
|
||||||
:customer_groups/name "group 2", :customer_groups/id 2,
|
:customer_groups/name "group 2", :customer_groups/id 2,
|
||||||
|
:customer_group_products/price 23
|
||||||
:customer_group_products/amount 6, :products/name "eggs"}
|
:customer_group_products/amount 6, :products/name "eggs"}
|
||||||
{:customers/name "klient 1", :customers/id 1
|
{:customers/name "klient 1", :customers/id 1
|
||||||
:customer_groups/name "group 2", :customer_groups/id 2,
|
: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
|
{:customers/name "klient 2", :customers/id 2
|
||||||
:customer_groups/name "group 3", :customer_groups/id 3,
|
: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
|
{:customers/name "klient 2", :customers/id 2
|
||||||
:customer_groups/name "group 3", :customer_groups/id 3,
|
: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
|
(deftest format-products-tests
|
||||||
(testing "products amounts get formatted properly"
|
(testing "products amounts get formatted properly"
|
||||||
@ -40,17 +46,21 @@
|
|||||||
{"ble" {:id 32} "bla" {:id 1 :products {}}}))
|
{"ble" {:id 32} "bla" {:id 1 :products {}}}))
|
||||||
|
|
||||||
(is (= (sut/insert-products {} {:id 1 :name "bla"}
|
(is (= (sut/insert-products {} {:id 1 :name "bla"}
|
||||||
[{:products/name "milk" :customer_group_products/amount 1}
|
[{:products/name "milk" :customer_group_products/amount 1 :customer_group_products/price nil}
|
||||||
{:products/name "carrots" :customer_group_products/amount 12}
|
{:products/name "carrots" :customer_group_products/amount 12 :customer_group_products/price nil}
|
||||||
{:products/name "eggs" :customer_group_products/amount 3}])
|
{:products/name "eggs" :customer_group_products/amount 3 :customer_group_products/price 31}])
|
||||||
{"bla" {:id 1 :products {:eggs 3 :carrots 12 :milk 1}}})))
|
{"bla" {:id 1 :products {:eggs {:amount 3 :price 31} :carrots {:amount 12 :price nil} :milk {:amount 1 :price nil}}}})))
|
||||||
|
|
||||||
(testing "extracting product groups works"
|
(testing "extracting product groups works"
|
||||||
(is (= (sut/extract-product-groups {:ble "ble"} sample-customers)
|
(is (= (sut/extract-product-groups {:ble "ble"} sample-customers)
|
||||||
{:ble "ble"
|
{:ble "ble",
|
||||||
:product-groups {"group1" {:id 1, :products {:eggs 2, :milk 32}},
|
:product-groups {"group1" {:id 1, :products {:eggs {:amount 2, :price 43},
|
||||||
"group 2" {:id 2, :products {:milk 1, :eggs 6, :carrots 89}},
|
:milk {:amount 32, :price nil}}},
|
||||||
"group 3" {:id 3, :products {:milk 41, :eggs 6}}}})))
|
"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"
|
(testing "extracting product groups stops if no values"
|
||||||
(is (= (sut/extract-product-groups {:ble "ble"} nil)
|
(is (= (sut/extract-product-groups {:ble "ble"} nil)
|
||||||
@ -74,9 +84,15 @@
|
|||||||
(testing "customer groups are mapped correctly"
|
(testing "customer groups are mapped correctly"
|
||||||
(with-redefs [sql/query (constantly sample-customers)]
|
(with-redefs [sql/query (constantly sample-customers)]
|
||||||
(is (= (sut/get-all "1")
|
(is (= (sut/get-all "1")
|
||||||
[{:id 1 :name "klient 1" :product-groups {"group1" {:id 1 :products {:eggs 2 :milk 32}}
|
[{:id 1, :name "klient 1",
|
||||||
"group 2" {:id 2 :products {:milk 1 :eggs 6 :carrots 89}}}}
|
:product-groups {"group1" {:id 1, :products {:eggs {:amount 2, :price 43},
|
||||||
{:id 2 :name "klient 2" :product-groups {"group 3" {:id 3 :products {:milk 41 :eggs 6}}}}])))))
|
: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!
|
(deftest test-create!
|
||||||
(testing "correct format is returned"
|
(testing "correct format is returned"
|
||||||
@ -100,10 +116,10 @@
|
|||||||
|
|
||||||
sql/insert-multi! (fn [_tx table cols products]
|
sql/insert-multi! (fn [_tx table cols products]
|
||||||
(is (= table :customer_group_products))
|
(is (= table :customer_group_products))
|
||||||
(is (= cols [:customer_group_id :product_id :amount]))
|
(is (= cols [:customer_group_id :product_id :amount :price]))
|
||||||
(is (= products [[group-id 1 34]
|
(is (= products [[group-id 1 34 21]
|
||||||
[group-id 2 25]
|
[group-id 2 25 43]
|
||||||
[group-id 3 13]]))
|
[group-id 3 13 nil]]))
|
||||||
:ok)]
|
:ok)]
|
||||||
(testing "the correct query is used to check if group exists"
|
(testing "the correct query is used to check if group exists"
|
||||||
(with-redefs [jdbc/execute-one!
|
(with-redefs [jdbc/execute-one!
|
||||||
@ -111,7 +127,10 @@
|
|||||||
(is (= query ["SELECT * FROM customer_groups WHERE user_id = ? AND customer_id = ? AND id =?"
|
(is (= query ["SELECT * FROM customer_groups WHERE user_id = ? AND customer_id = ? AND id =?"
|
||||||
user-id customer-id nil]))
|
user-id customer-id nil]))
|
||||||
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"
|
(testing "product groups get created"
|
||||||
(with-redefs [jdbc/execute-one! (constantly nil) ; the group doesn't yet exist
|
(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}))
|
(is (= group {:name "bla" :customer_id customer-id :user_id user-id}))
|
||||||
{:customer_groups/id group-id}) ; create a new group
|
{:customer_groups/id group-id}) ; create a new group
|
||||||
sql/update! (fn [&args] (is nil "The group shouldn't be updated"))]
|
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"
|
(testing "existing product groups get updated"
|
||||||
(with-redefs [jdbc/execute-one! (constantly true) ; the group should exist
|
(with-redefs [jdbc/execute-one! (constantly true) ; the group should exist
|
||||||
@ -129,4 +151,6 @@
|
|||||||
(is (= query {:id group-id})))
|
(is (= query {:id group-id})))
|
||||||
sql/insert! (fn [&args] (is nil "The group shouldn't be created"))]
|
sql/insert! (fn [&args] (is nil "The group shouldn't be created"))]
|
||||||
(sut/save-product-group user-id customer-id
|
(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}}}))))))
|
||||||
|
@ -9,22 +9,23 @@
|
|||||||
|
|
||||||
(defn raw-order-row [& {:keys [id notes status date user_id user_name products]
|
(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"
|
: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
|
(if products
|
||||||
(for [[product amount] products]
|
(for [[product {:keys [amount price]}] products]
|
||||||
(merge #:orders{:id id :notes notes :status status :order_date date}
|
(merge #:orders{:id id :notes notes :status status :order_date date}
|
||||||
#:customers{:id user_id :name user_name}
|
#: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}
|
[(merge #:orders{:id id :notes notes :status status :order_date date}
|
||||||
#:customers{:id user_id :name user_name}
|
#: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
|
(deftest structure-order-test
|
||||||
(testing "basic structure"
|
(testing "basic structure"
|
||||||
(is (= (sut/structure-order (raw-order-row))
|
(is (= (sut/structure-order (raw-order-row))
|
||||||
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
|
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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"
|
(testing "missing products"
|
||||||
(is (= (sut/structure-order (raw-order-row :products nil))
|
(is (= (sut/structure-order (raw-order-row :products nil))
|
||||||
@ -41,7 +42,7 @@
|
|||||||
(is (= (sut/get-order :tx "1" 123)
|
(is (= (sut/get-order :tx "1" 123)
|
||||||
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
|
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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"
|
(testing "Only 1 item returned"
|
||||||
(with-redefs [sql/query (fn [_ [query & params]]
|
(with-redefs [sql/query (fn [_ [query & params]]
|
||||||
@ -52,7 +53,7 @@
|
|||||||
(is (= (sut/get-order :tx "1" 123)
|
(is (= (sut/get-order :tx "1" 123)
|
||||||
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
|
{:id 1, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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
|
(deftest test-get-all
|
||||||
(testing "correct values returned"
|
(testing "correct values returned"
|
||||||
@ -67,22 +68,22 @@
|
|||||||
(is (= (sut/get-all "1")
|
(is (= (sut/get-all "1")
|
||||||
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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",
|
{:id 3, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 43, :name "John"},
|
: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",
|
{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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",
|
"2020-01-03" [{:id 2, :notes "note", :state :pending, :day "2020-01-03",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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!
|
(deftest test-replace!
|
||||||
(testing "basic replace order"
|
(testing "basic replace order"
|
||||||
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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))
|
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||||
jdbc/execute-one! (constantly nil)
|
jdbc/execute-one! (constantly nil)
|
||||||
products/products-map (constantly {"eggs" 1 "milk" 2})
|
products/products-map (constantly {"eggs" 1 "milk" 2})
|
||||||
@ -91,23 +92,23 @@
|
|||||||
(is (= table :order_products))
|
(is (= table :order_products))
|
||||||
(is (= by {:order_id (:id order)})))
|
(is (= by {:order_id (:id order)})))
|
||||||
sql/insert-multi! (fn [_ _ cols values]
|
sql/insert-multi! (fn [_ _ cols values]
|
||||||
(is (= cols [:order_id :product_id :amount]))
|
(is (= cols [:order_id :product_id :amount :price]))
|
||||||
(is (= values [[1 1 12] [1 2 3]])))
|
(is (= values [[1 1 12 43] [1 2 3 nil]])))
|
||||||
sql/query (constantly (concat
|
sql/query (constantly (concat
|
||||||
(raw-order-row :id 1 :status "waiting")
|
(raw-order-row :id 1 :status "waiting")
|
||||||
(raw-order-row :id 4)))]
|
(raw-order-row :id 4)))]
|
||||||
(is (= (sut/replace! :user-id order)
|
(is (= (sut/replace! :user-id order)
|
||||||
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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",
|
{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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"
|
(testing "replace order from different day"
|
||||||
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-02",
|
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-02",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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))
|
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||||
jdbc/execute-one! (constantly {:orders/order_date #inst "2020-01-01"})
|
jdbc/execute-one! (constantly {:orders/order_date #inst "2020-01-01"})
|
||||||
products/products-map (constantly {"eggs" 1 "milk" 2})
|
products/products-map (constantly {"eggs" 1 "milk" 2})
|
||||||
@ -116,23 +117,23 @@
|
|||||||
(is (= table :order_products))
|
(is (= table :order_products))
|
||||||
(is (= by {:order_id (:id order)})))
|
(is (= by {:order_id (:id order)})))
|
||||||
sql/insert-multi! (fn [_ _ cols values]
|
sql/insert-multi! (fn [_ _ cols values]
|
||||||
(is (= cols [:order_id :product_id :amount]))
|
(is (= cols [:order_id :product_id :amount :price]))
|
||||||
(is (= values [[1 1 12] [1 2 3]])))
|
(is (= values [[1 1 12 65] [1 2 3 nil]])))
|
||||||
sql/query (constantly (concat
|
sql/query (constantly (concat
|
||||||
(raw-order-row :id 1 :status "waiting" :date #inst "2020-01-02")
|
(raw-order-row :id 1 :status "waiting" :date #inst "2020-01-02")
|
||||||
(raw-order-row :id 4)))]
|
(raw-order-row :id 4)))]
|
||||||
(is (= (sut/replace! :user-id order)
|
(is (= (sut/replace! :user-id order)
|
||||||
{"2020-01-01" [{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
{"2020-01-01" [{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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",
|
"2020-01-02" [{:id 1, :notes "note", :state :waiting, :day "2020-01-02",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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"
|
(testing "unknown products are ignored"
|
||||||
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
(let [order {:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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))
|
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||||
jdbc/execute-one! (constantly nil)
|
jdbc/execute-one! (constantly nil)
|
||||||
products/products-map (constantly {"eggs" 1 "candles" 2})
|
products/products-map (constantly {"eggs" 1 "candles" 2})
|
||||||
@ -141,18 +142,18 @@
|
|||||||
(is (= table :order_products))
|
(is (= table :order_products))
|
||||||
(is (= by {:order_id (:id order)})))
|
(is (= by {:order_id (:id order)})))
|
||||||
sql/insert-multi! (fn [_ _ cols values]
|
sql/insert-multi! (fn [_ _ cols values]
|
||||||
(is (= cols [:order_id :product_id :amount]))
|
(is (= cols [:order_id :product_id :amount :price]))
|
||||||
(is (= values [[1 1 12]])))
|
(is (= values [[1 1 12 89]])))
|
||||||
sql/query (constantly (concat
|
sql/query (constantly (concat
|
||||||
(raw-order-row :id 1 :status "waiting")
|
(raw-order-row :id 1 :status "waiting")
|
||||||
(raw-order-row :id 4)))]
|
(raw-order-row :id 4)))]
|
||||||
(is (= (sut/replace! :user-id order)
|
(is (= (sut/replace! :user-id order)
|
||||||
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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",
|
{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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!
|
(deftest test-delete!
|
||||||
(testing "non deleted items from day are returned"
|
(testing "non deleted items from day are returned"
|
||||||
@ -165,7 +166,7 @@
|
|||||||
(is (= (sut/delete! :user-id 1)
|
(is (= (sut/delete! :user-id 1)
|
||||||
{"2020-01-01" [{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
{"2020-01-01" [{:id 4, :notes "note", :state :pending, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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"
|
(testing "nothing returned if no date set for the given order"
|
||||||
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||||
@ -188,7 +189,7 @@
|
|||||||
(is (= (sut/change-state! :user-id 1 "fulfilled")
|
(is (= (sut/change-state! :user-id 1 "fulfilled")
|
||||||
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
: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"]
|
(is (= @updates [["UPDATE products SET amount = amount - ? WHERE name = ?" 12 "eggs"]
|
||||||
["UPDATE products SET amount = amount - ? WHERE name = ?" 3 "milk"]])))))
|
["UPDATE products SET amount = amount - ? WHERE name = ?" 3 "milk"]])))))
|
||||||
|
|
||||||
@ -200,7 +201,7 @@
|
|||||||
(is (= (sut/change-state! :user-id 1 "waiting")
|
(is (= (sut/change-state! :user-id 1 "waiting")
|
||||||
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
{"2020-01-01" [{:id 1, :notes "note", :state :waiting, :day "2020-01-01",
|
||||||
:who {:id 2, :name "mr blobby"},
|
:who {:id 2, :name "mr blobby"},
|
||||||
:products {:eggs 12 :milk 3}}]}))
|
:products {:eggs {:amount 12 :price nil} :milk {:amount 3 :price 423}}}]}))
|
||||||
(is (= @updates [])))))
|
(is (= @updates [])))))
|
||||||
|
|
||||||
(testing "unknown states cause an exception"
|
(testing "unknown states cause an exception"
|
||||||
|
@ -109,8 +109,10 @@
|
|||||||
|
|
||||||
sql/insert-multi! (fn [_tx table cols products]
|
sql/insert-multi! (fn [_tx table cols products]
|
||||||
(is (= table :bla_products))
|
(is (= table :bla_products))
|
||||||
(is (= cols [:bla_id :product_id :amount]))
|
(is (= cols [:bla_id :product_id :amount :price]))
|
||||||
(is (= products [[item-id 1 34]
|
(is (= products [[item-id 1 34 nil]
|
||||||
[item-id 2 25]
|
[item-id 2 25 32]
|
||||||
[item-id 3 13]])))]
|
[item-id 3 13 nil]])))]
|
||||||
(sut/update-products-mapping! :tx 123 :bla item-id {:eggs 34 :milk 25 :carrots 13})))))
|
(sut/update-products-mapping!
|
||||||
|
:tx 123 :bla item-id
|
||||||
|
{:eggs {:amount 34 :price nil} :milk {:amount 25 :price 32} :carrots {:amount 13}})))))
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
:editable-number-inputs (get-setting :editable-number-inputs false) ; only allow number modifications in the edit modal
|
: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)
|
: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
|
:backend-url (get-setting :backend-url
|
||||||
(if (= (.. js/window -location -href) "http://localhost:8280/")
|
(if (= (.. js/window -location -href) "http://localhost:8280/")
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
[re-frame.core :as re-frame]
|
[re-frame.core :as re-frame]
|
||||||
[reagent.core :as reagent]
|
[reagent.core :as reagent]
|
||||||
[chicken-master.html :as html]
|
[chicken-master.html :as html]
|
||||||
|
[chicken-master.config :as config]
|
||||||
[chicken-master.subs :as subs]))
|
[chicken-master.subs :as subs]))
|
||||||
|
|
||||||
(defn num-or-nil [val]
|
(defn num-or-nil [val]
|
||||||
@ -31,11 +32,18 @@
|
|||||||
(map (fn [[k v]] [(str/split k "-") v]))
|
(map (fn [[k v]] [(str/split k "-") v]))
|
||||||
(group-by (comp last first))
|
(group-by (comp last first))
|
||||||
(map #(sort-by first (second %)))
|
(map #(sort-by first (second %)))
|
||||||
(map (fn [[[_ amount] [_ product]]] [(keyword product) (num-or-nil amount)]))
|
(map (partial reduce (fn [col [[k _] val]] (assoc col (keyword k) val)) {}))
|
||||||
(remove (comp nil? first))
|
(filter :product)
|
||||||
(remove (comp zero? second))
|
(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)
|
(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 {})))
|
(into {})))
|
||||||
|
|
||||||
(defn product-item [available state what]
|
(defn product-item [available state what]
|
||||||
@ -51,8 +59,14 @@
|
|||||||
(for [product (->> available (concat [what]) (remove nil?) sort vec)]
|
(for [product (->> available (concat [what]) (remove nil?) sort vec)]
|
||||||
[:option {:key (gensym) :value product} (name product)])
|
[:option {:key (gensym) :value product} (name product)])
|
||||||
[:option {:key (gensym) :value nil} "-"]]]
|
[:option {:key (gensym) :value nil} "-"]]]
|
||||||
(number-input (str "amount-" id) nil (@state what)
|
(number-input (str "amount-" id) nil (get-in @state [what :amount])
|
||||||
#(swap! state assoc what (-> % .-target .-value num-or-nil)))]))
|
#(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]
|
(defn products-edit [state & {:keys [available-prods getter-fn]
|
||||||
:or {available-prods @(re-frame/subscribe [::subs/available-products])}}]
|
:or {available-prods @(re-frame/subscribe [::subs/available-products])}}]
|
||||||
@ -72,7 +86,7 @@
|
|||||||
:on-click #(getter-fn (dissoc @state nil))} "ok"])
|
:on-click #(getter-fn (dissoc @state nil))} "ok"])
|
||||||
products)))))
|
products)))))
|
||||||
|
|
||||||
(defn format-product [settings [product amount]]
|
(defn format-product [settings [product {:keys [amount price]}]]
|
||||||
[:div {:key (gensym) :class :product}
|
[:div {:key (gensym) :class :product}
|
||||||
[:span {:class :product-name} product]
|
[:span {:class :product-name} product]
|
||||||
(if (settings :editable-number-inputs)
|
(if (settings :editable-number-inputs)
|
||||||
|
@ -15,10 +15,10 @@
|
|||||||
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble"
|
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble"
|
||||||
"day" "2020-10-10"
|
"day" "2020-10-10"
|
||||||
"product-eggs" "eggs" "amount-eggs" "12"
|
"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"})
|
"product-milk" "milk" "amount-milk" "3.2"})
|
||||||
{:who {:name "bla" :id 123} :day "2020-10-10" :notes "ble"
|
{: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"
|
(testing "duplicate products"
|
||||||
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble"
|
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble"
|
||||||
@ -27,7 +27,8 @@
|
|||||||
"product-cows1" "cows" "amount-cows1" "1"
|
"product-cows1" "cows" "amount-cows1" "1"
|
||||||
"product-cows2" "cows" "amount-cows2" "2"
|
"product-cows2" "cows" "amount-cows2" "2"
|
||||||
"product-milk" "milk" "amount-milk" "3.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"
|
(testing "unselected are ignored"
|
||||||
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble" "day" "2020-10-10"
|
(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-bad2" "" "amount-bad2" "1"
|
||||||
"product-milk" "milk" "amount-milk" "3.2"
|
"product-milk" "milk" "amount-milk" "3.2"
|
||||||
"product-bad3" "" "amount-bad3" "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"
|
(testing "items with 0 are removed"
|
||||||
(is (= (sut/format-raw-order {"who" "bla" "who-id" "123" "notes" "ble" "day" "2020-10-10"
|
(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-eggs1" "eggs" "amount-eggs1" "0"
|
||||||
"product-cow" "cow" "amount-cow" "0"
|
"product-cow" "cow" "amount-cow" "0"
|
||||||
"product-milk" "milk" "amount-milk" "3.2"})
|
"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
|
(def customers
|
||||||
[{:id 1 :name "mr blobby" :product-groups {"group 1" {:products {:eggs 1 :carrots 2}}
|
[{:id 1 :name "mr blobby" :product-groups {"group 1" {:products {:eggs 1 :carrots 2}}
|
||||||
|
@ -44,3 +44,40 @@
|
|||||||
(testing "nil prices are handled"
|
(testing "nil prices are handled"
|
||||||
(is (nil? (sut/format-price nil)))
|
(is (nil? (sut/format-price nil)))
|
||||||
(is (nil? (sut/normalise-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}}))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user