LCOV - code coverage report
Current view: top level - matchscore/domain/template - command.clj Coverage Total Hit
Test: lcov.info Lines: 90.7 % 75 68
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.template.command
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.domain.payment-reminder.validation :as payment-reminder-validation]
       6              :    [matchscore.domain.template.entity :as entity]
       7              :    [matchscore.domain.template.repository.postgres :as template-postgres-repo]
       8              :    [matchscore.utils.errors :as utils.errors]
       9              :    [matchscore.utils.nanoid :as nanoid]
      10              :    [matchscore.utils.rrule :as rrule])
      11              :   (:import
      12              :    (java.time Instant)))
      13              : 
      14              : 
      15            4 : (defn- check-id-not-taken
      16              :   [repo template-data]
      17           32 :   (if (template-postgres-repo/find-by-id repo (:uuid template-data) nil)
      18            0 :     (either/left
      19            0 :      (utils.errors/build-error
      20              :       :check
      21            0 :       {:code    :error.template/conflict-id
      22              :        :message "Template with such id already exists"
      23            0 :        :context {:input-data template-data}}))
      24           12 :     (either/right template-data)))
      25              : 
      26              : 
      27            4 : (defn- check-automation-settings
      28              :   [data]
      29           16 :   (let [enabled? (:automatic-event-creation data)
      30           12 :         delay    (:automatic-event-creation-delay-minutes data)]
      31            4 :     (if
      32           47 :       (and
      33           49 :        (or (not (contains? data :automatic-event-creation)) (boolean? enabled?))
      34              :        (or
      35           24 :         (not (contains? data :automatic-event-creation-delay-minutes))
      36           15 :         (and (integer? delay) (<= 1 delay 525600))))
      37           12 :       (either/right data)
      38            2 :       (either/left
      39            3 :        (utils.errors/build-error
      40              :         :check
      41            5 :         {:code :error.template/invalid-automation-settings
      42              :          :message
      43              :          "Automatic creation requires a boolean toggle and a whole-minute delay between 1 and 525600."})))))
      44              : 
      45              : 
      46            4 : (defn- check-automation-delay-fits-schedule
      47              :   "The next event is created for the first scheduled start after the delay has
      48              :    elapsed, so a delay reaching that start would silently skip an occurrence."
      49              :   [{:keys [rrule duration]
      50              :     :as   template}]
      51           35 :   (let [delay     (or (:automatic-event-creation-delay-minutes template) 5)
      52            6 :         gap       (when
      53           61 :                     (and (:automatic-event-creation template) (seq rrule) (pos? (or duration 0)))
      54           10 :                     (rrule/min-occurrence-gap-ms rrule (Instant/now)))
      55           30 :         max-delay (when gap (max 0 (long (quot (- gap duration 1) 60000))))]
      56           42 :     (if (or (nil? gap) (<= delay max-delay))
      57           12 :       (either/right template)
      58            2 :       (either/left
      59            3 :        (utils.errors/build-error
      60              :         :check
      61            5 :         {:code    :error.template/automation-delay-too-long
      62            4 :          :message (if (pos? max-delay)
      63            4 :                     (str
      64              :                      "Automatic creation delay must not exceed "
      65            1 :                      max-delay
      66              :                      " minutes for this schedule, otherwise the next occurrence is skipped.")
      67              :                     "Events in this schedule follow each other too closely for automatic creation.")
      68            3 :          :context {:max-delay-minutes max-delay}})))))
      69              : 
      70              : 
      71            4 : (defn create-template!
      72              :  "Creates a new event template. Validates data and checks for ID conflicts.
      73              :    Returns Either<Error, EventTemplate>."
      74              :  [{:keys [repos]
      75              :    :as   ctx}
      76              :   template-data]
      77           16 :  (let [repo   (:template-postgres-repo repos)
      78           63 :        data   (cond-> (update template-data :visibility #(or % "public"))
      79           32 :                 (not (:public-id template-data)) (assoc :public-id (nanoid/generate)))
      80          124 :        result (c/mlet [_ (check-automation-settings data)
      81           20 :                        _ (check-automation-delay-fits-schedule data)
      82           24 :                        _ (payment-reminder-validation/check-template ctx nil data)
      83           16 :                        _ (check-id-not-taken repo data)]
      84           16 :                 (template-postgres-repo/save! repo data))]
      85            4 :    result))
      86              : 
      87              : 
      88            4 : (defn- find-by-id-or-fail
      89              :   [repo template-id]
      90           30 :   (if-let [existing (template-postgres-repo/find-by-id repo template-id nil)]
      91            9 :     (either/right existing)
      92            0 :     (either/left
      93            0 :      (utils.errors/build-error
      94              :       :check
      95            0 :       {:code    :error.template/not-found
      96              :        :message "Template not found."}))))
      97              : 
      98              : 
      99            4 : (defn- save-if-changed
     100              :   [ctx repo existing template-data]
     101           15 :   (if (entity/has-changes? existing template-data)
     102           15 :     (let [updated (entity/merge-updates existing template-data)]
     103           57 :       (c/mlet [_ (payment-reminder-validation/check-template ctx existing updated)
     104           12 :                saved (template-postgres-repo/save! repo updated)]
     105            6 :         (c/return
     106           12 :          {:code :success.template/ok
     107            3 :           :data saved})))
     108            2 :     (either/right
     109            4 :      {:code :success.template/no-changes
     110            1 :       :data existing})))
     111              : 
     112              : 
     113            4 : (defn update-template!
     114              :  "Updates an existing event template. Validates data and checks for ID conflicts.
     115              :    Returns Either<Error, EventTemplate>."
     116              :  [{:keys [repos]
     117              :    :as   ctx}
     118              :   template-data]
     119           12 :  (let [repo   (:template-postgres-repo repos)
     120           93 :        result (c/mlet [_ (check-automation-settings template-data)
     121           24 :                        existing (find-by-id-or-fail repo (:uuid template-data))
     122            9 :                        _ (check-automation-delay-fits-schedule
     123           12 :                           (entity/merge-updates existing template-data))
     124           18 :                        result   (save-if-changed ctx repo existing template-data)]
     125            9 :                 (c/return result))]
     126            3 :    result))
     127              : 
     128              : 
     129            4 : (defn delete-template!
     130              :  [{:keys [repos]} template-id]
     131            4 :  (let [repo (:template-postgres-repo repos)]
     132           11 :    (c/mlet [_ (find-by-id-or-fail repo template-id)]
     133            4 :      (template-postgres-repo/delete! repo template-id))))
        

Generated by: LCOV version 2.0-1