Line data Source code
1 4 : (ns matchscore.application.class-template.add-participants-from-template
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [honey.sql :as sql]
6 : [matchscore.domain.class-template-participant.query :as class-template-participant-query]
7 : [matchscore.domain.class-template-participant.repository.postgres
8 : :as
9 : class-template-participant-postgres-repo]
10 : [matchscore.domain.class-template.query :as class-template-query]
11 : [matchscore.domain.template-participant.query :as template-participant-query]
12 : [matchscore.utils.errors :as utils.errors]
13 : [next.jdbc :as jdbc]
14 : [next.jdbc.result-set :as rs])
15 : (:import
16 : (java.time Instant)))
17 :
18 :
19 4 : (defn- find-class-template-or-fail
20 : [repos class-template-uuid template-uuid]
21 3 : (let [class-template (class-template-query/get-class-template
22 3 : (:class-template-postgres-repo repos)
23 1 : class-template-uuid)]
24 7 : (cond (nil? class-template)
25 0 : (either/left
26 0 : (utils.errors/build-error
27 : :check
28 0 : {:code :error.class-template/not-found
29 : :message "Class template not found."}))
30 :
31 6 : (not= template-uuid (:template-id class-template))
32 2 : (either/left
33 3 : (utils.errors/build-error
34 : :check
35 5 : {:code :error.class-template/not-found
36 : :message "Class template not found."}))
37 :
38 3 : :else (either/right class-template))))
39 :
40 :
41 4 : (defn- lock-class-template!
42 : "Acquires a row-level lock on the class-template (template_classes) row and
43 : returns the locked row (uuid + max-players) so the capacity check and insert
44 : below run atomically against concurrent bulk-adds and updates. Returns nil
45 : when the row no longer exists (e.g. concurrently deleted)."
46 : [tx-opts class-template-uuid]
47 2 : (jdbc/execute-one!
48 1 : tx-opts
49 2 : (sql/format
50 9 : {:select [:uuid :max-players]
51 2 : :from [:template_classes]
52 7 : :where [:= :uuid [:cast class-template-uuid :uuid]]
53 : :for :update})))
54 :
55 :
56 4 : (defn execute!
57 : [{:keys [repos pg]
58 : :as _ctx}
59 : class-template-uuid
60 : template-uuid
61 : participant-ids]
62 12 : (c/mlet [_class-template (find-class-template-or-fail repos class-template-uuid template-uuid)]
63 10 : (let [requested-ids (vec (distinct (or participant-ids [])))
64 3 : template-participant-repo (:template-participant-postgres-repo repos)
65 2 : template-participants (template-participant-query/get-participants-by-template-id
66 1 : template-participant-repo
67 1 : template-uuid)
68 6 : template-participant-ids (set (map :participant-id template-participants))
69 : ;; Preserve a deterministic order: requested order when participant-ids
70 : ;; are provided, otherwise template participant order. Sets are only used
71 : ;; for membership filtering, never as the ordered candidate sequence.
72 4 : candidate-ids (if (seq requested-ids)
73 4 : (filterv template-participant-ids requested-ids)
74 0 : (vec (distinct (map :participant-id template-participants))))
75 2 : candidate-count (count candidate-ids)
76 2 : now (Instant/now)]
77 : ;; Capacity check + insert run inside one transaction, guarded by a row lock
78 : ;; on the class-template, so concurrent requests cannot overfill max-players.
79 : ;; max-players is read from the locked row (not the earlier unlocked read) so
80 : ;; concurrent updates/deletes cannot race the capacity calculation.
81 4 : (jdbc/with-transaction
82 1 : [tx pg]
83 7 : (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
84 3 : tx-repo (class-template-participant-postgres-repo/make-repository tx-opts)
85 4 : locked-class-template (lock-class-template! tx-opts class-template-uuid)]
86 5 : (if (nil? locked-class-template)
87 0 : (either/left
88 0 : (utils.errors/build-error
89 : :check
90 0 : {:code :error.class-template/not-found
91 : :message "Class template not found."}))
92 4 : (let [max-players (:max-players locked-class-template)
93 : current-class-parts
94 2 : (class-template-participant-query/get-participants-by-class-template-id
95 1 : tx-repo
96 1 : class-template-uuid)
97 6 : current-class-part-ids (set (map :participant-id current-class-parts))
98 2 : current-count (count current-class-parts)
99 4 : eligible-ids (remove current-class-part-ids candidate-ids)
100 2 : to-add-ids (if max-players
101 8 : (take (max 0 (- max-players current-count)) eligible-ids)
102 1 : eligible-ids)
103 2 : to-add (map
104 1 : (fn [pid]
105 11 : {:class-template-id class-template-uuid
106 1 : :participant-id pid
107 : :status "registered"
108 1 : :registered-at now
109 1 : :roles []
110 1 : :extra {}
111 : :source "matchscore"})
112 1 : to-add-ids)]
113 2 : (c/fmap
114 1 : (fn [{:keys [inserted-count]}]
115 4 : {:added-count inserted-count
116 3 : :skipped-count (- candidate-count inserted-count)})
117 4 : (class-template-participant-postgres-repo/bulk-insert! tx-repo to-add)))))))))
|