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

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

Generated by: LCOV version 2.0-1