Line data Source code
1 4 : (ns matchscore.application.event.join
2 : "Event registration of a player, by themselves or by an event admin.
3 : Resolves the class the user plays in, then registers them for the event and that class."
4 : (:require
5 : [cats.core :as c]
6 : [cats.monad.either :as either]
7 : [matchscore.application.class.add-participant :as app-class-add-participant]
8 : [matchscore.application.event.add-participant :as app-event-add-participant]
9 : [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
10 : [matchscore.domain.class.query :as class-query]
11 : [matchscore.domain.class.repository.postgres :as class-postgres-repo]
12 : [matchscore.domain.event-participant.repository.postgres :as event-participant-postgres-repo]
13 : [matchscore.domain.participant.repository.postgres :as participant-postgres-repo]
14 : [matchscore.domain.user.repository.postgres :as user-postgres-repo]
15 : [matchscore.telegram.notifications-trigger :as notifications-trigger]
16 : [matchscore.utils.errors :as utils.errors]
17 : [next.jdbc :as jdbc]
18 : [next.jdbc.result-set :as rs]))
19 :
20 :
21 4 : (defn- resolve-class
22 : "Picks the class to join. An explicit class must belong to the event; without one the
23 : only class of the event is used. Returns Either<Error, Class|nil> -- nil when the
24 : event has no classes at all."
25 : [classes class-uuid]
26 8 : (cond class-uuid
27 21 : (if-let [class (some #(when (= (str class-uuid) (str (:uuid %))) %) classes)]
28 3 : (either/right class)
29 2 : (either/left
30 3 : (utils.errors/build-error
31 : :check
32 5 : {:code :error.class/not-found
33 : :message "Class not found."})))
34 :
35 8 : (< 1 (count classes))
36 4 : (either/left
37 6 : (utils.errors/build-error
38 : :check
39 12 : {:code :error.event/class-required
40 : :message "Choose a class to join."
41 26 : :context {:classes (mapv #(select-keys % [:uuid :public-id :name]) classes)}}))
42 :
43 10 : :else (either/right (first classes))))
44 :
45 :
46 4 : (defn execute!
47 : "Registers the user for the event. When the event has a single class the user is added
48 : to it automatically; with several classes `class-uuid` is required.
49 : Returns Either<Error, {:event-participant EventParticipant :class-participant ClassParticipant|nil}>."
50 : [{:keys [repos]
51 : :as ctx}
52 : event-uuid
53 : user-uuid
54 : class-uuid]
55 18 : (c/mlet [class (resolve-class
56 12 : (class-query/get-classes-by-event-id (:class-postgres-repo repos) event-uuid)
57 2 : class-uuid)]
58 4 : (if class
59 16 : (app-class-add-participant/execute! ctx event-uuid (:uuid class) user-uuid)
60 2 : (c/fmap
61 1 : (fn [event-participant]
62 5 : {:event-participant event-participant
63 : :class-participant nil})
64 5 : (app-event-add-participant/execute! ctx event-uuid user-uuid)))))
65 :
66 :
67 4 : (defn- resolve-classes
68 : "The classes an admin adds the user to. Every explicit class must belong to the event.
69 : Without explicit classes a player goes to the only class (several → `class-required`),
70 : anyone else into no class. Returns Either<Error, [Class]>."
71 : [classes class-uuids player?]
72 11 : (cond (seq class-uuids)
73 32 : (let [by-uuid (into {} (map (juxt #(str (:uuid %)) identity)) classes)
74 22 : picked (keep #(get by-uuid (str %)) (distinct class-uuids))]
75 16 : (if (= (count picked) (count (distinct class-uuids)))
76 10 : (either/right (vec picked))
77 2 : (either/left
78 3 : (utils.errors/build-error
79 : :check
80 5 : {:code :error.class/not-found
81 : :message "Class not found."}))))
82 :
83 21 : player? (c/fmap #(if % [%] []) (resolve-class classes nil))
84 :
85 3 : :else (either/right [])))
86 :
87 :
88 4 : (defn- add-in-context!
89 : [ctx event-uuid user-uuid {:keys [class-uuids roles]}]
90 27 : (let [roles (vec (distinct (or (not-empty roles) ["player"])))
91 12 : player? (boolean (some #{"player"} roles))
92 6 : opts {:roles roles}]
93 14 : (c/mlet [classes
94 4 : (resolve-classes
95 16 : (class-query/get-classes-by-event-id (:class-postgres-repo (:repos ctx)) event-uuid)
96 2 : class-uuids
97 2 : player?)]
98 8 : (if (empty? classes)
99 4 : (c/fmap
100 2 : (fn [event-participant]
101 8 : {:event-participant event-participant
102 2 : :class-participants []})
103 12 : (app-event-add-participant/execute! ctx event-uuid user-uuid opts))
104 4 : (reduce
105 2 : (fn [acc class]
106 30 : (c/mlet [added acc
107 : {:keys [event-participant class-participant]}
108 18 : (app-class-add-participant/execute! ctx event-uuid (:uuid class) user-uuid opts)]
109 4 : (c/return
110 21 : {:event-participant (or (:event-participant added) event-participant)
111 12 : :class-participants (conj (:class-participants added) class-participant)})))
112 4 : (either/right
113 8 : {:event-participant nil
114 2 : :class-participants []})
115 2 : classes)))))
116 :
117 :
118 4 : (defn- make-tx-repos
119 : [repos tx-opts]
120 4 : (merge
121 2 : repos
122 18 : {:class-postgres-repo (class-postgres-repo/make-repository tx-opts)
123 6 : :user-postgres-repo (user-postgres-repo/make-repository tx-opts)
124 6 : :participant-postgres-repo (participant-postgres-repo/make-repository tx-opts)
125 6 : :event-participant-postgres-repo (event-participant-postgres-repo/make-repository tx-opts)
126 6 : :class-participant-postgres-repo (class-participant-postgres-repo/make-repository tx-opts)}))
127 :
128 :
129 4 : (defn add!
130 : "An event admin adds a user with `roles` (default `[\"player\"]`) to the event and to each of
131 : `class-uuids`. A player needs a class when the event has any (picked automatically when there
132 : is only one); a non-player (e.g. an admin) may be added without one. All classes are checked
133 : before anything is written, and the writes are one transaction: a failure in any class (an
134 : Either error or an exception) leaves neither the event registration nor an earlier class behind.
135 : The Telegram refresh is scheduled once, after the commit.
136 : Returns Either<Error, {:event-participant EventParticipant
137 : :class-participants [ClassParticipant]}>."
138 : [{:keys [repos pg]
139 : :as ctx}
140 : event-uuid
141 : user-uuid
142 : opts]
143 12 : (let [ds (or pg (:ds (:class-postgres-repo repos)))
144 2 : result (try
145 8 : (jdbc/with-transaction
146 2 : [tx ds]
147 14 : (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
148 : ;; No redis/goose inside the transaction: the nested commands must not
149 : ;; schedule notifications for state that may still roll back.
150 6 : tx-ctx (->
151 2 : ctx
152 10 : (assoc :pg tx
153 8 : :repos (make-tx-repos repos tx-opts))
154 2 : (dissoc :redis :goose))
155 12 : result (add-in-context! tx-ctx event-uuid user-uuid opts)]
156 8 : (if (either/left? result)
157 14 : (throw (ex-info "tx-rollback" {::result result}))
158 2 : result)))
159 : (catch clojure.lang.ExceptionInfo e
160 26 : (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))]
161 18 : (when (either/right? result) (notifications-trigger/trigger-debounced! ctx event-uuid))
162 2 : result))
|