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

            Line data    Source code
       1            4 : (ns matchscore.telegram.template-summary
       2              :   (:require
       3              :    [clojure.string :as str]
       4              :    [matchscore.utils.rrule :as rrule])
       5              :   (:import
       6              :    (java.time        Instant ZoneId ZonedDateTime)
       7              :    (java.time.format DateTimeFormatter)))
       8              : 
       9              : 
      10           16 : (def ^:private time-formatter (DateTimeFormatter/ofPattern "HH:mm"))
      11              : 
      12              : 
      13           16 : (def ^:private date-formatter (DateTimeFormatter/ofPattern "dd.MM.yyyy"))
      14              : 
      15              : 
      16            4 : (def ^:private weekday-names
      17           72 :   {:en {"MO" "Monday"
      18              :         "TU" "Tuesday"
      19              :         "WE" "Wednesday"
      20              :         "TH" "Thursday"
      21              :         "FR" "Friday"
      22              :         "SA" "Saturday"
      23              :         "SU" "Sunday"}
      24           60 :    :uk {"MO" "понеділок"
      25              :         "TU" "вівторок"
      26              :         "WE" "середу"
      27              :         "TH" "четвер"
      28              :         "FR" "п’ятницю"
      29              :         "SA" "суботу"
      30              :         "SU" "неділю"}})
      31              : 
      32              : 
      33           39 : (defn- value-at [m k] (or (get m k) (get m (name k))))
      34              : 
      35              : 
      36            4 : (defn- recurrence-data
      37              :   [template]
      38           17 :   (or
      39           20 :    (when (seq (:rrule template))
      40            2 :      (try
      41           14 :        (rrule/vevent->rrule-map (rrule/parse-rrule-string (:rrule template)))
      42              :        (catch Exception _ nil)))
      43            3 :    (:rrule-json template)))
      44              : 
      45              : 
      46            4 : (defn- zone-id
      47              :   [template recurrence]
      48            2 :   (try
      49           18 :     (ZoneId/of (or (:timezone template) (value-at recurrence :timezone) "UTC"))
      50            0 :     (catch Exception _ (ZoneId/of "UTC"))))
      51              : 
      52              : 
      53            4 : (defn- start-at
      54              :   [template recurrence]
      55           27 :   (when-let [value (value-at recurrence :dtstart)]
      56            2 :     (try
      57           22 :       (ZonedDateTime/ofInstant (Instant/parse (str value)) (zone-id template recurrence))
      58              :       (catch Exception _ nil))))
      59              : 
      60              : 
      61            4 : (defn- english-list
      62              :   [values]
      63            8 :   (case (count values)
      64              :     0 ""
      65            6 :     1 (first values)
      66            9 :     2 (str (first values) " and " (second values))
      67            0 :     (str (str/join ", " (butlast values)) ", and " (last values))))
      68              : 
      69              : 
      70            4 : (defn- localized-list
      71              :   [locale values]
      72           18 :   (if (= :uk locale) (str/join ", " values) (english-list values)))
      73              : 
      74              : 
      75            4 : (defn- weekday-code
      76              :   [^ZonedDateTime start]
      77           22 :   (some->
      78            1 :     start
      79              :     .getDayOfWeek
      80              :     .name
      81            1 :     (subs 0 2)))
      82              : 
      83              : 
      84            4 : (defn- weekday-labels
      85              :   [locale recurrence start]
      86           14 :   (let [configured (seq (value-at recurrence :byday))
      87           17 :         codes      (or configured (when start [(weekday-code start)]))]
      88            2 :     (->>
      89            2 :       codes
      90            6 :       (map
      91           18 :        #(let [code (last (re-find #"(?:[+-]?\d+)?([A-Z]{2})$" (str %)))]
      92           20 :           (or (get-in weekday-names [locale code]) code)))
      93            6 :       (remove str/blank?)
      94            2 :       vec)))
      95              : 
      96              : 
      97            4 : (defn- interval-label
      98              :   [locale freq interval]
      99           15 :   (let [n (long (or interval 1))]
     100            8 :     (if (= :uk locale)
     101            3 :       (case freq
     102            0 :         :daily   (if (= 1 n) "Щодня" (str "Кожні " n " дні"))
     103            5 :         :weekly  (if (= 1 n) "Щотижня" (str "Кожні " n " тижні"))
     104            0 :         :monthly (if (= 1 n) "Щомісяця" (str "Кожні " n " місяці"))
     105            0 :         :yearly  (if (= 1 n) "Щороку" (str "Кожні " n " роки"))
     106            0 :         :hourly  (if (= 1 n) "Щогодини" (str "Кожні " n " години"))
     107            0 :         (str
     108              :          "Повторюється ("
     109            0 :          (some->
     110            0 :            freq
     111            0 :            name
     112            0 :            str/upper-case)
     113              :          ")"))
     114            6 :       (case freq
     115            0 :         :daily   (if (= 1 n) "Every day" (str "Every " n " days"))
     116           15 :         :weekly  (if (= 1 n) "Every week" (str "Every " n " weeks"))
     117            0 :         :monthly (if (= 1 n) "Every month" (str "Every " n " months"))
     118            0 :         :yearly  (if (= 1 n) "Every year" (str "Every " n " years"))
     119            0 :         :hourly  (if (= 1 n) "Every hour" (str "Every " n " hours"))
     120            0 :         (str
     121              :          "Repeats ("
     122            0 :          (some->
     123            0 :            freq
     124            0 :            name
     125            0 :            str/upper-case)
     126              :          ")")))))
     127              : 
     128              : 
     129            4 : (defn- recurrence-details
     130              :   [locale template recurrence]
     131           18 :   (let [freq       (some->
     132            8 :                      (value-at recurrence :freq)
     133            2 :                      keyword)
     134            8 :         interval   (value-at recurrence :interval)
     135            8 :         start      (start-at template recurrence)
     136           10 :         weekdays   (weekday-labels locale recurrence start)
     137           12 :         month-days (seq (value-at recurrence :bymonthday))
     138           10 :         base       (interval-label locale freq interval)
     139           20 :         day-part   (cond (and (= :weekly freq) (seq weekdays))
     140           23 :                          (str (if (= :uk locale) " у " " on ") (localized-list locale weekdays))
     141              : 
     142            0 :                          (and (= :monthly freq) month-days)
     143            0 :                          (str
     144            0 :                           (if (= :uk locale) " у день місяця " " on day ")
     145            0 :                           (str/join ", " month-days))
     146              : 
     147              :                          :else "")
     148            6 :         time-part  (when start
     149           19 :                      (str (if (= :uk locale) " о " " at ") (.format start time-formatter)))]
     150           10 :     (str base day-part time-part)))
     151              : 
     152              : 
     153            4 : (defn format-schedule
     154              :  "Returns a human-readable template occurrence schedule."
     155              :  [locale template]
     156           19 :  (let [locale     (if (= :uk locale) :uk :en)
     157            9 :        recurrence (recurrence-data template)
     158           25 :        freq       (some->
     159           12 :                     (value-at recurrence :freq)
     160            2 :                     keyword)
     161           12 :        start      (start-at template recurrence)]
     162           22 :    (cond freq (recurrence-details locale template recurrence)
     163              : 
     164            3 :          start
     165            0 :          (str
     166            0 :           (if (= :uk locale) "Один раз " "Once on ")
     167            0 :           (.format start date-formatter)
     168            0 :           (if (= :uk locale) " о " " at ")
     169            0 :           (.format start time-formatter))
     170              : 
     171           15 :          :else (if (= :uk locale) "Розклад повторень не налаштовано" "No recurring schedule"))))
     172              : 
     173              : 
     174            4 : (defn- template-label
     175              :   [locale template]
     176           48 :   (let [name (some->
     177            9 :                (:name template)
     178            3 :                str
     179            3 :                str/trim)]
     180           15 :     (if (seq name) name (if (= :uk locale) "Шаблон без назви" "Unnamed template"))))
     181              : 
     182              : 
     183            4 : (defn- format-fee-amount
     184              :   [fee]
     185           24 :   (let [amount (cond (integer? fee) (bigdec fee)
     186           14 :                      (instance? java.math.BigDecimal fee) fee
     187            3 :                      (number? fee)  (bigdec fee)
     188              :                      :else          nil)]
     189           15 :     (when amount (.toPlainString (.stripTrailingZeros ^java.math.BigDecimal amount)))))
     190              : 
     191              : 
     192            4 : (defn- format-price
     193              :   [locale {:keys [fee fee-type currency]}]
     194           27 :   (when-let [amount (format-fee-amount fee)]
     195           69 :     (let [currency   (some->
     196            3 :                        currency
     197            3 :                        str
     198            3 :                        str/trim
     199            3 :                        not-empty)
     200            9 :           type-label (case fee-type
     201            0 :                        "total"      (if (= :uk locale) "загалом" "total")
     202           15 :                        "per-person" (if (= :uk locale) "з людини" "per person")
     203              :                        nil)]
     204           54 :       (str amount (when currency (str " " currency)) (when type-label (str " (" type-label ")"))))))
     205              : 
     206              : 
     207            4 : (defn- format-club
     208              :   [club]
     209            5 :   (let [location (value-at club :location)
     210            5 :         venue    (let [name    (value-at club :name)
     211            4 :                        network (value-at club :network)]
     212           20 :                    (cond (and (seq name) (seq network)) (str name " (" network ")")
     213            3 :                          (seq name)    name
     214            3 :                          (seq network) network))
     215            2 :         parts    (remove
     216            1 :                   str/blank?
     217            5 :                   [(value-at club :country)
     218            4 :                    (value-at club :city)
     219            1 :                    venue
     220            4 :                    (value-at location :address)])]
     221            9 :     (when (seq parts) (str/join " / " parts))))
     222              : 
     223              : 
     224            4 : (defn- format-location
     225              :   [template]
     226           31 :   (let [clubs  (if (contains? template :clubs) (:clubs template) [(:club template)])
     227            6 :         labels (->>
     228            3 :                  clubs
     229            9 :                  (keep format-club)
     230            3 :                  distinct
     231            3 :                  vec)]
     232           17 :     (when (seq labels) (str/join "; " labels))))
     233              : 
     234              : 
     235            4 : (defn- format-details
     236              :   [locale template]
     237           69 :   (let [public-id (some->
     238            9 :                     (:public-id template)
     239            3 :                     str
     240            3 :                     str/trim
     241            3 :                     not-empty)
     242            9 :         location  (format-location template)
     243           12 :         price     (format-price locale template)]
     244           31 :     (cond-> []
     245           24 :       public-id (conj (str "🆔 " public-id))
     246           30 :       true      (conj (str "🗓 " (format-schedule locale template)))
     247           10 :       location  (conj (str "📍 " location))
     248           18 :       price     (conj (str "💳 " price)))))
     249              : 
     250              : 
     251            4 : (defn format-linked-templates
     252              :  "Formats every template linked to one Telegram chat/forum topic."
     253              :  [locale templates]
     254           18 :  (let [locale (if (= :uk locale) :uk :en)
     255           12 :        header (if (= :uk locale)
     256            0 :                 (str "📋 Шаблони в цьому чаті (" (count templates) ")")
     257           18 :                 (str "📋 Templates in this chat (" (count templates) ")"))]
     258            9 :    (str
     259            3 :     header
     260              :     "\n\n"
     261            6 :     (->>
     262            3 :       templates
     263            6 :       (map-indexed
     264            3 :        (fn [idx template]
     265           12 :         (str
     266            6 :          (inc idx)
     267           12 :          ". "    (template-label locale template)
     268           21 :          "\n   " (str/join "\n   " (format-details locale template)))))
     269            3 :       (str/join "\n\n")))))
        

Generated by: LCOV version 2.0-1