LCOV - code coverage report
Current view: top level - matchscore/domain/group_participant/repository - postgres.clj Coverage Total Hit
Test: lcov.info Lines: 80.3 % 178 143
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.group-participant.repository.postgres
       2              :   (:require
       3              :    [honey.sql :as sql]
       4              :    [matchscore.domain.group-participant.entity :as entity]
       5              :    [matchscore.utils.audit :as audit]
       6              :    [matchscore.utils.db :as utils.db]
       7              :    [matchscore.utils.pagination :as qh]
       8              :    [next.jdbc :as jdbc]))
       9              : 
      10              : 
      11            4 : (defprotocol
      12              :   GroupParticipantPostgresRepository
      13              :   (find-by-id
      14              :    [this id]
      15              :    "Find group participant by UUID.")
      16              :   (find-by-group-id
      17              :    [this group-id]
      18              :    "Find all participants in a group. Returns seq.")
      19              :   (find-by-group-ids
      20              :    [this group-ids]
      21              :    "Find all participants for multiple groups in one batch query. Returns seq.")
      22              :   (find-by-participant-id
      23              :    [this participant-id]
      24              :    "Find all groups for a participant. Returns seq.")
      25              :   (search
      26              :    [this filters pagination]
      27              :    "Search. filters: {:groupId {...} :participantId {...} :status {...}}. sort: #{:slot}, default: :slot")
      28              :   (upsert!
      29              :    [this gp-data]
      30              :    "Insert or update a group participant. Returns [ok? result error-message].")
      31              :   (bulk-upsert!
      32              :    [this gp-data-seq]
      33              :    "Insert or update multiple group participants. Returns [true results nil] or [false :database-error msg].")
      34              :   (bulk-insert!
      35              :    [this gp-data-seq]
      36              :    "Insert multiple group participants, skipping uuid conflicts. Returns [true results nil] or [false :database-error msg].")
      37              :   (bulk-upsert-into-empty-slots!
      38              :    [this gp-data-seq]
      39              :    "Upsert group participants only into empty slots (participant_id IS NULL). Returns [true results nil] or [false :database-error msg].")
      40              :   (delete-by-group-id!
      41              :    [this group-id]
      42              :    "Delete all group participants for a group. Returns [true nil nil] or [false :database-error msg]."))
      43              : 
      44              : 
      45            4 : (defn- filters->specs
      46              :   "Converts a GraphQL filters map to a seq of [column op value] specs."
      47              :   [{:keys [groupId participantId status]}]
      48            2 :   (qh/typed-filters->specs
      49            7 :    {:group-id       (utils.db/cast-uuid-filter groupId)
      50            3 :     :participant-id (utils.db/cast-uuid-filter participantId)
      51            1 :     :status         status}))
      52              : 
      53              : 
      54            4 : (defn- row->group-participant
      55              :   "Converts a database row to a domain GroupParticipant map."
      56              :   [row]
      57           32 :   (when row (entity/make-group-participant (utils.db/unqualify-keys row))))
      58              : 
      59              : 
      60           12 : (def ^:private valid-sort-fields #{:slot})
      61              : 
      62              : 
      63            8 : (def ^:private default-sort :slot)
      64              : 
      65              : 
      66            4 : (def ^:private base-query
      67           28 :   {:select [[:gp.uuid :uuid]
      68           12 :             [:gp.group-id :group-id]
      69           12 :             [:gp.participant-id :participant-id]
      70           12 :             [:gp.created-datetime :created-datetime]
      71           12 :             [:gp.status :status]
      72           12 :             [:gp.slot :slot]
      73           12 :             [:gp.source :source]]
      74           16 :    :from   [[:group-participants :gp]]})
      75              : 
      76              : 
      77            4 : (defrecord
      78              :   PostgresGroupParticipantRepository
      79              :   [ds]
      80              :   GroupParticipantPostgresRepository
      81              : 
      82              :   (find-by-id
      83              :    [_ id]
      84           24 :    (let [query  (assoc base-query :where [:= :gp.uuid [:cast id :uuid]])
      85            2 :          result (->>
      86            6 :                   (sql/format query)
      87            6 :                   (jdbc/execute! ds)
      88            2 :                   first)]
      89            6 :      (row->group-participant result)))
      90              : 
      91              :   (find-by-group-id
      92              :    [_ group-id]
      93            9 :    (let [query  (->
      94            3 :                   base-query
      95           24 :                   (assoc :where [:= :gp.group-id [:cast group-id :uuid]]))
      96            3 :          result (->>
      97            9 :                   (sql/format query)
      98            9 :                   (jdbc/execute! ds)
      99            6 :                   (mapv row->group-participant))]
     100            3 :      result))
     101              : 
     102              :   (find-by-group-ids
     103              :    [_ group-ids]
     104           16 :    (if (empty? group-ids)
     105            0 :      []
     106           12 :      (let [query  (->
     107            4 :                     base-query
     108           52 :                     (assoc :where [:in :gp.group-id (mapv #(vector :cast % :uuid) group-ids)]))
     109            4 :            result (->>
     110           12 :                     (sql/format query)
     111           12 :                     (jdbc/execute! ds)
     112            8 :                     (mapv row->group-participant))]
     113            4 :        result)))
     114              : 
     115              :   (find-by-participant-id
     116              :    [_ participant-id]
     117            3 :    (let [query  (->
     118            1 :                   base-query
     119            8 :                   (assoc :where [:= :gp.participant-id [:cast participant-id :uuid]]))
     120            1 :          result (->>
     121            3 :                   (sql/format query)
     122            3 :                   (jdbc/execute! ds)
     123            2 :                   (mapv row->group-participant))]
     124            1 :      result))
     125              : 
     126              :   (search
     127              :    [_ filters pagination]
     128            4 :    (let [specs (filters->specs filters)
     129            2 :          query (qh/build-search-query
     130            1 :                 base-query
     131            1 :                 specs
     132            1 :                 valid-sort-fields
     133            1 :                 default-sort
     134            1 :                 pagination
     135            5 :                 {:sort-field-casts {:slot :integer}})
     136            1 :          rows  (->>
     137            3 :                  (sql/format query)
     138            3 :                  (jdbc/execute! ds)
     139            2 :                  (mapv row->group-participant))
     140            6 :          opts  (when-not (:cursor pagination)
     141            5 :                  (let [cnt-query (qh/build-count-query base-query specs)
     142            3 :                        total     (->>
     143            3 :                                    (sql/format cnt-query)
     144            3 :                                    (jdbc/execute-one! ds)
     145            1 :                                    utils.db/unqualify-keys
     146              :                                    :total)]
     147            3 :                    {:total-count total}))]
     148            7 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
     149              : 
     150              :   (upsert!
     151              :    [_ gp-data]
     152            3 :    (try
     153            6 :      (let [{:keys [uuid group-id participant-id status slot source]} gp-data
     154           21 :            query  {:insert-into   :group_participants
     155           36 :                    :values        [{:uuid           [:cast uuid :uuid]
     156           12 :                                     :group_id       [:cast group-id :uuid]
     157            7 :                                     :participant_id (when participant-id
     158            4 :                                                       [:cast participant-id :uuid])
     159           15 :                                     :status         (or status "pending")
     160            3 :                                     :slot           slot
     161           15 :                                     :source         (or source "matchscore")}]
     162            6 :                    :on-conflict   [:uuid]
     163           21 :                    :do-update-set {:fields [:group_id :participant_id :status :slot]}
     164            6 :                    :returning     [:*]}
     165            6 :            result (->>
     166            6 :                     (sql/format
     167            9 :                      (audit/stamp query)
     168           15 :                      {:quoted       true
     169              :                       :quoted-snake true})
     170            9 :                     (jdbc/execute! ds)
     171            3 :                     first
     172            3 :                     row->group-participant)]
     173           12 :        [true result nil])
     174            0 :      (catch Exception e [false :database-error (.getMessage e)])))
     175              : 
     176              :   (bulk-upsert!
     177              :    [_ gp-data-seq]
     178           12 :    (if (empty? gp-data-seq)
     179            0 :      [true [] nil]
     180            3 :      (try
     181            9 :        (let [rows   (mapv
     182            3 :                      (fn [{:keys [uuid group-id participant-id status slot source]}]
     183           33 :                       {:uuid           [:cast uuid :uuid]
     184           12 :                        :group_id       [:cast group-id :uuid]
     185           21 :                        :participant_id (when participant-id [:cast participant-id :uuid])
     186           16 :                        :status         (or status "active")
     187            3 :                        :slot           slot
     188           15 :                        :source         (or source "matchscore")})
     189            3 :                      gp-data-seq)
     190           21 :              query  {:insert-into   :group_participants
     191            3 :                      :values        rows
     192            6 :                      :on-conflict   [:uuid]
     193           21 :                      :do-update-set {:fields [:group_id :participant_id :status :slot]}
     194            6 :                      :returning     [:*]}
     195            3 :              result (->>
     196            6 :                       (sql/format
     197            9 :                        (audit/stamp query)
     198           15 :                        {:quoted       true
     199              :                         :quoted-snake true})
     200            9 :                       (jdbc/execute! ds)
     201            6 :                       (mapv row->group-participant))]
     202           12 :          [true result nil])
     203            0 :        (catch Exception e [false :database-error (.getMessage e)]))))
     204              : 
     205              :   (bulk-insert!
     206              :    [_ gp-data-seq]
     207            0 :    (if (empty? gp-data-seq)
     208            0 :      [true [] nil]
     209            0 :      (try
     210            0 :        (let [rows   (mapv
     211            0 :                      (fn [{:keys [uuid group-id participant-id status slot source]}]
     212            0 :                       {:uuid           [:cast uuid :uuid]
     213            0 :                        :group_id       [:cast group-id :uuid]
     214            0 :                        :participant_id (when participant-id [:cast participant-id :uuid])
     215            0 :                        :status         (or status "active")
     216            0 :                        :slot           slot
     217            0 :                        :source         (or source "matchscore")})
     218            0 :                      gp-data-seq)
     219            0 :              query  {:insert-into :group_participants
     220            0 :                      :values      rows
     221            0 :                      :on-conflict [:uuid]
     222              :                      :do-nothing  true
     223            0 :                      :returning   [:*]}
     224            0 :              result (->>
     225            0 :                       (sql/format
     226            0 :                        (audit/stamp query)
     227            0 :                        {:quoted       true
     228              :                         :quoted-snake true})
     229            0 :                       (jdbc/execute! ds)
     230            0 :                       (mapv row->group-participant))]
     231            0 :          [true result nil])
     232            0 :        (catch Exception e [false :database-error (.getMessage e)]))))
     233              : 
     234              :   (bulk-upsert-into-empty-slots!
     235              :    [_ gp-data-seq]
     236            4 :    (if (empty? gp-data-seq)
     237            4 :      [true [] nil]
     238            1 :      (try
     239            3 :        (let [rows   (mapv
     240            1 :                      (fn [{:keys [uuid group-id participant-id status slot source]}]
     241           11 :                       {:uuid           [:cast uuid :uuid]
     242            4 :                        :group_id       [:cast group-id :uuid]
     243            7 :                        :participant_id (when participant-id [:cast participant-id :uuid])
     244            5 :                        :status         (or status "active")
     245            1 :                        :slot           slot
     246            5 :                        :source         (or source "matchscore")})
     247            1 :                      gp-data-seq)
     248            7 :              query  {:insert-into   :group_participants
     249            1 :                      :values        rows
     250            2 :                      :on-conflict   [:uuid]
     251            8 :                      :do-update-set {:fields [:group_id :participant_id :status :slot]
     252            4 :                                      :where  [:is :group-participants.participant-id nil]}
     253            2 :                      :returning     [:*]}
     254            1 :              result (->>
     255            2 :                       (sql/format
     256            3 :                        (audit/stamp query)
     257            5 :                        {:quoted       true
     258              :                         :quoted-snake true})
     259            3 :                       (jdbc/execute! ds)
     260            2 :                       (mapv row->group-participant))]
     261            4 :          [true result nil])
     262            0 :        (catch Exception e [false :database-error (.getMessage e)]))))
     263              : 
     264              :   (delete-by-group-id!
     265              :    [_ group-id]
     266            0 :    (let [query {:delete-from :group_participants
     267            0 :                 :where       [:= :group_id [:cast group-id :uuid]]}]
     268            0 :      (try
     269            0 :        (jdbc/execute! ds (sql/format query))
     270            0 :        [true nil nil]
     271            0 :        (catch Exception e [false :database-error (.getMessage e)])))))
     272              : 
     273              : 
     274            4 : (defn make-repository
     275              :  "Creates a PostgresGroupParticipantRepository from a ds."
     276              :  [ds]
     277           12 :  (->PostgresGroupParticipantRepository ds))
        

Generated by: LCOV version 2.0-1