Line data Source code
1 4 : (ns matchscore.application.class.remove-participant
2 : "Removes a participant from an event class.
3 : A player is registered for an event only through its classes, so leaving the last
4 : one drops the event registration too. Admins keep theirs."
5 : (:require
6 : [cats.core :as c]
7 : [cats.monad.either :as either]
8 : [matchscore.application.event.remove-participant :as app-event-remove-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.utils.errors :as utils.errors]))
14 :
15 :
16 4 : (defn- find-class-or-fail
17 : [repos class-uuid event-uuid]
18 7 : (let [class (class-query/get-class (:class-postgres-repo repos) class-uuid)]
19 14 : (if (or (nil? class) (not= event-uuid (:event-id class)))
20 0 : (either/left
21 0 : (utils.errors/build-error
22 : :check
23 0 : {:code :error.class/not-found
24 : :message "Class not found."}))
25 3 : (either/right class))))
26 :
27 :
28 4 : (defn- find-class-participant-or-fail
29 : [repos class-uuid participant-id]
30 7 : (if-let [existing (class-participant-postgres-repo/find-by-class-and-participant
31 3 : (:class-participant-postgres-repo repos)
32 1 : class-uuid
33 1 : participant-id)]
34 3 : (either/right existing)
35 2 : (either/left
36 3 : (utils.errors/build-error
37 : :check
38 5 : {:code :error.class-participant/not-found
39 : :message "Participant not found in class."}))))
40 :
41 :
42 4 : (defn- in-another-class?
43 : "True when the participant is registered in a class of the event other than `class-uuid`."
44 : [repos event-uuid class-uuid participant-id]
45 1 : (let [other-class-ids
46 1 : (->>
47 6 : (class-postgres-repo/find-by-event-id (:class-postgres-repo repos) event-uuid)
48 6 : (map (comp str :uuid))
49 6 : (remove #{(str class-uuid)})
50 1 : set)]
51 1 : (boolean
52 2 : (some
53 5 : (comp other-class-ids str :class-id)
54 2 : (class-participant-postgres-repo/find-by-participant-id
55 3 : (:class-participant-postgres-repo repos)
56 1 : participant-id)))))
57 :
58 :
59 12 : (defn- admin? [event-participant] (boolean (some #{"admin"} (:roles event-participant))))
60 :
61 :
62 4 : (defn execute!
63 : "Removes the participant from the class. A non-admin leaving their last class of the
64 : event is removed from the event as well.
65 : Returns Either<Error, {:event-registration-removed? boolean}>."
66 : [{:keys [repos]
67 : :as ctx}
68 : event-uuid
69 : class-uuid
70 : participant-id]
71 19 : (c/mlet [_ (find-class-or-fail repos class-uuid event-uuid)
72 5 : _ (find-class-participant-or-fail repos class-uuid participant-id)]
73 3 : (let [event-participant (event-participant-postgres-repo/find-by-id
74 3 : (:event-participant-postgres-repo repos)
75 1 : event-uuid
76 1 : participant-id)
77 8 : last-class? (and
78 1 : event-participant
79 5 : (not (admin? event-participant))
80 7 : (not (in-another-class? repos event-uuid class-uuid participant-id)))]
81 2 : (c/fmap
82 6 : (constantly {:event-registration-removed? (boolean last-class?)})
83 2 : (if last-class?
84 5 : (app-event-remove-participant/execute! ctx event-uuid participant-id)
85 2 : (class-participant-postgres-repo/delete-by-class-and-participant!
86 3 : (:class-participant-postgres-repo repos)
87 1 : class-uuid
88 1 : participant-id))))))
|