LCOV - code coverage report
Current view: top level - matchscore/api - groups.clj Coverage Total Hit
Test: lcov.info Lines: 91.0 % 122 111
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.api.groups
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.application.group-participants.fill-all-from-class
       6              :     :as
       7              :     group-participants-fill-all-app]
       8              :    [matchscore.application.group-participants.fill-from-class :as group-participants-fill-app]
       9              :    [matchscore.application.group-participants.update :as group-participants-set-app]
      10              :    [matchscore.application.group-participants.update-one :as group-participant-update-one-app]
      11              :    [matchscore.db.postgres.telegram-match-notifications :as notification-db]
      12              :    [matchscore.domain.group.command :as group-cmd]
      13              :    [matchscore.domain.group.query :as group-query]
      14              :    [matchscore.jobs.telegram-match-notifications :as match-notification-jobs]
      15              :    [matchscore.utils.nanoid :as nanoid]
      16              :    [matchscore.utils.uuid :as uuid-utils]
      17              :    [ring.util.http-status :as status]
      18              :    [schemas.api.core :as api-schemas]))
      19              : 
      20              : 
      21            4 : (defn- schedule-draw-sync!
      22              :   [ctx draw-id]
      23           12 :   (when-let [event-id (notification-db/event-id-for-draw (:pg ctx) draw-id)]
      24            6 :     (match-notification-jobs/schedule-event-sync! (:goose ctx) event-id)))
      25              : 
      26              : 
      27              : ;; Handlers
      28            4 : (defn groups-create-handler
      29              :  {:malli/schema     [:=>
      30              :                      [:cat api-schemas/RingRequest api-schemas/GroupRequestSchema]
      31              :                      api-schemas/GroupResponseSchema]
      32              :   :malli/parameters {:path api-schemas/DrawUuidPathParamsSchema}
      33              :   :authz            {:obj :event
      34              :                      :act :update
      35              :                      :via :draw
      36              :                      :id  :route-params/draw-uuid}
      37              :   :summary          "Create group"
      38              :   :description      "Creates a new group for a draw"}
      39              :  [{:keys [ctx route-params]
      40              :    :as   _request}
      41              :   group-data]
      42            4 :  (let [draw-uuid  (:draw-uuid route-params)
      43            2 :        group-data (->
      44            1 :                     group-data
      45            5 :                     (assoc :uuid (uuid-utils/generate-uuid7-str))
      46            6 :                     (assoc :public-id (nanoid/generate 15))
      47            4 :                     (assoc :draw-id draw-uuid)
      48            9 :                     (update :config #(or % {}))
      49            7 :                     (update :source #(or % "matchscore")))
      50            4 :        result     (group-cmd/create-group! ctx group-data)]
      51            4 :    (if (either/right? result)
      52            7 :      {:body   {:message "Group created successfully."
      53            3 :                :data    (c/extract result)}
      54            1 :       :status status/created}
      55            0 :      (let [{:keys [code message]} (c/extract result)]
      56            0 :        {:body   {:message message
      57              :                  :data    nil}
      58            0 :         :status (if (= code :error.group/upsert-failed)
      59            0 :                   status/internal-server-error
      60            0 :                   status/bad-request)}))))
      61              : 
      62              : 
      63            4 : (defn groups-update-handler
      64              :  {:malli/schema     [:=>
      65              :                      [:cat api-schemas/RingRequest api-schemas/GroupRequestSchema]
      66              :                      api-schemas/GroupResponseSchema]
      67              :   :malli/parameters {:path api-schemas/GroupAndDrawPathParamsSchema}
      68              :   :authz            {:obj :event
      69              :                      :act :update
      70              :                      :via :draw
      71              :                      :id  :route-params/draw-uuid}
      72              :   :summary          "Update group"
      73              :   :description      "Updates an existing group"}
      74              :  [{:keys [ctx route-params]
      75              :    :as   _request}
      76              :   group-data]
      77            4 :  (let [draw-uuid  (:draw-uuid route-params)
      78            3 :        group-uuid (:group-uuid route-params)
      79            5 :        group-data (assoc group-data
      80            1 :                    :uuid    group-uuid
      81            1 :                    :draw-id draw-uuid)
      82            4 :        result     (group-cmd/update-group! ctx group-data)]
      83            4 :    (if (either/right? result)
      84            4 :      (let [{:keys [code data]} (c/extract result)]
      85            3 :        (case code
      86            7 :          :success.group/ok         {:body   {:message "Group updated successfully."
      87            1 :                                              :data    data}
      88            1 :                                     :status status/ok}
      89              : 
      90            0 :          :success.group/no-changes {:body   {:message "No changes detected."
      91            0 :                                              :data    data}
      92            0 :                                     :status status/ok}))
      93            4 :      (let [{:keys [code message]} (c/extract result)]
      94            8 :        {:body   {:message message
      95              :                  :data    nil}
      96            3 :         :status (case code
      97            1 :                   :error.group/not-found     status/not-found
      98            0 :                   :error.group/upsert-failed status/internal-server-error
      99            0 :                   status/bad-request)}))))
     100              : 
     101              : 
     102            4 : (defn groups-get-by-draw-handler
     103              :  {:malli/schema     [:=> [:cat api-schemas/RingRequest] api-schemas/GroupListResponseSchema]
     104              :   :malli/parameters {:path api-schemas/DrawUuidPathParamsSchema}
     105              :   :authz            {:obj :event
     106              :                      :act :read
     107              :                      :via :draw
     108              :                      :id  :route-params/draw-uuid}
     109              :   :summary          "Get groups by draw"
     110              :   :description      "Retrieves all groups for a specific draw"}
     111              :  [{:keys [repos route-params]
     112              :    :as   _request}]
     113            1 :  (let [groups
     114            8 :        (group-query/get-groups-by-draw (:group-postgres-repo repos) (:draw-uuid route-params))]
     115            7 :    {:body   {:message "Groups retrieved successfully."
     116            1 :              :data    groups}
     117            1 :     :status status/ok}))
     118              : 
     119              : 
     120            4 : (defn groups-delete-handler
     121              :  {:malli/schema     [:=> [:cat api-schemas/RingRequest] api-schemas/GroupDeleteResponseSchema]
     122              :   :malli/parameters {:path api-schemas/GroupAndDrawPathParamsSchema}
     123              :   :authz            {:obj :event
     124              :                      :act :update
     125              :                      :via :draw
     126              :                      :id  :route-params/draw-uuid}
     127              :   :summary          "Delete group"
     128              :   :description      "Deletes a group by UUID"}
     129              :  [{:keys [ctx route-params]
     130              :    :as   _request}]
     131           10 :  (let [result (group-cmd/delete-group! ctx (:group-uuid route-params) (:draw-uuid route-params))]
     132            4 :    (if (either/right? result)
     133            1 :      (do
     134            6 :        (schedule-draw-sync! ctx (:draw-uuid route-params))
     135            6 :        {:body   {:message "Group deleted successfully."}
     136            1 :         :status status/ok})
     137            1 :      (throw
     138            3 :       (ex-info
     139              :        "Group not found"
     140            4 :        {:type    :api-not-found
     141            7 :         :message (str "Group with uuid " (:group-uuid route-params) " not found")})))))
     142              : 
     143              : 
     144            4 : (defn- participants-error->status
     145              :   [code]
     146            5 :   (cond (contains?
     147            5 :          #{:error.group/not-found :error.group-participant/not-found :error.draw/not-found
     148              :            :error.stage/not-found}
     149            1 :          code)
     150            1 :         status/not-found
     151              : 
     152            3 :         (= code :error.group-participant/upsert-failed) status/internal-server-error
     153            4 :         (= code :error.group-participant/no-class-participants-available) status/bad-request
     154            0 :         :else status/bad-request))
     155              : 
     156              : 
     157            4 : (defn groups-set-participants-handler
     158              :  {:malli/schema     [:=>
     159              :                      [:cat api-schemas/RingRequest api-schemas/GroupParticipantsSetRequestSchema]
     160              :                      api-schemas/GroupParticipantsSetResponseSchema]
     161              :   :malli/parameters {:path api-schemas/GroupAndDrawPathParamsSchema}
     162              :   :authz            {:obj :event
     163              :                      :act :update
     164              :                      :via :draw
     165              :                      :id  :route-params/draw-uuid}
     166              :   :summary          "Fill group participants"
     167              :   :description      "Fills empty group participant slots with the provided participant IDs"}
     168              :  [{:keys [ctx route-params]
     169              :    :as   _request}
     170              :   {:keys [participant-ids]}]
     171            4 :  (let [group-uuid (:group-uuid route-params)
     172            3 :        draw-uuid  (:draw-uuid route-params)
     173            6 :        result     (group-participants-set-app/execute! ctx group-uuid draw-uuid participant-ids)]
     174            4 :    (if (either/right? result)
     175            1 :      (do
     176            4 :        (schedule-draw-sync! ctx draw-uuid)
     177            7 :        {:body   {:message "Group participants updated successfully."
     178            3 :                  :data    (c/extract result)}
     179            1 :         :status status/ok})
     180            4 :      (let [{:keys [code message]} (c/extract result)]
     181            8 :        {:body   {:message message
     182              :                  :data    nil}
     183            3 :         :status (participants-error->status code)}))))
     184              : 
     185              : 
     186            4 : (defn groups-fill-from-class-handler
     187              :  {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/GroupParticipantsFillResponseSchema]
     188              :   :malli/parameters {:path api-schemas/GroupAndDrawPathParamsSchema}
     189              :   :authz {:obj :event
     190              :           :act :update
     191              :           :via :draw
     192              :           :id  :route-params/draw-uuid}
     193              :   :summary "Fill group from class"
     194              :   :description
     195              :   "Fills empty group participant slots from class participants not used in the same draw"}
     196              :  [{:keys [ctx route-params]
     197              :    :as   _request}]
     198            4 :  (let [group-uuid (:group-uuid route-params)
     199            3 :        draw-uuid  (:draw-uuid route-params)
     200            5 :        result     (group-participants-fill-app/execute! ctx group-uuid draw-uuid)]
     201            4 :    (if (either/right? result)
     202            1 :      (do
     203            4 :        (schedule-draw-sync! ctx draw-uuid)
     204            7 :        {:body   {:message "Group participants filled successfully."
     205            3 :                  :data    (c/extract result)}
     206            1 :         :status status/ok})
     207            4 :      (let [{:keys [code message]} (c/extract result)]
     208            8 :        {:body   {:message message
     209              :                  :data    nil}
     210            3 :         :status (participants-error->status code)}))))
     211              : 
     212              : 
     213            4 : (defn groups-fill-all-from-class-handler
     214              :  {:malli/schema
     215              :   [:=> [:cat api-schemas/RingRequest] api-schemas/GroupParticipantsFillAllResponseSchema]
     216              :   :malli/parameters {:path api-schemas/DrawUuidPathParamsSchema}
     217              :   :authz {:obj :event
     218              :           :act :update
     219              :           :via :draw
     220              :           :id  :route-params/draw-uuid}
     221              :   :summary "Fill all groups from class"
     222              :   :description "Fills empty slots across all draw groups from class participants in draw order"}
     223              :  [{:keys [ctx route-params]
     224              :    :as   _request}]
     225            4 :  (let [draw-uuid (:draw-uuid route-params)
     226            4 :        result    (group-participants-fill-all-app/execute! ctx draw-uuid)]
     227            4 :    (if (either/right? result)
     228            1 :      (do
     229            4 :        (schedule-draw-sync! ctx draw-uuid)
     230            7 :        {:body   {:message "Group participants filled successfully."
     231            3 :                  :data    (c/extract result)}
     232            1 :         :status status/ok})
     233            4 :      (let [{:keys [code message]} (c/extract result)]
     234            8 :        {:body   {:message message
     235              :                  :data    nil}
     236            3 :         :status (participants-error->status code)}))))
     237              : 
     238              : 
     239            4 : (defn groups-update-participant-handler
     240              :  {:malli/schema     [:=>
     241              :                      [:cat api-schemas/RingRequest api-schemas/GroupParticipantUpdateRequestSchema]
     242              :                      api-schemas/GroupParticipantUpdateResponseSchema]
     243              :   :malli/parameters {:path api-schemas/GroupParticipantUuidPathParamsSchema}
     244              :   :authz            {:obj :event
     245              :                      :act :update
     246              :                      :via :draw
     247              :                      :id  :route-params/draw-uuid}
     248              :   :summary          "Update group participant"
     249              :   :description      "Updates status, slot, and/or participant-uuid of a single group participant"}
     250              :  [{:keys [ctx route-params]
     251              :    :as   _request}
     252              :   updates]
     253            4 :  (let [gp-uuid (:group-participant-uuid route-params)
     254            3 :        group-uuid (:group-uuid route-params)
     255            3 :        draw-uuid (:draw-uuid route-params)
     256            7 :        result (group-participant-update-one-app/execute! ctx gp-uuid group-uuid draw-uuid updates)]
     257            4 :    (if (either/right? result)
     258            1 :      (do
     259            4 :        (schedule-draw-sync! ctx draw-uuid)
     260            7 :        {:body   {:message "Group participant updated successfully."
     261            3 :                  :data    (c/extract result)}
     262            1 :         :status status/ok})
     263            4 :      (let [{:keys [code message]} (c/extract result)]
     264            8 :        {:body   {:message message
     265              :                  :data    nil}
     266            3 :         :status (participants-error->status code)}))))
        

Generated by: LCOV version 2.0-1