Line data Source code
1 4 : (ns matchscore.application.event.add-participant
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.event-participant.command :as event-participant-command]
6 : [matchscore.domain.participant.repository.postgres :as participant-postgres-repo]
7 : [matchscore.domain.user.repository.postgres :as user-postgres-repo]
8 : [matchscore.telegram.notifications-trigger :as notifications-trigger]
9 : [matchscore.utils.errors :as utils.errors])
10 : (:import
11 : (java.time Instant)))
12 :
13 :
14 4 : (defn- find-user-or-fail
15 : [repos user-uuid]
16 22 : (if-let [user (user-postgres-repo/find-by-id (:user-postgres-repo repos) user-uuid)]
17 6 : (either/right user)
18 2 : (either/left
19 3 : (utils.errors/build-error
20 : :check
21 5 : {:code :error.event-participant/user-not-found
22 : :message "User not found."}))))
23 :
24 :
25 4 : (defn execute!
26 : "Registers the user for the event without a class. `:roles` defaults to `[\"player\"]`."
27 6 : ([ctx event-id user-uuid] (execute! ctx event-id user-uuid nil))
28 : ([{:keys [repos]
29 : :as ctx}
30 : event-id
31 : user-uuid
32 : {:keys [roles]
33 : :or {roles ["player"]}}]
34 2 : (let [result
35 36 : (c/mlet [_ (find-user-or-fail repos user-uuid)
36 4 : participant (participant-postgres-repo/ensure-person-participant-for-user!
37 6 : (:participant-postgres-repo repos)
38 2 : user-uuid)]
39 4 : (event-participant-command/register-participant!
40 2 : ctx
41 26 : {:event-id event-id
42 6 : :participant-id (:uuid participant)
43 : :status "registered"
44 2 : :roles roles
45 2 : :extra {}
46 4 : :registered-at (Instant/now)
47 : :attendance-confirmed false
48 : :source "matchscore"}))]
49 18 : (when (either/right? result) (notifications-trigger/trigger-debounced! ctx event-id))
50 2 : result)))
|