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

            Line data    Source code
       1            4 : (ns matchscore.domain.club.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.club.entity :as entity]
       9              :    [matchscore.utils.audit :as audit]
      10              :    [matchscore.utils.db :as utils.db]
      11              :    [matchscore.utils.errors :as utils.errors]
      12              :    [matchscore.utils.pagination :as qh]
      13              :    [next.jdbc :as jdbc]))
      14              : 
      15              : 
      16            4 : (defprotocol
      17              :   ClubPostgresRepository
      18              :   (find-by-id
      19              :    [this id]
      20              :    "Find club by UUID string. Returns Club map or nil.")
      21              :   (get-all
      22              :    [this]
      23              :    "Returns all active clubs as a list of Club maps.")
      24              :   (get-all-admin
      25              :    [this]
      26              :    "Returns all clubs including inactive (soft-deleted) ones. Admin use only.")
      27              :   (search
      28              :    [this filters pagination]
      29              :    "Search active clubs by filters and pagination.
      30              :     filters: {:city \"...\" :country \"...\" :network \"...\"}
      31              :     pagination: {:limit 20 :cursor \"base64...\" :sort-by :name :sort-dir :asc}
      32              :     Returns {:items [...] :has-next-page bool :end-cursor \"base64-or-nil\"}")
      33              :   (save!
      34              :    [this club]
      35              :    "Persists a club (insert or update). Returns Either<Error, Club>.")
      36              :   (deactivate!
      37              :    [this id]
      38              :    "Sets is_active = false for the club (soft delete). Returns Either<Error, Club>."))
      39              : 
      40              : 
      41           20 : (def ^:private valid-sort-fields #{:name :city :country})
      42              : 
      43              : 
      44            8 : (def ^:private default-sort :name)
      45              : 
      46              : 
      47            4 : (def ^:private base-query
      48           20 :   {:select [:*]
      49            8 :    :from   [:clubs]})
      50              : 
      51              : 
      52            4 : (defn- row->club
      53              :   "Converts a database row to a domain Club map."
      54              :   [row]
      55           32 :   (when row (entity/make-club (utils.db/unqualify-keys row))))
      56              : 
      57              : 
      58            4 : (defn- club->row
      59              :   "Converts a domain Club map to a database row for upsert."
      60              :   [club]
      61            4 :   (let [{:keys [uuid
      62              :                 public_id
      63              :                 sl_uuid
      64              :                 name
      65              :                 network
      66              :                 country
      67              :                 city
      68              :                 phone_number
      69              :                 location
      70              :                 working_hours
      71              :                 images
      72              :                 is_active]}
      73           16 :         (cske/transform-keys csk/->snake_case_keyword club)]
      74           68 :     {:uuid          [:cast uuid :uuid]
      75            4 :      :public_id     public_id
      76           13 :      :sl_uuid       (when sl_uuid [:cast sl_uuid :uuid])
      77            4 :      :name          name
      78            4 :      :network       network
      79            4 :      :country       country
      80            4 :      :city          city
      81            4 :      :phone_number  phone_number
      82           41 :      :location      [:cast (json/generate-string (or location {})) :jsonb]
      83           41 :      :working_hours [:cast (json/generate-string (or working_hours [])) :jsonb]
      84           41 :      :images        [:cast (json/generate-string (or images [])) :jsonb]
      85           25 :      :is_active     (if (nil? is_active) true is_active)}))
      86              : 
      87              : 
      88            4 : (defn- filters->specs
      89              :   "Converts a GraphQL filters map to a seq of [column op value] specs.
      90              :    Always constrains to active clubs."
      91              :   [{:keys [city country network]}]
      92            6 :   (conj
      93            6 :    (qh/typed-filters->specs
      94           15 :     {:city    city
      95            3 :      :country country
      96            3 :      :network network})
      97           12 :    [:is-active := true]))
      98              : 
      99              : 
     100            4 : (defrecord
     101              :   PostgresClubRepository
     102              :   [ds]
     103              :   ClubPostgresRepository
     104              : 
     105              :   (get-all
     106              :    [_]
     107           18 :    (let [query (assoc base-query
     108           12 :                 :where    [:= :is-active true]
     109           12 :                 :order-by [[:name :asc]])]
     110            3 :      (->>
     111            9 :        (sql/format query)
     112            9 :        (jdbc/execute! ds)
     113            6 :        (mapv row->club))))
     114              : 
     115              :   (get-all-admin
     116              :    [_]
     117            9 :    (let [query (assoc base-query :order-by [[:name :asc]])]
     118            1 :      (->>
     119            3 :        (sql/format query)
     120            3 :        (jdbc/execute! ds)
     121            2 :        (mapv row->club))))
     122              : 
     123              :   (find-by-id
     124              :    [_ id]
     125           21 :    (let [query  {:select [:*]
     126            6 :                  :from   [:clubs]
     127           21 :                  :where  [:= :uuid [:cast id :uuid]]}
     128            3 :          result (->>
     129            9 :                   (sql/format query)
     130            9 :                   (jdbc/execute! ds)
     131            3 :                   first)]
     132            9 :      (row->club result)))
     133              : 
     134              :   (search
     135              :    [_ filters pagination]
     136           12 :    (let [specs (filters->specs filters)
     137           21 :          query (qh/build-search-query base-query specs valid-sort-fields default-sort pagination)
     138            3 :          rows  (->>
     139            9 :                  (sql/format query)
     140            9 :                  (jdbc/execute! ds)
     141            6 :                  (mapv row->club))
     142           17 :          opts  (when-not (:cursor pagination)
     143           15 :                  (let [cnt-query (qh/build-count-query base-query specs)
     144            9 :                        total     (->>
     145            9 :                                    (sql/format cnt-query)
     146            9 :                                    (jdbc/execute-one! ds)
     147            3 :                                    utils.db/unqualify-keys
     148              :                                    :total)]
     149            9 :                    {:total-count total}))]
     150           21 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
     151              : 
     152              :   (save!
     153              :    [_ club]
     154            4 :    (try
     155           32 :      (let [query  {:insert-into   :clubs
     156           16 :                    :values        [(club->row club)]
     157            8 :                    :on-conflict   [:uuid]
     158           52 :                    :do-update-set {:fields [:name
     159              :                                             :network
     160              :                                             :country
     161              :                                             :city
     162              :                                             :phone_number
     163              :                                             :location
     164              :                                             :working_hours
     165              :                                             :images
     166              :                                             :sl_uuid
     167              :                                             :is_active]}
     168            8 :                    :returning     [:*]}
     169            4 :            result (->>
     170           20 :                     (sql/format (audit/stamp query))
     171           12 :                     (jdbc/execute! ds)
     172            4 :                     first)]
     173           20 :        (either/right (row->club result)))
     174              :      (catch Exception e
     175            0 :        (either/left
     176            0 :         (utils.errors/build-error
     177              :          :postgres
     178            0 :          {:message (.getMessage e)
     179            0 :           :context {:table :clubs
     180              :                     :op    :upsert
     181            0 :                     :uuid  (:uuid club)}})))))
     182              : 
     183              :   (deactivate!
     184              :    [_ id]
     185            2 :    (try
     186           14 :      (let [query  {:update    :clubs
     187            6 :                    :set       {:is-active false}
     188           14 :                    :where     [:= :uuid [:cast id :uuid]]
     189            4 :                    :returning [:*]}
     190            2 :            result (->>
     191           10 :                     (sql/format (audit/stamp query))
     192            6 :                     (jdbc/execute! ds)
     193            2 :                     first)]
     194            4 :        (if result
     195           10 :          (either/right (row->club result))
     196            0 :          (either/left
     197            0 :           (utils.errors/build-error
     198              :            :not-found
     199            0 :            {:code    :error.club/not-found
     200              :             :message "Club not found"
     201            0 :             :context {:uuid id}}))))
     202              :      (catch Exception e
     203            0 :        (either/left
     204            0 :         (utils.errors/build-error
     205              :          :postgres
     206            0 :          {:message (.getMessage e)
     207            0 :           :context {:table :clubs
     208              :                     :op    :deactivate
     209            0 :                     :uuid  id}}))))))
     210              : 
     211              : 
     212            4 : (defn make-repository
     213              :  "Creates a PostgresClubRepository from a ds."
     214              :  [ds]
     215           12 :  (->PostgresClubRepository ds))
        

Generated by: LCOV version 2.0-1