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

            Line data    Source code
       1            4 : (ns matchscore.application.group-participants.fill-from-class
       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.class-participant.repository.postgres :as class-participant-postgres-repo]
       8              :    [matchscore.domain.group-participant.repository.postgres :as group-participant-postgres-repo]
       9              :    [matchscore.domain.group.repository.postgres :as group-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 execute!
      17              :  "Fills empty group participant slots with class participants not used in the same draw.
      18              :   Existing group participant rows are reused. Returns Either<Error, seq<GroupParticipant>>."
      19              :  [{:keys [repos pg]} group-uuid draw-uuid]
      20           26 :  (c/mlet [_ (helpers/find-group-or-fail repos group-uuid draw-uuid)
      21            5 :           draw  (helpers/find-draw-or-fail repos draw-uuid)
      22            6 :           stage (helpers/find-stage-or-fail repos (:stage-id draw))]
      23            1 :    (try
      24            4 :      (jdbc/with-transaction
      25            1 :       [tx pg]
      26            7 :       (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
      27            3 :             tx-group-participant-repo (group-participant-postgres-repo/make-repository tx-opts)
      28            5 :             tx-repos (assoc repos
      29            1 :                       :group-participant-postgres-repo tx-group-participant-repo
      30            3 :                       :match-postgres-repo (match-postgres-repo/make-repository tx-opts))
      31            3 :             class-participant-repo (:class-participant-postgres-repo repos)
      32            3 :             group-repo (:group-postgres-repo repos)
      33            3 :             class-uuid (:class-id stage)
      34              :             class-participants
      35            4 :             (class-participant-postgres-repo/find-by-class-id class-participant-repo class-uuid)
      36            4 :             all-groups (group-postgres-repo/find-by-draw-id group-repo draw-uuid)
      37            4 :             group-uuids (mapv :uuid all-groups)
      38            2 :             used-group-participants (group-participant-postgres-repo/find-by-group-ids
      39            1 :                                      tx-group-participant-repo
      40            1 :                                      group-uuids)
      41            7 :             unavailable-ids (into #{} (keep :participant-id) used-group-participants)
      42              :             current-participants
      43            4 :             (group-participant-postgres-repo/find-by-group-id tx-group-participant-repo group-uuid)
      44            2 :             empty-slots (->>
      45            1 :                           current-participants
      46            9 :                           (filter #(nil? (:participant-id %)))
      47            1 :                           (sort-by :slot))
      48            1 :             to-add-ids (->>
      49            1 :                          class-participants
      50            3 :                          (map :participant-id)
      51            2 :                          (remove unavailable-ids))
      52            2 :             rows (map
      53            1 :                   (fn [[empty-slot participant-id]]
      54            6 :                    (assoc empty-slot
      55            1 :                     :participant-id participant-id
      56              :                     :status         "active"))
      57            5 :                   (map vector empty-slots to-add-ids))
      58              :             result
      59            6 :             (cond (empty? empty-slots) (either/right [])
      60              : 
      61            3 :                   (empty? to-add-ids)
      62            0 :                   (either/left
      63            0 :                    (utils.errors/build-error
      64              :                     :postgres
      65            0 :                     {:code    :error.group-participant/no-class-participants-available
      66              :                      :message "No class participants available to fill the group"}))
      67              : 
      68              :                   :else
      69            3 :                   (let [[ok? results error-message] (group-participant-postgres-repo/bulk-upsert!
      70            1 :                                                      tx-group-participant-repo
      71            1 :                                                      rows)]
      72            2 :                     (if ok?
      73           11 :                       (c/mlet [_ (sync-matches/execute! tx-repos group-uuid)]
      74            3 :                         (either/right results))
      75            0 :                       (either/left
      76            0 :                        (utils.errors/build-error
      77              :                         :postgres
      78            0 :                         {:code    :error.group-participant/upsert-failed
      79            0 :                          :message (str "Failed to fill group participants: " error-message)})))))]
      80            5 :         (if (either/left? result) (throw (ex-info "tx-rollback" {::result result})) result)))
      81              :      (catch clojure.lang.ExceptionInfo e
      82            0 :        (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))))
        

Generated by: LCOV version 2.0-1