Line data Source code
1 4 : (ns matchscore.application.scoreboard.participants
2 : "Batch-loads participants together with their nested user, Rankedin user
3 : or team record for scoreboard responses."
4 : (:require
5 : [matchscore.domain.participant.repository.postgres :as participant-postgres-repo]
6 : [matchscore.domain.team.repository.postgres :as team-postgres-repo]
7 : [matchscore.domain.user-rankedin.repository.postgres :as user-rankedin-postgres-repo]
8 : [matchscore.domain.user.repository.postgres :as user-postgres-repo]))
9 :
10 :
11 4 : (defn- by-uuid
12 : [records]
13 2 : (into
14 1 : {}
15 2 : (map
16 1 : (fn [r]
17 7 : [(str (:uuid r)) r]))
18 1 : records))
19 :
20 :
21 4 : (defn- distinct-ids
22 : [k participants]
23 2 : (distinct
24 3 : (keep
25 9 : #(some->
26 3 : (get % k)
27 1 : str)
28 1 : participants)))
29 :
30 :
31 4 : (defn- enrich-one
32 : [participant users-by-id rankedin-users-by-id teams-by-id]
33 10 : (cond-> participant
34 13 : (:user-id participant) (assoc :user (get users-by-id (str (:user-id participant))))
35 6 : (:user-id-rankedin participant) (assoc :user-rankedin
36 1 : (get
37 1 : rankedin-users-by-id
38 5 : (str (:user-id-rankedin participant))))
39 3 : (:team-id participant) (assoc :team (get teams-by-id (str (:team-id participant))))))
40 :
41 :
42 4 : (defn load-enriched-by-ids
43 : "Given a seq of participant uuid strings, returns a map of
44 : {participant-id-str → participant-record-with-:user/:user-rankedin/:team-attached}.
45 : Participant ids that resolve to nothing are absent from the map."
46 : [repos participant-ids]
47 3 : (let [ids (distinct
48 3 : (keep
49 9 : #(some->
50 : %
51 1 : str)
52 1 : participant-ids))]
53 4 : (if (empty? ids)
54 0 : {}
55 3 : (let [participants (participant-postgres-repo/find-by-ids
56 3 : (:participant-postgres-repo repos)
57 1 : ids)
58 4 : user-ids (distinct-ids :user-id participants)
59 4 : rankedin-ids (distinct-ids :user-id-rankedin participants)
60 4 : team-ids (distinct-ids :team-id participants)
61 5 : users (when (seq user-ids)
62 6 : (user-postgres-repo/find-by-ids (:user-postgres-repo repos) user-ids))
63 5 : rankedin (when (seq rankedin-ids)
64 2 : (user-rankedin-postgres-repo/find-by-ids
65 3 : (:user-rankedin-postgres-repo repos)
66 1 : rankedin-ids))
67 4 : teams (when (seq team-ids)
68 0 : (team-postgres-repo/find-by-ids (:team-postgres-repo repos) team-ids))
69 3 : users-by-id (by-uuid users)
70 3 : rankedin-by-id (by-uuid rankedin)
71 3 : teams-by-id (by-uuid teams)]
72 2 : (into
73 1 : {}
74 2 : (map
75 1 : (fn [p]
76 12 : [(str (:uuid p)) (enrich-one p users-by-id rankedin-by-id teams-by-id)]))
77 1 : participants)))))
|