Line data Source code
1 4 : (ns matchscore.application.bracket-participants.fill-from-class
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.application.advancement.common :as advancement-common]
6 : [matchscore.application.group-participants.helpers :as helpers]
7 : [matchscore.domain.bracket-match-slot.repository.postgres :as bms-postgres-repo]
8 : [matchscore.domain.bracket.command :as bracket-cmd]
9 : [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
10 : [matchscore.domain.match.repository.postgres :as match-postgres-repo]
11 : [matchscore.utils.errors :as utils.errors]
12 : [next.jdbc :as jdbc]
13 : [next.jdbc.result-set :as rs]))
14 :
15 :
16 4 : (defn- check-bracket-ownership
17 : "When bracket-uuid is given, verifies it exists and belongs to draw-uuid.
18 : Without this, a mismatched or non-existent bracket-uuid would silently
19 : resolve to zero round-1 slots (find-round1-slots-for-bracket just finds
20 : nothing to fill) and the request would report a no-op success instead of
21 : the not-found it should be."
22 : [repos draw-uuid bracket-uuid]
23 5 : (if (nil? bracket-uuid)
24 3 : (either/right nil)
25 20 : (c/mlet [bracket (bracket-cmd/find-by-id-or-fail (:bracket-postgres-repo repos) bracket-uuid)
26 4 : _ (bracket-cmd/check-belongs-to-draw bracket draw-uuid)]
27 3 : (c/return nil))))
28 :
29 :
30 4 : (defn execute!
31 : "Fills round-1 seed slots with class participants not already placed in the draw. By
32 : default fills every bracket under draw-uuid; when bracket-uuid is given, only that
33 : bracket's empty seed slots are filled and sibling brackets under the same draw are left
34 : untouched — 'already placed' is still computed across the WHOLE draw either way, so a
35 : participant already seated in a sibling bracket is never double-assigned. Slots are
36 : filled sequentially in source-seed order; any surplus seed slots are left untouched
37 : (TBD). Returns Either<Error, seq<BracketMatchSlot>>."
38 : [{:keys [repos pg]} draw-uuid & [bracket-uuid]]
39 25 : (c/mlet [draw (helpers/find-draw-or-fail repos draw-uuid)
40 7 : stage (helpers/find-stage-or-fail repos (:stage-id draw))
41 5 : _ (check-bracket-ownership repos draw-uuid bracket-uuid)]
42 1 : (try
43 4 : (jdbc/with-transaction
44 1 : [tx pg]
45 7 : (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
46 3 : tx-bms-repo (bms-postgres-repo/make-repository tx-opts)
47 3 : tx-match-repo (match-postgres-repo/make-repository tx-opts)
48 5 : tx-repos (assoc repos :bracket-match-slot-postgres-repo tx-bms-repo)
49 3 : class-uuid (:class-id stage)
50 2 : class-participants (class-participant-postgres-repo/find-by-class-id
51 3 : (:class-participant-postgres-repo repos)
52 1 : class-uuid)
53 4 : round1-slots (advancement-common/find-round1-slots tx-repos draw-uuid)
54 7 : used-ids (into #{} (keep :participant-id round1-slots))
55 : fillable-slots
56 2 : (if bracket-uuid
57 4 : (advancement-common/find-round1-slots-for-bracket tx-repos bracket-uuid)
58 1 : round1-slots)
59 2 : empty-seed-slots (->>
60 1 : fillable-slots
61 3 : (filter
62 15 : #(and (= "seed" (:source-type %)) (nil? (:participant-id %))))
63 1 : (sort-by :source-seed))
64 1 : to-add-ids (->>
65 1 : class-participants
66 3 : (map :participant-id)
67 2 : (remove used-ids))
68 5 : pairs (map vector empty-seed-slots to-add-ids)
69 : result
70 6 : (cond (empty? empty-seed-slots) (either/right [])
71 :
72 3 : (empty? to-add-ids)
73 2 : (either/left
74 3 : (utils.errors/build-error
75 : :postgres
76 5 : {:code :error.bracket-slot/no-class-participants-available
77 : :message "No class participants available to fill the bracket"}))
78 :
79 : :else
80 3 : (let [updated (mapv
81 1 : (fn [[slot pid]]
82 3 : (->
83 1 : slot
84 4 : (assoc :participant-id pid)
85 4 : (assoc :source-type "explicit_participant")
86 1 : (assoc :source-seed nil)))
87 1 : pairs)
88 8 : results (mapv #(bms-postgres-repo/upsert! tx-bms-repo %) updated)
89 2 : failed (first
90 2 : (filter
91 1 : (fn [[ok?]]
92 3 : (not ok?))
93 1 : results))]
94 2 : (if failed
95 0 : (either/left
96 0 : (utils.errors/build-error
97 : :postgres
98 0 : {:code :error.bracket-slot/upsert-failed
99 0 : :message (str "Failed to fill bracket participants: " (last failed))}))
100 : ;; Keep the matches row's home/away participant in sync with the slot
101 : ;; (bracket_match_slots is the source of truth; the matches columns are
102 : ;; a derived cache that other consumers, e.g. userMatches, rely on).
103 1 : (let [match-sync-results
104 2 : (mapv
105 1 : (fn [slot]
106 2 : (match-postgres-repo/update-bracket-slot-participant!
107 1 : tx-match-repo
108 3 : (:bracket-match-id slot)
109 3 : (:slot slot)
110 3 : (:participant-id slot)))
111 1 : updated)
112 6 : failed-sync (first (filter empty? match-sync-results))]
113 2 : (if failed-sync
114 0 : (either/left
115 0 : (utils.errors/build-error
116 : :postgres
117 0 : {:code :error.bracket-slot/match-sync-failed
118 : :message "Failed to sync bracket slot participant to match"}))
119 6 : (either/right (mapv second results)))))))]
120 12 : (if (either/left? result) (throw (ex-info "tx-rollback" {::result result})) result)))
121 : (catch clojure.lang.ExceptionInfo e
122 12 : (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))))
|