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

            Line data    Source code
       1            4 : (ns matchscore.domain.event-participant.repository.postgres
       2              :   (:require
       3              :    [cats.monad.either :as either]
       4              :    [cheshire.core :as json]
       5              :    [honey.sql :as sql]
       6              :    [matchscore.domain.event-participant.entity :as entity]
       7              :    [matchscore.utils.audit :as audit]
       8              :    [matchscore.utils.db :as utils.db]
       9              :    [matchscore.utils.errors :as utils.errors]
      10              :    [next.jdbc :as jdbc]))
      11              : 
      12              : 
      13            4 : (defprotocol
      14              :   EventParticipantPostgresRepository
      15              :   (find-by-id
      16              :    [this event-id participant-id]
      17              :    "Find event participant by composite key (event-id, participant-id).
      18              :       Returns EventParticipant map or nil.")
      19              :   (find-by-event-id
      20              :    [this event-id]
      21              :    "Find all event participants for a given event, ordered by registered_at asc.
      22              :       Returns a vector of EventParticipant maps.")
      23              :   (find-by-event-ids
      24              :    [this event-ids]
      25              :    "Find all event participants for multiple events in one batch query.
      26              :       Returns a vector of EventParticipant maps.")
      27              :   (find-by-event-id-and-user-id
      28              :    [this event-id user-id]
      29              :    "Find event participant for a matchscore user.")
      30              :   (find-by-event-ids-and-user-id
      31              :    [this event-ids user-id]
      32              :    "Find a matchscore user's event participants across multiple events in one batch query.
      33              :       Returns a vector of EventParticipant maps.")
      34              :   (delete-by-event-id-and-user-id!
      35              :    [this event-id user-id]
      36              :    "Deletes event participant for a matchscore user. Idempotent.")
      37              :   (save!
      38              :    [this participant]
      39              :    "Persists an event participant (insert or update).
      40              :       Returns Either<Error, EventParticipant>.")
      41              :   (delete!
      42              :    [this event-id participant-id]
      43              :    "Deletes an event participant by composite key.
      44              :       Returns Either<Error, nil>."))
      45              : 
      46              : 
      47            4 : (defn- row->participant
      48              :   "Converts a database row to a domain EventParticipant map."
      49              :   [row]
      50           32 :   (when row (entity/make-event-participant (utils.db/unqualify-keys row))))
      51              : 
      52              : 
      53            4 : (defn- participant->row
      54              :   "Converts a domain EventParticipant map to a database row for upsert."
      55              :   [participant]
      56            4 :   (let [{:keys [event-id
      57              :                 participant-id
      58              :                 seed
      59              :                 status
      60              :                 registered-at
      61              :                 confirmed-at
      62              :                 checked-in-at
      63              :                 extra
      64              :                 roles
      65              :                 attendance-confirmed
      66              :                 source]}
      67            4 :         participant]
      68           64 :     {:event_id       [:cast event-id :uuid]
      69           16 :      :participant_id [:cast participant-id :uuid]
      70            4 :      :seed           seed
      71           20 :      :status         (or status "registered")
      72           12 :      :registered_at  (when registered-at
      73           20 :                        (if (instance? java.time.Instant registered-at)
      74           12 :                          (java.sql.Timestamp/from registered-at)
      75            2 :                          registered-at))
      76            8 :      :confirmed_at   (when confirmed-at
      77            0 :                        (if (instance? java.time.Instant confirmed-at)
      78            0 :                          (java.sql.Timestamp/from confirmed-at)
      79            0 :                          confirmed-at))
      80            8 :      :checked_in_at  (when checked-in-at
      81            0 :                        (if (instance? java.time.Instant checked-in-at)
      82            0 :                          (java.sql.Timestamp/from checked-in-at)
      83            0 :                          checked-in-at))
      84           41 :      :extra          [:cast (json/generate-string (or extra {})) :jsonb]
      85           40 :      :roles          [:cast (json/generate-string (or roles [])) :jsonb]
      86           27 :      :attendance_confirmed (if (nil? attendance-confirmed) false attendance-confirmed)
      87           21 :      :source         (or source "matchscore")}))
      88              : 
      89              : 
      90            4 : (def ^:private base-query
      91           20 :   {:select [:*]
      92           16 :    :from   [[:event_participants :eop]]})
      93              : 
      94              : 
      95            4 : (defrecord
      96              :   PostgresEventParticipantRepository
      97              :   [ds]
      98              :   EventParticipantPostgresRepository
      99              : 
     100              :   (find-by-id
     101              :    [_ event-id participant-id]
     102           20 :    (let [query  (assoc base-query
     103            8 :                  :where [:and
     104           28 :                          [:= :eop.event-id [:cast event-id :uuid]]
     105           28 :                          [:= :eop.participant-id [:cast participant-id :uuid]]])
     106            4 :          result (->>
     107           12 :                   (sql/format query)
     108           12 :                   (jdbc/execute! ds)
     109            4 :                   first)]
     110           12 :      (row->participant result)))
     111              : 
     112              :   (find-by-event-id
     113              :    [_ event-id]
     114           12 :    (let [query (->
     115            4 :                  base-query
     116           40 :                  (assoc :where [:= :eop.event-id [:cast event-id :uuid]])
     117           20 :                  (assoc :order-by [[:eop.registered-at :asc]]))]
     118            4 :      (->>
     119           12 :        (sql/format query)
     120           12 :        (jdbc/execute! ds)
     121            8 :        (mapv row->participant))))
     122              : 
     123              :   (find-by-event-ids
     124              :    [_ event-ids]
     125            8 :    (if (empty? event-ids)
     126            1 :      []
     127            6 :      (let [query (->
     128            2 :                    base-query
     129           30 :                    (assoc :where [:in :eop.event-id (mapv #(vector :cast % :uuid) event-ids)])
     130           10 :                    (assoc :order-by [[:eop.registered-at :asc]]))]
     131            2 :        (->>
     132            6 :          (sql/format query)
     133            6 :          (jdbc/execute! ds)
     134            4 :          (mapv row->participant)))))
     135              : 
     136              :   (find-by-event-id-and-user-id
     137              :    [_ event-id user-id]
     138           28 :    (let [query  (assoc base-query
     139            8 :                  :select [:eop.*]
     140           32 :                  :join   [[:participants :p] [:= :eop.participant-id :p.uuid]]
     141            8 :                  :where  [:and
     142           28 :                           [:= :eop.event-id [:cast event-id :uuid]]
     143           28 :                           [:= :p.user-id [:cast user-id :uuid]]])
     144            4 :          result (->>
     145           12 :                   (sql/format query)
     146           12 :                   (jdbc/execute! ds)
     147            4 :                   first)]
     148           12 :      (row->participant result)))
     149              : 
     150              :   (find-by-event-ids-and-user-id
     151              :    [_ event-ids user-id]
     152           22 :    (if (or (empty? event-ids) (nil? user-id))
     153            0 :      []
     154           14 :      (let [query (assoc base-query
     155            4 :                   :select [:eop.*]
     156           16 :                   :join   [[:participants :p] [:= :eop.participant-id :p.uuid]]
     157            4 :                   :where  [:and
     158           24 :                            [:in :eop.event-id (mapv #(vector :cast % :uuid) event-ids)]
     159           14 :                            [:= :p.user-id [:cast user-id :uuid]]])]
     160            2 :        (->>
     161            6 :          (sql/format query)
     162            6 :          (jdbc/execute! ds)
     163            4 :          (mapv row->participant)))))
     164              : 
     165              :   (delete-by-event-id-and-user-id!
     166              :    [_ event-id user-id]
     167            0 :    (try
     168            0 :      (let [query {:delete-from :event_participants
     169            0 :                   :where       [:and
     170            0 :                                 [:= :event_id [:cast event-id :uuid]]
     171            0 :                                 [:in
     172              :                                  :participant_id
     173            0 :                                  {:select [:uuid]
     174            0 :                                   :from   [:participants]
     175            0 :                                   :where  [:= :user_id [:cast user-id :uuid]]}]]}]
     176            0 :        (jdbc/execute! ds (sql/format query))
     177            0 :        (either/right nil))
     178              :      (catch Exception e
     179            0 :        (either/left
     180            0 :         (utils.errors/build-error
     181              :          :postgres
     182            0 :          {:message (.getMessage e)
     183            0 :           :context {:table    :event_participants
     184              :                     :op       :delete-by-user
     185            0 :                     :event-id event-id
     186            0 :                     :user-id  user-id}})))))
     187              : 
     188              :   (save!
     189              :    [_ participant]
     190            4 :    (try
     191           32 :      (let [query  {:insert-into   :event_participants
     192           16 :                    :values        [(participant->row participant)]
     193           12 :                    :on-conflict   [:event_id :participant_id]
     194           48 :                    :do-update-set {:fields [:seed
     195              :                                             :status
     196              :                                             :confirmed_at
     197              :                                             :checked_in_at
     198              :                                             :extra
     199              :                                             :roles
     200              :                                             :attendance_confirmed
     201              :                                             :source
     202              :                                             :updated_datetime]}
     203            8 :                    :returning     [:*]}
     204            4 :            result (->>
     205           20 :                     (sql/format (audit/stamp query))
     206           12 :                     (jdbc/execute! ds)
     207            4 :                     first)]
     208           20 :        (either/right (row->participant result)))
     209              :      (catch Exception e
     210            0 :        (either/left
     211            0 :         (utils.errors/build-error
     212              :          :postgres
     213            0 :          {:message (.getMessage e)
     214            0 :           :context {:table          :event_participants
     215              :                     :op             :upsert
     216            0 :                     :event-id       (:event-id participant)
     217            0 :                     :participant-id (:participant-id participant)}})))))
     218              : 
     219              :   (delete!
     220              :    [_ event-id participant-id]
     221            2 :    (try
     222           10 :      (let [query {:delete-from :event_participants
     223            4 :                   :where       [:and
     224           14 :                                 [:= :event_id [:cast event-id :uuid]]
     225           14 :                                 [:= :participant_id [:cast participant-id :uuid]]]}]
     226           12 :        (jdbc/execute! ds (sql/format query))
     227            6 :        (either/right nil))
     228              :      (catch Exception e
     229            0 :        (either/left
     230            0 :         (utils.errors/build-error
     231              :          :postgres
     232            0 :          {:message (.getMessage e)
     233            0 :           :context {:table          :event_participants
     234              :                     :op             :delete
     235            0 :                     :event-id       event-id
     236            0 :                     :participant-id participant-id}}))))))
     237              : 
     238              : 
     239            4 : (defn make-repository
     240              :  "Creates a PostgresEventParticipantRepository from a ds."
     241              :  [ds]
     242           12 :  (->PostgresEventParticipantRepository ds))
        

Generated by: LCOV version 2.0-1