Line data Source code
1 4 : (ns matchscore.application.group-participants.fill-all-from-class
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.application.group-participants.helpers :as helpers]
6 : [matchscore.application.group-participants.sync-matches :as sync-matches]
7 : [matchscore.domain.class-participant.repository.postgres :as class-participant-postgres-repo]
8 : [matchscore.domain.group-participant.repository.postgres :as group-participant-postgres-repo]
9 : [matchscore.domain.group.repository.postgres :as group-postgres-repo]
10 : [matchscore.domain.match.repository.postgres :as match-postgres-repo]
11 : [matchscore.utils.errors :as utils.errors]
12 : [next.jdbc :as jdbc]
13 : [next.jdbc.result-set :as rs]))
14 :
15 :
16 4 : (defn execute!
17 : "Fills empty group participant slots across all groups in a draw with class participants,
18 : allocating in group order without overlap. Returns Either<Error, seq<GroupParticipant>>."
19 : [{:keys [repos pg]} draw-uuid]
20 18 : (c/mlet [draw (helpers/find-draw-or-fail repos draw-uuid)
21 6 : stage (helpers/find-stage-or-fail repos (:stage-id draw))]
22 1 : (try
23 4 : (jdbc/with-transaction
24 1 : [tx pg]
25 7 : (let [tx-opts (jdbc/with-options tx {:builder-fn rs/as-unqualified-kebab-maps})
26 3 : tx-gp-repo (group-participant-postgres-repo/make-repository tx-opts)
27 5 : tx-repos (assoc repos
28 1 : :group-participant-postgres-repo tx-gp-repo
29 2 : :match-postgres-repo (match-postgres-repo/make-repository
30 1 : tx-opts))
31 3 : class-uuid (:class-id stage)
32 2 : class-participants (class-participant-postgres-repo/find-by-class-id
33 3 : (:class-participant-postgres-repo repos)
34 1 : class-uuid)
35 3 : all-groups (sort-by
36 : :order
37 2 : (group-postgres-repo/find-by-draw-id
38 3 : (:group-postgres-repo repos)
39 1 : draw-uuid))
40 4 : group-uuids (mapv :uuid all-groups)
41 2 : all-gps (group-participant-postgres-repo/find-by-group-ids
42 1 : tx-gp-repo
43 1 : group-uuids)
44 7 : initially-unavailable (into #{} (keep :participant-id) all-gps)
45 3 : available-pool (remove
46 6 : #(contains? initially-unavailable (:participant-id %))
47 1 : class-participants)
48 4 : by-group (group-by :group-id all-gps)]
49 4 : (if (empty? available-pool)
50 1 : (throw
51 3 : (ex-info
52 : "tx-rollback"
53 4 : {::result (either/left
54 3 : (utils.errors/build-error
55 : :postgres
56 5 : {:code :error.group-participant/no-class-participants-available
57 : :message "No class participants available to fill the group"}))}))
58 2 : (loop [groups all-groups
59 1 : pool available-pool
60 1 : all-results []]
61 4 : (if (empty? groups)
62 3 : (either/right all-results)
63 4 : (let [group (first groups)
64 3 : group-uuid (:uuid group)
65 4 : current-gps (get by-group group-uuid [])
66 2 : empty-slots (->>
67 1 : current-gps
68 9 : (filter #(nil? (:participant-id %)))
69 1 : (sort-by :slot))
70 2 : n (count empty-slots)
71 4 : to-add (take n pool)]
72 6 : (cond (empty? empty-slots) (recur (rest groups) pool all-results)
73 :
74 3 : (empty? to-add) (recur (rest groups) pool all-results)
75 :
76 : :else
77 3 : (let [rows (map
78 1 : (fn [[empty-slot class-p]]
79 6 : (assoc empty-slot
80 3 : :participant-id (:participant-id class-p)
81 : :status "active"))
82 5 : (map vector empty-slots to-add))
83 : [ok? results error-msg]
84 4 : (group-participant-postgres-repo/bulk-upsert! tx-gp-repo rows)]
85 4 : (if-not ok?
86 0 : (throw
87 0 : (ex-info
88 : "tx-rollback"
89 0 : {::result
90 0 : (either/left
91 0 : (utils.errors/build-error
92 : :postgres
93 0 : {:code :error.group-participant/upsert-failed
94 0 : :message (str "Failed to fill group participants: " error-msg)}))}))
95 5 : (let [sync-result (sync-matches/execute! tx-repos group-uuid)]
96 4 : (if (either/left? sync-result)
97 0 : (throw (ex-info "tx-rollback" {::result sync-result}))
98 1 : (recur
99 : (rest groups)
100 : (drop (count to-add) pool)
101 : (into all-results results)))))))))))))
102 : (catch clojure.lang.ExceptionInfo e
103 12 : (if (contains? (ex-data e) ::result) (::result (ex-data e)) (throw e))))))
|