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

            Line data    Source code
       1            4 : (ns matchscore.domain.bracket-match-slot.repository.postgres
       2              :   (:require
       3              :    [honey.sql :as sql]
       4              :    [matchscore.domain.bracket-match-slot.entity :as entity]
       5              :    [matchscore.utils.audit :as audit]
       6              :    [matchscore.utils.db :as utils.db]
       7              :    [matchscore.utils.pagination :as qh]
       8              :    [next.jdbc :as jdbc]))
       9              : 
      10              : 
      11            4 : (defprotocol
      12              :   BracketMatchSlotPostgresRepository
      13              :   (find-by-id
      14              :    [this id]
      15              :    "Find bracket match slot by UUID.")
      16              :   (find-by-bracket-match-id
      17              :    [this bracket-match-id]
      18              :    "Find both slots (home+away) for a bracket match. Returns seq.")
      19              :   (find-by-bracket-match-ids
      20              :    [this bracket-match-ids]
      21              :    "Find all slots for multiple matches in one batch query. Returns seq.")
      22              :   (find-by-source-match-id
      23              :    [this source-match-id]
      24              :    "Find all slots whose source_match_id matches the given bracket_match uuid.
      25              :     Used to discover slots that depend on a bracket_match's winner/loser. Returns seq.")
      26              :   (search
      27              :    [this filters pagination]
      28              :    "Search. filters: {:bracketMatchId {...} :sourceType {...}}. sort: #{:slot}, default: :slot")
      29              :   (upsert!
      30              :    [this slot-data]
      31              :    "Insert or update a bracket match slot. Returns [ok? result error-message]."))
      32              : 
      33              : 
      34            4 : (defn- filters->specs
      35              :   "Converts a GraphQL filters map to a seq of [column op value] specs."
      36              :   [{:keys [bracketMatchId sourceType]}]
      37            2 :   (qh/typed-filters->specs
      38            6 :    {:bracket-match-id (utils.db/cast-uuid-filter bracketMatchId)
      39            1 :     :source-type      sourceType}))
      40              : 
      41              : 
      42            4 : (defn- row->bracket-match-slot
      43              :   "Converts a database row to a domain BracketMatchSlot map."
      44              :   [row]
      45           32 :   (when row (entity/make-bracket-match-slot (utils.db/unqualify-keys row))))
      46              : 
      47              : 
      48           12 : (def ^:private valid-sort-fields #{:slot})
      49              : 
      50              : 
      51            8 : (def ^:private default-sort :slot)
      52              : 
      53              : 
      54            4 : (def ^:private base-query
      55           28 :   {:select [[:bms.uuid :uuid]
      56           12 :             [:bms.bracket-match-id :bracket-match-id]
      57           12 :             [:bms.slot :slot]
      58           12 :             [:bms.source-type :source-type]
      59           12 :             [:bms.source-seed :source-seed]
      60           12 :             [:bms.source-match-id :source-match-id]
      61           12 :             [:bms.participant-id :participant-id]
      62           12 :             [:bms.source :source]]
      63           16 :    :from   [[:bracket-match-slots :bms]]})
      64              : 
      65              : 
      66            4 : (defrecord
      67              :   PostgresBracketMatchSlotRepository
      68              :   [ds]
      69              :   BracketMatchSlotPostgresRepository
      70              : 
      71              :   (find-by-id
      72              :    [_ id]
      73           24 :    (let [query  (assoc base-query :where [:= :bms.uuid [:cast id :uuid]])
      74            2 :          result (->>
      75            6 :                   (sql/format query)
      76            6 :                   (jdbc/execute! ds)
      77            2 :                   first)]
      78            6 :      (row->bracket-match-slot result)))
      79              : 
      80              :   (find-by-bracket-match-id
      81              :    [_ bracket-match-id]
      82            8 :    (let [query (->>
      83            4 :                  base-query
      84            4 :                  (merge
      85           40 :                   {:where    [:= :bms.bracket-match-id [:cast bracket-match-id :uuid]]
      86           16 :                    :order-by [[:bms.slot :asc]]}))]
      87            4 :      (->>
      88           12 :        (sql/format query)
      89           12 :        (jdbc/execute! ds)
      90            8 :        (mapv row->bracket-match-slot))))
      91              : 
      92              :   (find-by-bracket-match-ids
      93              :    [_ bracket-match-ids]
      94           16 :    (if (empty? bracket-match-ids)
      95            1 :      []
      96            8 :      (let [query (->>
      97            4 :                    base-query
      98            4 :                    (merge
      99           24 :                     {:where    [:in
     100              :                                 :bms.bracket-match-id
     101           36 :                                 (mapv #(vector :cast % :uuid) bracket-match-ids)]
     102           16 :                      :order-by [[:bms.slot :asc]]}))]
     103            4 :        (->>
     104           12 :          (sql/format query)
     105           12 :          (jdbc/execute! ds)
     106            8 :          (mapv row->bracket-match-slot)))))
     107              : 
     108              :   (find-by-source-match-id
     109              :    [_ source-match-id]
     110            8 :    (let [query (->>
     111            4 :                  base-query
     112            4 :                  (merge
     113           40 :                   {:where    [:= :bms.source-match-id [:cast source-match-id :uuid]]
     114           16 :                    :order-by [[:bms.slot :asc]]}))]
     115            4 :      (->>
     116           12 :        (sql/format query)
     117           12 :        (jdbc/execute! ds)
     118            8 :        (mapv row->bracket-match-slot))))
     119              : 
     120              :   (search
     121              :    [_ filters pagination]
     122            4 :    (let [specs (filters->specs filters)
     123            2 :          query (qh/build-search-query
     124            1 :                 base-query
     125            1 :                 specs
     126            1 :                 valid-sort-fields
     127            1 :                 default-sort
     128            1 :                 pagination
     129            5 :                 {:sort-field-casts {:slot :integer}})
     130            1 :          rows  (->>
     131            3 :                  (sql/format query)
     132            3 :                  (jdbc/execute! ds)
     133            2 :                  (mapv row->bracket-match-slot))
     134            6 :          opts  (when-not (:cursor pagination)
     135            5 :                  (let [cnt-query (qh/build-count-query base-query specs)
     136            3 :                        total     (->>
     137            3 :                                    (sql/format cnt-query)
     138            3 :                                    (jdbc/execute-one! ds)
     139            1 :                                    utils.db/unqualify-keys
     140              :                                    :total)]
     141            3 :                    {:total-count total}))]
     142            7 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
     143              : 
     144              :   (upsert!
     145              :    [_ slot-data]
     146            4 :    (try
     147            4 :      (let [{:keys [uuid
     148              :                    bracket-match-id
     149              :                    slot
     150              :                    source-type
     151              :                    source-seed
     152              :                    source-match-id
     153              :                    participant-id
     154              :                    source]}
     155            4 :            slot-data
     156              :            query
     157           28 :            {:insert-into   :bracket_match_slots
     158           56 :             :values        [{:uuid            [:cast uuid :uuid]
     159           16 :                              :bracket_match_id [:cast bracket-match-id :uuid]
     160            4 :                              :slot            slot
     161            4 :                              :source_type     source-type
     162            4 :                              :source_seed     source-seed
     163           28 :                              :source_match_id (when source-match-id [:cast source-match-id :uuid])
     164           28 :                              :participant_id  (when participant-id [:cast participant-id :uuid])
     165           20 :                              :source          (or source "matchscore")}]
     166            8 :             :on-conflict   [:uuid]
     167           36 :             :do-update-set {:fields [:bracket_match_id
     168              :                                      :slot
     169              :                                      :source_type
     170              :                                      :source_seed
     171              :                                      :source_match_id
     172              :                                      :participant_id]}
     173            8 :             :returning     [:*]}
     174            8 :            result (->>
     175            8 :                     (sql/format
     176           12 :                      (audit/stamp query)
     177           20 :                      {:quoted       true
     178              :                       :quoted-snake true})
     179           12 :                     (jdbc/execute! ds)
     180            4 :                     first
     181            4 :                     row->bracket-match-slot)]
     182           16 :        [true result nil])
     183            0 :      (catch Exception e [false :database-error (.getMessage e)]))))
     184              : 
     185              : 
     186            4 : (defn make-repository
     187              :  "Creates a PostgresBracketMatchSlotRepository from a ds."
     188              :  [ds]
     189           12 :  (->PostgresBracketMatchSlotRepository ds))
        

Generated by: LCOV version 2.0-1