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

            Line data    Source code
       1            4 : (ns matchscore.application.event.end
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [honey.sql :as sql]
       6              :    [matchscore.domain.event.command :as event-cmd]
       7              :    [matchscore.domain.event.repository.postgres :as event-postgres-repo]
       8              :    [matchscore.utils.errors :as utils.errors]
       9              :    [next.jdbc :as jdbc]))
      10              : 
      11              : 
      12            4 : (defn- find-event-or-fail
      13              :   [repo event-uuid]
      14           10 :   (if-let [event (event-postgres-repo/find-by-id repo event-uuid nil)]
      15            3 :     (either/right event)
      16            2 :     (either/left
      17            3 :      (utils.errors/build-error
      18              :       :check
      19            5 :       {:code    :error.event/not-found
      20              :        :message "Event not found."}))))
      21              : 
      22              : 
      23            4 : (defn- check-all-matches-terminal
      24              :   "Returns either/right if all matches for the event are in a terminal state,
      25              :    or either/left if any match is still scheduled, in_progress, or postponed.
      26              : 
      27              :    Matches missing a participant on either side are ignored: a slot stays nil
      28              :    for byes, empty group slots (a 3-player group in a 4-slot group), and
      29              :    placement-bracket positions that never get filled (TBD vs TBD). Such matches
      30              :    can never be played, so requiring both participants to be present keeps them
      31              :    from blocking event completion."
      32              :   [pg event-uuid]
      33           11 :   (let [query {:select [[[:count :*] :cnt]]
      34            2 :                :from   [:matches]
      35            2 :                :where  [:and
      36            7 :                         [:= :event-id [:cast event-uuid :uuid]]
      37            7 :                         [:in :state ["scheduled" "in_progress" "postponed"]]
      38            4 :                         [:is-not :home-participant-id nil]
      39            4 :                         [:is-not :away-participant-id nil]]}
      40            6 :         row   (jdbc/execute-one! pg (sql/format query))
      41            7 :         cnt   (or (:cnt row) 0)]
      42            3 :     (if (pos? cnt)
      43            2 :       (either/left
      44            3 :        (utils.errors/build-error
      45              :         :check
      46            6 :         {:code    :error.event/matches-not-completed
      47              :          :message "Not all matches are played."
      48            4 :          :context {:event-uuid     event-uuid
      49            1 :                    :unplayed-count cnt}}))
      50            3 :       (either/right nil))))
      51              : 
      52              : 
      53            4 : (defn execute!
      54              :  "Ends an event by validating all its matches are in a terminal state, then
      55              :    transitioning the event to 'completed'. Schedules Telegram cleanup and
      56              :    completion-notification jobs as a side effect via update-event-state!.
      57              :    Returns Either<Error, {:code <keyword> :data <Event>}>."
      58              :  [{:keys [repos pg]
      59              :    :as   ctx}
      60              :   event-uuid
      61              :   user-uuid]
      62            4 :  (let [event-repo (:event-postgres-repo repos)
      63            2 :        check      (c/>>=
      64            4 :                    (find-event-or-fail event-repo event-uuid)
      65            1 :                    (fn [_]
      66            4 :                     (check-all-matches-terminal pg event-uuid)))]
      67            4 :    (if (either/right? check)
      68            3 :      (event-cmd/update-event-state!
      69            3 :       (update
      70            1 :        ctx
      71              :        :pg
      72            1 :        (fn [ds]
      73            3 :         {:datasource ds}))
      74            5 :       {:uuid  event-uuid
      75              :        :state "completed"}
      76              :       :user-uuid
      77            1 :       user-uuid)
      78            1 :      check)))
        

Generated by: LCOV version 2.0-1