LCOV - code coverage report
Current view: top level - matchscore/application/class - copy_participants.clj Coverage Total Hit
Test: lcov.info Lines: 90.7 % 43 39
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.application.class.copy-participants
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.domain.class-participant.query :as class-participant-query]
       6              :    [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
       7              :    [matchscore.domain.class.query :as class-query]
       8              :    [matchscore.utils.errors :as utils.errors])
       9              :   (:import
      10              :    (java.time Instant)))
      11              : 
      12              : 
      13            4 : (defn- find-class-for-event-or-fail
      14              :   [repos class-uuid event-uuid]
      15            7 :   (let [class (class-query/get-class (:class-postgres-repo repos) class-uuid)]
      16            7 :     (cond (nil? class)
      17            2 :           (either/left
      18            3 :            (utils.errors/build-error
      19              :             :check
      20            5 :             {:code    :error.class/not-found
      21              :              :message "Class not found."}))
      22              : 
      23            6 :           (not= event-uuid (:event-id class))
      24            0 :           (either/left
      25            0 :            (utils.errors/build-error
      26              :             :check
      27            0 :             {:code    :error.class/not-found
      28              :              :message "Class not found."}))
      29              : 
      30            3 :           :else (either/right class))))
      31              : 
      32              : 
      33            4 : (defn execute!
      34              :  "Copies the given participant-ids from source-class-uuid into target-class-uuid.
      35              :    Both classes must belong to event-uuid.
      36              :    Skips participants already in the target class.
      37              :    Respects target max-players limit."
      38              :  [{:keys [repos]
      39              :    :as   _ctx}
      40              :   target-class-uuid
      41              :   source-class-uuid
      42              :   event-uuid
      43              :   participant-ids]
      44           19 :  (c/mlet [target-class (find-class-for-event-or-fail repos target-class-uuid event-uuid)
      45            5 :           _ (find-class-for-event-or-fail repos source-class-uuid event-uuid)]
      46            4 :    (let [repo          (:class-participant-postgres-repo repos)
      47            3 :          candidate-ids (set participant-ids)
      48            2 :          source-parts  (class-participant-query/get-class-participants-by-class
      49            1 :                         repo
      50            1 :                         source-class-uuid)
      51            2 :          target-parts  (class-participant-query/get-class-participants-by-class
      52            1 :                         repo
      53            1 :                         target-class-uuid)
      54            6 :          source-ids    (set (map :participant-id source-parts))
      55            6 :          target-ids    (set (map :participant-id target-parts))
      56              :          ;; only operate on candidates actually present in source
      57            7 :          eligible-ids  (remove target-ids (filter source-ids candidate-ids))
      58            3 :          max-players   (:max-players target-class)
      59            2 :          current-count (count target-parts)
      60            2 :          to-add-ids    (if max-players
      61            0 :                          (take (max 0 (- max-players current-count)) eligible-ids)
      62            1 :                          eligible-ids)
      63            2 :          now           (Instant/now)
      64            2 :          to-add        (map
      65            1 :                         (fn [pid]
      66           10 :                          {:class-id       target-class-uuid
      67            1 :                           :participant-id pid
      68              :                           :status         "registered"
      69            1 :                           :registered-at  now
      70            1 :                           :extra          {}
      71              :                           :source         "matchscore"})
      72            1 :                         to-add-ids)]
      73           11 :      (c/mlet [{:keys [inserted-count]} (class-participant-postgres-repo/bulk-insert! repo to-add)]
      74            2 :        (c/return
      75            4 :         {:added-count   inserted-count
      76            4 :          :skipped-count (- (count candidate-ids) inserted-count)})))))
        

Generated by: LCOV version 2.0-1