Line data Source code
1 4 : (ns matchscore.domain.participant.repository.postgres
2 : (:require
3 : [cats.monad.either :as either]
4 : [honey.sql :as sql]
5 : [matchscore.domain.participant.entity :as entity]
6 : [matchscore.utils.audit :as audit]
7 : [matchscore.utils.db :as utils.db]
8 : [matchscore.utils.errors :as utils.errors]
9 : [matchscore.utils.nanoid :as nanoid]
10 : [matchscore.utils.pagination :as qh]
11 : [next.jdbc :as jdbc]))
12 :
13 :
14 4 : (defprotocol
15 : ParticipantPostgresRepository
16 : (find-by-id
17 : [this id]
18 : "Find participant by UUID. Returns Participant map or nil.")
19 : (find-by-ids
20 : [this ids]
21 : "Find participants by multiple UUIDs. Returns seq.")
22 : (find-by-user-id
23 : [this user-id]
24 : "Find all participants for a matchscore user. Returns seq.")
25 : (find-one-by-user-id
26 : [this user-id]
27 : "Find the person participant for a matchscore user. Returns Participant map or nil.")
28 : (ensure-person-participant-for-user!
29 : [this user-id]
30 : "Returns the user's person participant, creating it when missing.")
31 : (find-by-team-id
32 : [this team-id]
33 : "Find all participants for a team. Returns seq.")
34 : (save!
35 : [this participant]
36 : "Persists a participant (insert). Returns Either<Error, Participant>.")
37 : (search
38 : [this filters pagination]
39 : "Search participants by filters and pagination.
40 : filters: {:type {...} :userId {...} :teamId {...}}
41 : pagination: {:limit 20 :cursor \"base64...\" :sort-by :created-datetime :sort-dir :asc}
42 : Returns {:items [...] :has-next-page bool :end-cursor \"base64-or-nil\"}"))
43 :
44 :
45 4 : (defn- filters->specs
46 : "Converts a GraphQL filters map to a seq of [column op value] specs."
47 : [{:keys [type userId teamId]}]
48 2 : (qh/typed-filters->specs
49 5 : {:type type
50 3 : :user-id (utils.db/cast-uuid-filter userId)
51 3 : :team-id (utils.db/cast-uuid-filter teamId)}))
52 :
53 :
54 4 : (defn- row->participant
55 : "Converts a database row to a domain Participant map."
56 : [row]
57 32 : (when row (entity/make-participant (utils.db/unqualify-keys row))))
58 :
59 :
60 12 : (def ^:private valid-sort-fields #{:created-datetime})
61 :
62 :
63 8 : (def ^:private default-sort :created-datetime)
64 :
65 :
66 4 : (def ^:private base-query
67 28 : {:select [[:p.uuid :uuid]
68 12 : [:p.public-id :public-id]
69 12 : [:p.source :source]
70 12 : [:p.user-id :user-id]
71 12 : [:p.user-id-rankedin :user-id-rankedin]
72 12 : [:p.team-id :team-id]
73 12 : [:p.type :type]
74 12 : [:p.created-datetime :created-datetime]]
75 16 : :from [[:participants :p]]})
76 :
77 :
78 4 : (defn- upsert-person-participant-by-user-id!
79 : [ds user-id]
80 : ;; Get-or-create: the conflict branch only re-reads the existing row, so the
81 : ;; audit columns are stamped on the insert alone and DO UPDATE leaves them be.
82 20 : (let [query (assoc
83 8 : (audit/stamp
84 20 : {:insert-into :participants
85 64 : :values [{:uuid [:cast (str (random-uuid)) :uuid]
86 12 : :public_id (nanoid/generate 15)
87 : :source "matchscore"
88 16 : :user-id [:cast user-id :uuid]
89 : :type "person"}]
90 8 : :returning [:*]})
91 32 : :on-conflict [:user-id {:where [:not= :user-id nil]}]
92 16 : :do-update-set {:fields [:user-id]})]
93 24 : (jdbc/execute-one! ds (sql/format query))))
94 :
95 :
96 4 : (defrecord
97 : PostgresParticipantRepository
98 : [ds]
99 : ParticipantPostgresRepository
100 :
101 : (find-by-id
102 : [_ id]
103 3 : (let [query (->
104 1 : base-query
105 8 : (assoc :where [:= :p.uuid [:cast id :uuid]]))
106 1 : result (->>
107 3 : (sql/format query)
108 3 : (jdbc/execute! ds)
109 1 : first)]
110 3 : (row->participant result)))
111 :
112 : (find-by-ids
113 : [_ ids]
114 12 : (if (empty? ids)
115 0 : []
116 9 : (let [query (->
117 3 : base-query
118 39 : (assoc :where [:in :p.uuid (mapv #(vector :cast % :uuid) ids)]))]
119 3 : (->>
120 9 : (sql/format query)
121 9 : (jdbc/execute! ds)
122 6 : (mapv row->participant)))))
123 :
124 : (find-by-user-id
125 : [_ user-id]
126 9 : (let [query (->
127 3 : base-query
128 24 : (assoc :where [:= :p.user-id [:cast user-id :uuid]]))
129 3 : result (->>
130 9 : (sql/format query)
131 9 : (jdbc/execute! ds)
132 6 : (mapv row->participant))]
133 3 : result))
134 :
135 : (find-one-by-user-id
136 : [this user-id]
137 0 : (first (filter #(= "person" (:type %)) (find-by-user-id this user-id))))
138 :
139 : (ensure-person-participant-for-user!
140 : [_ user-id]
141 4 : (try
142 32 : (either/right (row->participant (upsert-person-participant-by-user-id! ds user-id)))
143 : (catch Exception e
144 0 : (either/left
145 0 : (utils.errors/build-error
146 : :postgres
147 0 : {:message (.getMessage e)
148 0 : :context {:table :participants
149 : :op :upsert-person-by-user
150 0 : :user-id user-id}})))))
151 :
152 : (find-by-team-id
153 : [_ team-id]
154 3 : (let [query (->
155 1 : base-query
156 8 : (assoc :where [:= :p.team-id [:cast team-id :uuid]]))
157 1 : result (->>
158 3 : (sql/format query)
159 3 : (jdbc/execute! ds)
160 2 : (mapv row->participant))]
161 1 : result))
162 :
163 : (save!
164 : [_ participant]
165 4 : (try
166 8 : (let [{:keys [uuid public-id source user-id user-id-rankedin team-id type]} participant
167 20 : query {:insert-into :participants
168 52 : :values [{:uuid [:cast uuid :uuid]
169 4 : :public_id public-id
170 4 : :source source
171 28 : :user_id (when user-id [:cast user-id :uuid])
172 8 : :user_id_rankedin (when user-id-rankedin
173 0 : [:cast user-id-rankedin :uuid])
174 8 : :team_id (when team-id [:cast team-id :uuid])
175 4 : :type type}]
176 8 : :returning [:*]}
177 4 : result (->>
178 20 : (sql/format (audit/stamp query))
179 12 : (jdbc/execute! ds)
180 4 : first)]
181 20 : (either/right (row->participant result)))
182 : (catch Exception e
183 0 : (either/left
184 0 : (utils.errors/build-error
185 : :postgres
186 0 : {:message (.getMessage e)
187 0 : :context {:table :participants
188 : :op :insert
189 0 : :uuid (:uuid participant)}})))))
190 :
191 : (search
192 : [_ filters pagination]
193 4 : (let [specs (filters->specs filters)
194 2 : query (qh/build-search-query
195 1 : base-query
196 1 : specs
197 1 : valid-sort-fields
198 1 : default-sort
199 1 : pagination
200 5 : {:sort-field-casts {:created-datetime :timestamptz}})
201 1 : rows (->>
202 3 : (sql/format query)
203 3 : (jdbc/execute! ds)
204 2 : (mapv row->participant))
205 6 : opts (when-not (:cursor pagination)
206 5 : (let [cnt-query (qh/build-count-query base-query specs)
207 3 : total (->>
208 3 : (sql/format cnt-query)
209 3 : (jdbc/execute-one! ds)
210 1 : utils.db/unqualify-keys
211 : :total)]
212 3 : {:total-count total}))]
213 7 : (qh/paginate-results rows valid-sort-fields default-sort pagination opts))))
214 :
215 :
216 4 : (defn make-repository
217 : "Creates a PostgresParticipantRepository from a ds."
218 : [ds]
219 12 : (->PostgresParticipantRepository ds))
|