Skip to content

Commit 444c289

Browse files
committed
transform to oms order
1 parent a79d5e1 commit 444c289

File tree

8 files changed

+151
-10
lines changed

8 files changed

+151
-10
lines changed

clojure/wheel/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pom.xml.asc
1010
/.nrepl-port
1111
.hgignore
1212
.hg/
13+
tasks.todo
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns wheel.marketplace.tata-cliq.api
22
(:require [clj-http.client :as http]
3-
[wheel.xml :as wheel-xml]))
3+
[wheel.marketplace.tata-cliq.order :as tata-cliq-order]))
44

55
(defn ranging [{:keys [base-url bearer-token]} channel-id items]
66
(let [url (str base-url "/channels/" channel-id "/ranging")
@@ -9,22 +9,16 @@
99
:content-type :json
1010
:headers {:authorization auth-header}})))
1111

12-
(defn- vectorize [x]
13-
(if (vector? x)
14-
x
15-
(vector x)))
16-
1712
(defn new-orders [{:keys [base-url bearer-token]} channel-id]
1813
(let [url (str base-url "/channels/" channel-id "/new-orders")
1914
auth-header (str "Bearer " bearer-token)]
2015
(-> (http/get url {:headers {:authorization auth-header}})
2116
:body
22-
wheel-xml/xml-str->map
23-
(get-in [:orders :order])
24-
vectorize)))
17+
tata-cliq-order/parse-new-orders)))
2518

2619
(comment
27-
20+
(BigDecimal. "1")
21+
(Integer/parseInt "600001")
2822
(new-orders {:base-url "http://localhost:3000"
2923
:bearer-token "top-secret!"}
3024
"UB"))
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
(ns wheel.marketplace.tata-cliq.order
2+
(:require [wheel.oms.order :as order]
3+
[clojure.spec.alpha :as s]
4+
[clojure.set :as set]
5+
[wheel.xml :as w-xml]))
6+
7+
(defn- vectorize [x]
8+
(if (vector? x)
9+
x
10+
(vector x)))
11+
12+
(defn- coarce-new-order-lines [new-order-lines]
13+
(map (fn [ol]
14+
(update ol :price #(BigDecimal. %))) new-order-lines))
15+
16+
(defn- coarce-new-orders [orders]
17+
(map (fn [order]
18+
(-> (update-in order [:address-info :billing :pincode] #(Integer/parseInt %))
19+
(update-in [:address-info :shipping :pincode] #(Integer/parseInt %))
20+
(update-in [:payment-info :payment-cost] #(BigDecimal. %))
21+
(assoc :order-lines (coarce-new-order-lines (:order-lines order)))))
22+
orders))
23+
24+
(defn- cleanse-new-order [orders]
25+
(map #(assoc % :order-lines
26+
(vectorize (get-in % [:order-lines :order-line]))) orders))
27+
28+
(defn parse-new-orders [xml-response]
29+
(-> (w-xml/xml-str->map xml-response)
30+
(get-in [:orders :order])
31+
vectorize
32+
cleanse-new-order
33+
coarce-new-orders))
34+
35+
(defn- to-oms-address [tata-cliq-address]
36+
(set/rename-keys tata-cliq-address {:address1 :line1
37+
:address2 :line2}))
38+
39+
(defn to-oms-order [tata-cliq-order]
40+
{:post [(s/assert ::order/order %)]}
41+
(let [{:keys [address-info order-no order-lines payment-info]} tata-cliq-order
42+
{:keys [shipping billing]} address-info
43+
{:keys [payment-cost payment-id]} payment-info]
44+
{:billing (to-oms-address billing)
45+
:shipping (to-oms-address shipping)
46+
:order-no order-no
47+
:payments [{:amount payment-cost
48+
:reference-id payment-id}]
49+
:order-lines (map (fn [{:keys [article-number price]}]
50+
{:id article-number
51+
:sale-price price})
52+
order-lines)}))
53+
54+
(comment
55+
(s/check-asserts true)
56+
(map to-oms-order [{:address-info {:billing {:address1 "Plot No 222"
57+
:address2 "Ashok Nagar 42nd Street"
58+
:city "Chennai"
59+
:first-name "Tamizhvendan"
60+
:last-name "Sembiyan"
61+
:pincode 600001
62+
:state "TamilNadu"}
63+
:shipping {:address1 "Plot No 222"
64+
:address2 "Ashok Nagar 42nd Street"
65+
:city "Chennai"
66+
:first-name "Tamizhvendan"
67+
:last-name "Sembiyan"
68+
:pincode 600001
69+
:state "TamilNadu"}}
70+
:order-lines [{:article-number "200374"
71+
:price 900.0M
72+
:transaction-id "200058001702351"}]
73+
:order-no "181219-001-345786"
74+
:payment-info {:payment-cost 900.0M
75+
:payment-id "000000-1545216772601"}}]))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(ns wheel.oms.address
2+
(:require [clojure.spec.alpha :as s]
3+
[wheel.string :as w-str]))
4+
5+
(s/def ::first-name w-str/not-blank?)
6+
(s/def ::last-name w-str/not-blank?)
7+
(s/def ::line1 w-str/not-blank?)
8+
(s/def ::line2 w-str/not-blank?)
9+
(s/def ::city w-str/not-blank?)
10+
(s/def ::state w-str/not-blank?)
11+
(s/def ::pincode (s/int-in 110001 855118))
12+
13+
(s/def ::address (s/keys :req-un [::first-name ::line1 ::city ::state ::pincode]
14+
:opt-un [::last-name ::line2]))
15+
16+
(s/def ::shipping ::address)
17+
(s/def ::billing ::address)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns wheel.oms.order
2+
(:require [clojure.spec.alpha :as s]
3+
[wheel.oms.address :as addr]
4+
[wheel.oms.payment :as payment]
5+
[wheel.oms.order-line :as order-line]
6+
[wheel.string :as w-str]))
7+
8+
(s/def ::order-no w-str/not-blank?)
9+
(s/def ::payments (s/coll-of ::payment/payment :min-count 1))
10+
(s/def ::order-lines (s/coll-of ::order-line/order-line :min-count 1))
11+
(s/def ::order (s/keys :req-un [::order-no ::addr/shipping ::addr/billing ::payments
12+
::order-lines]))
13+
14+
(comment
15+
(s/valid? ::order {:order-no "181219-001-345786"
16+
:payments [{:amount 900M
17+
:reference-id "000000-1545216772601"}]
18+
:order-lines [{:id "200374"
19+
:sale-price 900M}]
20+
:billing {:first-name "Tamizhvendan"
21+
:last-name "Sembiyan"
22+
:line1 "Plot No 222"
23+
:line2 "Ashok Nagar 42nd Street"
24+
:city "Chennai"
25+
:state "TamilNadu"
26+
:pincode 600001}
27+
:shipping {:first-name "Tamizhvendan"
28+
:last-name "Sembiyan"
29+
:line1 "Plot No 222"
30+
:line2 "Ashok Nagar 42nd Street"
31+
:city "Chennai"
32+
:state "TamilNadu"
33+
:pincode 600001}}))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns wheel.oms.order-line
2+
(:require [wheel.oms.item :as item]
3+
[clojure.spec.alpha :as s]))
4+
5+
(s/def ::sale-price (s/and decimal? pos?))
6+
7+
(s/def ::order-line (s/keys :req-un [::item/id ::sale-price]))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns wheel.oms.payment
2+
(:require [clojure.spec.alpha :as s]
3+
[wheel.string :as w-str]))
4+
5+
(s/def ::amount (s/and decimal? pos?))
6+
(s/def ::reference-id w-str/not-blank?)
7+
8+
(s/def ::payment (s/keys :req-un [::amount ::reference-id]))
9+

clojure/wheel/src/wheel/string.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns wheel.string
2+
(:require [clojure.string :as str]))
3+
4+
(defn not-blank? [s]
5+
(and (string? s) (not (str/blank? s))))

0 commit comments

Comments
 (0)