Line data Source code
1 4 : (ns matchscore.api.brackets
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.application.bracket-participants.add :as bracket-participants-add-app]
6 : [matchscore.application.bracket-participants.fill-from-class :as bracket-participants-fill-app]
7 : [matchscore.application.bracket-participants.substitute :as bracket-participants-substitute-app]
8 : [matchscore.db.postgres.telegram-match-notifications :as notification-db]
9 : [matchscore.domain.bracket.command :as bracket-cmd]
10 : [matchscore.domain.bracket.query :as bracket-query]
11 : [matchscore.jobs.telegram-match-notifications :as match-notification-jobs]
12 : [matchscore.utils.nanoid :as nanoid]
13 : [matchscore.utils.uuid :as uuid-utils]
14 : [ring.util.http-status :as status]
15 : [schemas.api.core :as api-schemas]))
16 :
17 :
18 4 : (defn- schedule-draw-sync!
19 : [ctx draw-id]
20 12 : (when-let [event-id (notification-db/event-id-for-draw (:pg ctx) draw-id)]
21 6 : (match-notification-jobs/schedule-event-sync! (:goose ctx) event-id)))
22 :
23 :
24 : ;; Handlers
25 4 : (defn brackets-create-handler
26 : {:malli/schema [:=>
27 : [:cat api-schemas/RingRequest api-schemas/BracketRequestSchema]
28 : api-schemas/BracketResponseSchema]
29 : :malli/parameters {:path api-schemas/BracketDrawUuidPathParamsSchema}
30 : :authz {:obj :event
31 : :act :update
32 : :via :draw
33 : :id :route-params/draw-uuid}
34 : :summary "Create bracket"
35 : :description "Creates a new bracket for a draw"}
36 : [{:keys [ctx route-params]
37 : :as _request}
38 : bracket-data]
39 4 : (let [draw-uuid (:draw-uuid route-params)
40 2 : bracket-data (->
41 1 : bracket-data
42 5 : (assoc :uuid (uuid-utils/generate-uuid7-str))
43 6 : (assoc :public-id (nanoid/generate 15))
44 4 : (assoc :draw-id draw-uuid)
45 9 : (update :config #(or % {}))
46 7 : (update :source #(or % "matchscore")))
47 4 : result (bracket-cmd/create-bracket! ctx bracket-data)]
48 4 : (if (either/right? result)
49 7 : {:body {:message "Bracket created successfully."
50 3 : :data (c/extract result)}
51 1 : :status status/created}
52 0 : (let [{:keys [code message]} (c/extract result)]
53 0 : {:body {:message message
54 : :data nil}
55 0 : :status (case code
56 0 : :error.bracket/conflict-id status/conflict
57 0 : :error.bracket/upsert-failed status/internal-server-error
58 0 : status/bad-request)}))))
59 :
60 :
61 4 : (defn brackets-update-handler
62 : {:malli/schema [:=>
63 : [:cat api-schemas/RingRequest api-schemas/BracketRequestSchema]
64 : api-schemas/BracketResponseSchema]
65 : :malli/parameters {:path api-schemas/BracketAndDrawPathParamsSchema}
66 : :authz {:obj :event
67 : :act :update
68 : :via :draw
69 : :id :route-params/draw-uuid}
70 : :summary "Update bracket"
71 : :description "Updates an existing bracket"}
72 : [{:keys [ctx route-params]
73 : :as _request}
74 : bracket-data]
75 4 : (let [draw-uuid (:draw-uuid route-params)
76 3 : bracket-uuid (:bracket-uuid route-params)
77 5 : bracket-data (assoc bracket-data
78 1 : :uuid bracket-uuid
79 1 : :draw-id draw-uuid)
80 4 : result (bracket-cmd/update-bracket! ctx bracket-data)]
81 4 : (if (either/right? result)
82 4 : (let [{:keys [code data]} (c/extract result)]
83 3 : (case code
84 7 : :success.bracket/ok {:body {:message "Bracket updated successfully."
85 1 : :data data}
86 1 : :status status/ok}
87 :
88 0 : :success.bracket/no-changes {:body {:message "No changes detected."
89 0 : :data data}
90 0 : :status status/ok}))
91 4 : (let [{:keys [code message]} (c/extract result)]
92 8 : {:body {:message message
93 : :data nil}
94 3 : :status (case code
95 1 : :error.bracket/not-found status/not-found
96 0 : :error.bracket/upsert-failed status/internal-server-error
97 0 : status/internal-server-error)}))))
98 :
99 :
100 4 : (defn brackets-get-by-draw-handler
101 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/BracketListResponseSchema]
102 : :malli/parameters {:path api-schemas/BracketDrawUuidPathParamsSchema}
103 : :authz {:obj :event
104 : :act :read
105 : :via :draw
106 : :id :route-params/draw-uuid}
107 : :summary "Get brackets by draw"
108 : :description "Retrieves all brackets for a specific draw"}
109 : [{:keys [repos route-params]
110 : :as _request}]
111 3 : (let [brackets (bracket-query/get-brackets-by-draw
112 3 : (:bracket-postgres-repo repos)
113 3 : (:draw-uuid route-params))]
114 7 : {:body {:message "Brackets retrieved successfully."
115 1 : :data brackets}
116 1 : :status status/ok}))
117 :
118 :
119 4 : (defn- fill-error->status
120 : [code]
121 10 : (cond (contains? #{:error.draw/not-found :error.stage/not-found :error.bracket/not-found} code)
122 1 : status/not-found
123 :
124 3 : (= code :error.bracket-slot/upsert-failed) status/internal-server-error
125 4 : (= code :error.bracket-slot/no-class-participants-available) status/bad-request
126 0 : :else status/internal-server-error))
127 :
128 :
129 4 : (defn brackets-fill-from-class-handler
130 : {:malli/schema
131 : [:=> [:cat api-schemas/RingRequest] api-schemas/BracketParticipantsFillResponseSchema]
132 : :malli/parameters {:path api-schemas/BracketDrawUuidPathParamsSchema}
133 : :authz {:obj :event
134 : :act :update
135 : :via :draw
136 : :id :route-params/draw-uuid}
137 : :summary "Fill bracket from class"
138 : :description
139 : "Fills every bracket's first-round seed slots under the draw from class participants not
140 : used in the draw"}
141 : [{:keys [ctx route-params]
142 : :as _request}]
143 4 : (let [draw-uuid (:draw-uuid route-params)
144 4 : result (bracket-participants-fill-app/execute! ctx draw-uuid)]
145 4 : (if (either/right? result)
146 1 : (do
147 4 : (schedule-draw-sync! ctx draw-uuid)
148 7 : {:body {:message "Bracket participants filled successfully."
149 3 : :data (c/extract result)}
150 1 : :status status/ok})
151 4 : (let [{:keys [code message]} (c/extract result)]
152 8 : {:body {:message message
153 : :data nil}
154 3 : :status (fill-error->status code)}))))
155 :
156 :
157 4 : (defn brackets-fill-single-from-class-handler
158 : {:malli/schema
159 : [:=> [:cat api-schemas/RingRequest] api-schemas/BracketParticipantsFillResponseSchema]
160 : :malli/parameters {:path api-schemas/BracketAndDrawPathParamsSchema}
161 : :authz {:obj :event
162 : :act :update
163 : :via :draw
164 : :id :route-params/draw-uuid}
165 : :summary "Fill one bracket from class"
166 : :description
167 : "Fills only this bracket's first-round seed slots from class participants not used
168 : anywhere in the draw — sibling brackets under the same draw are left untouched"}
169 : [{:keys [ctx route-params]
170 : :as _request}]
171 4 : (let [draw-uuid (:draw-uuid route-params)
172 3 : bracket-uuid (:bracket-uuid route-params)
173 5 : result (bracket-participants-fill-app/execute! ctx draw-uuid bracket-uuid)]
174 4 : (if (either/right? result)
175 1 : (do
176 4 : (schedule-draw-sync! ctx draw-uuid)
177 7 : {:body {:message "Bracket participants filled successfully."
178 3 : :data (c/extract result)}
179 1 : :status status/ok})
180 4 : (let [{:keys [code message]} (c/extract result)]
181 8 : {:body {:message message
182 : :data nil}
183 3 : :status (fill-error->status code)}))))
184 :
185 :
186 4 : (defn- substitute-error->status
187 : [code]
188 3 : (case code
189 0 : :error.draw/not-found status/not-found
190 1 : :error.bracket-slot/not-found status/not-found
191 0 : :error.bracket-slot/not-substitutable status/bad-request
192 0 : :error.bracket-slot/upsert-failed status/internal-server-error
193 0 : status/internal-server-error))
194 :
195 :
196 4 : (defn brackets-substitute-participant-handler
197 : {:malli/schema
198 : [:=>
199 : [:cat api-schemas/RingRequest api-schemas/BracketParticipantSubstituteRequestSchema]
200 : api-schemas/BracketParticipantSubstituteResponseSchema]
201 : :malli/parameters {:path api-schemas/BracketParticipantSubstitutePathParamsSchema}
202 : :authz {:obj :event
203 : :act :update
204 : :via :draw
205 : :id :route-params/draw-uuid}
206 : :summary "Substitute bracket slot participant"
207 : :description
208 : "Substitutes the participant in a round-1 slot of the draw's main bracket.
209 : If the new participant is already in another round-1 slot, the two are swapped."}
210 : [{:keys [ctx route-params]
211 : :as _request}
212 : {:keys [participant-uuid]}]
213 4 : (let [draw-uuid (:draw-uuid route-params)
214 3 : slot-uuid (:bracket-match-slot-uuid route-params)
215 : result
216 6 : (bracket-participants-substitute-app/execute! ctx draw-uuid slot-uuid participant-uuid)]
217 4 : (if (either/right? result)
218 1 : (do
219 4 : (schedule-draw-sync! ctx draw-uuid)
220 7 : {:body {:message "Bracket slot participant substituted successfully."
221 3 : :data (c/extract result)}
222 1 : :status status/ok})
223 4 : (let [{:keys [code message]} (c/extract result)]
224 8 : {:body {:message message
225 : :data nil}
226 3 : :status (substitute-error->status code)}))))
227 :
228 :
229 4 : (defn- add-error->status
230 : [code]
231 0 : (case code
232 0 : :error.draw/not-found status/not-found
233 0 : :error.bracket-slot/not-found status/not-found
234 0 : :error.bracket-slot/not-empty status/bad-request
235 0 : :error.bracket-slot/already-in-draw status/bad-request
236 0 : :error.bracket-slot/upsert-failed status/internal-server-error
237 0 : status/internal-server-error))
238 :
239 :
240 4 : (defn brackets-add-participant-handler
241 : {:malli/schema [:=>
242 : [:cat api-schemas/RingRequest api-schemas/BracketParticipantAddRequestSchema]
243 : api-schemas/BracketParticipantAddResponseSchema]
244 : :malli/parameters {:path api-schemas/BracketParticipantAddPathParamsSchema}
245 : :authz {:obj :event
246 : :act :update
247 : :via :draw
248 : :id :route-params/draw-uuid}
249 : :summary "Add participant to bracket slot"
250 : :description
251 : "Adds a participant to an empty round-1 seed slot of the draw's main bracket.
252 : Fails if the slot is already occupied (use substitute) or the participant is
253 : already placed in another slot of the draw."}
254 : [{:keys [ctx route-params]
255 : :as _request}
256 : {:keys [participant-uuid]}]
257 0 : (let [draw-uuid (:draw-uuid route-params)
258 0 : slot-uuid (:bracket-match-slot-uuid route-params)
259 0 : result (bracket-participants-add-app/execute! ctx draw-uuid slot-uuid participant-uuid)]
260 0 : (if (either/right? result)
261 0 : (do
262 0 : (schedule-draw-sync! ctx draw-uuid)
263 0 : {:body {:message "Bracket slot participant added successfully."
264 0 : :data (c/extract result)}
265 0 : :status status/ok})
266 0 : (let [{:keys [code message]} (c/extract result)]
267 0 : {:body {:message message
268 : :data nil}
269 0 : :status (add-error->status code)}))))
270 :
271 :
272 4 : (defn brackets-delete-handler
273 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/BracketDeleteResponseSchema]
274 : :malli/parameters {:path api-schemas/BracketAndDrawPathParamsSchema}
275 : :authz {:obj :event
276 : :act :update
277 : :via :draw
278 : :id :route-params/draw-uuid}
279 : :summary "Delete bracket"
280 : :description "Deletes a bracket by UUID"}
281 : [{:keys [ctx route-params]
282 : :as _request}]
283 1 : (let [result
284 9 : (bracket-cmd/delete-bracket! ctx (:bracket-uuid route-params) (:draw-uuid route-params))]
285 4 : (if (either/right? result)
286 1 : (do
287 6 : (schedule-draw-sync! ctx (:draw-uuid route-params))
288 6 : {:body {:message "Bracket deleted successfully."}
289 1 : :status status/ok})
290 4 : (let [error (c/extract result)]
291 8 : {:body {:message (:message error)}
292 1 : :status status/not-found}))))
|