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

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

Generated by: LCOV version 2.0-1