Line data Source code
1 4 : (ns schemas.domain.events
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]
5 : [schemas.api.common :as api-common]
6 : [schemas.domain.club :as club-schemas]))
7 :
8 :
9 4 : (def PaymentReminderSchema
10 8 : [:and
11 8 : [:map
12 12 : [:enabled :boolean]
13 20 : [:timing {:optional true}
14 16 : [:enum "before-start" "after-start"]]
15 20 : [:hours {:optional true}
16 28 : [:int {:min 0
17 : :max 168}]]
18 20 : [:payment-method-ids {:optional true}
19 20 : [:vector [:ref :types/UuidString]]]
20 20 : [:payment-purpose {:optional true}
21 28 : [:maybe [:string {:max 160}]]]]
22 20 : [:fn {:error/message
23 : "Enabled payment reminders require timing, hours, and at least one payment method"}
24 4 : (fn [{:keys [enabled timing hours payment-method-ids]}]
25 27 : (or (not enabled) (and timing (some? hours) (seq payment-method-ids))))]])
26 :
27 :
28 4 : (def EventRecordSchema
29 28 : [:map {:swagger/title "EventRecord"
30 : :swagger/description "Complete event record from database"}
31 20 : [:uuid [:ref :types/UuidString]]
32 8 : [:public-id
33 36 : [:string {:min 1
34 : :description "Public-facing nanoid identifier"
35 : :example "V1StGXR8_Z5j"}]]
36 20 : [:template-id {:optional true}
37 20 : [:maybe [:ref :types/UuidString]]]
38 12 : [:datetime-start api-common/Datetime]
39 12 : [:datetime-end api-common/Datetime]
40 8 : [:state
41 28 : [:string {:min 1
42 : :max 50}]]
43 28 : [:visibility (into [:enum] api-common/visibility-options)]
44 20 : [:club-id {:optional true}
45 20 : [:maybe [:ref :types/UuidString]]]
46 20 : [:name {:optional true}
47 28 : [:maybe [:string {:max 255}]]]
48 20 : [:fee {:optional true}
49 12 : [:maybe number?]]
50 20 : [:fee-type {:optional true}
51 24 : [:maybe [:enum "total" "per-person"]]]
52 : ;; Imported events may use currencies outside the native creation choices.
53 : ;; Preserve the stored code, bounded by the database's VARCHAR(10) column.
54 20 : [:currency {:optional true}
55 8 : [:maybe
56 28 : [:string {:min 1
57 : :max 10}]]]
58 20 : [:duration {:optional true}
59 28 : [:maybe [:int {:min 1}]]]
60 28 : [:score-entry-mode (into [:enum] api-common/score-entry-mode-options)]
61 28 : [:score-entry-default (into [:enum] api-common/score-entry-default-options)]
62 20 : [:timezone {:optional true}
63 28 : [:maybe [:string {:max 100}]]]
64 20 : [:language {:optional true}
65 24 : [:maybe [:enum "en" "uk"]]]
66 12 : [:created-datetime api-common/Datetime]
67 12 : [:updated-datetime api-common/Datetime]
68 20 : [:source [:ref :matchscore/source]]
69 20 : [:rankedin-id {:optional true}
70 12 : [:maybe :int]]
71 20 : [:host-user-id {:optional true}
72 20 : [:maybe [:ref :types/UuidString]]]
73 20 : [:created-by {:optional true}
74 20 : [:maybe [:ref :types/UuidString]]]
75 20 : [:description {:optional true}
76 12 : [:maybe :string]]
77 20 : [:sport-code {:optional true}
78 28 : [:maybe (into [:enum] api-common/sport-options)]]
79 20 : [:activity-type-code {:optional true}
80 28 : [:maybe (into [:enum] api-common/activity-type-options)]]
81 20 : [:clubs {:optional true}
82 20 : [:maybe [:sequential club-schemas/ClubRecordSchema]]]
83 20 : [:club-ids {:optional true}
84 28 : [:maybe [:sequential [:ref :types/UuidString]]]]
85 20 : [:settings {:optional true}
86 8 : [:maybe
87 8 : [:map
88 20 : [:payments-enabled {:optional true}
89 12 : [:maybe :boolean]]
90 20 : [:payment-reminder {:optional true}
91 4 : PaymentReminderSchema]
92 : ;; The event's court plan from the stage planner: the courts in use from
93 : ;; :from minutes after the event start (club resource uuids), in time order.
94 20 : [:courts {:optional true}
95 72 : [:maybe [:sequential [:map [:from [:int {:min 0}]] [:ids [:sequential :string]]]]]]]]]
96 20 : [:images-version {:optional true}
97 20 : [:int {:min 0}]]
98 20 : [:images {:optional true}
99 20 : [:maybe [:vector :map]]]
100 20 : [:cover-image {:optional true}
101 12 : [:maybe :map]]])
102 :
103 :
104 4 : (defrecord
105 : EventRecord
106 : [uuid
107 : public-id
108 : template-id
109 : datetime-start
110 : datetime-end
111 : state
112 : visibility
113 : club-id
114 : name
115 : fee
116 : fee-type
117 : currency
118 : duration
119 : score-entry-mode
120 : score-entry-default
121 : timezone
122 : language
123 : created-datetime
124 : updated-datetime
125 : source
126 : rankedin-id
127 : host-user-id
128 : created-by
129 : description
130 : sport-code
131 : activity-type-code
132 : clubs
133 : club-ids
134 : settings
135 : images-version
136 : images
137 : cover-image])
138 :
139 :
140 4 : (defn make-event-record
141 : "Creates an Event record with validation.
142 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
143 : has been called before using this function."
144 : {:malli/schema [:=> [:cat :map] EventRecordSchema]}
145 : [{:keys [uuid
146 : public-id
147 : template-id
148 : datetime-start
149 : datetime-end
150 : state
151 : visibility
152 : club-id
153 : name
154 : fee
155 : fee-type
156 : currency
157 : duration
158 : score-entry-mode
159 : score-entry-default
160 : timezone
161 : language
162 : created-datetime
163 : updated-datetime
164 : source
165 : rankedin-id
166 : host-user-id
167 : created-by
168 : description
169 : sport-code
170 : activity-type-code
171 : clubs
172 : club-ids
173 : settings
174 : images-version
175 : images
176 : cover-image]
177 : :as event-data}]
178 5 : (if (m/validate EventRecordSchema event-data)
179 2 : (map->EventRecord
180 34 : {:uuid uuid
181 1 : :public-id public-id
182 1 : :template-id template-id
183 1 : :datetime-start datetime-start
184 1 : :datetime-end datetime-end
185 1 : :state state
186 1 : :visibility visibility
187 1 : :club-id club-id
188 1 : :name name
189 1 : :fee fee
190 1 : :fee-type fee-type
191 1 : :currency currency
192 1 : :duration duration
193 5 : :score-entry-mode (or score-entry-mode "either")
194 5 : :score-entry-default (or score-entry-default "game")
195 1 : :timezone timezone
196 5 : :language (or language "en")
197 1 : :created-datetime created-datetime
198 1 : :updated-datetime updated-datetime
199 5 : :source (or source "matchscore")
200 1 : :rankedin-id rankedin-id
201 1 : :host-user-id host-user-id
202 1 : :created-by created-by
203 1 : :description description
204 1 : :sport-code sport-code
205 1 : :activity-type-code activity-type-code
206 1 : :clubs clubs
207 1 : :club-ids club-ids
208 1 : :settings settings
209 5 : :images-version (or images-version 0)
210 1 : :images images
211 1 : :cover-image cover-image})
212 0 : (throw
213 0 : (ex-info
214 : "Invalid event data"
215 0 : {:type :internal-validation-error
216 0 : :schema EventRecordSchema
217 0 : :data event-data
218 0 : :humanized-errors (me/humanize (m/explain EventRecordSchema event-data))}))))
219 :
220 :
221 4 : (defn make-event-record-from-qualified
222 : "Creates an Event record from a map with :events/* namespaced keys."
223 : [{:events/keys [uuid
224 : public-id
225 : template-id
226 : datetime-start
227 : datetime-end
228 : state
229 : visibility
230 : club-id
231 : name
232 : fee
233 : fee-type
234 : currency
235 : duration
236 : score-entry-mode
237 : score-entry-default
238 : timezone
239 : language
240 : created-datetime
241 : updated-datetime
242 : source
243 : rankedin-id
244 : host-user-id
245 : created-by
246 : description
247 : sport-code
248 : activity-type-code
249 : settings
250 : images-version]}]
251 124 : (let [record {:uuid uuid
252 4 : :public-id public-id
253 4 : :template-id template-id
254 4 : :datetime-start datetime-start
255 4 : :datetime-end datetime-end
256 4 : :state state
257 4 : :visibility visibility
258 4 : :club-id club-id
259 4 : :name name
260 4 : :fee fee
261 4 : :fee-type fee-type
262 4 : :currency currency
263 4 : :duration duration
264 20 : :score-entry-mode (or score-entry-mode "either")
265 20 : :score-entry-default (or score-entry-default "game")
266 4 : :timezone timezone
267 21 : :language (or language "en")
268 4 : :created-datetime created-datetime
269 4 : :updated-datetime updated-datetime
270 4 : :source source
271 4 : :rankedin-id rankedin-id
272 4 : :host-user-id host-user-id
273 4 : :created-by created-by
274 4 : :description description
275 4 : :sport-code sport-code
276 4 : :activity-type-code activity-type-code
277 4 : :settings settings
278 21 : :images-version (or images-version 0)}]
279 20 : (if (m/validate EventRecordSchema record)
280 12 : (map->EventRecord record)
281 0 : (throw
282 0 : (ex-info
283 : "Invalid event data"
284 0 : {:type :internal-validation-error
285 0 : :schema EventRecordSchema
286 0 : :data record
287 0 : :humanized-errors (me/humanize (m/explain EventRecordSchema record))})))))
|