Line data Source code
1 4 : (ns matchscore.api.event-classes
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.application.class.add-participant :as app-class-add-participant]
6 : [matchscore.application.class.add-participants-from-event :as app-class-add-participants]
7 : [matchscore.application.class.copy-participants :as app-class-copy-participants]
8 : [matchscore.application.class.move-participants :as app-class-move-participants]
9 : [matchscore.application.class.remove-participant :as app-class-remove-participant]
10 : [matchscore.application.class.swap-participants :as app-class-swap-participants]
11 : [matchscore.application.event.class-participant-payment :as payment]
12 : [matchscore.domain.class-participant.command :as class-participant-cmd]
13 : [matchscore.domain.class.command :as class-cmd]
14 : [matchscore.domain.class.query :as class-query]
15 : [matchscore.domain.event-participant.query :as event-participant-query]
16 : [matchscore.telegram.notifications-trigger :as notifications-trigger]
17 : [matchscore.utils.nanoid :as nanoid]
18 : [matchscore.utils.uuid :as uuid-utils]
19 : [ring.util.http-status :as status]
20 : [schemas.api.classes :as classes-schemas]
21 : [schemas.api.core :as api-schemas]
22 : [taoensso.telemere :as t]))
23 :
24 :
25 4 : (defn- trigger-participants-refresh!
26 : [ctx event-uuid]
27 3 : (try
28 12 : (notifications-trigger/trigger-debounced! ctx event-uuid)
29 : (catch Throwable e
30 0 : (t/log!
31 : :error
32 0 : {:message "Failed to enqueue event participants refresh notification"
33 0 : :ctx ctx
34 0 : :event-uuid event-uuid
35 0 : :exception e}))))
36 :
37 :
38 4 : (defn- trigger-payment-post-refresh!
39 : [ctx event-uuid]
40 1 : (try
41 4 : (notifications-trigger/trigger-payment-refresh! ctx event-uuid)
42 : (catch Throwable e
43 0 : (t/log!
44 : :error
45 0 : {:message "Failed to enqueue Telegram payment post refresh"
46 0 : :event-uuid event-uuid
47 0 : :exception e}))))
48 :
49 :
50 4 : (defn event-class-create-handler
51 : {:malli/schema [:=>
52 : [:cat api-schemas/RingRequest api-schemas/ClassRequestSchema]
53 : api-schemas/EventClassResponseSchema]
54 : :malli/parameters {:path api-schemas/EventPathParamsSchema}
55 : :authz {:obj :event
56 : :act :update
57 : :id :route-params/event-uuid}
58 : :summary "Create event class"
59 : :description "Creates a new class under the given event."}
60 : [{:keys [ctx route-params]
61 : :as _request}
62 : class-data]
63 8 : (let [event-uuid (:event-uuid route-params)
64 16 : class-data (assoc class-data
65 4 : :uuid (uuid-utils/generate-uuid7-str)
66 6 : :public-id (nanoid/generate 15)
67 2 : :event-id event-uuid
68 : :source "matchscore")
69 8 : result (class-cmd/create-class! ctx class-data)]
70 8 : (if (either/right? result)
71 8 : (let [created (c/extract result)]
72 8 : (trigger-participants-refresh! ctx event-uuid)
73 26 : {:body {:message (str "Class \"" (:name created) "\" created successfully.")
74 2 : :data created}
75 2 : :status status/created})
76 4 : (let [error (c/extract result)]
77 9 : {:body {:message (:message error)
78 1 : :error error}
79 1 : :status status/bad-request}))))
80 :
81 :
82 4 : (defn event-class-get-by-event-handler
83 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/EventClassListResponseSchema]
84 : :malli/parameters {:path api-schemas/EventPathParamsSchema}
85 : :authz {:obj :event
86 : :act :read
87 : :id :route-params/event-uuid}
88 : :summary "List event classes"
89 : :description "Returns all classes for the given event."}
90 : [{:keys [ctx route-params]
91 : :as _request}]
92 4 : (let [event-uuid (:event-uuid route-params)
93 5 : repo (:class-postgres-repo (:repos ctx))
94 4 : classes (class-query/get-classes-by-event-id repo event-uuid)]
95 7 : {:body {:message "Classes retrieved successfully."
96 1 : :data classes}
97 1 : :status status/ok}))
98 :
99 :
100 4 : (defn event-class-update-handler
101 : {:malli/schema [:=>
102 : [:cat api-schemas/RingRequest api-schemas/ClassRequestSchema]
103 : api-schemas/EventClassResponseSchema]
104 : :malli/parameters {:path api-schemas/ClassAndEventPathParamsSchema}
105 : :authz {:obj :event
106 : :act :update
107 : :id :route-params/event-uuid}
108 : :summary "Update event class"
109 : :description "Updates an existing class by UUID."}
110 : [{:keys [ctx route-params]
111 : :as _request}
112 : class-data]
113 4 : (let [class-uuid (:class-uuid route-params)
114 3 : event-uuid (:event-uuid route-params)
115 5 : class-data (assoc class-data
116 1 : :uuid class-uuid
117 1 : :event-id event-uuid)
118 4 : result (class-cmd/update-class! ctx class-data)]
119 4 : (if (either/right? result)
120 4 : (let [{:keys [code data]} (c/extract result)]
121 5 : (cond (= code :success.class/ok)
122 1 : (do
123 4 : (trigger-participants-refresh! ctx event-uuid)
124 13 : {:body {:message (str "Class \"" (:name data) "\" updated successfully.")
125 1 : :data data}
126 1 : :status status/ok})
127 :
128 3 : (= code :success.class/no-changes)
129 7 : {:body {:message "No changes detected."
130 1 : :data data}
131 1 : :status status/ok}
132 :
133 : :else
134 0 : (throw
135 0 : (ex-info
136 : "Unexpected success code"
137 0 : {:type :internal-error
138 0 : :code code}))))
139 1 : (let [{:keys [code message]
140 : :as error}
141 3 : (c/extract result)]
142 7 : {:body {:message message
143 1 : :error error}
144 5 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
145 :
146 :
147 4 : (defn event-class-participant-create-handler
148 : {:malli/schema [:=>
149 : [:cat api-schemas/RingRequest api-schemas/ClassParticipantRequestSchema]
150 : api-schemas/EventClassParticipantResponseSchema]
151 : :malli/parameters {:path api-schemas/ClassAndEventPathParamsSchema}
152 : :authz {:obj :event
153 : :act :update
154 : :id :route-params/event-uuid}
155 : :summary "Add participant to event class"
156 : :description "Links an existing event participant to a class under that event."}
157 : [{:keys [ctx route-params]
158 : :as _request}
159 : {:keys [participant-id]}]
160 12 : (let [class-uuid (:class-uuid route-params)
161 9 : event-uuid (:event-uuid route-params)
162 15 : class-repo (:class-postgres-repo (:repos ctx))
163 15 : event-participant-repo (:event-participant-postgres-repo (:repos ctx))
164 12 : class (class-query/get-class class-repo class-uuid)]
165 48 : (cond (or (nil? class) (not= event-uuid (:event-id class)))
166 8 : {:body {:message "Class not found."
167 : :data nil}
168 1 : :status status/not-found}
169 :
170 9 : (nil?
171 6 : (event-participant-query/get-event-participant
172 3 : event-participant-repo
173 3 : event-uuid
174 3 : participant-id))
175 7 : {:body {:message "Participant is not registered for this event."
176 3 : :error {:participant-id participant-id}}
177 1 : :status status/bad-request}
178 :
179 : :else
180 33 : (let [participant-data {:class-id class-uuid
181 3 : :participant-id participant-id
182 : :status "registered"
183 6 : :registered-at (java.time.Instant/now)
184 3 : :extra {}
185 : :source "matchscore"}
186 12 : result (class-participant-cmd/register-participant! ctx participant-data)]
187 12 : (if (either/right? result)
188 3 : (do
189 12 : (trigger-participants-refresh! ctx event-uuid)
190 21 : {:body {:message "Participant added to class successfully."
191 9 : :data (c/extract result)}
192 3 : :status status/created})
193 4 : (let [error (c/extract result)]
194 9 : {:body {:message (:message error)
195 1 : :error error}
196 1 : :status status/bad-request}))))))
197 :
198 :
199 4 : (defn event-class-participant-create-register-event-handler
200 : {:malli/schema [:=>
201 : [:cat api-schemas/RingRequest api-schemas/EventAddParticipantRequestSchema]
202 : api-schemas/EventClassParticipantRegisterEventResponseSchema]
203 : :malli/parameters {:path api-schemas/ClassAndEventPathParamsSchema}
204 : :authz {:obj :event
205 : :act :update
206 : :id :route-params/event-uuid}
207 : :summary "Add user as participant to event class, registering it for the event too"
208 : :description
209 : "Registers a user as an event participant (creating a participant record if needed, and reusing an existing event registration if it already has one), then links it to the given class."}
210 : [{:keys [ctx route-params]
211 : :as _request}
212 : {:keys [user-uuid]}]
213 8 : (let [class-uuid (:class-uuid route-params)
214 6 : event-uuid (:event-uuid route-params)
215 12 : result (app-class-add-participant/execute! ctx event-uuid class-uuid user-uuid)]
216 8 : (if (either/right? result)
217 8 : (let [{:keys [event-participant class-participant]} (c/extract result)]
218 14 : {:body {:message "Participant added to event and class successfully."
219 8 : :data {:event-participant event-participant
220 2 : :class-participant class-participant}}
221 2 : :status status/created})
222 1 : (let [{:keys [code message]
223 : :as error}
224 3 : (c/extract result)]
225 7 : {:body {:message message
226 1 : :error error}
227 6 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
228 :
229 :
230 4 : (defn event-class-participant-remove-handler
231 : {:malli/schema
232 : [:=> [:cat api-schemas/RingRequest] api-schemas/EventClassParticipantDeleteResponseSchema]
233 : :malli/parameters {:path api-schemas/ClassParticipantPathParamsSchema}
234 : :authz {:obj :event
235 : :act :update
236 : :id :route-params/event-uuid}
237 : :summary "Remove participant from event class"
238 : :description
239 : "Removes a participant from a class. A player removed from their last class of the event is removed from the event too; admins keep their event registration."}
240 : [{:keys [ctx route-params]
241 : :as _request}]
242 4 : (let [event-uuid (:event-uuid route-params)
243 2 : result (app-class-remove-participant/execute!
244 1 : ctx
245 1 : event-uuid
246 3 : (:class-uuid route-params)
247 3 : (:participant-id route-params))]
248 4 : (if (either/right? result)
249 1 : (do
250 4 : (trigger-participants-refresh! ctx event-uuid)
251 6 : {:body {:message "Participant removed from class successfully."}
252 1 : :status status/ok})
253 1 : (let [{:keys [code message]
254 : :as error}
255 3 : (c/extract result)]
256 7 : (if (contains? #{:error.class/not-found :error.class-participant/not-found} code)
257 6 : {:body {:message message}
258 1 : :status status/not-found}
259 0 : {:body {:message message
260 0 : :error error}
261 0 : :status status/bad-request})))))
262 :
263 :
264 4 : (defn event-class-participant-update-handler
265 : {:malli/schema [:=>
266 : [:cat api-schemas/RingRequest api-schemas/ClassUpdateParticipantRequestSchema]
267 : api-schemas/EventClassParticipantUpdateResponseSchema]
268 : :malli/parameters {:path api-schemas/ClassParticipantPathParamsSchema}
269 : :authz {:obj :event
270 : :act :update
271 : :id :route-params/event-uuid}
272 : :summary "Update class participant"
273 : :description "Updates status for an existing class participant."}
274 : [{:keys [ctx route-params]
275 : :as _request}
276 : update-data]
277 4 : (let [class-uuid (:class-uuid route-params)
278 3 : event-uuid (:event-uuid route-params)
279 3 : participant-id (:participant-id route-params)
280 5 : class-repo (:class-postgres-repo (:repos ctx))
281 4 : class (class-query/get-class class-repo class-uuid)]
282 14 : (if (or (nil? class) (not= event-uuid (:event-id class)))
283 0 : {:body {:message "Class not found."}
284 0 : :status status/not-found}
285 6 : (let [participant-data (assoc update-data
286 1 : :class-id class-uuid
287 1 : :participant-id participant-id)
288 4 : result (class-participant-cmd/update-participant! ctx participant-data)]
289 4 : (if (either/right? result)
290 4 : (let [{:keys [code data]} (c/extract result)]
291 5 : (when (= code :success.class-participant/ok)
292 4 : (trigger-participants-refresh! ctx event-uuid))
293 12 : {:body {:message (if (= code :success.class-participant/ok)
294 : "Participant updated successfully."
295 : "No changes detected.")
296 1 : :data data}
297 1 : :status status/ok})
298 4 : (let [error (c/extract result)]
299 9 : {:body {:message (:message error)
300 1 : :error error}
301 6 : :status (if (= (:code error) :error.class-participant/not-found)
302 1 : status/not-found
303 1 : status/bad-request)}))))))
304 :
305 :
306 4 : (defn event-class-bulk-add-participants-from-event-handler
307 : {:malli/schema [:=>
308 : [:cat api-schemas/RingRequest]
309 : api-schemas/EventClassBulkAddFromEventResponseSchema]
310 : :malli/parameters {:path api-schemas/ClassAndEventPathParamsSchema}
311 : :authz {:obj :event
312 : :act :update
313 : :id :route-params/event-uuid}
314 : :summary "Bulk add event participants to class"
315 : :description "Adds all event participants not already in the class, up to max-players."}
316 : [{:keys [ctx route-params]
317 : :as _request}]
318 4 : (let [class-uuid (:class-uuid route-params)
319 3 : event-uuid (:event-uuid route-params)
320 5 : result (app-class-add-participants/execute! ctx class-uuid event-uuid)]
321 4 : (if (either/right? result)
322 4 : (let [{:keys [added-count skipped-count]} (c/extract result)]
323 8 : (when (pos? added-count) (trigger-participants-refresh! ctx event-uuid))
324 7 : {:body {:message "Participants added to class successfully."
325 4 : :data {:added-count added-count
326 1 : :skipped-count skipped-count}}
327 1 : :status status/ok})
328 1 : (let [{:keys [code message]
329 : :as error}
330 3 : (c/extract result)]
331 7 : {:body {:message message
332 1 : :error error}
333 6 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
334 :
335 :
336 4 : (defn event-class-bulk-copy-participants-handler
337 : {:malli/schema [:=>
338 : [:cat api-schemas/RingRequest api-schemas/ClassParticipantIdsRequestSchema]
339 : api-schemas/EventClassBulkCopyResponseSchema]
340 : :malli/parameters {:path api-schemas/ClassTransferPathParamsSchema}
341 : :authz {:obj :event
342 : :act :update
343 : :id :route-params/event-uuid}
344 : :summary "Copy participants between classes"
345 : :description
346 : "Copies the given participants from source class into target class, skipping shared participants and respecting target max-players."}
347 : [{:keys [ctx route-params]
348 : :as _request}
349 : {:keys [participant-ids]}]
350 4 : (let [class-uuid (:class-uuid route-params)
351 3 : event-uuid (:event-uuid route-params)
352 3 : source-class-uuid (:source-class-uuid route-params)
353 2 : result (app-class-copy-participants/execute!
354 1 : ctx
355 1 : class-uuid
356 1 : source-class-uuid
357 1 : event-uuid
358 1 : participant-ids)]
359 4 : (if (either/right? result)
360 4 : (let [{:keys [added-count skipped-count]} (c/extract result)]
361 8 : (when (pos? added-count) (trigger-participants-refresh! ctx event-uuid))
362 7 : {:body {:message "Participants copied to class successfully."
363 4 : :data {:added-count added-count
364 1 : :skipped-count skipped-count}}
365 1 : :status status/ok})
366 1 : (let [{:keys [code message]
367 : :as error}
368 3 : (c/extract result)]
369 7 : {:body {:message message
370 1 : :error error}
371 5 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
372 :
373 :
374 4 : (defn event-class-bulk-move-participants-handler
375 : {:malli/schema [:=>
376 : [:cat api-schemas/RingRequest api-schemas/ClassParticipantIdsRequestSchema]
377 : api-schemas/EventClassBulkMoveResponseSchema]
378 : :malli/parameters {:path api-schemas/ClassTransferPathParamsSchema}
379 : :authz {:obj :event
380 : :act :update
381 : :id :route-params/event-uuid}
382 : :summary "Move participants between classes"
383 : :description
384 : "Moves the given participants from source class into target class, skipping shared participants and respecting target max-players. Moved participants are removed from the source class."}
385 : [{:keys [ctx route-params]
386 : :as _request}
387 : {:keys [participant-ids]}]
388 4 : (let [class-uuid (:class-uuid route-params)
389 3 : event-uuid (:event-uuid route-params)
390 3 : source-class-uuid (:source-class-uuid route-params)
391 2 : result (app-class-move-participants/execute!
392 1 : ctx
393 1 : class-uuid
394 1 : source-class-uuid
395 1 : event-uuid
396 1 : participant-ids)]
397 4 : (if (either/right? result)
398 4 : (let [{:keys [moved-count skipped-count]} (c/extract result)]
399 8 : (when (pos? moved-count) (trigger-participants-refresh! ctx event-uuid))
400 7 : {:body {:message "Participants moved to class successfully."
401 4 : :data {:moved-count moved-count
402 1 : :skipped-count skipped-count}}
403 1 : :status status/ok})
404 1 : (let [{:keys [code message]
405 : :as error}
406 3 : (c/extract result)]
407 7 : {:body {:message message
408 1 : :error error}
409 5 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
410 :
411 :
412 4 : (defn event-class-swap-participants-handler
413 : {:malli/schema [:=>
414 : [:cat api-schemas/RingRequest api-schemas/ClassSwapRequestSchema]
415 : api-schemas/EventClassSwapResponseSchema]
416 : :malli/parameters {:path api-schemas/ClassSwapPathParamsSchema}
417 : :authz {:obj :event
418 : :act :update
419 : :id :route-params/event-uuid}
420 : :summary "Swap participants between two classes"
421 : :description
422 : "Swaps the given participants between two classes: those only in A move to B, those only in B move to A, shared ones are skipped. Each direction respects the receiving class max-players."}
423 : [{:keys [ctx route-params]
424 : :as _request}
425 : {:keys [class-a-participant-ids class-b-participant-ids]}]
426 4 : (let [class-uuid (:class-uuid route-params)
427 3 : event-uuid (:event-uuid route-params)
428 3 : other-class-uuid (:other-class-uuid route-params)
429 2 : result (app-class-swap-participants/execute!
430 1 : ctx
431 1 : class-uuid
432 1 : other-class-uuid
433 1 : event-uuid
434 1 : class-a-participant-ids
435 1 : class-b-participant-ids)]
436 4 : (if (either/right? result)
437 4 : (let [{:keys [moved-to-a-count moved-to-b-count skipped-count]} (c/extract result)]
438 6 : (when (pos? (+ moved-to-a-count moved-to-b-count))
439 4 : (trigger-participants-refresh! ctx event-uuid))
440 7 : {:body {:message "Participants swapped between classes successfully."
441 5 : :data {:moved-to-a-count moved-to-a-count
442 1 : :moved-to-b-count moved-to-b-count
443 1 : :skipped-count skipped-count}}
444 1 : :status status/ok})
445 1 : (let [{:keys [code message]
446 : :as error}
447 3 : (c/extract result)]
448 7 : {:body {:message message
449 1 : :error error}
450 5 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
451 :
452 :
453 4 : (defn event-class-delete-handler
454 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/EventClassDeleteResponseSchema]
455 : :malli/parameters {:path api-schemas/ClassAndEventPathParamsSchema}
456 : :authz {:obj :event
457 : :act :delete
458 : :id :route-params/event-uuid}
459 : :summary "Delete event class"
460 : :description
461 : "Deletes a class by UUID. Returns 400 when the class still has participants or is the only class of the event."}
462 : [{:keys [ctx route-params]
463 : :as _request}]
464 4 : (let [class-uuid (:class-uuid route-params)
465 3 : event-uuid (:event-uuid route-params)
466 5 : result (class-cmd/delete-class! ctx class-uuid event-uuid)]
467 4 : (if (either/right? result)
468 1 : (do
469 4 : (trigger-participants-refresh! ctx event-uuid)
470 6 : {:body {:message "Class deleted successfully."}
471 1 : :status status/ok})
472 1 : (let [{:keys [code message]
473 : :as error}
474 3 : (c/extract result)]
475 7 : {:body {:message message
476 1 : :error error}
477 6 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))
478 :
479 :
480 4 : (defn event-class-participant-mark-payment-handler
481 : {:malli/schema [:=>
482 : [:cat api-schemas/RingRequest classes-schemas/ClassParticipantPaymentRequestSchema]
483 : classes-schemas/EventClassParticipantPaymentResponseSchema]
484 : :malli/parameters {:path classes-schemas/ClassParticipantPathParamsSchema}
485 : :authz {:obj :event
486 : :act :update
487 : :id :route-params/event-uuid}
488 : :summary "Mark class participant payment as paid"
489 : :description
490 : "Marks the payment for a class registration as paid. Defaults amount/currency to event fee if not supplied."}
491 : [{:keys [ctx auth-headers route-params]
492 : :as _request}
493 : body]
494 4 : (let [class-uuid (:class-uuid route-params)
495 3 : participant-id (:participant-id route-params)
496 3 : marked-by (:user/uuid auth-headers)
497 2 : result (payment/mark-paid!
498 1 : ctx
499 7 : {:class-id class-uuid
500 1 : :participant-id participant-id
501 3 : :amount (:amount body)
502 3 : :currency (:currency body)
503 1 : :marked-by marked-by})]
504 4 : (if (either/right? result)
505 1 : (do
506 6 : (trigger-payment-post-refresh! ctx (:event-uuid route-params))
507 7 : {:body {:message "Payment marked as paid."
508 3 : :data (c/extract result)}
509 1 : :status status/ok})
510 1 : (let [{:keys [code message]
511 : :as error}
512 3 : (c/extract result)]
513 7 : {:body {:message message
514 1 : :error error}
515 6 : :status (cond (= code :error.class-participant/not-found) status/not-found
516 4 : (= code :error.event/payments-not-enabled) status/unprocessable-entity
517 0 : :else status/bad-request)}))))
518 :
519 :
520 4 : (defn event-class-participant-revert-payment-handler
521 : {:malli/schema [:=>
522 : [:cat api-schemas/RingRequest]
523 : classes-schemas/EventClassParticipantPaymentResponseSchema]
524 : :malli/parameters {:path classes-schemas/ClassParticipantPathParamsSchema}
525 : :authz {:obj :event
526 : :act :update
527 : :id :route-params/event-uuid}
528 : :summary "Revert class participant payment to unpaid"
529 : :description "Reverts the payment status for a class registration back to unpaid."}
530 : [{:keys [ctx route-params]
531 : :as _request}]
532 4 : (let [class-uuid (:class-uuid route-params)
533 3 : participant-id (:participant-id route-params)
534 2 : result (payment/revert!
535 1 : ctx
536 4 : {:class-id class-uuid
537 1 : :participant-id participant-id})]
538 4 : (if (either/right? result)
539 1 : (do
540 6 : (trigger-payment-post-refresh! ctx (:event-uuid route-params))
541 7 : {:body {:message "Payment reverted to unpaid."
542 3 : :data (c/extract result)}
543 1 : :status status/ok})
544 1 : (let [{:keys [code message]
545 : :as error}
546 3 : (c/extract result)]
547 7 : {:body {:message message
548 1 : :error error}
549 5 : :status (cond (= code :error.class-participant/not-found) status/not-found
550 0 : (= code :error.event/payments-not-enabled) status/unprocessable-entity
551 0 : :else status/bad-request)}))))
|