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

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

Generated by: LCOV version 2.0-1