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

            Line data    Source code
       1            4 : (ns matchscore.telegram.payment-reminder-rules
       2              :   (:require
       3              :    [clojure.string :as str]
       4              :    [matchscore.utils.rrule :as rrule])
       5              :   (:import
       6              :    (java.time Instant)))
       7              : 
       8              : 
       9            8 : (def ^:private hour-ms 3600000)
      10              : 
      11              : 
      12            4 : (defn- map-value
      13              :   [m key]
      14           12 :   (let [hyphen-name     (name key)
      15           15 :         underscore-name (str/replace hyphen-name "-" "_")]
      16            6 :     (some
      17            3 :      (fn [candidate]
      18           33 :       (when (contains? m candidate) [true (get m candidate)]))
      19           21 :      [key (keyword underscore-name) hyphen-name underscore-name])))
      20              : 
      21              : 
      22           22 : (defn- value [m key] (second (map-value m key)))
      23              : 
      24              : 
      25            4 : (defn resolve-config
      26              :  "Returns an event payment reminder override when its settings contain one,
      27              :    including an explicitly disabled override; otherwise returns the template
      28              :    reminder. Keyword, string, hyphenated, and underscored setting keys are accepted."
      29              :  [event template]
      30           12 :  (let [event-setting    (map-value
      31           43 :                          (or (:settings event) (get event "settings") {})
      32              :                          :payment-reminder)
      33            9 :        template-setting (map-value
      34           43 :                          (or (:settings template) (get template "settings") {})
      35              :                          :payment-reminder)]
      36           27 :    (if (first event-setting) (second event-setting) (second template-setting))))
      37              : 
      38              : 
      39            4 : (defn active-config
      40              :  "Returns the resolved reminder only when it is enabled and complete."
      41              :  [event template]
      42           15 :  (let [config (resolve-config event template)]
      43            5 :    (when
      44           28 :      (and
      45           18 :       (true? (value config :enabled))
      46            8 :       (value config :timing)
      47           12 :       (some? (value config :hours))
      48           10 :       (seq (value config :payment-method-ids)))
      49            2 :      config)))
      50              : 
      51              : 
      52            4 : (defn- non-blank
      53              :   [value]
      54           69 :   (some->
      55            3 :     value
      56            2 :     str
      57            2 :     str/trim
      58            2 :     not-empty))
      59              : 
      60              : 
      61            4 : (defn resolve-purpose
      62              :  "Most specific wins: the event's purpose, then the template's, then the
      63              :   method's own purpose from the profile. Never shown for cash."
      64              :  [method event template]
      65           26 :  (when-not (= "cash" (value method :kind))
      66           24 :    (or
      67           42 :     (some->
      68           12 :       (resolve-config event nil)
      69            6 :       (value :payment-purpose)
      70            1 :       non-blank)
      71           45 :     (some->
      72           12 :       (resolve-config {} template)
      73            6 :       (value :payment-purpose)
      74            2 :       non-blank)
      75           15 :     (non-blank (value method :purpose)))))
      76              : 
      77              : 
      78            4 : (defn post-at
      79              :  "Returns the reminder posting instant relative to the event start instant."
      80              :  [config ^Instant start]
      81           16 :  (let [seconds (* 3600 (long (value config :hours)))]
      82           12 :    (case (value config :timing)
      83            4 :      "before-start" (.minusSeconds start seconds)
      84            4 :      "after-start"  (.plusSeconds start seconds))))
      85              : 
      86              : 
      87            4 : (defn due?
      88              :  "True during the one-hour delivery window unless the event is cancelled.
      89              :    Completed events remain eligible only for after-start reminders."
      90              :  [config event ^Instant now]
      91           10 :  (let [timing  (value config :timing)
      92           14 :        state   (or (:state event) (get event "state"))
      93            8 :        start   (or
      94            6 :                 (:datetime-start event)
      95            0 :                 (get event "datetime-start")
      96            0 :                 (get event "datetime_start"))
      97            8 :        post-at (post-at config start)]
      98           25 :    (and
      99            8 :     (not= "cancelled" state)
     100           24 :     (not (and (= "before-start" timing) (= "completed" state)))
     101            8 :     (not (.isBefore now post-at))
     102            4 :     (.isBefore now (.plusSeconds post-at 3600)))))
     103              : 
     104              : 
     105            4 : (defn max-hours
     106              :  "Returns the allowed reminder hours, clamped to 0..168. gap-ms and duration-ms
     107              :    are milliseconds between occurrence starts and event duration respectively."
     108              :  [timing gap-ms duration-ms]
     109           11 :  (if (nil? gap-ms)
     110              :    168
     111            8 :    (let [hours (case timing
     112           18 :                  "before-start" (quot (- gap-ms (or duration-ms 0)) hour-ms)
     113            4 :                  "after-start"  (quot (dec gap-ms) hour-ms))]
     114           12 :      (long (max 0 (min 168 hours))))))
     115              : 
     116              : 
     117            4 : (defn template-max-hours
     118              :  "Returns the schedule-aware maximum reminder hours for a template. Template
     119              :    duration and recurrence gaps are measured in milliseconds."
     120              :  [timing template ^Instant now]
     121           13 :  (if (seq (:rrule template))
     122           24 :    (max-hours timing (rrule/min-occurrence-gap-ms (:rrule template) now) (:duration template))
     123              :    168))
        

Generated by: LCOV version 2.0-1