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

            Line data    Source code
       1            4 : (ns matchscore.telegram.payment-details-message
       2              :   (:require
       3              :    [clojure.string :as str]
       4              :    [matchscore.banks :as banks]
       5              :    [matchscore.telegram.formatting :as formatting]
       6              :    [matchscore.telegram.localization :as l10n]
       7              :    [matchscore.telegram.payment-reminder-rules :as rules]))
       8              : 
       9              : 
      10            4 : (def post-kinds
      11              :   "Telegram message kinds of the live payment post: the scheduled reminder and
      12              :    a manual send."
      13           12 :   #{"payment_details_post" "payment_details_manual_post"})
      14              : 
      15              : 
      16           19 : (defn- escaped [value] (formatting/escape-html (str value)))
      17              : 
      18              : 
      19            4 : (defn- bank-name
      20              :   [method locale]
      21           18 :   (if (= banks/other-bank-id (:bank-id method))
      22            8 :     (some->
      23            3 :       (:bank-name method)
      24            1 :       escaped)
      25           61 :     (some->
      26            9 :       (:bank-id method)
      27            1 :       banks/find-bank
      28            4 :       (banks/display-name locale)
      29            1 :       escaped)))
      30              : 
      31              : 
      32            4 : (defn- label-names-bank?
      33              :   "True when the label already mentions the bank (\"monobank · Картка\" for
      34              :    \"monobank (Універсал Банк)\"), so the title does not repeat it."
      35              :   [label bank]
      36           10 :   (let [short-name (str/lower-case (str/trim (str/replace bank #"\s*\(.*\)" "")))]
      37           14 :     (and (seq short-name) (str/includes? (str/lower-case (str label)) short-name))))
      38              : 
      39              : 
      40            4 : (defn- title-line
      41              :   [icon method locale]
      42           12 :   (str
      43            3 :    icon
      44           18 :    (when-not (str/blank? icon) " ")
      45           15 :    "<b>"  (escaped (:label method))
      46           24 :    "</b>" (when-let [bank (bank-name method locale)]
      47           13 :             (when-not (label-names-bank? (:label method) bank) (str " · " bank)))))
      48              : 
      49              : 
      50            4 : (defn- method-lines
      51              :   [event
      52              :    template
      53              :    locale
      54              :    {:keys [kind value note recipient-name tax-id]
      55              :     :as   method}]
      56           18 :   (let [purpose (rules/resolve-purpose method event template)]
      57           29 :     (cond->
      58            9 :       (case kind
      59           39 :         "card" [(title-line "💳" method locale) (str "<code>" (escaped value) "</code>")]
      60           26 :         "iban" [(title-line "🏦" method locale) (str "<code>" (escaped value) "</code>")]
      61            6 :         "link" [(title-line "🔗" method locale)
      62           11 :                 (str "<a href=\"" (escaped value) "\">" (escaped value) "</a>")]
      63            6 :         "cash" [(title-line "💵" method locale)]
      64           13 :         [(title-line "📝" method locale) (str "<code>" (escaped value) "</code>")])
      65           31 :       (and (contains? #{"card" "iban"} kind) recipient-name)
      66            3 :       (conj
      67            4 :        (str
      68            4 :         (l10n/t locale :payment-reminder/recipient)
      69              :         ": <code>"
      70            3 :         (escaped recipient-name)
      71              :         "</code>"))
      72              : 
      73           23 :       (and (= "iban" kind) tax-id)
      74            3 :       (conj
      75            4 :        (str
      76            2 :         (l10n/t
      77            1 :          locale
      78            6 :          (if (= 10 (count tax-id)) :payment-reminder/tax-id-rnokpp :payment-reminder/tax-id-edrpou))
      79              :         ": <code>"
      80            3 :         (escaped tax-id)
      81              :         "</code>"))
      82              : 
      83            3 :       purpose
      84            3 :       (conj
      85            4 :        (str
      86            9 :         (l10n/t locale (if (= "link" kind) :payment-reminder/comment :payment-reminder/purpose))
      87              :         ": <code>"
      88            3 :         (escaped purpose)
      89              :         "</code>"))
      90              : 
      91           63 :       (some->
      92            3 :         note
      93            1 :         str
      94            1 :         str/trim
      95            1 :         not-empty)
      96            4 :       (conj (escaped note)))))
      97              : 
      98              : 
      99            4 : (defn player-statuses
     100              :  "Collapses class registrations into one entry per player, in registration
     101              :    order. A player is paid when every registration is paid, claimed when each
     102              :    is paid or claimed (at least one claimed), and unpaid otherwise."
     103              :  [registrations]
     104           10 :  (let [groups (group-by :participant-id registrations)]
     105            2 :    (->>
     106            2 :      registrations
     107            6 :      (map :participant-id)
     108            2 :      distinct
     109            4 :      (mapv
     110            2 :       (fn [participant-id]
     111            8 :        (let [rows     (get groups participant-id)
     112           12 :              statuses (set (map :payment-status rows))]
     113           12 :          {:participant-id participant-id
     114           10 :           :user-id        (:user-id (first rows))
     115           10 :           :name           (:name (first rows))
     116           20 :           :status         (cond (= #{"paid"} statuses) :paid
     117           12 :                                 (every? #{"paid" "claimed"} statuses) :claimed
     118              :                                 :else :unpaid)}))))))
     119              : 
     120              : 
     121            4 : (defn tracking-summary
     122              :  [players]
     123           10 :  (let [by-status (group-by :status players)]
     124           22 :    {:paid     (vec (:paid by-status))
     125           10 :     :claimed  (vec (:claimed by-status))
     126            8 :     :unpaid   (count (:unpaid by-status))
     127            4 :     :total    (count players)
     128           30 :     :all-paid (and (seq players) (every? #(= :paid (:status %)) players))}))
     129              : 
     130              : 
     131            8 : (def ^:private max-listed-names 30)
     132              : 
     133              : 
     134            4 : (defn- name-list
     135              :   [locale players]
     136           10 :   (let [names (map #(escaped (:name %)) players)
     137            4 :         shown (take max-listed-names names)
     138            5 :         more  (- (count names) (count shown))]
     139            2 :     (str
     140            4 :      (str/join ", " shown)
     141            3 :      (when (pos? more) (str " " (l10n/t locale :payment-tracking/and-more more))))))
     142              : 
     143              : 
     144            4 : (defn- tracking-lines
     145              :   [locale {:keys [paid claimed unpaid total]}]
     146           21 :   (when (and total (pos? total))
     147            6 :     (cond->
     148            4 :       [(str
     149              :         "✅ "
     150            7 :         (l10n/t locale :payment-tracking/paid (count paid) total)
     151           12 :         (when (seq paid) (str ": " (name-list locale paid))))]
     152            6 :       (seq claimed) (conj
     153            4 :                      (str
     154            4 :                       "🕓 " (l10n/t locale :payment-tracking/claimed)
     155            4 :                       ": "  (name-list locale claimed)))
     156           11 :       (pos? unpaid) (conj (str "⏳ " (l10n/t locale :payment-tracking/unpaid unpaid))))))
     157              : 
     158              : 
     159            4 : (defn- header-lines
     160              :   [event locale]
     161           12 :   (let [datetime (formatting/event-datetime-detail event)
     162           12 :         fee      (formatting/format-fee-detail locale event)
     163           15 :         header   (str
     164              :                   "💳 <b>"
     165           12 :                   (l10n/t locale :payment-reminder/title)
     166              :                   "</b>: <b>"
     167           21 :                   (formatting/escape-html (str (:name event)))
     168              :                   "</b>")]
     169           21 :     (cond-> [header]
     170           27 :       datetime (conj (l10n/t locale :payment-reminder/datetime datetime))
     171           15 :       fee      (conj (l10n/t locale :payment-reminder/fee fee)))))
     172              : 
     173              : 
     174            4 : (defn format-closed-message
     175              :  "The payment post once it no longer takes payments: `reason` is :all-paid,
     176              :    :cancelled, or :replaced (a newer post took its place)."
     177              :  [event locale reason tracking]
     178            6 :  (str
     179           14 :   (str/join "\n" (header-lines event locale))
     180              :   "\n\n"
     181            6 :   (case reason
     182           10 :     :all-paid  (str "✅ " (l10n/t locale :payment-tracking/all-paid (:total tracking)))
     183            7 :     :cancelled (str "❌ " (l10n/t locale :payment-tracking/cancelled))
     184           14 :     :replaced  (str "⬇️ " (l10n/t locale :payment-tracking/replaced)))))
     185              : 
     186              : 
     187            4 : (defn format-message
     188            7 :  ([event template methods locale] (format-message event template methods locale nil))
     189              :  ([event template methods locale tracking]
     190           15 :   (let [details  (header-lines event locale)
     191              :         ;; Methods of several organizers are grouped under each owner's name,
     192              :         ;; so players know who to pay (and who takes the cash).
     193           12 :         groups   (partition-by :user-id methods)
     194           12 :         several? (< 1 (count groups))
     195           90 :         blocks   (for [group groups
     196           15 :                        :let  [owner (:owner-name (first group))
     197            9 :                               lines (map
     198           27 :                                      #(str/join "\n" (method-lines event template locale %))
     199            3 :                                      group)]]
     200            6 :                    (str
     201           24 :                     (when (and several? (not (str/blank? owner)))
     202            7 :                       (str "👤 <b>" (escaped owner) "</b>\n"))
     203           12 :                     (str/join "\n\n" lines)))]
     204            9 :     (str/join
     205              :      "\n\n"
     206           38 :      (cond-> [(str/join "\n" details) (str/join "\n\n" blocks)]
     207           18 :        (seq (tracking-lines locale tracking))
     208            8 :        (conj (str/join "\n" (tracking-lines locale tracking))))))))
     209              : 
     210              : 
     211           28 : (defn- truncate [value] (subs value 0 (min 40 (count value))))
     212              : 
     213              : 
     214            4 : (defn copy-keyboard
     215            5 :  ([methods locale] (copy-keyboard methods locale false))
     216              :  ([methods locale claim-button?]
     217            9 :   (let [rows (mapcat
     218            3 :               (fn [{:keys [kind label value resolved-purpose]}]
     219           24 :                (when (contains? #{"card" "iban"} kind)
     220           11 :                  (cond->
     221           33 :                    [[{:text      (truncate (str "📋 " label))
     222            9 :                       :copy_text {:text value}}]]
     223           23 :                    (and (= "iban" kind) resolved-purpose)
     224            1 :                    (conj
     225            6 :                     [{:text      (truncate
     226            7 :                                   (str "📋 " (l10n/t locale :payment-reminder/copy-purpose)))
     227            3 :                       :copy_text {:text resolved-purpose}}]))))
     228            3 :               methods)
     229           20 :         rows (cond-> (vec rows)
     230            4 :                claim-button? (conj
     231            9 :                               [{:text (l10n/t locale :payment-tracking/claim-button)
     232              :                                 :callback_data "p"}]))]
     233           24 :     (when (seq rows) {:inline_keyboard rows}))))
        

Generated by: LCOV version 2.0-1