Line data Source code
1 4 : (ns matchscore.application.class.swap-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 : "Swaps participants between class-a-uuid and class-b-uuid, both under event-uuid.
35 : class-a-participant-ids are moved from A to B; class-b-participant-ids are moved from B to A.
36 : Only IDs actually present in their respective class are moved; others are skipped.
37 : Each direction is capped by the receiving class's available capacity."
38 : [{:keys [repos]
39 : :as _ctx}
40 : class-a-uuid
41 : class-b-uuid
42 : event-uuid
43 : class-a-participant-ids
44 : class-b-participant-ids]
45 19 : (c/mlet [class-a (find-class-for-event-or-fail repos class-a-uuid event-uuid)
46 5 : class-b (find-class-for-event-or-fail repos class-b-uuid event-uuid)]
47 4 : (let [repo (:class-participant-postgres-repo repos)
48 4 : parts-a (class-participant-query/get-class-participants-by-class repo class-a-uuid)
49 4 : parts-b (class-participant-query/get-class-participants-by-class repo class-b-uuid)
50 6 : ids-a (set (map :participant-id parts-a))
51 6 : ids-b (set (map :participant-id parts-b))
52 6 : to-b-candidates (vec (filter ids-a class-a-participant-ids))
53 6 : to-a-candidates (vec (filter ids-b class-b-participant-ids))
54 1 : skipped-count (+
55 5 : (- (count class-a-participant-ids) (count to-b-candidates))
56 5 : (- (count class-b-participant-ids) (count to-a-candidates)))
57 4 : space-in-a (when (:max-players class-a)
58 0 : (max
59 : 0
60 0 : (+ (- (:max-players class-a) (count parts-a)) (count to-b-candidates))))
61 4 : space-in-b (when (:max-players class-b)
62 0 : (max
63 : 0
64 0 : (+ (- (:max-players class-b) (count parts-b)) (count to-a-candidates))))
65 5 : to-a-ids (vec (if space-in-a (take space-in-a to-a-candidates) to-a-candidates))
66 5 : to-b-ids (vec (if space-in-b (take space-in-b to-b-candidates) to-b-candidates))
67 2 : now (Instant/now)
68 1 : make-record (fn [class-uuid pid]
69 10 : {:class-id class-uuid
70 1 : :participant-id pid
71 : :status "registered"
72 1 : :registered-at now
73 1 : :extra {}
74 : :source "matchscore"})]
75 30 : (c/mlet [_ (class-participant-postgres-repo/bulk-insert!
76 1 : repo
77 8 : (map #(make-record class-a-uuid %) to-a-ids))
78 4 : _ (class-participant-postgres-repo/bulk-insert!
79 1 : repo
80 8 : (map #(make-record class-b-uuid %) to-b-ids))
81 3 : _ (class-participant-postgres-repo/bulk-delete-by-class-and-participants!
82 1 : repo
83 1 : class-a-uuid
84 1 : to-b-ids)
85 2 : _ (class-participant-postgres-repo/bulk-delete-by-class-and-participants!
86 1 : repo
87 1 : class-b-uuid
88 1 : to-a-ids)]
89 2 : (c/return
90 6 : {:moved-to-a-count (count to-a-ids)
91 2 : :moved-to-b-count (count to-b-ids)
92 1 : :skipped-count skipped-count})))))
|