Line data Source code
1 4 : (ns matchscore.application.class.move-participants
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.utils.errors :as utils.errors])
9 : (:import
10 : (java.time Instant)))
11 :
12 :
13 4 : (defn- find-class-for-event-or-fail
14 : [repos class-uuid event-uuid]
15 7 : (let [class (class-query/get-class (:class-postgres-repo repos) class-uuid)]
16 7 : (cond (nil? class)
17 2 : (either/left
18 3 : (utils.errors/build-error
19 : :check
20 5 : {:code :error.class/not-found
21 : :message "Class not found."}))
22 :
23 6 : (not= event-uuid (:event-id class))
24 0 : (either/left
25 0 : (utils.errors/build-error
26 : :check
27 0 : {:code :error.class/not-found
28 : :message "Class not found."}))
29 :
30 3 : :else (either/right class))))
31 :
32 :
33 4 : (defn execute!
34 : "Moves the given participant-ids from source-class-uuid into target-class-uuid.
35 : Both classes must belong to event-uuid.
36 : Participants already in the target class (shared) are skipped entirely.
37 : Eligible participants are added to target and removed from source,
38 : subject to target max-players capacity."
39 : [{:keys [repos]
40 : :as _ctx}
41 : target-class-uuid
42 : source-class-uuid
43 : event-uuid
44 : participant-ids]
45 19 : (c/mlet [target-class (find-class-for-event-or-fail repos target-class-uuid event-uuid)
46 5 : _ (find-class-for-event-or-fail repos source-class-uuid event-uuid)]
47 4 : (let [repo (:class-participant-postgres-repo repos)
48 3 : candidate-ids (set participant-ids)
49 2 : source-parts (class-participant-query/get-class-participants-by-class
50 1 : repo
51 1 : source-class-uuid)
52 2 : target-parts (class-participant-query/get-class-participants-by-class
53 1 : repo
54 1 : target-class-uuid)
55 6 : source-ids (set (map :participant-id source-parts))
56 6 : target-ids (set (map :participant-id target-parts))
57 : ;; only operate on candidates actually present in source and not yet in target
58 7 : eligible-ids (remove target-ids (filter source-ids candidate-ids))
59 3 : max-players (:max-players target-class)
60 2 : current-count (count target-parts)
61 2 : to-move-ids (vec
62 2 : (if max-players
63 0 : (take (max 0 (- max-players current-count)) eligible-ids)
64 1 : eligible-ids))
65 2 : now (Instant/now)
66 2 : to-add (map
67 1 : (fn [pid]
68 10 : {:class-id target-class-uuid
69 1 : :participant-id pid
70 : :status "registered"
71 1 : :registered-at now
72 1 : :extra {}
73 : :source "matchscore"})
74 1 : to-move-ids)]
75 18 : (c/mlet [{:keys [inserted-count]} (class-participant-postgres-repo/bulk-insert! repo to-add)
76 2 : _ (class-participant-postgres-repo/bulk-delete-by-class-and-participants!
77 1 : repo
78 1 : source-class-uuid
79 1 : to-move-ids)]
80 2 : (c/return
81 4 : {:moved-count inserted-count
82 4 : :skipped-count (- (count candidate-ids) inserted-count)})))))
|