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

            Line data    Source code
       1            4 : (ns matchscore.domain.resource.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              :    [cheshire.core :as json]
       7              :    [honey.sql :as sql]
       8              :    [matchscore.domain.resource.entity :as entity]
       9              :    [matchscore.utils.audit :as audit]
      10              :    [matchscore.utils.db :as utils.db]
      11              :    [matchscore.utils.errors :as utils.errors]
      12              :    [next.jdbc :as jdbc]))
      13              : 
      14              : 
      15            4 : (defprotocol
      16              :   ResourcePostgresRepository
      17              :   (find-by-id
      18              :    [this id]
      19              :    "Find resource by UUID string. Returns Resource map or nil.")
      20              :   (find-by-club-id
      21              :    [this club-id]
      22              :    "Find all resources for a club. Returns a vector of Resource maps.")
      23              :   (save!
      24              :    [this resource]
      25              :    "Persists a resource (insert or update). Returns Either<Error, Resource>.")
      26              :   (deactivate!
      27              :    [this id]
      28              :    "Sets is_active = false for the resource. Returns Either<Error, Resource>."))
      29              : 
      30              : 
      31            4 : (defn- row->resource
      32              :   [row]
      33            3 :   (when row
      34            4 :     (let [data (utils.db/unqualify-keys row)]
      35            2 :       (entity/make-resource
      36            3 :        (update
      37            1 :         data
      38              :         :attributes
      39            1 :         (fn [v]
      40            6 :          (cond (string? v) (json/parse-string v true)
      41            4 :                (nil? v)    {}
      42            1 :                :else       v)))))))
      43              : 
      44              : 
      45            4 : (defn- resource->row
      46              :   [resource]
      47            1 :   (let [{:keys [uuid public_id club_id name resource_type is_active attributes]}
      48            4 :         (cske/transform-keys csk/->snake_case_keyword resource)]
      49           12 :     {:uuid          [:cast uuid :uuid]
      50            1 :      :public_id     public_id
      51            4 :      :club_id       [:cast club_id :uuid]
      52            1 :      :name          name
      53            1 :      :resource_type resource_type
      54            6 :      :is_active     (if (nil? is_active) true is_active)
      55           10 :      :attributes    [:cast (json/generate-string (or attributes {})) :jsonb]}))
      56              : 
      57              : 
      58            4 : (defrecord
      59              :   PostgresResourceRepository
      60              :   [ds]
      61              :   ResourcePostgresRepository
      62              : 
      63              :   (find-by-id
      64              :    [_ id]
      65            7 :    (let [query  {:select [:*]
      66            2 :                  :from   [:club-resources]
      67           13 :                  :where  [:and [:= :uuid [:cast id :uuid]] [:= :is-active true]]}
      68            1 :          result (->>
      69            3 :                   (sql/format query)
      70            3 :                   (jdbc/execute! ds)
      71            1 :                   first)]
      72            3 :      (row->resource result)))
      73              : 
      74              :   (find-by-club-id
      75              :    [_ club-id]
      76            8 :    (let [query {:select   [:*]
      77            2 :                 :from     [:club-resources]
      78           13 :                 :where    [:and [:= :club-id [:cast club-id :uuid]] [:= :is-active true]]
      79            4 :                 :order-by [[:name :asc]]}]
      80            1 :      (->>
      81            3 :        (sql/format query)
      82            3 :        (jdbc/execute! ds)
      83            2 :        (mapv row->resource))))
      84              : 
      85              :   (save!
      86              :    [_ resource]
      87            1 :    (try
      88            8 :      (let [query  {:insert-into   :club-resources
      89            4 :                    :values        [(resource->row resource)]
      90            2 :                    :on-conflict   [:uuid]
      91            7 :                    :do-update-set {:fields [:name :resource-type :is-active :attributes]}
      92            2 :                    :returning     [:*]}
      93            1 :            result (->>
      94            5 :                     (sql/format (audit/stamp query))
      95            3 :                     (jdbc/execute! ds)
      96            1 :                     first)]
      97            5 :        (either/right (row->resource result)))
      98              :      (catch Exception e
      99            0 :        (either/left
     100            0 :         (utils.errors/build-error
     101              :          :postgres
     102            0 :          {:message (.getMessage e)
     103            0 :           :context {:table :club-resources
     104              :                     :op    :upsert
     105            0 :                     :uuid  (:uuid resource)}})))))
     106              : 
     107              :   (deactivate!
     108              :    [_ id]
     109            1 :    (try
     110            7 :      (let [query  {:update    :club-resources
     111            3 :                    :set       {:is-active false}
     112            7 :                    :where     [:= :uuid [:cast id :uuid]]
     113            2 :                    :returning [:*]}
     114            1 :            result (->>
     115            5 :                     (sql/format (audit/stamp query))
     116            3 :                     (jdbc/execute! ds)
     117            1 :                     first)]
     118            2 :        (if result
     119            5 :          (either/right (row->resource result))
     120            0 :          (either/left
     121            0 :           (utils.errors/build-error
     122              :            :not-found
     123            0 :            {:code    :error.resource/not-found
     124              :             :message "Resource not found"
     125            0 :             :context {:uuid id}}))))
     126              :      (catch Exception e
     127            0 :        (either/left
     128            0 :         (utils.errors/build-error
     129              :          :postgres
     130            0 :          {:message (.getMessage e)
     131            0 :           :context {:table :club-resources
     132              :                     :op    :deactivate
     133            0 :                     :uuid  id}}))))))
     134              : 
     135              : 
     136            4 : (defn make-repository
     137              :  "Creates a PostgresResourceRepository from a ds."
     138              :  [ds]
     139           12 :  (->PostgresResourceRepository ds))
        

Generated by: LCOV version 2.0-1