Line data Source code
1 4 : (ns matchscore.application.class.add-participants-from-event
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.class-participant.query :as class-participant-query]
6 : [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
7 : [matchscore.domain.class.query :as class-query]
8 : [matchscore.domain.event-participant.query :as event-participant-query]
9 : [matchscore.utils.errors :as utils.errors])
10 : (:import
11 : (java.time Instant)))
12 :
13 :
14 4 : (defn- find-class-or-fail
15 : [repos class-uuid event-uuid]
16 7 : (let [class (class-query/get-class (:class-postgres-repo repos) class-uuid)]
17 7 : (cond (nil? class)
18 2 : (either/left
19 3 : (utils.errors/build-error
20 : :check
21 5 : {:code :error.class/not-found
22 : :message "Class not found."}))
23 :
24 6 : (not= event-uuid (:event-id class))
25 0 : (either/left
26 0 : (utils.errors/build-error
27 : :check
28 0 : {:code :error.class/not-found
29 : :message "Class not found."}))
30 :
31 3 : :else (either/right class))))
32 :
33 :
34 4 : (defn- check-capacity
35 : [max-players current-count eligible-count]
36 11 : (if (and max-players (> eligible-count (- max-players current-count)))
37 2 : (either/left
38 3 : (utils.errors/build-error
39 : :check
40 6 : {:code :error.class/capacity-full
41 : :message
42 : "Adding these participants would exceed the class's max players limit. Increase class capacity first."
43 5 : :context {:max-players max-players
44 1 : :current-count current-count
45 1 : :eligible-count eligible-count}}))
46 3 : (either/right nil)))
47 :
48 :
49 4 : (defn execute!
50 : [{:keys [repos]
51 : :as _ctx}
52 : class-uuid
53 : event-uuid]
54 12 : (c/mlet [class (find-class-or-fail repos class-uuid event-uuid)]
55 4 : (let [event-participant-repo (:event-participant-postgres-repo repos)
56 3 : class-participant-repo (:class-participant-postgres-repo repos)
57 : event-participants
58 4 : (event-participant-query/get-participants-by-event-id event-participant-repo event-uuid)
59 12 : player-participants (filter #(contains? (set (:roles %)) "player") event-participants)
60 6 : event-participant-ids (set (map :participant-id player-participants))
61 : current-class-parts
62 4 : (class-participant-query/get-class-participants-by-class class-participant-repo class-uuid)
63 6 : current-class-part-ids (set (map :participant-id current-class-parts))
64 4 : eligible-ids (remove current-class-part-ids event-participant-ids)
65 3 : max-players (:max-players class)
66 2 : current-count (count current-class-parts)
67 2 : now (Instant/now)
68 2 : to-add (map
69 1 : (fn [pid]
70 10 : {:class-id class-uuid
71 1 : :participant-id pid
72 : :status "registered"
73 1 : :registered-at now
74 1 : :extra {}
75 : :source "matchscore"})
76 1 : eligible-ids)]
77 20 : (c/mlet [_ (check-capacity max-players current-count (count eligible-ids))
78 2 : {:keys [inserted-count]} (class-participant-postgres-repo/bulk-insert!
79 1 : class-participant-repo
80 1 : to-add)]
81 2 : (c/return
82 4 : {:added-count inserted-count
83 4 : :skipped-count (- (count event-participant-ids) inserted-count)})))))
|