Line data Source code
1 4 : (ns matchscore.domain.event-participant.command
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.event-participant.entity :as entity]
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 : [taoensso.telemere :as t]))
10 :
11 :
12 4 : (defn- check-composite-key-not-taken
13 : [repo participant-data]
14 4 : (if
15 8 : (event-participant-postgres-repo/find-by-id
16 4 : repo
17 12 : (:event-id participant-data)
18 12 : (:participant-id participant-data))
19 4 : (either/left
20 6 : (utils.errors/build-error
21 : :check
22 12 : {:code :error.event-participant/conflict
23 : :message "Participant is already registered for this event"
24 6 : :context {:input-data participant-data}}))
25 12 : (either/right participant-data)))
26 :
27 :
28 4 : (defn register-participant!
29 : "Registers a participant into an event. Validates data and checks for composite key conflicts.
30 : Returns Either<Error, EventParticipant>."
31 : [{:keys [repos]} participant-data]
32 16 : (let [repo (:event-participant-postgres-repo repos)
33 44 : result (c/mlet [_ (check-composite-key-not-taken repo participant-data)]
34 16 : (event-participant-postgres-repo/save! repo participant-data))]
35 4 : result))
36 :
37 :
38 4 : (defn- find-by-composite-key-or-fail
39 : [repo event-id participant-id]
40 20 : (if-let [existing (event-participant-postgres-repo/find-by-id repo event-id participant-id)]
41 6 : (either/right existing)
42 4 : (either/left
43 6 : (utils.errors/build-error
44 : :check
45 10 : {:code :error.event-participant/not-found
46 : :message "Event participant not found."}))))
47 :
48 :
49 4 : (defn- check-status
50 : [{:keys [status]
51 : :as participant-data}]
52 23 : (if (or (nil? status) (entity/valid-status? status))
53 6 : (either/right participant-data)
54 2 : (either/left
55 3 : (utils.errors/build-error
56 : :check
57 6 : {:code :error.event-participant/invalid-status
58 : :message "Unknown participant status."
59 3 : :context {:status status}}))))
60 :
61 :
62 4 : (defn- save-if-changed
63 : [repo existing participant-data]
64 10 : (if (entity/has-changes? existing participant-data)
65 6 : (c/fmap
66 12 : #(hash-map :code :success.event-participant/ok :data %)
67 14 : (event-participant-postgres-repo/save! repo (entity/merge-updates existing participant-data)))
68 4 : (either/right
69 8 : {:code :success.event-participant/no-changes
70 2 : :data existing})))
71 :
72 :
73 20 : (def ^:private visible-statuses #{"registered" "confirmed" "checked_in"})
74 :
75 :
76 4 : (defn update-participant!
77 : "Updates an existing event participant. Checks for existence first and rejects statuses
78 : a participant cannot hold. Returns Either<Error, {:code kw :data EventParticipant}>."
79 : [{:keys [repos]
80 : :as ctx}
81 : participant-data]
82 8 : (let [repo (:event-participant-postgres-repo repos)
83 46 : result (c/mlet [existing (find-by-composite-key-or-fail
84 2 : repo
85 6 : (:event-id participant-data)
86 6 : (:participant-id participant-data))
87 8 : _ (check-status participant-data)
88 10 : r (save-if-changed repo existing participant-data)]
89 14 : (when (= :success.event-participant/ok (:code r))
90 14 : (let [old-visible? (contains? visible-statuses (:status existing))
91 4 : new-visible? (contains?
92 2 : visible-statuses
93 17 : (or (:status participant-data) (:status existing)))]
94 14 : (when (or old-visible? new-visible?)
95 2 : (try
96 12 : (notifications-trigger/trigger-debounced! ctx (:event-id participant-data))
97 : (catch Throwable e
98 0 : (t/log!
99 : :error
100 0 : {:message "Failed to enqueue participant refresh notification"
101 0 : :ctx ctx
102 0 : :participant-data participant-data
103 0 : :exception e}))))))
104 6 : (c/return r))]
105 2 : result))
|