LCOV - code coverage report
Current view: top level - matchscore/application/template - occurrence_exceptions.clj Coverage Total Hit
Test: lcov.info Lines: 75.0 % 64 48
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.application.template.occurrence-exceptions
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.db.postgres.template-occurrence-exceptions :as exception-db]
       6              :    [matchscore.domain.template.repository.postgres :as template-repo]
       7              :    [matchscore.jobs.scheduler :as scheduler]
       8              :    [matchscore.utils.errors :as errors]
       9              :    [matchscore.utils.rrule :as rrule]
      10              :    [next.jdbc :as jdbc]
      11              :    [next.jdbc.result-set :as rs]
      12              :    [taoensso.telemere :as t])
      13              :   (:import
      14              :    (java.time Instant)))
      15              : 
      16              : 
      17           15 : (defn- datasource [pg] (if (and (map? pg) (:datasource pg)) (:datasource pg) pg))
      18              : 
      19              : 
      20            4 : (defn- error
      21              :   [code message & [context]]
      22            2 :   (either/left
      23            3 :    (errors/build-error
      24              :     :validation
      25            5 :     (cond->
      26            4 :       {:code    code
      27            1 :        :message message}
      28            3 :       context (assoc :context context)))))
      29              : 
      30              : 
      31            4 : (defn- scheduled-occurrence?
      32              :   [rrule-str ^Instant start]
      33           13 :   (and (seq rrule-str) (= start (rrule/next-occurrence rrule-str (.minusSeconds start 1)))))
      34              : 
      35              : 
      36            4 : (defn list-for-template
      37              :  [{:keys [pg]} template-id]
      38            6 :  (exception-db/list-for-template (datasource pg) template-id))
      39              : 
      40              : 
      41            4 : (defn create!
      42              :  [{:keys [pg goose]}
      43              :   template-id
      44              :   {:keys [occurrence-start]
      45              :    :as   data}
      46              :   user-id]
      47            4 :  (let [ds (datasource pg)
      48            3 :        start (exception-db/instant occurrence-start)
      49              :        result
      50            4 :        (jdbc/with-transaction
      51            1 :         [tx ds]
      52            2 :         (jdbc/execute-one!
      53            1 :          tx
      54            3 :          ["SELECT uuid FROM templates WHERE uuid = ?::uuid FOR UPDATE" template-id])
      55            3 :         (let [repo     (template-repo/make-repository
      56            6 :                         (jdbc/with-options tx {:builder-fn rs/as-kebab-maps}))
      57            5 :               template (template-repo/find-by-id repo template-id nil)]
      58            9 :           (cond (nil? template) (error :error.template/not-found "Template not found.")
      59              : 
      60            5 :                 (not (.isAfter start (Instant/now)))
      61            0 :                 (error
      62              :                  :error.template-occurrence/past
      63              :                  "Only a future occurrence can be skipped."
      64            0 :                  {:occurrence-start (str start)})
      65              : 
      66            8 :                 (not (scheduled-occurrence? (:rrule template) start))
      67            4 :                 (error
      68              :                  :error.template-occurrence/not-scheduled
      69              :                  "The selected time is not an occurrence in this template schedule."
      70            5 :                  {:occurrence-start (str start)})
      71              : 
      72            5 :                 (exception-db/event-exists? tx template-id start)
      73            0 :                 (error
      74              :                  :error.template-occurrence/event-exists
      75              :                  "An event already exists for this occurrence. Change that event's status instead."
      76            0 :                  {:occurrence-start (str start)})
      77              : 
      78              :                 :else
      79            2 :                 (either/right
      80            2 :                  (exception-db/save!
      81            1 :                   tx
      82            6 :                   (assoc data
      83            1 :                    :template-id      template-id
      84            1 :                    :occurrence-start start
      85            1 :                    :created-by       user-id))))))]
      86            9 :    (when (and (either/right? result) goose)
      87            0 :      (let [exception (c/extract result)]
      88            0 :        (when (or (:notify-linked-chats exception) (:notify-participants exception))
      89            0 :          (try
      90            0 :            (scheduler/schedule-job
      91            0 :             goose
      92              :             `matchscore.jobs.telegram-event-ended/send-template-occurrence-skipped-job
      93            0 :             {:args [template-id (:uuid exception)]})
      94              :            (catch Exception e
      95            0 :              (t/log!
      96              :               :error
      97            0 :               {:message      "Failed to schedule skipped occurrence notification"
      98            0 :                :template-id  template-id
      99            0 :                :exception-id (:uuid exception)
     100            0 :                :exception    e}))))))
     101            1 :    result))
     102              : 
     103              : 
     104            4 : (defn delete!
     105              :  [{:keys [pg]} template-id exception-id]
     106            4 :  (let [ds (datasource pg)]
     107            4 :    (jdbc/with-transaction
     108            1 :     [tx ds]
     109            2 :     (jdbc/execute-one!
     110            1 :      tx
     111            3 :      ["SELECT uuid FROM templates WHERE uuid = ?::uuid FOR UPDATE" template-id])
     112           10 :     (if-let [deleted (exception-db/delete! tx template-id exception-id)]
     113            3 :       (either/right deleted)
     114            0 :       (error :error.template-occurrence/not-found "Skipped occurrence not found.")))))
        

Generated by: LCOV version 2.0-1