Line data Source code
1 4 : (ns matchscore.application.event.remove-participant
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
6 : [matchscore.domain.event-participant.repository.postgres :as event-participant-postgres-repo]
7 : [matchscore.telegram.notifications-trigger :as notifications-trigger]
8 : [matchscore.utils.errors :as utils.errors]
9 : [next.jdbc :as jdbc]
10 : [next.jdbc.result-set :as rs]))
11 :
12 :
13 4 : (defn- find-event-participant-or-fail
14 : [repos event-id participant-id]
15 14 : (if-let [ep (event-participant-postgres-repo/find-by-id
16 6 : (:event-participant-postgres-repo repos)
17 2 : event-id
18 2 : participant-id)]
19 6 : (either/right ep)
20 2 : (either/left
21 3 : (utils.errors/build-error
22 : :check
23 5 : {:code :error.event-participant/not-found
24 : :message "Participant not found in event."}))))
25 :
26 :
27 4 : (defn- make-tx-repos
28 : [repos tx-opts]
29 4 : (merge
30 2 : repos
31 12 : {:class-participant-postgres-repo (class-participant-postgres-repo/make-repository tx-opts)
32 6 : :event-participant-postgres-repo (event-participant-postgres-repo/make-repository tx-opts)}))
33 :
34 :
35 4 : (defn execute!
36 : [{:keys [repos pg]
37 : :as ctx}
38 : event-id
39 : participant-id]
40 2 : (let [result
41 2 : (try
42 8 : (jdbc/with-transaction
43 2 : [tx pg]
44 14 : (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
45 8 : tx-repos (make-tx-repos repos tx-opts)
46 32 : result (c/mlet [_ (find-event-participant-or-fail
47 2 : tx-repos
48 2 : event-id
49 2 : participant-id)
50 : _
51 4 : (class-participant-postgres-repo/delete-by-participant-and-event!
52 6 : (:class-participant-postgres-repo tx-repos)
53 2 : participant-id
54 2 : event-id)]
55 4 : (event-participant-postgres-repo/delete!
56 6 : (:event-participant-postgres-repo tx-repos)
57 2 : event-id
58 2 : participant-id))]
59 17 : (if (either/left? result) (throw (ex-info "tx-rollback" {::result result})) result)))
60 : (catch clojure.lang.ExceptionInfo e
61 12 : (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))]
62 18 : (when (either/right? result) (notifications-trigger/trigger-debounced! ctx event-id))
63 2 : result))
|