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

            Line data    Source code
       1            4 : (ns matchscore.domain.match-segment.repository.postgres
       2              :   (:require
       3              :    [honey.sql :as sql]
       4              :    [matchscore.domain.match-segment.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              :   MatchSegmentPostgresRepository
      13              :   (find-by-id
      14              :    [this id]
      15              :    "Find match segment by UUID.")
      16              :   (find-by-match-id
      17              :    [this match-id]
      18              :    "Find all top-level segments for a match, ordered by order. Returns seq.")
      19              :   (find-by-match-ids
      20              :    [this match-ids]
      21              :    "Find all top-level segments for multiple matches in one batch query. Returns seq.")
      22              :   (find-by-bracket-match-ids
      23              :    [this bracket-match-ids]
      24              :    "Find all top-level segments for multiple bracket matches via direct matches or series+match join. Returns seq including :bracket-match-id for grouping.")
      25              :   (find-by-parent-id
      26              :    [this parent-id]
      27              :    "Find child segments of a segment, ordered by order. Returns seq.")
      28              :   (search
      29              :    [this filters pagination]
      30              :    "Search. filters: {:matchId {...} :parentId {...} :type {...} :state {...}}. sort: #{:order}, default: :order")
      31              :   (save!
      32              :    [this segment]
      33              :    "Upsert a match segment. Returns the saved row as a domain map.")
      34              :   (delete-by-ids!
      35              :    [this ids]
      36              :    "Delete segments by a list of UUID strings. Returns execute result.")
      37              :   (delete-by-match-id!
      38              :    [this match-id]
      39              :    "Delete all segments for a given match UUID string. Returns execute result."))
      40              : 
      41              : 
      42            4 : (defn- filters->specs
      43              :   "Converts a GraphQL filters map to a seq of [column op value] specs."
      44              :   [{:keys [matchId parentId type state]}]
      45            2 :   (qh/typed-filters->specs
      46            8 :    {:match-id  (utils.db/cast-uuid-filter matchId)
      47            3 :     :parent-id (utils.db/cast-uuid-filter parentId)
      48            1 :     :type      type
      49            1 :     :state     state}))
      50              : 
      51              : 
      52            4 : (defn- row->match-segment
      53              :   "Converts a database row to a domain MatchSegment map."
      54              :   [row]
      55           32 :   (when row (entity/make-match-segment (utils.db/unqualify-keys row))))
      56              : 
      57              : 
      58           12 : (def ^:private valid-sort-fields #{:order})
      59              : 
      60              : 
      61            8 : (def ^:private default-sort :order)
      62              : 
      63              : 
      64            4 : (defn- match-segment->row
      65              :   "Converts a domain MatchSegment map to a database row for upsert."
      66              :   [{:keys [uuid match-id parent-id type state home-score away-score winner-id order source]}]
      67           18 :   (cond->
      68           26 :     {:uuid       [:cast (str uuid) :uuid]
      69           12 :      :match_id   [:cast (str match-id) :uuid]
      70           10 :      :state      (or state "completed")
      71            2 :      :home_score home-score
      72            2 :      :away_score away-score
      73           10 :      :source     (or source "matchscore")}
      74           10 :     order     (assoc :order order)
      75            2 :     parent-id (assoc :parent_id [:cast (str parent-id) :uuid])
      76           10 :     type      (assoc :type type)
      77            2 :     winner-id (assoc :winner_id [:cast (str winner-id) :uuid])))
      78              : 
      79              : 
      80            4 : (def ^:private base-query
      81           28 :   {:select [[:ms.uuid :uuid]
      82           12 :             [:ms.match-id :match-id]
      83           12 :             [:ms.parent-id :parent-id]
      84           12 :             [:ms.type :type]
      85           12 :             [:ms.state :state]
      86           12 :             [:ms.home-score :home-score]
      87           12 :             [:ms.away-score :away-score]
      88           12 :             [:ms.winner-id :winner-id]
      89           12 :             [:ms.started-at :started-at]
      90           12 :             [:ms.ended-at :ended-at]
      91           12 :             [:ms.extra :extra]
      92           12 :             [:ms.created-datetime :created-datetime]
      93           16 :             [[:raw "ms.\"order\""]]
      94           12 :             [:ms.source :source]]
      95           16 :    :from   [[:match-segments :ms]]})
      96              : 
      97              : 
      98            4 : (defrecord
      99              :   PostgresMatchSegmentRepository
     100              :   [ds]
     101              :   MatchSegmentPostgresRepository
     102              : 
     103              :   (find-by-id
     104              :    [_ id]
     105           24 :    (let [query  (assoc base-query :where [:= :ms.uuid [:cast id :uuid]])
     106            2 :          result (->>
     107            6 :                   (sql/format query)
     108            6 :                   (jdbc/execute! ds)
     109            2 :                   first)]
     110            6 :      (row->match-segment result)))
     111              : 
     112              :   (find-by-match-id
     113              :    [_ match-id]
     114           12 :    (let [query (merge
     115            4 :                 base-query
     116           64 :                 {:where    [:and [:= :ms.match-id [:cast match-id :uuid]] [:is :ms.parent-id nil]]
     117           16 :                  :order-by [[:ms/order :asc]]})]
     118            4 :      (->>
     119           12 :        (sql/format query)
     120           12 :        (jdbc/execute! ds)
     121            8 :        (mapv row->match-segment))))
     122              : 
     123              :   (find-by-match-ids
     124              :    [_ match-ids]
     125            8 :    (if (empty? match-ids)
     126            1 :      []
     127            6 :      (let [query (merge
     128            2 :                   base-query
     129           10 :                   {:where    [:and
     130           24 :                               [:in :ms.match-id (mapv #(vector :cast % :uuid) match-ids)]
     131            8 :                               [:is :ms.parent-id nil]]
     132           14 :                    :order-by [[:ms.match-id :asc] [:ms/order :asc]]})]
     133            2 :        (->>
     134            6 :          (sql/format query)
     135            6 :          (jdbc/execute! ds)
     136            4 :          (mapv row->match-segment)))))
     137              : 
     138              :   (find-by-bracket-match-ids
     139              :    [_ bracket-match-ids]
     140            4 :    (if (empty? bracket-match-ids)
     141            0 :      []
     142            1 :      (let [query
     143           11 :            {:select    [[:ms.uuid :uuid]
     144            3 :                         [:ms.match-id :match-id]
     145            3 :                         [:ms.parent-id :parent-id]
     146            3 :                         [:ms.type :type]
     147            3 :                         [:ms.state :state]
     148            3 :                         [:ms.home-score :home-score]
     149            3 :                         [:ms.away-score :away-score]
     150            3 :                         [:ms.winner-id :winner-id]
     151            3 :                         [:ms.started-at :started-at]
     152            3 :                         [:ms.ended-at :ended-at]
     153            3 :                         [:ms.extra :extra]
     154            3 :                         [:ms.created-datetime :created-datetime]
     155            5 :                         [[:raw "ms.\"order\""] :order]
     156            3 :                         [:ms.source :source]
     157            6 :                         [[:coalesce :m.bracket-match-id :s.bracket-match-id] :bracket-match-id]]
     158            4 :             :from      [[:match-segments :ms]]
     159            8 :             :join      [[:matches :m] [:= :ms.match-id :m.uuid]]
     160            8 :             :left-join [[:series :s] [:= :m.series-id :s.uuid]]
     161            2 :             :where     [:and
     162            4 :                         [:is :ms.parent-id nil]
     163            2 :                         [:or
     164           12 :                          [:in :m.bracket-match-id (mapv #(vector :cast % :uuid) bracket-match-ids)]
     165            3 :                          [:in
     166              :                           :s.bracket-match-id
     167            9 :                           (mapv #(vector :cast % :uuid) bracket-match-ids)]]]
     168            7 :             :order-by  [[[:coalesce :m.bracket-match-id :s.bracket-match-id] :asc]
     169            3 :                         [:ms.match-id :asc]
     170            3 :                         [:ms/order :asc]]}]
     171            1 :        (->>
     172            3 :          (sql/format query)
     173            3 :          (jdbc/execute! ds)
     174            2 :          (mapv utils.db/unqualify-keys)))))
     175              : 
     176              :   (find-by-parent-id
     177              :    [_ parent-id]
     178            3 :    (let [query (merge
     179            1 :                 base-query
     180           10 :                 {:where    [:= :ms.parent-id [:cast parent-id :uuid]]
     181            4 :                  :order-by [[:ms/order :asc]]})]
     182            1 :      (->>
     183            3 :        (sql/format query)
     184            3 :        (jdbc/execute! ds)
     185            2 :        (mapv row->match-segment))))
     186              : 
     187              :   (search
     188              :    [_ filters pagination]
     189            4 :    (let [specs (filters->specs filters)
     190            2 :          query (qh/build-search-query
     191            1 :                 base-query
     192            1 :                 specs
     193            1 :                 valid-sort-fields
     194            1 :                 default-sort
     195            1 :                 pagination
     196            6 :                 {:sort-field-casts {:order :integer}
     197            5 :                  :sort-col-exprs   {:order [:raw "\"order\""]}})
     198            1 :          rows  (->>
     199            3 :                  (sql/format query)
     200            3 :                  (jdbc/execute! ds)
     201            2 :                  (mapv row->match-segment))
     202            6 :          opts  (when-not (:cursor pagination)
     203            5 :                  (let [cnt-query (qh/build-count-query base-query specs)
     204            3 :                        total     (->>
     205            3 :                                    (sql/format cnt-query)
     206            3 :                                    (jdbc/execute-one! ds)
     207            1 :                                    utils.db/unqualify-keys
     208              :                                    :total)]
     209            3 :                    {:total-count total}))]
     210            7 :      (qh/paginate-results rows valid-sort-fields default-sort pagination opts)))
     211              : 
     212              :   (save!
     213              :    [_ segment]
     214           16 :    (let [query  {:insert-into   :match_segments
     215            8 :                  :values        [(match-segment->row segment)]
     216            4 :                  :on-conflict   [:uuid]
     217           18 :                  :do-update-set {:fields [:home_score :away_score :state :order :type :winner_id]}
     218            4 :                  :returning     [:*]}
     219            2 :          result (->>
     220           16 :                   (sql/format (audit/stamp query) {:quoted true})
     221            6 :                   (jdbc/execute! ds)
     222            2 :                   first)]
     223            6 :      (row->match-segment result)))
     224              : 
     225              :   (delete-by-ids!
     226              :    [_ ids]
     227           10 :    (when (seq ids)
     228           10 :      (let [query {:delete-from :match_segments
     229           28 :                   :where       [:in :uuid (mapv #(vector :cast (str %) :uuid) ids)]}]
     230            2 :        (->>
     231            6 :          (sql/format query)
     232            4 :          (jdbc/execute! ds)))))
     233              : 
     234              :   (delete-by-match-id!
     235              :    [_ match-id]
     236           10 :    (let [query {:delete-from :match_segments
     237           18 :                 :where       [:= :match-id [:cast (str match-id) :uuid]]}]
     238            2 :      (->>
     239            6 :        (sql/format query)
     240            4 :        (jdbc/execute! ds)))))
     241              : 
     242              : 
     243            4 : (defn make-repository
     244              :  "Creates a PostgresMatchSegmentRepository from a ds."
     245              :  [ds]
     246           12 :  (->PostgresMatchSegmentRepository ds))
        

Generated by: LCOV version 2.0-1