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

            Line data    Source code
       1            4 : (ns matchscore.api.name-reminders
       2              :   "Handlers for nudging players to confirm their name: the organizer's reminder
       3              :    post in the event's Telegram chats, and the app reporting that the bot may now
       4              :    message the user privately."
       5              :   (:require
       6              :    [matchscore.db.postgres.name-reminders :as name-db]
       7              :    [matchscore.domain.event.repository.postgres :as event-postgres-repo]
       8              :    [matchscore.domain.telegram-link.query :as telegram-link-query]
       9              :    [matchscore.jobs.scheduler :as scheduler]
      10              :    [matchscore.jobs.telegram-name-reminder :as name-jobs]
      11              :    [matchscore.telegram.name-reminder :as name-reminder]
      12              :    [ring.util.http-status :as status]
      13              :    [schemas.api.core :as api-schemas]
      14              :    [taoensso.carmine :as car]))
      15              : 
      16              : 
      17            4 : (defn- reminder-status
      18              :   "{:unconfirmed-count :linked-chat-count :retry-after-seconds} for the event."
      19              :   [{:keys [pg repos redis goose]} event]
      20            6 :   (let [event-id (str (:uuid event))
      21           41 :         ttl      (car/wcar redis (car/ttl (name-reminder/group-key event-id)))]
      22            9 :     {:unconfirmed-count   (count (name-db/unconfirmed-event-participants pg event-id))
      23            1 :      :linked-chat-count   (count
      24            2 :                            (telegram-link-query/resolve-links-for-event
      25            6 :                             {:pg    pg
      26            1 :                              :repos repos
      27            1 :                              :redis redis
      28            1 :                              :goose goose}
      29            1 :                             event))
      30            9 :      :retry-after-seconds (when (and ttl (pos? ttl)) ttl)}))
      31              : 
      32              : 
      33            4 : (defn- find-event
      34              :   [repos event-id]
      35            7 :   (event-postgres-repo/find-by-id (:event-postgres-repo repos) event-id {}))
      36              : 
      37              : 
      38            4 : (defn event-name-reminder-status-handler
      39              :  {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/NameReminderStatusResponseSchema]
      40              :   :malli/parameters {:path api-schemas/EventPathParamsSchema}
      41              :   :authz {:obj :event
      42              :           :act :update
      43              :           :id  :route-params/event-uuid}
      44              :   :summary "Players whose name needs confirming"
      45              :   :description
      46              :   "How many players of the event have not confirmed their name, how many Telegram chats are linked, and how long until the next reminder can be posted."}
      47              :  [{:keys [repos route-params]
      48              :    :as   request}]
      49           11 :  (if-let [event (find-event repos (:event-uuid route-params))]
      50            4 :    {:status status/ok
      51            4 :     :body   {:message "Name reminder status."
      52            4 :              :data    (reminder-status request event)}}
      53            0 :    {:status status/not-found
      54            0 :     :body   {:message "Event not found."}}))
      55              : 
      56              : 
      57            4 : (defn event-name-reminder-post-handler
      58              :  {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/NameReminderPostResponseSchema]
      59              :   :malli/parameters {:path api-schemas/EventPathParamsSchema}
      60              :   :authz {:obj :event
      61              :           :act :update
      62              :           :id  :route-params/event-uuid}
      63              :   :summary "Ask players in Telegram to confirm their names"
      64              :   :description
      65              :   "Schedules one post to every Telegram chat linked to the event that mentions the players whose name is not confirmed and links to the profile. At most once per event per day."}
      66              :  [{:keys [repos redis goose route-params]
      67              :    :as   request}]
      68            4 :  (let [event-id (:event-uuid route-params)
      69            4 :        event    (find-event repos event-id)
      70            7 :        state    (when event (reminder-status request event))
      71            4 :        token    (str (random-uuid))]
      72            5 :    (cond
      73            4 :      (nil? event)
      74            0 :      {:status status/not-found
      75            0 :       :body   {:message "Event not found."}}
      76              : 
      77            4 :      (zero? (:unconfirmed-count state))
      78            0 :      {:status status/bad-request
      79            0 :       :body   {:message "Every player has confirmed their name."}}
      80              : 
      81            4 :      (zero? (:linked-chat-count state))
      82            4 :      {:status status/bad-request
      83            3 :       :body   {:message "Link a Telegram chat to the event first."}}
      84              : 
      85            7 :      (not (name-reminder/reserve-group-cooldown! redis event-id token))
      86            4 :      {:status status/too-many-requests
      87            4 :       :body   {:message "A name reminder was already posted for this event today."
      88            1 :                :data    state}}
      89              : 
      90              :      :else
      91            1 :      (do
      92            1 :        (try
      93            9 :          (scheduler/schedule-job goose `name-jobs/group-name-reminder-job {:args [event-id token]})
      94              :          (catch Throwable e
      95              :            ;; Nothing was queued: let the organizer try again instead of waiting a day.
      96            5 :            (name-reminder/release-group-cooldown! redis event-id token)
      97            2 :            (throw e)))
      98            4 :        {:status status/accepted
      99            4 :         :body   {:message "Name reminder post scheduled."
     100            4 :                  :data    (assoc state
     101            1 :                            :retry-after-seconds name-reminder/group-reminder-ttl-seconds)}}))))
     102              : 
     103              : 
     104            4 : (defn account-telegram-bot-access-handler
     105              :  {:malli/schema [:=> [:cat api-schemas/RingRequest] api-schemas/TelegramBotAccessResponseSchema]
     106              :   :authz {:obj     :account
     107              :           :act     :update
     108              :           :create? true}
     109              :   :summary "Record that the bot may message the user"
     110              :   :description
     111              :   "Called by the mini app after the user allows the bot to send them messages. If the claim is wrong, the next private message fails and access is marked denied again."}
     112              :  [{:keys [auth-headers]
     113              :    :as   request}]
     114            8 :  (if-let [telegram-id (:telegram/id auth-headers)]
     115            1 :    (do
     116            7 :      (name-jobs/set-bot-access! (:ctx request) telegram-id "confirmed")
     117            4 :      {:status status/ok
     118            3 :       :body   {:message "Telegram bot access recorded."}})
     119            0 :    {:status status/bad-request
     120            0 :     :body   {:message "No Telegram account."}}))
        

Generated by: LCOV version 2.0-1