Line data Source code
1 4 : (ns matchscore.application.bracket-participants.add
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.match.repository.postgres :as match-postgres-repo]
9 : [matchscore.utils.errors :as utils.errors]
10 : [next.jdbc :as jdbc]
11 : [next.jdbc.result-set :as rs]))
12 :
13 :
14 4 : (defn- update-slot-and-sync-match!
15 : "Upserts the slot with new-pid (promoting it to an explicit_participant slot) and syncs the
16 : bracket match's home/away participant. Returns [ok? result error-message]."
17 : [tx-bms tx-match slot new-pid]
18 0 : (let [updated (->
19 0 : slot
20 0 : (assoc :participant-id new-pid)
21 0 : (assoc :source-type "explicit_participant")
22 0 : (assoc :source-seed nil))
23 0 : [ok? result err] (bms-postgres-repo/upsert! tx-bms updated)]
24 0 : (if-not ok?
25 0 : [false :database-error err]
26 0 : (do
27 0 : (match-postgres-repo/update-bracket-slot-participant!
28 0 : tx-match
29 0 : (:bracket-match-id slot)
30 0 : (:slot slot)
31 0 : new-pid)
32 0 : [true result nil]))))
33 :
34 :
35 4 : (defn execute!
36 : "Adds a participant to a single empty round-1 slot of the draw's main bracket.
37 : Unlike substitute, this targets currently-empty slots (seed, winner_of, loser_of) and lets the
38 : caller choose exactly which participant goes into which slot, promoting it to an
39 : explicit_participant slot (an organizer override that also detaches winner_of/loser_of slots
40 : from their feeder match).
41 : Fails if the slot is already occupied (use substitute instead) or if the participant is
42 : already present in another round-1 slot of the same draw.
43 : Returns Either<Error, BracketMatchSlot>."
44 : [{:keys [repos pg]} draw-uuid slot-uuid participant-uuid]
45 0 : (c/mlet [_ (helpers/find-draw-or-fail repos draw-uuid)]
46 0 : (try
47 0 : (jdbc/with-transaction
48 0 : [tx pg]
49 0 : (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
50 0 : tx-bms (bms-postgres-repo/make-repository tx-opts)
51 0 : tx-match (match-postgres-repo/make-repository tx-opts)
52 0 : tx-repos (assoc repos
53 0 : :bracket-match-slot-postgres-repo tx-bms
54 0 : :match-postgres-repo tx-match)
55 0 : round1 (advancement-common/find-round1-slots tx-repos draw-uuid)
56 0 : target (some #(when (= slot-uuid (:uuid %)) %) round1)
57 : already-placed?
58 0 : (when participant-uuid
59 0 : (some
60 0 : #(and (= participant-uuid (:participant-id %)) (not= slot-uuid (:uuid %)))
61 0 : round1))
62 0 : outcome (cond
63 0 : (nil? target)
64 0 : (either/left
65 0 : (utils.errors/build-error
66 : :check
67 0 : {:code :error.bracket-slot/not-found
68 : :message "Bracket match slot not found in this draw's first round."}))
69 :
70 0 : (some? (:participant-id target))
71 0 : (either/left
72 0 : (utils.errors/build-error
73 : :check
74 0 : {:code :error.bracket-slot/not-empty
75 : :message "Slot already has a participant. Use substitute instead."}))
76 :
77 0 : already-placed?
78 0 : (either/left
79 0 : (utils.errors/build-error
80 : :check
81 0 : {:code :error.bracket-slot/already-in-draw
82 : :message "Participant is already placed in another slot of this draw."}))
83 :
84 : :else
85 0 : (let [[ok? result err]
86 0 : (update-slot-and-sync-match! tx-bms tx-match target participant-uuid)]
87 0 : (if ok?
88 0 : (either/right result)
89 0 : (either/left
90 0 : (utils.errors/build-error
91 : :postgres
92 0 : {:code :error.bracket-slot/upsert-failed
93 0 : :message (str "Failed to add bracket slot participant: " err)})))))]
94 0 : (if (either/left? outcome) (throw (ex-info "tx-rollback" {::result outcome})) outcome)))
95 : (catch clojure.lang.ExceptionInfo e
96 0 : (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))))
|