LCOV - code coverage report
Current view: top level - matchscore/api - draws.clj Coverage Total Hit
Test: lcov.info Lines: 83.7 % 86 72
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.api.draws
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.application.draw.create-by-type :as draw-create-by-type-app]
       6              :    [matchscore.db.postgres.telegram-match-notifications :as notification-db]
       7              :    [matchscore.domain.draw.command :as draw-cmd]
       8              :    [matchscore.domain.draw.query :as draw-query]
       9              :    [matchscore.jobs.telegram-match-notifications :as match-notification-jobs]
      10              :    [matchscore.utils.nanoid :as nanoid]
      11              :    [matchscore.utils.uuid :as uuid-utils]
      12              :    [ring.util.http-status :as status]
      13              :    [schemas.api.core :as api-schemas]))
      14              : 
      15              : 
      16           16 : (def ^:private structured-draw-types #{"group" "bracket"})
      17              : 
      18              : 
      19            4 : (defn- schedule-stage-sync!
      20              :   [ctx stage-id]
      21           12 :   (when-let [event-id (notification-db/event-id-for-stage (:pg ctx) stage-id)]
      22            6 :     (match-notification-jobs/schedule-event-sync! (:goose ctx) event-id)))
      23              : 
      24              : 
      25            4 : (defn- -draw-error-status
      26              :   [code]
      27            0 :   (case code
      28            0 :     :error.draw/conflict-id   status/conflict
      29            0 :     :error.draw/not-found     status/not-found
      30            0 :     :error.draw/upsert-failed status/internal-server-error
      31            0 :     status/bad-request))
      32              : 
      33              : 
      34              : ;; Handlers
      35            4 : (defn draws-create-handler
      36              :  {:malli/schema     [:=>
      37              :                      [:cat api-schemas/RingRequest api-schemas/DrawRequestSchema]
      38              :                      api-schemas/DrawResponseSchema]
      39              :   :malli/parameters {:path api-schemas/StageUuidPathParamsSchema}
      40              :   :authz            {:obj :event
      41              :                      :act :update
      42              :                      :via :stage
      43              :                      :id  :route-params/stage-uuid}
      44              :   :summary          "Create draw"
      45              :   :description      "Creates a new draw for a stage"}
      46              :  [{:keys [ctx route-params]
      47              :    :as   _request}
      48              :   draw-data]
      49            4 :  (let [stage-uuid (:stage-uuid route-params)
      50            3 :        draw-type  (:type draw-data)]
      51            5 :    (if (contains? structured-draw-types draw-type)
      52            3 :      (let [result (draw-create-by-type-app/execute!
      53            1 :                    ctx
      54            1 :                    stage-uuid
      55            5 :                    {:type   draw-type
      56            3 :                     :name   (:name draw-data)
      57            7 :                     :config (or (:config draw-data) {})})]
      58            4 :        (if (either/right? result)
      59            1 :          (do
      60            4 :            (schedule-stage-sync! ctx stage-uuid)
      61            7 :            {:body   {:message "Draw created successfully."
      62            5 :                      :data    (:draw (c/extract result))}
      63            1 :             :status status/created})
      64            0 :          (let [{:keys [code message]} (c/extract result)]
      65            0 :            {:body   {:message message
      66              :                      :data    nil}
      67            0 :             :status (-draw-error-status code)})))
      68            3 :      (let [draw-data (->
      69            1 :                        draw-data
      70            5 :                        (assoc :uuid (uuid-utils/generate-uuid7-str))
      71            6 :                        (assoc :public-id (nanoid/generate 15))
      72            4 :                        (assoc :stage-id stage-uuid)
      73            9 :                        (update :config #(or % {}))
      74            7 :                        (update :source #(or % "matchscore")))
      75            4 :            result    (draw-cmd/create-draw! ctx draw-data)]
      76            4 :        (if (either/right? result)
      77            1 :          (do
      78            4 :            (schedule-stage-sync! ctx stage-uuid)
      79            7 :            {:body   {:message "Draw created successfully."
      80            3 :                      :data    (c/extract result)}
      81            1 :             :status status/created})
      82            0 :          (let [{:keys [code message]} (c/extract result)]
      83            0 :            {:body   {:message message
      84              :                      :data    nil}
      85            0 :             :status (-draw-error-status code)}))))))
      86              : 
      87              : 
      88            4 : (defn draws-update-handler
      89              :  {:malli/schema     [:=>
      90              :                      [:cat api-schemas/RingRequest api-schemas/DrawRequestSchema]
      91              :                      api-schemas/DrawResponseSchema]
      92              :   :malli/parameters {:path api-schemas/DrawAndStagePathParamsSchema}
      93              :   :authz            {:obj :event
      94              :                      :act :update
      95              :                      :via :stage
      96              :                      :id  :route-params/stage-uuid}
      97              :   :summary          "Update draw"
      98              :   :description      "Updates an existing draw"}
      99              :  [{:keys [ctx route-params]
     100              :    :as   _request}
     101              :   draw-data]
     102            4 :  (let [stage-uuid (:stage-uuid route-params)
     103            3 :        draw-uuid  (:draw-uuid route-params)
     104            5 :        draw-data  (assoc draw-data
     105            1 :                    :uuid     draw-uuid
     106            1 :                    :stage-id stage-uuid)
     107            4 :        result     (draw-cmd/update-draw! ctx draw-data)]
     108            4 :    (if (either/right? result)
     109            4 :      (let [{:keys [code data]} (c/extract result)]
     110            4 :        (schedule-stage-sync! ctx stage-uuid)
     111            3 :        (case code
     112            7 :          :success.draw/ok         {:body   {:message "Draw updated successfully."
     113            1 :                                             :data    data}
     114            1 :                                    :status status/ok}
     115              : 
     116            7 :          :success.draw/no-changes {:body   {:message "No changes detected."
     117            1 :                                             :data    data}
     118            1 :                                    :status status/ok}))
     119            4 :      (let [{:keys [code message]} (c/extract result)]
     120            8 :        {:body   {:message message
     121              :                  :data    nil}
     122            3 :         :status (case code
     123            0 :                   :error.draw/conflict-id   status/conflict
     124            1 :                   :error.draw/not-found     status/not-found
     125            0 :                   :error.draw/upsert-failed status/internal-server-error
     126            0 :                   status/bad-request)}))))
     127              : 
     128              : 
     129            4 : (defn draws-get-by-stage-handler
     130              :  {:malli/schema     [:=> [:cat api-schemas/RingRequest] api-schemas/DrawListResponseSchema]
     131              :   :malli/parameters {:path api-schemas/StageUuidPathParamsSchema}
     132              :   :authz            {:obj :event
     133              :                      :act :read
     134              :                      :via :stage
     135              :                      :id  :route-params/stage-uuid}
     136              :   :summary          "Get draws by stage"
     137              :   :description      "Retrieves all draws for a specific stage"}
     138              :  [{:keys [repos route-params]
     139              :    :as   _request}]
     140            9 :  (let [draws (draw-query/get-draws-by-stage (:draw-postgres-repo repos) (:stage-uuid route-params))]
     141            7 :    {:body   {:message "Draws retrieved successfully."
     142            1 :              :data    draws}
     143            1 :     :status status/ok}))
     144              : 
     145              : 
     146            4 : (defn draws-delete-handler
     147              :  {:malli/schema     [:=> [:cat api-schemas/RingRequest] api-schemas/DrawDeleteResponseSchema]
     148              :   :malli/parameters {:path api-schemas/DrawAndStagePathParamsSchema}
     149              :   :authz            {:obj :event
     150              :                      :act :update
     151              :                      :via :stage
     152              :                      :id  :route-params/stage-uuid}
     153              :   :summary          "Delete draw"
     154              :   :description      "Deletes a draw by UUID"}
     155              :  [{:keys [ctx route-params]
     156              :    :as   _request}]
     157           10 :  (let [result (draw-cmd/delete-draw! ctx (:draw-uuid route-params) (:stage-uuid route-params))]
     158            4 :    (if (either/right? result)
     159            1 :      (do
     160            6 :        (schedule-stage-sync! ctx (:stage-uuid route-params))
     161            6 :        {:body   {:message "Draw deleted successfully."}
     162            1 :         :status status/ok})
     163            4 :      (let [error (c/extract result)]
     164            8 :        {:body   {:message (:message error)}
     165            1 :         :status status/not-found}))))
        

Generated by: LCOV version 2.0-1