Line data Source code
1 4 : (ns matchscore.api.class-templates
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.application.class-template.add-participants-from-template
6 : :as
7 : app-class-template-add-participants]
8 : [matchscore.application.class-template.copy-participants
9 : :as
10 : app-class-template-copy-participants]
11 : [matchscore.application.class-template.move-participants
12 : :as
13 : app-class-template-move-participants]
14 : [matchscore.application.class-template.swap-participants
15 : :as
16 : app-class-template-swap-participants]
17 : [matchscore.domain.class-template-participant.command :as class-template-participant-cmd]
18 : [matchscore.domain.class-template-participant.repository.postgres
19 : :as
20 : class-template-participant-postgres-repo]
21 : [matchscore.domain.class-template.command :as class-template-cmd]
22 : [matchscore.domain.class-template.query :as class-template-query]
23 : [matchscore.utils.nanoid :as nanoid]
24 : [matchscore.utils.uuid :as uuid-utils]
25 : [ring.util.http-status :as status]
26 : [schemas.api.core :as api-schemas]))
27 :
28 :
29 4 : (defn class-template-create-handler
30 : {:malli/schema [:=>
31 : [:cat api-schemas/RingRequest api-schemas/ClassTemplateRequestSchema]
32 : api-schemas/ClassTemplateResponseSchema]
33 : :malli/parameters {:path api-schemas/TemplatePathParamsSchema}
34 : :authz {:obj :template
35 : :act :update
36 : :id :route-params/template-uuid}
37 : :summary "Create class template"
38 : :description "Creates a new class template under the given event template."}
39 : [{:keys [ctx route-params]
40 : :as _request}
41 : class-template-data]
42 4 : (let [template-uuid (:template-uuid route-params)
43 6 : class-template-data (assoc class-template-data
44 2 : :uuid (uuid-utils/generate-uuid7-str)
45 3 : :public-id (nanoid/generate 15)
46 1 : :template-id template-uuid)
47 4 : result (class-template-cmd/create-class-template! ctx class-template-data)]
48 4 : (if (either/right? result)
49 4 : (let [created (c/extract result)]
50 13 : {:body {:message (str "Class template \"" (:name created) "\" created successfully.")
51 1 : :data created}
52 1 : :status status/created})
53 0 : (let [error (c/extract result)]
54 0 : {:body {:message (:message error)
55 0 : :error error}
56 0 : :status status/bad-request}))))
57 :
58 :
59 4 : (defn class-template-get-by-template-handler
60 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/ClassTemplateListResponseSchema]
61 : :malli/parameters {:path api-schemas/TemplatePathParamsSchema}
62 : :authz {:obj :template
63 : :act :read
64 : :id :route-params/template-uuid}
65 : :summary "List class templates"
66 : :description "Returns all class templates for the given event template."}
67 : [{:keys [ctx route-params]
68 : :as _request}]
69 4 : (let [template-uuid (:template-uuid route-params)
70 5 : repo (:class-template-postgres-repo (:repos ctx))
71 4 : templates (class-template-query/get-class-templates-by-template-id repo template-uuid)]
72 7 : {:body {:message "Class templates retrieved successfully."
73 1 : :data templates}
74 1 : :status status/ok}))
75 :
76 :
77 4 : (defn class-template-update-handler
78 : {:malli/schema [:=>
79 : [:cat api-schemas/RingRequest api-schemas/ClassTemplateRequestSchema]
80 : api-schemas/ClassTemplateResponseSchema]
81 : :malli/parameters {:path api-schemas/ClassTemplateAndTemplatePathParamsSchema}
82 : :authz {:obj :template
83 : :act :update
84 : :id :route-params/template-uuid}
85 : :summary "Update class template"
86 : :description "Updates an existing class template by UUID."}
87 : [{:keys [ctx route-params]
88 : :as _request}
89 : class-template-data]
90 4 : (let [class-template-uuid (:class-template-uuid route-params)
91 3 : template-uuid (:template-uuid route-params)
92 5 : class-template-data (assoc class-template-data
93 1 : :uuid class-template-uuid
94 1 : :template-id template-uuid)
95 4 : result (class-template-cmd/update-class-template! ctx class-template-data)]
96 4 : (if (either/right? result)
97 4 : (let [{:keys [code data]} (c/extract result)]
98 5 : (cond (= code :success.class-template/ok)
99 13 : {:body {:message (str "Class template \"" (:name data) "\" updated successfully.")
100 1 : :data data}
101 1 : :status status/ok}
102 :
103 3 : (= code :success.class-template/no-changes)
104 7 : {:body {:message "No changes detected."
105 1 : :data data}
106 1 : :status status/ok}
107 :
108 : :else
109 0 : (throw
110 0 : (ex-info
111 : "Unexpected success code"
112 0 : {:type :internal-error
113 0 : :code code}))))
114 4 : (let [error (c/extract result)]
115 9 : {:body {:message (:message error)
116 1 : :error error}
117 1 : :status status/bad-request}))))
118 :
119 :
120 4 : (defn class-template-delete-handler
121 : {:malli/schema [:=>
122 : [:cat api-schemas/RingRequest]
123 : api-schemas/ClassTemplateDeleteResponseSchema]
124 : :malli/parameters {:path api-schemas/ClassTemplateAndTemplatePathParamsSchema}
125 : :authz {:obj :template
126 : :act :delete
127 : :id :route-params/template-uuid}
128 : :summary "Delete class template"
129 : :description "Deletes a class template by UUID."}
130 : [{:keys [ctx route-params]
131 : :as _request}]
132 4 : (let [class-template-uuid (:class-template-uuid route-params)
133 3 : template-uuid (:template-uuid route-params)
134 2 : result (class-template-cmd/delete-class-template!
135 1 : ctx
136 1 : class-template-uuid
137 1 : template-uuid)]
138 4 : (if (either/right? result)
139 6 : {:body {:message "Class template deleted successfully."}
140 1 : :status status/ok}
141 4 : (let [error (c/extract result)]
142 9 : {:body {:message (:message error)
143 1 : :error error}
144 1 : :status status/not-found}))))
145 :
146 :
147 4 : (defn class-template-participant-remove-handler
148 : {:malli/schema
149 : [:=> [:cat api-schemas/RingRequest] api-schemas/ClassTemplateParticipantDeleteResponseSchema]
150 : :malli/parameters {:path api-schemas/ClassTemplateParticipantPathParamsSchema}
151 : :authz {:obj :template
152 : :act :update
153 : :id :route-params/template-uuid}
154 : :summary "Remove participant from class template"
155 : :description
156 : "Removes a participant from a class template. Does not affect template registration."}
157 : [{:keys [ctx route-params]
158 : :as _request}]
159 4 : (let [class-template-uuid (:class-template-uuid route-params)
160 3 : template-uuid (:template-uuid route-params)
161 3 : participant-id (:participant-id route-params)
162 5 : class-template-repo (:class-template-postgres-repo (:repos ctx))
163 5 : repo (:class-template-participant-postgres-repo (:repos ctx))
164 2 : class-template (class-template-query/get-class-template
165 1 : class-template-repo
166 1 : class-template-uuid)]
167 3 : (cond
168 14 : (or (nil? class-template) (not= template-uuid (:template-id class-template)))
169 6 : {:body {:message "Class template not found."}
170 1 : :status status/not-found}
171 :
172 3 : (nil?
173 5 : (class-template-participant-postgres-repo/find-by-id repo class-template-uuid participant-id))
174 6 : {:body {:message "Participant not found in class template."}
175 1 : :status status/not-found}
176 :
177 : :else
178 3 : (let [result (class-template-participant-postgres-repo/delete!
179 1 : repo
180 1 : class-template-uuid
181 1 : participant-id)]
182 4 : (if (either/right? result)
183 6 : {:body {:message "Participant removed from class template successfully."}
184 1 : :status status/ok}
185 0 : (let [error (c/extract result)]
186 0 : {:body {:message (:message error)
187 0 : :error error}
188 0 : :status status/bad-request}))))))
189 :
190 :
191 4 : (defn class-template-participant-update-handler
192 : {:malli/schema [:=>
193 : [:cat api-schemas/RingRequest api-schemas/ClassUpdateParticipantRequestSchema]
194 : api-schemas/ClassTemplateParticipantUpdateResponseSchema]
195 : :malli/parameters {:path api-schemas/ClassTemplateParticipantPathParamsSchema}
196 : :authz {:obj :template
197 : :act :update
198 : :id :route-params/template-uuid}
199 : :summary "Update class template participant"
200 : :description "Updates status for an existing class template participant."}
201 : [{:keys [ctx route-params]
202 : :as _request}
203 : update-data]
204 4 : (let [class-template-uuid (:class-template-uuid route-params)
205 3 : template-uuid (:template-uuid route-params)
206 3 : participant-id (:participant-id route-params)
207 5 : class-template-repo (:class-template-postgres-repo (:repos ctx))
208 2 : class-template (class-template-query/get-class-template
209 1 : class-template-repo
210 1 : class-template-uuid)]
211 15 : (if (or (nil? class-template) (not= template-uuid (:template-id class-template)))
212 6 : {:body {:message "Class template not found."}
213 1 : :status status/not-found}
214 6 : (let [participant-data (assoc update-data
215 1 : :class-template-id class-template-uuid
216 1 : :participant-id participant-id)
217 4 : result (class-template-participant-cmd/update-participant! ctx participant-data)]
218 4 : (if (either/right? result)
219 4 : (let [{:keys [code data]} (c/extract result)]
220 11 : {:body {:message (if (= code :success.class-template-participant/ok)
221 : "Participant updated successfully."
222 : "No changes detected.")
223 1 : :data data}
224 1 : :status status/ok})
225 0 : (let [error (c/extract result)]
226 0 : {:body {:message (:message error)
227 0 : :error error}
228 0 : :status (if (= (:code error) :error.class-template-participant/not-found)
229 0 : status/not-found
230 0 : status/bad-request)}))))))
231 :
232 :
233 4 : (defn class-template-bulk-add-participants-from-template-handler
234 : {:malli/schema [:=>
235 : [:cat api-schemas/RingRequest api-schemas/ClassParticipantIdsRequestSchema]
236 : api-schemas/ClassTemplateBulkAddFromTemplateResponseSchema]
237 : :malli/parameters {:path api-schemas/ClassTemplateAndTemplatePathParamsSchema}
238 : :authz {:obj :template
239 : :act :update
240 : :id :route-params/template-uuid}
241 : :summary "Bulk add template participants to class template"
242 : :description
243 : "Adds requested template participants not already in the class template, up to max-players."}
244 : [{:keys [ctx route-params]
245 : :as _request}
246 : {:keys [participant-ids]}]
247 4 : (let [template-uuid (:template-uuid route-params)
248 3 : class-template-uuid (:class-template-uuid route-params)
249 2 : result (app-class-template-add-participants/execute!
250 1 : ctx
251 1 : class-template-uuid
252 1 : template-uuid
253 1 : participant-ids)]
254 4 : (if (either/right? result)
255 4 : (let [{:keys [added-count skipped-count]} (c/extract result)]
256 7 : {:body {:message "Participants added to class template successfully."
257 4 : :data {:added-count added-count
258 1 : :skipped-count skipped-count}}
259 1 : :status status/ok})
260 1 : (let [{:keys [code message]
261 : :as error}
262 3 : (c/extract result)]
263 4 : (if (= code :error.class-template/not-found)
264 7 : {:body {:message message
265 1 : :error error}
266 1 : :status status/not-found}
267 : ;; Non-client failures (e.g. DB/repository errors) must surface as 5xx,
268 : ;; not be masked as 400. Throw with a :type so wrap-exception-handling
269 : ;; maps it to a 500 response.
270 0 : (throw
271 0 : (ex-info
272 0 : (or message "Failed to add participants to class template.")
273 0 : {:type :internal-error
274 0 : :error error})))))))
275 :
276 :
277 4 : (defn class-template-bulk-copy-participants-handler
278 : {:malli/schema [:=>
279 : [:cat api-schemas/RingRequest api-schemas/ClassParticipantIdsRequestSchema]
280 : api-schemas/ClassTemplateBulkCopyResponseSchema]
281 : :malli/parameters {:path api-schemas/ClassTemplateTransferPathParamsSchema}
282 : :authz {:obj :template
283 : :act :update
284 : :id :route-params/template-uuid}
285 : :summary "Copy participants between class templates"
286 : :description
287 : "Copies the given participants from source class template into target class template, skipping shared participants and respecting target max-players."}
288 : [{:keys [ctx route-params]
289 : :as _request}
290 : {:keys [participant-ids]}]
291 4 : (let [template-uuid (:template-uuid route-params)
292 3 : class-template-uuid (:class-template-uuid route-params)
293 3 : source-class-uuid (:source-class-uuid route-params)
294 2 : result (app-class-template-copy-participants/execute!
295 1 : ctx
296 1 : class-template-uuid
297 1 : source-class-uuid
298 1 : template-uuid
299 1 : participant-ids)]
300 4 : (if (either/right? result)
301 4 : (let [{:keys [added-count skipped-count]} (c/extract result)]
302 7 : {:body {:message "Participants copied to class template successfully."
303 4 : :data {:added-count added-count
304 1 : :skipped-count skipped-count}}
305 1 : :status status/ok})
306 1 : (let [{:keys [code message]
307 : :as error}
308 3 : (c/extract result)]
309 4 : (if (= code :error.class-template/not-found)
310 7 : {:body {:message message
311 1 : :error error}
312 1 : :status status/not-found}
313 0 : (throw
314 0 : (ex-info
315 0 : (or message "Failed to copy participants between class templates.")
316 0 : {:type :internal-error
317 0 : :error error})))))))
318 :
319 :
320 4 : (defn class-template-bulk-move-participants-handler
321 : {:malli/schema [:=>
322 : [:cat api-schemas/RingRequest api-schemas/ClassParticipantIdsRequestSchema]
323 : api-schemas/ClassTemplateBulkMoveResponseSchema]
324 : :malli/parameters {:path api-schemas/ClassTemplateTransferPathParamsSchema}
325 : :authz {:obj :template
326 : :act :update
327 : :id :route-params/template-uuid}
328 : :summary "Move participants between class templates"
329 : :description
330 : "Moves the given participants from source class template into target class template, skipping shared participants and respecting target max-players. Moved participants are removed from the source class template."}
331 : [{:keys [ctx route-params]
332 : :as _request}
333 : {:keys [participant-ids]}]
334 4 : (let [template-uuid (:template-uuid route-params)
335 3 : class-template-uuid (:class-template-uuid route-params)
336 3 : source-class-uuid (:source-class-uuid route-params)
337 2 : result (app-class-template-move-participants/execute!
338 1 : ctx
339 1 : class-template-uuid
340 1 : source-class-uuid
341 1 : template-uuid
342 1 : participant-ids)]
343 4 : (if (either/right? result)
344 4 : (let [{:keys [moved-count skipped-count]} (c/extract result)]
345 7 : {:body {:message "Participants moved to class template successfully."
346 4 : :data {:moved-count moved-count
347 1 : :skipped-count skipped-count}}
348 1 : :status status/ok})
349 1 : (let [{:keys [code message]
350 : :as error}
351 3 : (c/extract result)]
352 4 : (if (= code :error.class-template/not-found)
353 7 : {:body {:message message
354 1 : :error error}
355 1 : :status status/not-found}
356 0 : (throw
357 0 : (ex-info
358 0 : (or message "Failed to move participants between class templates.")
359 0 : {:type :internal-error
360 0 : :error error})))))))
361 :
362 :
363 4 : (defn class-template-swap-participants-handler
364 : {:malli/schema [:=>
365 : [:cat api-schemas/RingRequest api-schemas/ClassSwapRequestSchema]
366 : api-schemas/ClassTemplateSwapResponseSchema]
367 : :malli/parameters {:path api-schemas/ClassTemplateSwapPathParamsSchema}
368 : :authz {:obj :template
369 : :act :update
370 : :id :route-params/template-uuid}
371 : :summary "Swap participants between two class templates"
372 : :description
373 : "Swaps the given participants between two class templates: those only in A move to B, those only in B move to A, shared ones are skipped. Each direction respects the receiving class template max-players."}
374 : [{:keys [ctx route-params]
375 : :as _request}
376 : {:keys [class-a-participant-ids class-b-participant-ids]}]
377 4 : (let [template-uuid (:template-uuid route-params)
378 3 : class-template-uuid (:class-template-uuid route-params)
379 3 : other-class-uuid (:other-class-uuid route-params)
380 2 : result (app-class-template-swap-participants/execute!
381 1 : ctx
382 1 : class-template-uuid
383 1 : other-class-uuid
384 1 : template-uuid
385 1 : class-a-participant-ids
386 1 : class-b-participant-ids)]
387 4 : (if (either/right? result)
388 4 : (let [{:keys [moved-to-a-count moved-to-b-count skipped-count]} (c/extract result)]
389 7 : {:body {:message "Participants swapped between class templates successfully."
390 5 : :data {:moved-to-a-count moved-to-a-count
391 1 : :moved-to-b-count moved-to-b-count
392 1 : :skipped-count skipped-count}}
393 1 : :status status/ok})
394 1 : (let [{:keys [code message]
395 : :as error}
396 3 : (c/extract result)]
397 4 : (if (= code :error.class-template/not-found)
398 7 : {:body {:message message
399 1 : :error error}
400 1 : :status status/not-found}
401 0 : (throw
402 0 : (ex-info
403 0 : (or message "Failed to swap participants between class templates.")
404 0 : {:type :internal-error
405 0 : :error error})))))))
|