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

            Line data    Source code
       1            4 : (ns matchscore.application.class-template.swap-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            2 :           (either/left
      22            3 :            (utils.errors/build-error
      23              :             :check
      24            5 :             {:code    :error.class-template/not-found
      25              :              :message "Class template not found."}))
      26              : 
      27            6 :           (not= template-uuid (:template-id class-template))
      28            0 :           (either/left
      29            0 :            (utils.errors/build-error
      30              :             :check
      31            0 :             {: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              :  "Swaps participants between class-template-a-uuid and class-template-b-uuid, both under template-uuid.
      39              :    class-a-participant-ids are moved from A to B; class-b-participant-ids are moved from B to A.
      40              :    Only IDs actually present in their respective class template are moved; others are skipped.
      41              :    Each direction is capped by the receiving class template's available capacity."
      42              :  [{:keys [repos]
      43              :    :as   _ctx}
      44              :   class-template-a-uuid
      45              :   class-template-b-uuid
      46              :   template-uuid
      47              :   class-a-participant-ids
      48              :   class-b-participant-ids]
      49           16 :  (c/mlet [class-template-a (find-class-template-for-template-or-fail
      50            1 :                             repos
      51            1 :                             class-template-a-uuid
      52            1 :                             template-uuid)
      53            2 :           class-template-b (find-class-template-for-template-or-fail
      54            1 :                             repos
      55            1 :                             class-template-b-uuid
      56            1 :                             template-uuid)]
      57            4 :    (let [repo            (:class-template-participant-postgres-repo repos)
      58            2 :          parts-a         (class-template-participant-query/get-participants-by-class-template-id
      59            1 :                           repo
      60            1 :                           class-template-a-uuid)
      61            2 :          parts-b         (class-template-participant-query/get-participants-by-class-template-id
      62            1 :                           repo
      63            1 :                           class-template-b-uuid)
      64            6 :          ids-a           (set (map :participant-id parts-a))
      65            6 :          ids-b           (set (map :participant-id parts-b))
      66            6 :          to-b-candidates (vec (filter ids-a class-a-participant-ids))
      67            6 :          to-a-candidates (vec (filter ids-b class-b-participant-ids))
      68            1 :          skipped-count   (+
      69            5 :                           (- (count class-a-participant-ids) (count to-b-candidates))
      70            5 :                           (- (count class-b-participant-ids) (count to-a-candidates)))
      71            4 :          space-in-a      (when (:max-players class-template-a)
      72            0 :                            (max
      73              :                             0
      74            0 :                             (+
      75            0 :                              (- (:max-players class-template-a) (count parts-a))
      76            0 :                              (count to-b-candidates))))
      77            4 :          space-in-b      (when (:max-players class-template-b)
      78            0 :                            (max
      79              :                             0
      80            0 :                             (+
      81            0 :                              (- (:max-players class-template-b) (count parts-b))
      82            0 :                              (count to-a-candidates))))
      83            5 :          to-a-ids        (vec (if space-in-a (take space-in-a to-a-candidates) to-a-candidates))
      84            5 :          to-b-ids        (vec (if space-in-b (take space-in-b to-b-candidates) to-b-candidates))
      85            2 :          now             (Instant/now)
      86            1 :          make-record     (fn [class-template-uuid pid]
      87           11 :                           {:class-template-id class-template-uuid
      88            1 :                            :participant-id pid
      89              :                            :status         "registered"
      90            1 :                            :registered-at  now
      91            1 :                            :roles          []
      92            1 :                            :extra          {}
      93              :                            :source         "matchscore"})]
      94           25 :      (c/mlet
      95            5 :        [_ (class-template-participant-postgres-repo/bulk-insert!
      96            1 :            repo
      97            8 :            (map #(make-record class-template-a-uuid %) to-a-ids))
      98            4 :         _ (class-template-participant-postgres-repo/bulk-insert!
      99            1 :            repo
     100            8 :            (map #(make-record class-template-b-uuid %) to-b-ids))
     101            3 :         _ (class-template-participant-postgres-repo/bulk-delete-by-class-template-and-participants!
     102            1 :            repo
     103            1 :            class-template-a-uuid
     104            1 :            to-b-ids)
     105            2 :         _ (class-template-participant-postgres-repo/bulk-delete-by-class-template-and-participants!
     106            1 :            repo
     107            1 :            class-template-b-uuid
     108            1 :            to-a-ids)]
     109            2 :        (c/return
     110            6 :         {:moved-to-a-count (count to-a-ids)
     111            2 :          :moved-to-b-count (count to-b-ids)
     112            1 :          :skipped-count    skipped-count})))))
        

Generated by: LCOV version 2.0-1