LCOV - code coverage report
Current view: top level - matchscore/application/bracket_participants - substitute.clj Coverage Total Hit
Test: lcov.info Lines: 78.0 % 59 46
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.application.bracket-participants.substitute
       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 and syncs the bracket match's home/away participant.
      16              :    Returns [ok? result error-message]."
      17              :   [tx-bms tx-match slot new-pid]
      18            9 :   (let [[ok? result err] (bms-postgres-repo/upsert! tx-bms (assoc slot :participant-id new-pid))]
      19            4 :     (if-not ok?
      20            0 :       [false :database-error err]
      21            1 :       (do
      22            2 :         (match-postgres-repo/update-bracket-slot-participant!
      23            1 :          tx-match
      24            3 :          (:bracket-match-id slot)
      25            3 :          (:slot slot)
      26            1 :          new-pid)
      27            4 :         [true result nil]))))
      28              : 
      29              : 
      30            4 : (defn execute!
      31              :  "Substitutes the participant in a round-1 explicit_participant slot.
      32              :   If the new participant-uuid is already in another round-1 slot of the same
      33              :   draw, swaps the two slots atomically.
      34              :   Returns Either<Error, BracketMatchSlot>."
      35              :  [{:keys [repos pg]} draw-uuid slot-uuid participant-uuid]
      36           11 :  (c/mlet [_ (helpers/find-draw-or-fail repos draw-uuid)]
      37            1 :    (try
      38            4 :      (jdbc/with-transaction
      39            1 :       [tx pg]
      40            7 :       (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
      41            3 :             tx-bms (bms-postgres-repo/make-repository tx-opts)
      42            3 :             tx-match (match-postgres-repo/make-repository tx-opts)
      43            5 :             tx-repos (assoc repos
      44            1 :                       :bracket-match-slot-postgres-repo tx-bms
      45            1 :                       :match-postgres-repo tx-match)
      46            4 :             round1 (advancement-common/find-round1-slots tx-repos draw-uuid)
      47           12 :             target (some #(when (= slot-uuid (:uuid %)) %) round1)
      48              :             outcome
      49            7 :             (cond (nil? target)
      50            2 :                   (either/left
      51            3 :                    (utils.errors/build-error
      52              :                     :check
      53            5 :                     {:code    :error.bracket-slot/not-found
      54              :                      :message "Bracket match slot not found in this draw's first round."}))
      55              : 
      56            6 :                   (not= "explicit_participant" (:source-type target))
      57            0 :                   (either/left
      58            0 :                    (utils.errors/build-error
      59              :                     :check
      60            0 :                     {:code    :error.bracket-slot/not-substitutable
      61              :                      :message "Only explicit_participant slots can be substituted."}))
      62              : 
      63              :                   :else
      64            4 :                   (let [old-pid     (:participant-id target)
      65            3 :                         conflicting (when participant-uuid
      66            3 :                                       (some
      67            3 :                                        #(when
      68            5 :                                           (and
      69            5 :                                            (= participant-uuid (:participant-id %))
      70            5 :                                            (not= slot-uuid (:uuid %)))
      71              :                                           %)
      72            1 :                                        round1))
      73              :                         [ok1? result1 err1]
      74            6 :                         (update-slot-and-sync-match! tx-bms tx-match target participant-uuid)]
      75            4 :                     (if-not ok1?
      76            0 :                       (either/left
      77            0 :                        (utils.errors/build-error
      78              :                         :postgres
      79            0 :                         {:code    :error.bracket-slot/upsert-failed
      80            0 :                          :message (str "Failed to update bracket slot: " err1)}))
      81            2 :                       (if conflicting
      82            1 :                         (let [[ok2? _ err2]
      83            6 :                               (update-slot-and-sync-match! tx-bms tx-match conflicting old-pid)]
      84            2 :                           (if ok2?
      85            3 :                             (either/right result1)
      86            0 :                             (either/left
      87            0 :                              (utils.errors/build-error
      88              :                               :postgres
      89            0 :                               {:code    :error.bracket-slot/upsert-failed
      90            0 :                                :message (str "Failed to swap bracket slot: " err2)}))))
      91            0 :                         (either/right result1)))))]
      92           12 :         (if (either/left? outcome) (throw (ex-info "tx-rollback" {::result outcome})) outcome)))
      93              :      (catch clojure.lang.ExceptionInfo e
      94           12 :        (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))))
        

Generated by: LCOV version 2.0-1