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

            Line data    Source code
       1            4 : (ns matchscore.db.postgres.name-reminders
       2              :   "Queries behind the Telegram nudges that ask players to confirm their name.
       3              :    A name needs confirming while `users.name_confirmed_at` is NULL; locking a
       4              :    name in the admin page also sets it, so locked names are never nudged."
       5              :   (:require
       6              :    [next.jdbc :as jdbc]
       7              :    [next.jdbc.result-set :as rs]))
       8              : 
       9              : 
      10            8 : (def max-reminders
      11              :   "Private reminders a user gets at most; confirming the name stops them earlier."
      12              :   3)
      13              : 
      14              : 
      15            8 : (def reminder-interval-days "Days between two private reminders." 7)
      16              : 
      17              : 
      18           16 : (def ^:private kebab {:builder-fn rs/as-unqualified-kebab-maps})
      19              : 
      20              : 
      21            8 : (def ^:private needs-name-sql
      22              :   "NOT u.is_fake AND u.telegram_id IS NOT NULL AND u.name_confirmed_at IS NULL")
      23              : 
      24              : 
      25            4 : (defn due-user-ids
      26              :  "Users the bot may message privately whose name still needs confirming and who
      27              :   are due a reminder, oldest reminder first. At most `limit` ids."
      28              :  [ds limit]
      29            3 :  (mapv
      30              :   :uuid
      31            2 :   (jdbc/execute!
      32            1 :    ds
      33            9 :    [(str
      34              :      "SELECT u.uuid FROM users u WHERE "
      35            1 :      needs-name-sql
      36              :      " AND u.telegram_bot_access_status = 'confirmed'"
      37              :      " AND u.name_reminder_count < ?" " AND (u.name_reminder_sent_at IS NULL"
      38              :      "      OR u.name_reminder_sent_at <= now() - make_interval(days => ?::int))"
      39              :      " ORDER BY u.name_reminder_sent_at NULLS FIRST, u.uuid LIMIT ?")
      40            1 :     max-reminders
      41            1 :     reminder-interval-days
      42            1 :     limit]
      43            1 :    kebab)))
      44              : 
      45              : 
      46            4 : (defn claim-reminder!
      47              :  "Counts a private reminder for the user if one is still due, and returns the user
      48              :   row ({:uuid :name :telegram-id :settings :name-reminder-count}); nil when it is not
      49              :   due (already sent, name confirmed, bot not allowed). Claiming before sending means
      50              :   a failed send loses that reminder instead of risking a duplicate."
      51              :  [ds user-id]
      52            2 :  (jdbc/execute-one!
      53            1 :   ds
      54           11 :   [(str
      55              :     "UPDATE users u SET name_reminder_count = u.name_reminder_count + 1,"
      56              :     " name_reminder_sent_at = now()"
      57              :     " WHERE u.uuid = ?::uuid AND "
      58            1 :     needs-name-sql
      59              :     " AND u.telegram_bot_access_status = 'confirmed'"
      60              :     " AND u.name_reminder_count < ?" " AND (u.name_reminder_sent_at IS NULL"
      61              :     "      OR u.name_reminder_sent_at <= now() - make_interval(days => ?::int))"
      62              :     " RETURNING u.uuid, u.name, u.telegram_id, u.settings, u.name_reminder_count")
      63            3 :    (str user-id)
      64            1 :    max-reminders
      65            1 :    reminder-interval-days]
      66            1 :   kebab))
      67              : 
      68              : 
      69            4 : (defn mark-reminder-sent!
      70              :  "Records a private name message sent outside the reminder schedule (for example the
      71              :   answer to /start), so the next scheduled reminder keeps its gap."
      72              :  [ds user-id]
      73            2 :  (jdbc/execute-one!
      74            1 :   ds
      75            5 :   ["UPDATE users SET name_reminder_sent_at = now() WHERE uuid = ?::uuid" (str user-id)]))
      76              : 
      77              : 
      78            4 : (defn latest-event-language
      79              :  "Language of the event the user joined most recently, or nil. Players who joined
      80              :   from a group chat never picked an app language, so this is the best guess for the
      81              :   language of a private message to them."
      82              :  [ds user-id]
      83            4 :  (:language
      84            4 :   (jdbc/execute-one!
      85            2 :    ds
      86            4 :    ["SELECT e.language FROM event_participants ep
      87              :        JOIN participants p ON p.uuid = ep.participant_id
      88              :        JOIN events e ON e.uuid = ep.event_id
      89              :       WHERE p.user_id = ?::uuid
      90              :       ORDER BY ep.created_datetime DESC
      91              :       LIMIT 1"
      92            6 :     (str user-id)]
      93            2 :    kebab)))
      94              : 
      95              : 
      96            4 : (defn unconfirmed-event-participants
      97              :  "Players of the event (not withdrawn) whose name still needs confirming and who
      98              :   have Telegram, by name: [{:uuid :name :telegram-id}]."
      99              :  [ds event-id]
     100            2 :  (jdbc/execute!
     101            1 :   ds
     102            8 :   [(str
     103              :     "SELECT DISTINCT u.uuid, u.name, u.telegram_id FROM event_participants ep"
     104              :     " JOIN participants p ON p.uuid = ep.participant_id"
     105              :     " JOIN users u ON u.uuid = p.user_id"
     106              :     " WHERE ep.event_id = ?::uuid AND ep.status IS DISTINCT FROM 'withdrawn' AND "
     107            1 :     needs-name-sql
     108              :     " ORDER BY u.name")
     109            3 :    (str event-id)]
     110            1 :   kebab))
     111              : 
     112              : 
     113            4 : (defn set-bot-access!
     114              :  "Records whether the bot may message the Telegram user privately: \"confirmed\"
     115              :   after they start the bot or allow it in the app, \"denied\" after they block it.
     116              :   Returns the number of users updated."
     117              :  [ds telegram-id status]
     118            4 :  (:next.jdbc/update-count
     119            4 :   (jdbc/execute-one!
     120            2 :    ds
     121            4 :    ["UPDATE users
     122              :         SET telegram_bot_access_status = ?,
     123              :             telegram_bot_access_updated_at = now()
     124              :       WHERE telegram_id = ?
     125              :         AND telegram_bot_access_status IS DISTINCT FROM ?"
     126            2 :     status
     127            2 :     telegram-id
     128            2 :     status])))
        

Generated by: LCOV version 2.0-1