Line data Source code
1 4 : (ns matchscore.api.scoreboard
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.access.resource :as access-resource]
6 : [matchscore.application.scoreboard.get :as app-scoreboard]
7 : [matchscore.domain.event.repository.postgres :as event-repo]
8 : [ring.util.http-status :as status]
9 : [schemas.api.core :as api-schemas]))
10 :
11 :
12 4 : (defn- public-user
13 : [user]
14 11 : (select-keys user [:uuid :public-id :name :username :photo-url :country-code :is-fake]))
15 :
16 :
17 4 : (defn- public-user-rankedin
18 : [user]
19 11 : (select-keys user [:uuid :rankedin-id :source :first-name :last-name :username :photo-url]))
20 :
21 :
22 4 : (defn- public-team [team] (select-keys team [:uuid :public-id :name :photo-url]))
23 :
24 :
25 4 : (defn- public-participant
26 : [participant]
27 3 : (when participant
28 16 : (cond-> (select-keys participant [:uuid :public-id :type])
29 11 : (:user participant) (assoc :user (public-user (:user participant)))
30 6 : (:user-rankedin participant) (assoc :user-rankedin
31 5 : (public-user-rankedin (:user-rankedin participant)))
32 3 : (:team participant) (assoc :team (public-team (:team participant))))))
33 :
34 :
35 4 : (defn- public-scoreboard
36 : [data]
37 2 : (mapv
38 1 : (fn [draw]
39 3 : (update
40 1 : draw
41 : :results
42 1 : (fn [units]
43 2 : (mapv
44 1 : (fn [unit]
45 4 : (update
46 1 : unit
47 : :results
48 3 : #(mapv
49 1 : (fn [placement]
50 8 : (cond-> (update placement :participant public-participant)
51 3 : (:pending-match placement) (update-in
52 0 : [:pending-match :participants]
53 0 : (fn [participants]
54 0 : (mapv public-participant participants)))))
55 : %)))
56 1 : units))))
57 1 : data))
58 :
59 :
60 4 : (defn class-scoreboard-get-handler
61 : {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/ScoreboardResponseSchema]
62 : :malli/parameters {:path api-schemas/ClassAndEventPathParamsSchema}
63 : :authz {:public? true}
64 : :summary "Tournament scoreboard for a class"
65 : :description
66 : "Returns final placements grouped by each draw of the last stage of the class. Group draws are split per group; monrad bracket draws are combined into one ordered placement list under the main bracket."}
67 : [{:keys [ctx route-params]
68 : :as _request}]
69 4 : (let [class-uuid (:class-uuid route-params)
70 3 : event-uuid (:event-uuid route-params)
71 2 : event (event-repo/find-by-id
72 6 : (get-in ctx [:repos :event-postgres-repo])
73 1 : event-uuid
74 3 : {:joins #{}})]
75 7 : (if-not (access-resource/event-readable? ctx event)
76 0 : {:body {:message "Event not found."
77 0 : :error {:code :error.event/not-found}}
78 0 : :status status/not-found}
79 6 : (let [result (app-scoreboard/execute! ctx event-uuid class-uuid)]
80 4 : (if (either/right? result)
81 7 : {:body {:message "Scoreboard retrieved successfully."
82 8 : :data (cond-> (c/extract result)
83 10 : (nil? (get-in ctx [:auth-headers :user/uuid])) public-scoreboard)}
84 1 : :status status/ok}
85 1 : (let [{:keys [code message]
86 : :as error}
87 3 : (c/extract result)]
88 7 : {:body {:message message
89 1 : :error error}
90 5 : :status (if (= code :error.class/not-found) status/not-found status/bad-request)}))))))
|