|
11 | 11 | (s/def ::parent-id ::id) |
12 | 12 |
|
13 | 13 | (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}) |
15 | 15 | (s/def ::system-event-name #{:system/parsing-failed |
16 | 16 | :system/channel-not-found |
17 | 17 | :system/processing-failed}) |
|
39 | 39 |
|
40 | 40 | (s/def ::error-message (s/and string? (complement clojure.string/blank?))) |
41 | 41 | (s/def ::stacktrace (s/and string? (complement clojure.string/blank?))) |
42 | | -(defmethod payload-type :ranging/failed [_] |
43 | | - (s/keys :req-un [::error-message ::stacktrace])) |
44 | 42 |
|
45 | 43 | (s/def ::message-type ::oms-message/type) |
46 | 44 | (defmethod payload-type :system/parsing-failed [_] |
47 | 45 | (s/keys :req-un [::error-message ::message-type])) |
48 | 46 |
|
49 | 47 | (defmethod payload-type :system/channel-not-found [_] |
50 | | - (s/keys :req-un [::channel-id])) |
| 48 | + (s/keys :req-un [::channel-id ::message-type])) |
51 | 49 |
|
52 | 50 | (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])) |
54 | 59 |
|
55 | 60 | (defmethod payload-type :default [_] |
56 | 61 | (s/keys :req-un [::type])) |
|
73 | 78 | (defn domain? [event] |
74 | 79 | (and (s/valid? ::event event) (= :domain (:type event)))) |
75 | 80 |
|
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] |
77 | 83 | :or {level :info |
78 | 84 | type :domain}}] |
79 | 85 | {:post [(s/assert ::event %)]} |
|
83 | 89 | :level level |
84 | 90 | :type type |
85 | 91 | :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)))) |
89 | 96 |
|
90 | 97 | (defn oms [oms-event-name message] |
91 | 98 | {:pre [(s/assert ::oms-event-name oms-event-name) |
|
99 | 106 | {:error-message (with-out-str (stacktrace/print-throwable ex)) |
100 | 107 | :stacktrace (with-out-str (stacktrace/print-stack-trace ex 3))}) |
101 | 108 |
|
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))) |
108 | 124 |
|
109 | 125 | (defn parsing-failed [parent-id message-type error-message] |
110 | 126 | {:pre [(s/assert uuid? parent-id) |
|
118 | 134 | :type :system |
119 | 135 | :level :error)) |
120 | 136 |
|
| 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 | + |
121 | 162 | (comment |
122 | 163 | (s/check-asserts true) |
123 | 164 | (oms :oms/items-ranged "hello") |
|
0 commit comments