LCOV - code coverage report
Current view: top level - matchscore/domain/payment_reminder - validation.clj Coverage Total Hit
Test: lcov.info Lines: 95.8 % 95 91
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.payment-reminder.validation
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [clojure.set :as set]
       6              :    [matchscore.db.postgres.payment-methods :as payment-methods]
       7              :    [matchscore.db.postgres.payment-reminder-neighbours :as neighbours]
       8              :    [matchscore.telegram.payment-reminder-rules :as rules]
       9              :    [matchscore.utils.audit :as audit]
      10              :    [matchscore.utils.errors :as utils.errors]
      11              :    [matchscore.utils.rrule :as rrule])
      12              :   (:import
      13              :    (java.time Instant OffsetDateTime ZonedDateTime)
      14              :    (java.util Date)))
      15              : 
      16              : 
      17           24 : (def ^:private neighbour-window-seconds (* 169 60 60))
      18              : 
      19              : 
      20            8 : (def ^:private same-occurrence-tolerance-ms 60000)
      21              : 
      22              : 
      23           28 : (defn- reminder [entity] (get-in entity [:settings :payment-reminder]))
      24              : 
      25              : 
      26           27 : (defn- selected-ids [entity] (set (or (:payment-method-ids (reminder entity)) [])))
      27              : 
      28              : 
      29            4 : (defn- foreign-method-error
      30              :   []
      31            3 :   (utils.errors/build-error
      32              :    :check
      33            5 :    {:code    :error.payment-reminder/foreign-method
      34              :     :message "You can only attach your own payment details."}))
      35              : 
      36              : 
      37            4 : (defn- check-ownership
      38              :   [{:keys [pg]} existing merged]
      39           24 :   (if-let [actor-id (audit/actor-id)]
      40           18 :     (let [new-ids (set/difference (selected-ids merged) (selected-ids existing))
      41            2 :           owned   (->>
      42            8 :                     (payment-methods/find-live-by-ids pg new-ids)
      43           11 :                     (filter #(= actor-id (:user-id %)))
      44            6 :                     (map :uuid)
      45            2 :                     set)]
      46           18 :       (if (= new-ids owned) (either/right merged) (either/left (foreign-method-error))))
      47           12 :     (either/right merged)))
      48              : 
      49              : 
      50            4 : (defn- hours-error
      51              :   [timing max-hours]
      52            3 :   (utils.errors/build-error
      53              :    :check
      54            5 :    {:code    :error.payment-reminder/hours-exceed-schedule
      55            5 :     :message (str
      56              :               "Payment details can be posted at most "
      57            1 :               max-hours
      58              :               " hours "
      59            6 :               (if (= timing "before-start") "before" "after")
      60              :               " the start for this schedule")
      61            4 :     :context {:max-hours max-hours
      62            1 :               :timing    timing}}))
      63              : 
      64              : 
      65            4 : (defn- check-hours
      66              :   [entity config max-hours]
      67           12 :   (if (<= (:hours config) max-hours)
      68            6 :     (either/right entity)
      69            8 :     (either/left (hours-error (:timing config) max-hours))))
      70              : 
      71              : 
      72            4 : (defn- active-own-config
      73              :   [entity]
      74           44 :   (let [config (reminder entity)] (when (true? (:enabled config)) config)))
      75              : 
      76              : 
      77            4 : (defn check-template
      78              :  "Checks newly selected payment methods and the template's schedule-aware hours."
      79              :  [ctx existing-template merged-template]
      80           16 :  (let [config (active-own-config merged-template)]
      81           48 :    (c/mlet [_ (check-ownership ctx existing-template merged-template)]
      82            8 :      (if config
      83            4 :        (check-hours
      84            2 :         merged-template
      85            2 :         config
      86           16 :         (rules/template-max-hours (:timing config) merged-template (Instant/now)))
      87           12 :        (either/right merged-template)))))
      88              : 
      89              : 
      90            4 : (defn- ->instant
      91              :   [value]
      92            7 :   (cond (instance? Instant value)        value
      93            4 :         (instance? OffsetDateTime value) (.toInstant ^OffsetDateTime value)
      94            5 :         (instance? ZonedDateTime value)  (.toInstant ^ZonedDateTime value)
      95            0 :         (instance? Date value)           (.toInstant ^Date value)
      96            0 :         :else                            value))
      97              : 
      98              : 
      99            4 : (defn- recurrence-starts
     100              :   [template ^Instant from ^Instant to]
     101            6 :   (if (seq (:rrule template))
     102            1 :     (->>
     103           13 :       (rrule/get-occurrences (rrule/parse-rrule-string (:rrule template)) (str from) (str to))
     104            5 :       (map (comp ->instant :start)))
     105            0 :     []))
     106              : 
     107              : 
     108            4 : (defn- event-max-hours
     109              :   [{:keys [pg]} event template timing]
     110            4 :   (let [start       (:datetime-start event)
     111            2 :         from        (.minusSeconds ^Instant start neighbour-window-seconds)
     112            2 :         to          (.plusSeconds ^Instant start neighbour-window-seconds)
     113            1 :         not-current (fn [^Instant candidate]
     114            1 :                      (>
     115            5 :                       (Math/abs (- (.toEpochMilli candidate) (.toEpochMilli start)))
     116            1 :                       same-occurrence-tolerance-ms))
     117            2 :         starts      (->>
     118            2 :                       (concat
     119            5 :                        (recurrence-starts template from to)
     120            2 :                        (map
     121            1 :                         ->instant
     122           11 :                         (neighbours/sibling-starts pg (:template-id event) (:uuid event) from to)))
     123            3 :                       (filter not-current)
     124            1 :                       distinct
     125            1 :                       sort)
     126            8 :         prev        (last (take-while #(.isBefore ^Instant % start) starts))
     127           10 :         next        (first (drop-while #(not (.isAfter ^Instant % start)) starts))
     128            3 :         gap-ms      (case timing
     129            0 :                       "before-start" (when prev (- (.toEpochMilli start) (.toEpochMilli prev)))
     130            6 :                       "after-start"  (when next (- (.toEpochMilli next) (.toEpochMilli start))))]
     131            7 :     (rules/max-hours timing gap-ms (:duration event))))
     132              : 
     133              : 
     134            4 : (defn- inherited-config-to-check
     135              :   [existing event template]
     136            4 :   (when
     137           34 :     (and
     138            3 :      existing
     139           18 :      (nil? (reminder event))
     140              :      (or
     141           24 :       (not= (:datetime-start existing) (:datetime-start event))
     142           21 :       (not= (:duration existing) (:duration event))))
     143            4 :     (rules/active-config event template)))
     144              : 
     145              : 
     146            4 : (defn check-event
     147              :  "Checks newly selected methods and schedule neighbours for an event reminder.
     148              :    Inherited template reminders are checked only on schedule-changing updates."
     149              :  [ctx existing-event merged-event template-or-nil]
     150           16 :  (let [config (or
     151            9 :                (active-own-config merged-event)
     152           12 :                (inherited-config-to-check existing-event merged-event template-or-nil))]
     153           36 :    (c/mlet [_ (check-ownership ctx existing-event merged-event)]
     154           21 :      (if (and config (:template-id merged-event))
     155            2 :        (check-hours
     156            1 :         merged-event
     157            1 :         config
     158            8 :         (event-max-hours ctx merged-event template-or-nil (:timing config)))
     159            9 :        (either/right merged-event)))))
        

Generated by: LCOV version 2.0-1