LCOV - code coverage report
Current view: top level - matchscore/db/postgres - payment_tracking.clj Coverage Total Hit
Test: lcov.info Lines: 100.0 % 67 67
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.db.postgres.payment-tracking
       2              :   "Per-player payment state of an event, as shown on the live Telegram payment
       3              :    post, and the player's own \"I paid\" claim."
       4              :   (:require
       5              :    [cheshire.core :as json]
       6              :    [honey.sql :as sql]
       7              :    [matchscore.utils.audit :as audit]
       8              :    [next.jdbc :as jdbc]
       9              :    [next.jdbc.result-set :as rs]))
      10              : 
      11              : 
      12           16 : (def ^:private jdbc-options {:builder-fn rs/as-unqualified-kebab-maps})
      13              : 
      14              : 
      15            4 : (def ^:private uncounted-statuses
      16              :   "Registrations that no longer owe a payment."
      17           12 :   ["withdrawn" "disqualified"])
      18              : 
      19              : 
      20            4 : (def ^:private counted-registration
      21            8 :   [:and
      22           28 :    [:= :c.event-id [:cast :?event-id :uuid]]
      23           40 :    [:or [:is :cp.status nil] [:not-in :cp.status uncounted-statuses]]])
      24              : 
      25              : 
      26            4 : (defn registrations
      27              :  "Counted class registrations of the event, one row per registration:
      28              :    {:participant-id :user-id :name :payment-status}."
      29              :  [ds event-id]
      30            2 :  (jdbc/execute!
      31            1 :   ds
      32            2 :   (sql/format
      33           11 :    {:select    [[:cp.participant-id :participant-id]
      34            3 :                 [:p.user-id :user-id]
      35            6 :                 [[:coalesce :u.name :p.public-id] :name]
      36            6 :                 [[:coalesce :cp.payment-status "unpaid"] :payment-status]]
      37            4 :     :from      [[:event-class-participants :cp]]
      38            4 :     :join      [[:event-classes :c]
      39            4 :                 [:= :c.uuid :cp.class-id]
      40            3 :                 [:participants :p]
      41            4 :                 [:= :p.uuid :cp.participant-id]]
      42            8 :     :left-join [[:users :u] [:= :u.uuid :p.user-id]]
      43            1 :     :where     counted-registration
      44            4 :     :order-by  [[:cp.registered-at :asc]]}
      45            5 :    {:params {:event-id event-id}})
      46            1 :   jdbc-options))
      47              : 
      48              : 
      49            4 : (defn claim!
      50              :  "Marks the user's unpaid counted registrations in the event as claimed.
      51              :    Returns how many registrations changed."
      52              :  [ds event-id user-id]
      53            1 :  (let [ids
      54            1 :        (->>
      55            1 :          (jdbc/execute!
      56            1 :           ds
      57            2 :           (sql/format
      58            8 :            {:select [:cp.class-id :cp.participant-id]
      59            4 :             :from   [[:event-class-participants :cp]]
      60            4 :             :join   [[:event-classes :c]
      61            4 :                      [:= :c.uuid :cp.class-id]
      62            3 :                      [:participants :p]
      63            4 :                      [:= :p.uuid :cp.participant-id]]
      64            2 :             :where  [:and
      65            1 :                      counted-registration
      66            7 :                      [:= :p.user-id [:cast :?user-id :uuid]]
      67            7 :                      [:= [:coalesce :cp.payment-status "unpaid"] "unpaid"]]}
      68            6 :            {:params {:event-id event-id
      69            1 :                      :user-id  user-id}})
      70            1 :           jdbc-options))]
      71           26 :    (doseq [{:keys [class-id participant-id]} ids]
      72            2 :      (jdbc/execute!
      73            1 :       ds
      74            2 :       ["UPDATE event_class_participants
      75              :            SET payment_status = 'claimed', updated_by = ?::uuid
      76              :          WHERE class_id = ?::uuid AND participant_id = ?::uuid
      77              :            AND coalesce(payment_status, 'unpaid') = 'unpaid'"
      78            2 :        (audit/actor-id)
      79            1 :        class-id
      80            1 :        participant-id]))
      81            2 :    (count ids)))
      82              : 
      83              : 
      84            4 : (defn save-post-selection!
      85              :  "Remembers the payment methods of the event's live payment post."
      86              :  [ds event-id method-ids]
      87            4 :  (jdbc/execute!
      88            2 :   ds
      89            4 :   ["INSERT INTO event_payment_posts (event_id, payment_method_ids, updated_datetime)
      90              :          VALUES (?::uuid, ?::jsonb, now())
      91              :     ON CONFLICT (event_id) DO UPDATE
      92              :           SET payment_method_ids = EXCLUDED.payment_method_ids,
      93              :               updated_datetime = now()"
      94            2 :    event-id
      95           10 :    (json/generate-string (vec method-ids))]))
      96              : 
      97              : 
      98            4 : (defn post-selection
      99              :  "Payment method ids of the event's live payment post, or nil."
     100              :  [ds event-id]
     101           16 :  (some->
     102            2 :    (jdbc/execute-one!
     103            1 :     ds
     104            2 :     ["SELECT payment_method_ids::text AS ids FROM event_payment_posts WHERE event_id = ?::uuid"
     105            1 :      event-id]
     106            1 :     jdbc-options)
     107              :    :ids
     108            1 :    json/parse-string))
        

Generated by: LCOV version 2.0-1