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

            Line data    Source code
       1            4 : (ns matchscore.domain.user-rankedin.repository.postgres
       2              :   (:require
       3              :    [camel-snake-kebab.core :as csk]
       4              :    [camel-snake-kebab.extras :as cske]
       5              :    [cats.monad.either :as either]
       6              :    [honey.sql :as sql]
       7              :    [matchscore.domain.user-rankedin.entity :as entity]
       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              :   UserRankedinPostgresRepository
      15              :   (find-by-id
      16              :    [this id]
      17              :    "Find RankedIn user by UUID string. Returns UserRankedin map or nil.")
      18              :   (find-by-ids
      19              :    [this ids]
      20              :    "Find RankedIn users by multiple UUIDs. Returns seq.")
      21              :   (find-by-rankedin-id
      22              :    [this rankedin-id]
      23              :    [this rankedin-id source]
      24              :    "Find a user by its external id within a source (defaults to rankedin).")
      25              :   (save!
      26              :    [this user-rankedin]
      27              :    "Persists a RankedIn user (insert or update). Returns Either<Error, UserRankedin>."))
      28              : 
      29              : 
      30            4 : (defn- row->user-rankedin
      31              :   "Converts a database row to a domain UserRankedin map."
      32              :   [row]
      33           16 :   (when row (entity/make-user-rankedin (utils.db/unqualify-keys row))))
      34              : 
      35              : 
      36            4 : (defn- user-rankedin->row
      37              :   "Converts a domain UserRankedin map to a database row for upsert."
      38              :   [user-rankedin]
      39            2 :   (let [{:keys [uuid
      40              :                 rankedin_id
      41              :                 source
      42              :                 first_name
      43              :                 last_name
      44              :                 username
      45              :                 photo_url
      46              :                 profile_url
      47              :                 country_code
      48              :                 rating]}
      49            8 :         (cske/transform-keys csk/->snake_case_keyword user-rankedin)]
      50           30 :     {:uuid         [:cast uuid :uuid]
      51            2 :      :rankedin_id  rankedin_id
      52           10 :      :source       (or source "rankedin")
      53            2 :      :first_name   first_name
      54            2 :      :last_name    last_name
      55            2 :      :username     username
      56            2 :      :photo_url    photo_url
      57            2 :      :profile_url  profile_url
      58            2 :      :country_code country_code
      59            2 :      :rating       rating}))
      60              : 
      61              : 
      62            4 : (defrecord
      63              :   PostgresUserRankedinRepository
      64              :   [ds]
      65              :   UserRankedinPostgresRepository
      66              : 
      67              :   (find-by-id
      68              :    [_ id]
      69           14 :    (let [query  {:select [:*]
      70            4 :                  :from   [:rankedin_users]
      71           14 :                  :where  [:= :uuid [:cast id :uuid]]}
      72            2 :          result (->>
      73            6 :                   (sql/format query)
      74            6 :                   (jdbc/execute! ds)
      75            2 :                   first)]
      76            6 :      (row->user-rankedin result)))
      77              : 
      78              :   (find-by-ids
      79              :    [_ ids]
      80            8 :    (if (empty? ids)
      81            1 :      []
      82           14 :      (let [query {:select [:*]
      83            4 :                   :from   [:rankedin_users]
      84           24 :                   :where  [:in :uuid (mapv #(vector :cast % :uuid) ids)]}]
      85            2 :        (->>
      86            6 :          (sql/format query)
      87            6 :          (jdbc/execute! ds)
      88            4 :          (mapv row->user-rankedin)))))
      89              : 
      90            5 :   (find-by-rankedin-id [this rankedin-id] (find-by-rankedin-id this rankedin-id "rankedin"))
      91              : 
      92              :   (find-by-rankedin-id
      93              :    [_ rankedin-id source]
      94            7 :    (let [query  {:select [:*]
      95            2 :                  :from   [:rankedin_users]
      96           10 :                  :where  [:and [:= :rankedin_id rankedin-id] [:= :source source]]}
      97            1 :          result (->>
      98            3 :                   (sql/format query)
      99            3 :                   (jdbc/execute! ds)
     100            1 :                   first)]
     101            3 :      (row->user-rankedin result)))
     102              : 
     103              :   (save!
     104              :    [_ user-rankedin]
     105            2 :    (try
     106           16 :      (let [query  {:insert-into   :rankedin_users
     107            8 :                    :values        [(user-rankedin->row user-rankedin)]
     108            4 :                    :on-conflict   [:uuid]
     109           24 :                    :do-update-set {:fields [:rankedin_id
     110              :                                             :source
     111              :                                             :first_name
     112              :                                             :last_name
     113              :                                             :username
     114              :                                             :photo_url
     115              :                                             :profile_url
     116              :                                             :country_code
     117              :                                             :rating]}
     118            4 :                    :returning     [:*]}
     119            2 :            result (->>
     120            6 :                     (sql/format query)
     121            6 :                     (jdbc/execute! ds)
     122            2 :                     first)]
     123           10 :        (either/right (row->user-rankedin result)))
     124              :      (catch Exception e
     125            0 :        (either/left
     126            0 :         (utils.errors/build-error
     127              :          :postgres
     128            0 :          {:message (.getMessage e)
     129            0 :           :context {:table :rankedin_users
     130              :                     :op    :upsert
     131            0 :                     :uuid  (:uuid user-rankedin)}}))))))
     132              : 
     133              : 
     134            4 : (defn make-repository
     135              :  "Creates a PostgresUserRankedinRepository from a ds."
     136              :  [ds]
     137           12 :  (->PostgresUserRankedinRepository ds))
        

Generated by: LCOV version 2.0-1