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

            Line data    Source code
       1            4 : (ns matchscore.application.class.add-participant
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.domain.class-participant.command :as class-participant-command]
       6              :    [matchscore.domain.class.query :as class-query]
       7              :    [matchscore.domain.event-participant.command :as event-participant-command]
       8              :    [matchscore.domain.event-participant.repository.postgres :as event-participant-postgres-repo]
       9              :    [matchscore.domain.participant.repository.postgres :as participant-postgres-repo]
      10              :    [matchscore.domain.user.repository.postgres :as user-postgres-repo]
      11              :    [matchscore.telegram.notifications-trigger :as notifications-trigger]
      12              :    [matchscore.utils.errors :as utils.errors])
      13              :   (:import
      14              :    (java.time Instant)))
      15              : 
      16              : 
      17            4 : (defn- find-user-or-fail
      18              :   [repos user-uuid]
      19           44 :   (if-let [user (user-postgres-repo/find-by-id (:user-postgres-repo repos) user-uuid)]
      20           12 :     (either/right user)
      21            2 :     (either/left
      22            3 :      (utils.errors/build-error
      23              :       :check
      24            5 :       {:code    :error.event-participant/user-not-found
      25              :        :message "User not found."}))))
      26              : 
      27              : 
      28            4 : (defn- find-class-or-fail
      29              :   [repos class-uuid event-uuid]
      30           28 :   (let [class (class-query/get-class (:class-postgres-repo repos) class-uuid)]
      31           28 :     (cond (nil? class)
      32            0 :           (either/left
      33            0 :            (utils.errors/build-error
      34              :             :check
      35            0 :             {:code    :error.class/not-found
      36              :              :message "Class not found."}))
      37              : 
      38           24 :           (not= event-uuid (:event-id class))
      39            2 :           (either/left
      40            3 :            (utils.errors/build-error
      41              :             :check
      42            5 :             {:code    :error.class/not-found
      43              :              :message "Class not found."}))
      44              : 
      45           12 :           :else (either/right class))))
      46              : 
      47              : 
      48            4 : (defn- ensure-event-participant!
      49              :   "Returns the existing event participant when the participant is already registered
      50              :    for the event, otherwise registers it with `roles`. Returns Either<Error, EventParticipant>."
      51              :   [{:keys [repos]
      52              :     :as   ctx}
      53              :    event-uuid
      54              :    participant-id
      55              :    roles]
      56           16 :   (let [repo (:event-participant-postgres-repo repos)]
      57           38 :     (if-let [existing (event-participant-postgres-repo/find-by-id repo event-uuid participant-id)]
      58            9 :       (either/right existing)
      59            8 :       (event-participant-command/register-participant!
      60            4 :        ctx
      61           52 :        {:event-id       event-uuid
      62            4 :         :participant-id participant-id
      63              :         :status         "registered"
      64            4 :         :roles          roles
      65            4 :         :extra          {}
      66            8 :         :registered-at  (Instant/now)
      67              :         :attendance-confirmed false
      68              :         :source         "matchscore"}))))
      69              : 
      70              : 
      71            4 : (defn execute!
      72              :  "Resolves the user's participant record (creating it when missing), registers it for
      73              :    the event first if it isn't registered yet, then links it to the given event class.
      74              :    `:roles` (default `[\"player\"]`) only applies when the event registration is new.
      75              :    Returns Either<Error, {:event-participant EventParticipant :class-participant ClassParticipant}>."
      76           28 :  ([ctx event-uuid class-uuid user-uuid] (execute! ctx event-uuid class-uuid user-uuid nil))
      77              :  ([{:keys [repos]
      78              :     :as   ctx}
      79              :    event-uuid
      80              :    class-uuid
      81              :    user-uuid
      82              :    {:keys [roles]
      83              :     :or   {roles ["player"]}}]
      84            4 :   (let [result
      85          160 :         (c/mlet [_ (find-class-or-fail repos class-uuid event-uuid)
      86           28 :                  _ (find-user-or-fail repos user-uuid)
      87           16 :                  participant (participant-postgres-repo/ensure-person-participant-for-user!
      88           12 :                               (:participant-postgres-repo repos)
      89            4 :                               user-uuid)
      90            4 :                  event-participant
      91           32 :                  (ensure-event-participant! ctx event-uuid (:uuid participant) roles)
      92            8 :                  class-participant (class-participant-command/register-participant!
      93            4 :                                     ctx
      94           40 :                                     {:class-id       class-uuid
      95           12 :                                      :participant-id (:uuid participant)
      96              :                                      :status         "registered"
      97            8 :                                      :registered-at  (Instant/now)
      98            4 :                                      :extra          {}
      99              :                                      :source         "matchscore"})]
     100            8 :           (c/return
     101           16 :            {:event-participant event-participant
     102            4 :             :class-participant class-participant}))]
     103           36 :     (when (either/right? result) (notifications-trigger/trigger-debounced! ctx event-uuid))
     104            4 :     result)))
        

Generated by: LCOV version 2.0-1