Skip to content

Commit 993ad25

Browse files
committed
add processing
1 parent 868e3d9 commit 993ad25

File tree

8 files changed

+127
-25
lines changed

8 files changed

+127
-25
lines changed

clojure/wheel/resources/config.edn

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@
1111
:qmgr #or [#env "WHEEL_APP_MQ_QMGR" "QM1"]
1212
:user-id #or [#env "WHEEL_APP_MQ_USER_ID" "app"]
1313
:password #or [#env "WHEEL_APP_MQ_PASSWORD" "test123"]}}
14-
:settings {:oms {:ranging-queue-name "DEV.QUEUE.1"}}}
14+
:settings {:oms {:ranging-queue-name "DEV.QUEUE.1"}
15+
:channels {"UA" {:channel-name :tata-cliq
16+
:base-url "http://localhost:3000"
17+
:bearer-token "top-secret!"}
18+
"UB" {:channel-name :tata-cliq
19+
:base-url "http://localhost:3000"
20+
:bearer-token "top-secret!"}}}}

clojure/wheel/src/wheel/infra/config.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
(defn oms-settings []
2222
(get-in root [:settings :oms]))
2323

24+
(defn get-channel-cofig [channel-id]
25+
(get-in root [:settings :channels channel-id]))
26+
2427
(comment
2528
(mount/start)
26-
(database)
29+
(get-channel-cofig "UA")
2730
(mount/stop))

clojure/wheel/src/wheel/infra/core.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
[wheel.infra.database :as db]
77
[wheel.infra.ibmmq :as ibmmq]
88
[wheel.infra.oms :as oms]
9-
[wheel.middleware.ranging :as ranging]))
9+
[wheel.middleware.ranging :as ranging]
10+
[wheel.marketplace.tata-cliq.core :as tata-cliq]))
1011

1112
(defn start-app
1213
([]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(ns wheel.marketplace.tata-cliq.api
2+
(:require [clj-http.client :as http]))
3+
4+
(defn ranging [{:keys [base-url bearer-token]} channel-id items]
5+
(let [url (str base-url "/channels/" channel-id "/ranging")
6+
auth-header (str "Bearer " bearer-token)]
7+
(http/post url {:form-params items
8+
:content-type :json
9+
:headers {:authorization auth-header}})))
10+
11+
(comment
12+
(ranging {:base-url "http://localhost:3000"
13+
:bearer-token "top-secret!"}
14+
"UB" [{:sku "SKU1"
15+
:ean "EAN1"}]))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(ns wheel.marketplace.tata-cliq.core
2+
(:require [wheel.marketplace.tata-cliq.api :as tata-cliq]
3+
[wheel.middleware.ranging :as ranging]
4+
[wheel.oms.message :as oms-message]
5+
[wheel.middleware.event :as event]
6+
[clojure.spec.alpha :as s]
7+
[clojure.set :as set]))
8+
9+
(defmethod ranging/process-ranging :tata-cliq
10+
[{:keys [channel-name]
11+
:as channel-config}
12+
{:keys [id]
13+
:as oms-msg}
14+
{:keys [channel-id items]
15+
:as channel-items}]
16+
{:pre [(s/assert ::oms-message/oms-message oms-msg)
17+
(s/assert ::ranging/channel-items channel-items)]}
18+
(try
19+
(tata-cliq/ranging channel-config channel-id
20+
(map #(set/rename-keys % {:id :sku}) items))
21+
(event/ranging-succeeded id channel-id channel-name items)
22+
(catch Throwable ex
23+
(event/processing-failed ex id :ranging channel-id channel-name))))

clojure/wheel/src/wheel/middleware/core.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
(defmulti xsd-resource-file-path :type)
99
(defmulti parse :type)
1010
(defmulti spec :type)
11-
(defmulti process (fn [type _]
12-
type))
11+
(defmulti process (fn [oms-msg parsed-oms-message]
12+
(:type oms-msg)))
1313

1414
(defn- validate-message [oms-msg]
1515
(-> (xsd-resource-file-path oms-msg)
@@ -25,7 +25,7 @@
2525
[(event/parsing-failed id type err)]
2626
(let [parsed-oms-message (parse oms-msg)]
2727
(if (s/valid? (spec oms-msg) parsed-oms-message)
28-
(process type parsed-oms-message)
28+
(process oms-msg parsed-oms-message)
2929
[(event/parsing-failed
3030
id type
3131
(s/explain-str (spec oms-msg) parsed-oms-message))]))))

clojure/wheel/src/wheel/middleware/event.clj

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
(s/def ::parent-id ::id)
1212

1313
(s/def ::oms-event-name #{:oms/items-ranged})
14-
(s/def ::domain-event-name #{:ranging/succeeded :ranging/failed})
14+
(s/def ::domain-event-name #{:ranging/succeeded})
1515
(s/def ::system-event-name #{:system/parsing-failed
1616
:system/channel-not-found
1717
:system/processing-failed})
@@ -39,18 +39,23 @@
3939

4040
(s/def ::error-message (s/and string? (complement clojure.string/blank?)))
4141
(s/def ::stacktrace (s/and string? (complement clojure.string/blank?)))
42-
(defmethod payload-type :ranging/failed [_]
43-
(s/keys :req-un [::error-message ::stacktrace]))
4442

4543
(s/def ::message-type ::oms-message/type)
4644
(defmethod payload-type :system/parsing-failed [_]
4745
(s/keys :req-un [::error-message ::message-type]))
4846

4947
(defmethod payload-type :system/channel-not-found [_]
50-
(s/keys :req-un [::channel-id]))
48+
(s/keys :req-un [::channel-id ::message-type]))
5149

5250
(defmethod payload-type :system/processing-failed [_]
53-
(s/keys :req-un [::error-message ::stacktrace]))
51+
(s/keys :req-un [::error-message ::stacktrace]
52+
:opt-un [::message-type]))
53+
54+
(s/def ::ranged-item
55+
(s/keys :req-un [::item/ean ::item/id]))
56+
(s/def ::ranged-items (s/coll-of ::ranged-item :min-count 1))
57+
(defmethod payload-type :ranging/succeeded [_]
58+
(s/keys :req-un [::ranged-items]))
5459

5560
(defmethod payload-type :default [_]
5661
(s/keys :req-un [::type]))
@@ -73,7 +78,8 @@
7378
(defn domain? [event]
7479
(and (s/valid? ::event event) (= :domain (:type event))))
7580

76-
(defn- event [name payload &{:keys [level type parent-id]
81+
(defn- event [name payload &{:keys [level type parent-id
82+
channel-id channel-name]
7783
:or {level :info
7884
type :domain}}]
7985
{:post [(s/assert ::event %)]}
@@ -83,9 +89,10 @@
8389
:level level
8490
:type type
8591
:payload (assoc payload :type name)}]
86-
(if parent-id
87-
(assoc event :parent-id parent-id)
88-
event)))
92+
(cond-> event
93+
parent-id (assoc :parent-id parent-id)
94+
channel-id (assoc :channel-id channel-id)
95+
channel-name (assoc :channel-name channel-name))))
8996

9097
(defn oms [oms-event-name message]
9198
{:pre [(s/assert ::oms-event-name oms-event-name)
@@ -99,12 +106,21 @@
99106
{:error-message (with-out-str (stacktrace/print-throwable ex))
100107
:stacktrace (with-out-str (stacktrace/print-stack-trace ex 3))})
101108

102-
(defn processing-failed [ex]
103-
{:post [(s/assert ::event %)]}
104-
(event :system/processing-failed
105-
(ex->map ex)
106-
:type :system
107-
:level :error))
109+
(defn processing-failed
110+
([ex]
111+
{:post [(s/assert ::event %)]}
112+
(event :system/processing-failed
113+
(ex->map ex)
114+
:type :system
115+
:level :error))
116+
([ex parent-id message-type channel-id channel-name]
117+
{:post [(s/assert ::event %)]}
118+
(event :system/processing-failed
119+
(assoc (ex->map ex) :message-type message-type)
120+
:parent-id parent-id
121+
:channel-id channel-id
122+
:channel-name channel-name
123+
:level :error)))
108124

109125
(defn parsing-failed [parent-id message-type error-message]
110126
{:pre [(s/assert uuid? parent-id)
@@ -118,6 +134,31 @@
118134
:type :system
119135
:level :error))
120136

137+
(defn channel-not-found [parent-id message-type channel-id]
138+
{:pre [(s/assert uuid? parent-id)
139+
(s/assert ::oms-message/type message-type)
140+
(s/assert ::channel/id channel-id)]
141+
:post [(s/assert ::event %)]}
142+
(event :system/channel-not-found
143+
{:channel-id channel-id
144+
:message-type message-type}
145+
:parent-id parent-id
146+
:type :system
147+
:level :error))
148+
149+
(defn ranging-succeeded [parent-id channel-id channel-name items]
150+
{:pre [(s/assert uuid? parent-id)
151+
(s/assert ::channel/id channel-id)
152+
(s/assert ::channel/name channel-name)
153+
(s/assert ::ranged-items items)]
154+
:post [(s/assert ::event %)]}
155+
(event :ranging/succeeded
156+
{:ranged-items items}
157+
:parent-id parent-id
158+
:channel-id channel-id
159+
:channel-name channel-name))
160+
161+
121162
(comment
122163
(s/check-asserts true)
123164
(oms :oms/items-ranged "hello")

clojure/wheel/src/wheel/middleware/ranging.clj

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
(:require [clojure.xml :as xml]
33
[clojure.spec.alpha :as s]
44
[wheel.oms.item :as oms-item]
5+
[wheel.middleware.event :as event]
6+
[wheel.infra.config :as config]
57
[wheel.marketplace.channel :as channel]
68
[wheel.middleware.core :as middleware])
79
(:import [java.io StringBufferInputStream]))
@@ -18,7 +20,7 @@
1820
(s/def ::message
1921
(s/coll-of ::channel-items :min-count 1))
2022

21-
(defn- to-channel-item [{:keys [EAN ItemID]}]
23+
(defn- to-item [{:keys [EAN ItemID]}]
2224
{:ean EAN
2325
:id ItemID})
2426

@@ -31,16 +33,27 @@
3133
(group-by :ChannelID)
3234
(map (fn [[id xs]]
3335
{:channel-id id
34-
:items (map to-channel-item xs)}))))
36+
:items (map to-item xs)}))))
3537

3638
(defmethod middleware/xsd-resource-file-path :ranging [_]
3739
"oms/message_schema/ranging.xsd")
3840

3941
(defmethod middleware/spec :ranging [_]
4042
::message)
4143

42-
(defmethod middleware/process :ranging [_ ranging-message]
43-
(throw (Exception. "todo")))
44+
(defmulti process-ranging (fn [{:keys [channel-name]} oms-msg ranging-message]
45+
channel-name))
46+
47+
(defmethod middleware/process :ranging [{:keys [id]
48+
:as oms-msg} ranging-message]
49+
(for [{:keys [channel-id]
50+
:as ch-ranging-message} ranging-message]
51+
(if-let [channel-config (config/get-channel-cofig channel-id)]
52+
(try
53+
(process-ranging channel-config oms-msg ch-ranging-message)
54+
(catch Throwable ex
55+
(event/processing-failed ex id :ranging channel-id (:channel-name channel-config))))
56+
(event/channel-not-found id :ranging channel-id))))
4457

4558
(comment
4659
(s/check-asserts true)

0 commit comments

Comments
 (0)