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

            Line data    Source code
       1            4 : (ns matchscore.application.class-template.move-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              :  "Moves the given participant-ids from source-class-template-uuid into target-class-template-uuid.
      39              :    Both class templates must belong to template-uuid.
      40              :    Participants already in the target class template (shared) are skipped entirely.
      41              :    Eligible participants are added to target and removed from source,
      42              :    subject to target max-players capacity."
      43              :  [{:keys [repos]
      44              :    :as   _ctx}
      45              :   target-class-template-uuid
      46              :   source-class-template-uuid
      47              :   template-uuid
      48              :   participant-ids]
      49           16 :  (c/mlet [target-class-template (find-class-template-for-template-or-fail
      50            1 :                                  repos
      51            1 :                                  target-class-template-uuid
      52            1 :                                  template-uuid)
      53              :           _
      54            5 :           (find-class-template-for-template-or-fail repos source-class-template-uuid template-uuid)]
      55            4 :    (let [repo          (:class-template-participant-postgres-repo repos)
      56            3 :          candidate-ids (set participant-ids)
      57            2 :          source-parts  (class-template-participant-query/get-participants-by-class-template-id
      58            1 :                         repo
      59            1 :                         source-class-template-uuid)
      60            2 :          target-parts  (class-template-participant-query/get-participants-by-class-template-id
      61            1 :                         repo
      62            1 :                         target-class-template-uuid)
      63            6 :          source-ids    (set (map :participant-id source-parts))
      64            6 :          target-ids    (set (map :participant-id target-parts))
      65              :          ;; only operate on candidates actually present in source and not yet in target
      66            7 :          eligible-ids  (remove target-ids (filter source-ids candidate-ids))
      67            3 :          max-players   (:max-players target-class-template)
      68            2 :          current-count (count target-parts)
      69            2 :          to-move-ids   (vec
      70            2 :                         (if max-players
      71            8 :                           (take (max 0 (- max-players current-count)) eligible-ids)
      72            1 :                           eligible-ids))
      73            2 :          now           (Instant/now)
      74            2 :          to-add        (map
      75            1 :                         (fn [pid]
      76           11 :                          {:class-template-id target-class-template-uuid
      77            1 :                           :participant-id pid
      78              :                           :status         "registered"
      79            1 :                           :registered-at  now
      80            1 :                           :roles          []
      81            1 :                           :extra          {}
      82              :                           :source         "matchscore"})
      83            1 :                         to-move-ids)]
      84           14 :      (c/mlet
      85            4 :        [{:keys [inserted-count]} (class-template-participant-postgres-repo/bulk-insert! repo to-add)
      86            2 :         _ (class-template-participant-postgres-repo/bulk-delete-by-class-template-and-participants!
      87            1 :            repo
      88            1 :            source-class-template-uuid
      89            1 :            to-move-ids)]
      90            2 :        (c/return
      91            4 :         {:moved-count   inserted-count
      92            4 :          :skipped-count (- (count candidate-ids) inserted-count)})))))
        

Generated by: LCOV version 2.0-1