LCOV - code coverage report
Current view: top level - matchscore/api - stages.clj Coverage Total Hit
Test: lcov.info Lines: 85.9 % 99 85
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.api.stages
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.application.advancement.create :as advancement-create-app]
       6              :    [matchscore.application.stage.create-by-template :as stage-create-by-template-app]
       7              :    [matchscore.db.postgres.telegram-match-notifications :as notification-db]
       8              :    [matchscore.domain.stage.command :as stage-cmd]
       9              :    [matchscore.domain.stage.query :as stage-query]
      10              :    [matchscore.jobs.telegram-match-notifications :as match-notification-jobs]
      11              :    [matchscore.utils.nanoid :as nanoid]
      12              :    [matchscore.utils.uuid :as uuid-utils]
      13              :    [ring.util.http-status :as status]
      14              :    [schemas.api.core :as api-schemas]))
      15              : 
      16              : 
      17            4 : (defn- schedule-class-sync!
      18              :   [ctx class-id]
      19           24 :   (when-let [event-id (notification-db/event-id-for-class (:pg ctx) class-id)]
      20           12 :     (match-notification-jobs/schedule-event-sync! (:goose ctx) event-id)))
      21              : 
      22              : 
      23              : ;; Handlers
      24            4 : (defn stages-create-handler
      25              :  {:malli/schema     [:=>
      26              :                      [:cat api-schemas/RingRequest api-schemas/StageRequestSchema]
      27              :                      api-schemas/StageResponseSchema]
      28              :   :malli/parameters {:path api-schemas/ClassUuidPathParamsSchema}
      29              :   :authz            {:obj :event
      30              :                      :act :update
      31              :                      :via :class
      32              :                      :id  :route-params/class-uuid}
      33              :   :summary          "Create stage"
      34              :   :description      "Creates a new stage for a class"}
      35              :  [{:keys [ctx route-params]
      36              :    :as   _request}
      37              :   stage-data]
      38            4 :  (let [class-uuid (:class-uuid route-params)
      39            2 :        stage-data (->
      40            1 :                     stage-data
      41            5 :                     (assoc :uuid (uuid-utils/generate-uuid7-str))
      42            6 :                     (assoc :public-id (nanoid/generate 15))
      43            4 :                     (assoc :class-id class-uuid)
      44            9 :                     (update :state #(or % "pending"))
      45            7 :                     (update :source #(or % "matchscore")))
      46            4 :        result     (stage-cmd/create-stage! ctx stage-data)]
      47            4 :    (if (either/right? result)
      48            1 :      (do
      49            4 :        (schedule-class-sync! ctx class-uuid)
      50            7 :        {:body   {:message "Stage created successfully."
      51            3 :                  :data    (c/extract result)}
      52            1 :         :status status/created})
      53            0 :      (let [error (c/extract result)]
      54            0 :        {:body   {:message (:message error)
      55              :                  :data    nil}
      56            0 :         :status status/bad-request}))))
      57              : 
      58              : 
      59            4 : (defn stages-create-by-template-handler
      60              :  {:malli/schema     [:=>
      61              :                      [:cat api-schemas/RingRequest api-schemas/StageByTemplateRequestSchema]
      62              :                      api-schemas/StagesByTemplateResponseSchema]
      63              :   :malli/parameters {:path api-schemas/ClassUuidPathParamsSchema}
      64              :   :authz            {:obj :event
      65              :                      :act :update
      66              :                      :via :class
      67              :                      :id  :route-params/class-uuid}
      68              :   :summary          "Create stages by template"
      69              :   :description      "Creates qualification and main-draw stages for a class using a named template"}
      70              :  [{:keys [ctx route-params]
      71              :    :as   _request}
      72              :   template-data]
      73            8 :  (let [class-uuid    (:class-uuid route-params)
      74            4 :        template-data (->
      75            2 :                        template-data
      76           18 :                        (update :qualification-draw-name #(or % "Qualification stage"))
      77           14 :                        (update :main-draw-name #(or % "Main draw")))
      78           10 :        result        (stage-create-by-template-app/execute! ctx class-uuid template-data)]
      79            8 :    (if (either/right? result)
      80            2 :      (do
      81            8 :        (schedule-class-sync! ctx class-uuid)
      82           14 :        {:body   {:message "Stages created successfully."
      83            6 :                  :data    (c/extract result)}
      84            2 :         :status status/created})
      85            4 :      (let [error (c/extract result)]
      86           10 :        {:body   {:message (:message error)
      87              :                  :data    nil}
      88            1 :         :status status/bad-request}))))
      89              : 
      90              : 
      91            4 : (defn stages-update-handler
      92              :  {:malli/schema     [:=>
      93              :                      [:cat api-schemas/RingRequest api-schemas/StageRequestSchema]
      94              :                      api-schemas/StageResponseSchema]
      95              :   :malli/parameters {:path api-schemas/StageAndClassPathParamsSchema}
      96              :   :authz            {:obj :event
      97              :                      :act :update
      98              :                      :via :class
      99              :                      :id  :route-params/class-uuid}
     100              :   :summary          "Update stage"
     101              :   :description      "Updates an existing stage"}
     102              :  [{:keys [ctx route-params]
     103              :    :as   _request}
     104              :   stage-data]
     105            4 :  (let [class-uuid (:class-uuid route-params)
     106            3 :        stage-uuid (:stage-uuid route-params)
     107            5 :        stage-data (assoc stage-data
     108            1 :                    :uuid     stage-uuid
     109            1 :                    :class-id class-uuid)
     110            4 :        result     (stage-cmd/update-stage! ctx stage-data)]
     111            4 :    (if (either/right? result)
     112            4 :      (let [{:keys [code data]} (c/extract result)]
     113            4 :        (schedule-class-sync! ctx class-uuid)
     114            3 :        (case code
     115            7 :          :success.stage/ok         {:body   {:message "Stage updated successfully."
     116            1 :                                              :data    data}
     117            1 :                                     :status status/ok}
     118              : 
     119            0 :          :success.stage/no-changes {:body   {:message "No changes detected."
     120            0 :                                              :data    data}
     121            0 :                                     :status status/ok}))
     122            4 :      (let [error (c/extract result)]
     123           10 :        {:body   {:message (:message error)
     124              :                  :data    nil}
     125            1 :         :status status/not-found}))))
     126              : 
     127              : 
     128            4 : (defn stages-get-by-class-handler
     129              :  {:malli/schema     [:=> [:cat api-schemas/RingRequest] api-schemas/StageListResponseSchema]
     130              :   :malli/parameters {:path api-schemas/ClassUuidPathParamsSchema}
     131              :   :authz            {:obj :event
     132              :                      :act :read
     133              :                      :via :class
     134              :                      :id  :route-params/class-uuid}
     135              :   :summary          "Get stages by class"
     136              :   :description      "Retrieves all stages for a specific class"}
     137              :  [{:keys [repos route-params]
     138              :    :as   _request}]
     139            1 :  (let [stages
     140            8 :        (stage-query/get-stages-by-class (:stage-postgres-repo repos) (:class-uuid route-params))]
     141            7 :    {:body   {:message "Stages retrieved successfully."
     142            1 :              :data    stages}
     143            1 :     :status status/ok}))
     144              : 
     145              : 
     146            4 : (defn- play-error->status
     147              :   [code]
     148            0 :   (cond (contains?
     149            0 :          #{:error.stage/not-found :error.class/not-found :error.advancement/target-group-not-found}
     150            0 :          code)
     151            0 :         status/not-found
     152              : 
     153            0 :         :else status/bad-request))
     154              : 
     155              : 
     156            4 : (defn stages-play-handler
     157              :  {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/StagePlayResponseSchema]
     158              :   :malli/parameters {:path api-schemas/StageAndClassPathParamsSchema}
     159              :   :authz {:obj :event
     160              :           :act :update
     161              :           :via :class
     162              :           :id  :route-params/class-uuid}
     163              :   :summary "Play stage"
     164              :   :description "Marks the stage as completed and generates advancements from advancement rules"}
     165              :  [{:keys [ctx route-params]
     166              :    :as   _request}]
     167            4 :  (let [stage-uuid (:stage-uuid route-params)
     168            3 :        class-uuid (:class-uuid route-params)
     169           16 :        result     (c/mlet [_ (stage-cmd/update-stage!
     170            1 :                               ctx
     171            6 :                               {:uuid     stage-uuid
     172            1 :                                :class-id class-uuid
     173              :                                :state    "completed"})
     174            4 :                            advancements (advancement-create-app/execute! ctx stage-uuid)]
     175            3 :                     (c/return advancements))]
     176            4 :    (if (either/right? result)
     177            1 :      (do
     178            4 :        (schedule-class-sync! ctx class-uuid)
     179            7 :        {:body   {:message "Stage played successfully."
     180            5 :                  :data    {:advancements (c/extract result)}}
     181            1 :         :status status/ok})
     182            0 :      (let [error (c/extract result)]
     183            0 :        {:body   {:message (:message error)
     184              :                  :data    nil}
     185            0 :         :status (play-error->status (:code error))}))))
     186              : 
     187              : 
     188            4 : (defn stages-delete-handler
     189              :  {:malli/schema     [:=> [:cat api-schemas/RingRequest] api-schemas/StageDeleteResponseSchema]
     190              :   :malli/parameters {:path api-schemas/StageAndClassPathParamsSchema}
     191              :   :authz            {:obj :event
     192              :                      :act :update
     193              :                      :via :class
     194              :                      :id  :route-params/class-uuid}
     195              :   :summary          "Delete stage"
     196              :   :description      "Deletes a stage by UUID"}
     197              :  [{:keys [ctx route-params]
     198              :    :as   _request}]
     199           10 :  (let [result (stage-cmd/delete-stage! ctx (:stage-uuid route-params) (:class-uuid route-params))]
     200            4 :    (if (either/right? result)
     201            1 :      (do
     202            6 :        (schedule-class-sync! ctx (:class-uuid route-params))
     203            6 :        {:body   {:message "Stage deleted successfully."}
     204            1 :         :status status/ok})
     205            4 :      (let [error (c/extract result)]
     206            8 :        {:body   {:message (:message error)}
     207            1 :         :status status/not-found}))))
        

Generated by: LCOV version 2.0-1