LCOV - code coverage report
Current view: top level - matchscore/application/group_participants - update_one.clj Coverage Total Hit
Test: lcov.info Lines: 83.8 % 68 57
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.application.group-participants.update-one
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.application.group-participants.helpers :as helpers]
       6              :    [matchscore.application.group-participants.sync-matches :as sync-matches]
       7              :    [matchscore.domain.group-participant.repository.postgres :as group-participant-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- find-group-participant-or-fail
      15              :   [repos group-participant-uuid group-uuid]
      16            3 :   (let [gp (group-participant-postgres-repo/find-by-id
      17            3 :             (:group-participant-postgres-repo repos)
      18            1 :             group-participant-uuid)]
      19            7 :     (cond (nil? gp)
      20            2 :           (either/left
      21            3 :            (utils.errors/build-error
      22              :             :check
      23            5 :             {:code    :error.group-participant/not-found
      24              :              :message "Group participant not found."}))
      25              : 
      26            6 :           (not= group-uuid (:group-id gp))
      27            0 :           (either/left
      28            0 :            (utils.errors/build-error
      29              :             :check
      30            0 :             {:code    :error.group-participant/not-found
      31              :              :message "Group participant not found."}))
      32              : 
      33            3 :           :else (either/right gp))))
      34              : 
      35              : 
      36            4 : (defn- swap-or-upsert!
      37              :   "Updates the target GP to merged-gp. If merged-gp's participant-id is
      38              :    already assigned to a different slot in the same group, performs a
      39              :    3-step null-intermediate swap to avoid UNIQUE(group_id, participant_id)
      40              :    violations:
      41              :      1. Clear target GP's participant_id.
      42              :      2. Assign old participant_id to the conflicting GP.
      43              :      3. Assign new participant_id to target GP.
      44              :    Returns [ok? result error-message]."
      45              :   [repo group-participants merged-gp]
      46            4 :   (let [new-participant-id (:participant-id merged-gp)
      47            3 :         target-uuid        (:uuid merged-gp)
      48            2 :         old-participant-id (:participant-id
      49           12 :                             (some #(when (= target-uuid (:uuid %)) %) group-participants))
      50            3 :         conflicting        (when new-participant-id
      51            1 :                              (->>
      52            1 :                                group-participants
      53            3 :                                (filter
      54            5 :                                 #(and
      55            5 :                                   (= new-participant-id (:participant-id %))
      56            5 :                                   (not= target-uuid (:uuid %))))
      57            1 :                                first))]
      58            2 :     (if conflicting
      59            1 :       (let [[ok1? _ err1]
      60            8 :             (group-participant-postgres-repo/upsert! repo (assoc merged-gp :participant-id nil))]
      61            4 :         (if-not ok1?
      62            0 :           [false :database-error err1]
      63            3 :           (let [[ok2? _ err2] (group-participant-postgres-repo/upsert!
      64            1 :                                repo
      65            5 :                                (assoc conflicting :participant-id old-participant-id))]
      66            4 :             (if-not ok2?
      67            0 :               [false :database-error err2]
      68            4 :               (group-participant-postgres-repo/upsert! repo merged-gp)))))
      69            4 :       (group-participant-postgres-repo/upsert! repo merged-gp))))
      70              : 
      71              : 
      72            4 : (defn execute!
      73              :  "Updates a single group participant's participant, status, and/or slot.
      74              :   Returns Either<Error, GroupParticipant>."
      75              :  [{:keys [repos pg]} group-participant-uuid group-uuid draw-uuid updates]
      76           19 :  (c/mlet [_ (helpers/find-group-or-fail repos group-uuid draw-uuid)
      77            5 :           gp (find-group-participant-or-fail repos group-participant-uuid group-uuid)]
      78            1 :    (try
      79            4 :      (jdbc/with-transaction
      80            1 :       [tx pg]
      81            7 :       (let [tx-opts            (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
      82            3 :             tx-group-participant-repo (group-participant-postgres-repo/make-repository tx-opts)
      83            5 :             tx-repos           (assoc repos
      84            1 :                                 :group-participant-postgres-repo tx-group-participant-repo
      85            3 :                                 :match-postgres-repo (match-postgres-repo/make-repository tx-opts))
      86            6 :             updates            (cond-> updates
      87            4 :                                  (contains? updates :participant-uuid)
      88            4 :                                  (assoc :participant-id (:participant-uuid updates)))
      89           10 :             merged             (merge gp (select-keys updates [:participant-id :status :slot]))
      90            2 :             group-participants (group-participant-postgres-repo/find-by-group-id
      91            1 :                                 tx-group-participant-repo
      92            1 :                                 group-uuid)
      93              :             [ok? result error-message]
      94            5 :             (swap-or-upsert! tx-group-participant-repo group-participants merged)
      95            2 :             outcome            (if ok?
      96           11 :                                  (c/mlet [_ (sync-matches/execute! tx-repos group-uuid)]
      97            3 :                                    (either/right result))
      98            0 :                                  (either/left
      99            0 :                                   (utils.errors/build-error
     100              :                                    :postgres
     101            0 :                                    {:code    :error.group-participant/upsert-failed
     102            0 :                                     :message (str
     103              :                                               "Failed to update group participant: "
     104            0 :                                               error-message)})))]
     105            5 :         (if (either/left? outcome) (throw (ex-info "tx-rollback" {::result outcome})) outcome)))
     106              :      (catch clojure.lang.ExceptionInfo e
     107            0 :        (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))))
        

Generated by: LCOV version 2.0-1