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" }}]))
0 commit comments