Line data Source code
1 4 : (ns matchscore.db.postgres.telegram-match-notifications
2 : (:require
3 : [matchscore.telegram.match-notifications :as match-notifications]
4 : [matchscore.utils.db :as utils.db]
5 : [next.jdbc :as jdbc]))
6 :
7 :
8 13 : (defn- row [value] (utils.db/unqualify-keys value))
9 :
10 :
11 4 : (defn event-context
12 : [ds event-id]
13 8 : (some->
14 2 : (jdbc/execute-one!
15 1 : ds
16 2 : ["SELECT e.uuid,
17 : e.public_id,
18 : e.name,
19 : e.state,
20 : e.language,
21 : e.timezone,
22 : e.settings,
23 : e.template_id,
24 : t.settings AS template_settings
25 : FROM events e
26 : LEFT JOIN templates t ON t.uuid = e.template_id
27 : WHERE e.uuid = ?::uuid"
28 3 : (str event-id)])
29 1 : row))
30 :
31 :
32 4 : (defn event-id-for-class
33 : [ds class-id]
34 46 : (some->
35 16 : (jdbc/execute-one! ds ["SELECT event_id FROM event_classes WHERE uuid = ?::uuid" (str class-id)])
36 2 : row
37 : :event-id
38 2 : str))
39 :
40 :
41 4 : (defn event-id-for-stage
42 : [ds stage-id]
43 23 : (some->
44 2 : (jdbc/execute-one!
45 1 : ds
46 2 : ["SELECT c.event_id
47 : FROM stages s
48 : JOIN event_classes c ON c.uuid = s.class_id
49 : WHERE s.uuid = ?::uuid"
50 3 : (str stage-id)])
51 1 : row
52 : :event-id
53 1 : str))
54 :
55 :
56 4 : (defn event-id-for-draw
57 : [ds draw-id]
58 46 : (some->
59 4 : (jdbc/execute-one!
60 2 : ds
61 4 : ["SELECT c.event_id
62 : FROM draws d
63 : JOIN stages s ON s.uuid = d.stage_id
64 : JOIN event_classes c ON c.uuid = s.class_id
65 : WHERE d.uuid = ?::uuid"
66 6 : (str draw-id)])
67 2 : row
68 : :event-id
69 2 : str))
70 :
71 :
72 4 : (defn active-event-ids-for-template
73 : [ds template-id]
74 2 : (->>
75 4 : (jdbc/execute!
76 2 : ds
77 4 : ["SELECT uuid
78 : FROM events
79 : WHERE template_id = ?::uuid
80 : AND state = 'active'"
81 6 : (str template-id)])
82 4 : (mapv #(str (:uuid (row %))))))
83 :
84 :
85 4 : (defn effective-enabled?
86 : [event]
87 4 : (and
88 5 : (= "active" (:state event))
89 7 : (match-notifications/effective-event-enabled? (:settings event) (:template-settings event))))
90 :
91 :
92 4 : (defn enabled-event-ids
93 : "Returns a bounded page of active events whose effective flag is enabled."
94 : [ds after-id limit]
95 1 : (->>
96 2 : (jdbc/execute!
97 1 : ds
98 2 : ["SELECT e.uuid
99 : FROM events e
100 : LEFT JOIN templates t ON t.uuid = e.template_id
101 : WHERE e.state = 'active'
102 : AND (?::uuid IS NULL OR e.uuid > ?::uuid)
103 : AND COALESCE(
104 : (e.settings ->> 'telegram-match-notifications-enabled')::boolean,
105 : (e.settings ->> 'telegram_match_notifications_enabled')::boolean,
106 : (e.settings ->> 'telegramMatchNotificationsEnabled')::boolean,
107 : (t.settings ->> 'telegram-match-notifications-enabled')::boolean,
108 : (t.settings ->> 'telegram_match_notifications_enabled')::boolean,
109 : (t.settings ->> 'telegramMatchNotificationsEnabled')::boolean,
110 : false)
111 : ORDER BY e.uuid
112 : LIMIT ?"
113 1 : after-id
114 1 : after-id
115 2 : (long limit)])
116 9 : (mapv #(str (:uuid (row %))))))
117 :
118 :
119 8 : (def ^:private match-context-sql
120 : "SELECT m.uuid,
121 : m.home_participant_id,
122 : m.away_participant_id,
123 : m.state,
124 : m.court,
125 : m.datetime_start,
126 : m.datetime_end,
127 : m.updated_datetime,
128 : e.uuid AS event_id,
129 : e.public_id AS event_public_id,
130 : e.name AS event_name,
131 : e.language,
132 : e.timezone,
133 : ec.uuid AS class_id,
134 : ec.public_id AS class_public_id,
135 : s.uuid AS stage_id,
136 : s.public_id AS stage_public_id,
137 : COALESCE(gd.uuid, bd.uuid) AS draw_id,
138 : COALESCE(gd.public_id, bd.public_id) AS draw_public_id,
139 : COALESCE(NULLIF(bm.label, ''), NULLIF(br.name, ''), NULLIF(g.name, ''), NULLIF(s.name, '')) AS round_label,
140 : hp.user_id AS home_user_id,
141 : hu.telegram_id AS home_telegram_id,
142 : hu.telegram_bot_access_status AS home_bot_access_status,
143 : hu.settings AS home_user_settings,
144 : COALESCE(hu.name, NULLIF(concat_ws(' ', hru.first_name, hru.last_name), ''), ht.name) AS home_name,
145 : ap.user_id AS away_user_id,
146 : au.telegram_id AS away_telegram_id,
147 : au.telegram_bot_access_status AS away_bot_access_status,
148 : au.settings AS away_user_settings,
149 : COALESCE(au.name, NULLIF(concat_ws(' ', aru.first_name, aru.last_name), ''), atm.name) AS away_name,
150 : mr.uuid AS result_uuid,
151 : mr.winner_id,
152 : mr.loser_id,
153 : mr.home_score,
154 : mr.away_score,
155 : mr.outcome,
156 : mr.ended_at,
157 : mr.updated_datetime AS result_updated_datetime,
158 : COALESCE(ms.segments, '[]'::jsonb) AS segments
159 : FROM matches m
160 : LEFT JOIN stages s ON s.uuid = m.stage_id
161 : LEFT JOIN event_classes ec ON ec.uuid = s.class_id
162 : JOIN events e ON e.uuid = COALESCE(m.event_id, ec.event_id)
163 : LEFT JOIN groups g ON g.uuid = m.group_id
164 : LEFT JOIN draws gd ON gd.uuid = g.draw_id
165 : LEFT JOIN bracket_matches bm ON bm.uuid = m.bracket_match_id
166 : LEFT JOIN bracket_rounds br ON br.uuid = bm.bracket_round_id
167 : LEFT JOIN brackets b ON b.uuid = br.bracket_id
168 : LEFT JOIN draws bd ON bd.uuid = b.draw_id
169 : LEFT JOIN participants hp ON hp.uuid = m.home_participant_id
170 : LEFT JOIN users hu ON hu.uuid = hp.user_id
171 : LEFT JOIN rankedin_users hru ON hru.uuid = hp.user_id_rankedin
172 : LEFT JOIN teams ht ON ht.uuid = hp.team_id
173 : LEFT JOIN participants ap ON ap.uuid = m.away_participant_id
174 : LEFT JOIN users au ON au.uuid = ap.user_id
175 : LEFT JOIN rankedin_users aru ON aru.uuid = ap.user_id_rankedin
176 : LEFT JOIN teams atm ON atm.uuid = ap.team_id
177 : LEFT JOIN match_results mr ON mr.match_id = m.uuid
178 : LEFT JOIN LATERAL (
179 : SELECT jsonb_agg(
180 : jsonb_build_object(
181 : 'uuid', seg.uuid,
182 : 'home-score', seg.home_score,
183 : 'away-score', seg.away_score,
184 : 'winner-id', seg.winner_id,
185 : 'state', seg.state,
186 : 'order', seg.\"order\")
187 : ORDER BY seg.\"order\") AS segments
188 : FROM match_segments seg
189 : WHERE seg.match_id = m.uuid
190 : ) ms ON true")
191 :
192 :
193 4 : (defn- row->match
194 : [db-row]
195 4 : (let [m (row db-row)]
196 16 : (cond-> (assoc m :segments (vec (or (:segments m) [])))
197 3 : (:result-uuid m) (assoc :result
198 0 : {:uuid (:result-uuid m)
199 0 : :winner-id (:winner-id m)
200 0 : :loser-id (:loser-id m)
201 0 : :home-score (:home-score m)
202 0 : :away-score (:away-score m)
203 0 : :outcome (:outcome m)
204 0 : :ended-at (:ended-at m)
205 0 : :updated-datetime (:result-updated-datetime m)}))))
206 :
207 :
208 4 : (defn event-matches
209 : [ds event-id]
210 1 : (->>
211 2 : (jdbc/execute!
212 1 : ds
213 4 : [(str
214 1 : match-context-sql
215 : " WHERE e.uuid = ?::uuid
216 : AND m.home_participant_id IS NOT NULL
217 : AND m.away_participant_id IS NOT NULL
218 : AND m.state NOT IN ('cancelled', 'walkover')
219 : ORDER BY m.uuid")
220 3 : (str event-id)])
221 2 : (mapv row->match)))
222 :
223 :
224 4 : (defn match-context
225 : [ds match-id]
226 8 : (some->
227 22 : (jdbc/execute-one! ds [(str match-context-sql " WHERE m.uuid = ?::uuid") (str match-id)])
228 0 : row->match))
229 :
230 :
231 4 : (defn card
232 : [ds match-id user-id]
233 8 : (some->
234 2 : (jdbc/execute-one!
235 1 : ds
236 2 : ["SELECT *
237 : FROM telegram_match_notifications
238 : WHERE match_id = ?::uuid AND recipient_user_id = ?::uuid"
239 3 : (str match-id)
240 3 : (str user-id)])
241 1 : row))
242 :
243 :
244 4 : (defn event-cards
245 : [ds event-id]
246 1 : (->>
247 2 : (jdbc/execute!
248 1 : ds
249 2 : ["SELECT n.*,
250 : u.settings AS user_settings,
251 : u.telegram_bot_access_status AS bot_access_status
252 : FROM telegram_match_notifications n
253 : JOIN users u ON u.uuid = n.recipient_user_id
254 : WHERE n.event_id = ?::uuid"
255 3 : (str event-id)])
256 2 : (mapv row)))
257 :
258 :
259 4 : (defn save-card!
260 : [ds
261 : {:keys [event-id
262 : match-id
263 : recipient-user-id
264 : participant-id
265 : telegram-id
266 : pairing-revision
267 : result-revision
268 : status
269 : message-id
270 : last-error]}]
271 8 : (some->
272 2 : (jdbc/execute-one!
273 1 : ds
274 2 : ["INSERT INTO telegram_match_notifications
275 : (event_id, match_id, recipient_user_id, participant_id, telegram_id,
276 : message_id, pairing_revision, result_revision, status, last_error,
277 : last_attempt_at, sent_at)
278 : VALUES (?::uuid, ?::uuid, ?::uuid, ?::uuid, ?, ?, ?, ?, ?, ?, now(),
279 : CASE WHEN ? = 'sent' THEN now() ELSE NULL END)
280 : ON CONFLICT (match_id, recipient_user_id) DO UPDATE SET
281 : event_id = EXCLUDED.event_id,
282 : participant_id = EXCLUDED.participant_id,
283 : message_id = CASE
284 : WHEN EXCLUDED.telegram_id IS DISTINCT FROM
285 : telegram_match_notifications.telegram_id
286 : THEN EXCLUDED.message_id
287 : ELSE COALESCE(
288 : EXCLUDED.message_id,
289 : telegram_match_notifications.message_id)
290 : END,
291 : telegram_id = EXCLUDED.telegram_id,
292 : pairing_revision = EXCLUDED.pairing_revision,
293 : result_revision = EXCLUDED.result_revision,
294 : status = EXCLUDED.status,
295 : last_error = EXCLUDED.last_error,
296 : last_attempt_at = now(),
297 : sent_at = CASE WHEN EXCLUDED.status = 'sent'
298 : THEN COALESCE(telegram_match_notifications.sent_at, now())
299 : ELSE telegram_match_notifications.sent_at END
300 : RETURNING *"
301 3 : (str event-id)
302 3 : (str match-id)
303 3 : (str recipient-user-id)
304 3 : (str participant-id)
305 2 : (long telegram-id)
306 1 : message-id
307 1 : pairing-revision
308 1 : result-revision
309 1 : status
310 1 : last-error
311 1 : status])
312 1 : row))
313 :
314 :
315 4 : (defn update-card!
316 : [ds card-id {:keys [message-id pairing-revision result-revision status last-error]}]
317 0 : (some->
318 0 : (jdbc/execute-one!
319 0 : ds
320 0 : ["UPDATE telegram_match_notifications
321 : SET message_id = COALESCE(?, message_id),
322 : pairing_revision = COALESCE(?, pairing_revision),
323 : result_revision = ?,
324 : status = ?,
325 : last_error = ?,
326 : last_attempt_at = now(),
327 : sent_at = CASE WHEN ? = 'sent' THEN COALESCE(sent_at, now()) ELSE sent_at END
328 : WHERE uuid = ?::uuid
329 : RETURNING *"
330 0 : message-id
331 0 : pairing-revision
332 0 : result-revision
333 0 : status
334 0 : last-error
335 0 : status
336 0 : (str card-id)])
337 0 : row))
338 :
339 :
340 4 : (defn ensure-alert!
341 : [ds {:keys [event-id match-id recipient-user-id result-revision status]}]
342 0 : (some->
343 0 : (jdbc/execute-one!
344 0 : ds
345 0 : ["INSERT INTO telegram_match_result_alerts
346 : (event_id, match_id, recipient_user_id, result_revision, status, last_attempt_at)
347 : VALUES (?::uuid, ?::uuid, ?::uuid, ?, ?, now())
348 : ON CONFLICT (match_id, recipient_user_id, result_revision) DO NOTHING
349 : RETURNING *"
350 0 : (str event-id)
351 0 : (str match-id)
352 0 : (str recipient-user-id)
353 0 : result-revision
354 0 : status])
355 0 : row))
356 :
357 :
358 4 : (defn alert
359 : [ds match-id user-id result-revision]
360 0 : (some->
361 0 : (jdbc/execute-one!
362 0 : ds
363 0 : ["SELECT *
364 : FROM telegram_match_result_alerts
365 : WHERE match_id = ?::uuid
366 : AND recipient_user_id = ?::uuid
367 : AND result_revision = ?"
368 0 : (str match-id)
369 0 : (str user-id)
370 0 : result-revision])
371 0 : row))
372 :
373 :
374 4 : (defn update-alert!
375 : [ds alert-id {:keys [status message-id last-error]}]
376 0 : (some->
377 0 : (jdbc/execute-one!
378 0 : ds
379 0 : ["UPDATE telegram_match_result_alerts
380 : SET status = ?,
381 : message_id = COALESCE(?, message_id),
382 : last_error = ?,
383 : last_attempt_at = now(),
384 : sent_at = CASE WHEN ? = 'sent' THEN COALESCE(sent_at, now()) ELSE sent_at END
385 : WHERE uuid = ?::uuid
386 : RETURNING *"
387 0 : status
388 0 : message-id
389 0 : last-error
390 0 : status
391 0 : (str alert-id)])
392 0 : row))
393 :
394 :
395 4 : (defn retryable-alerts
396 : [ds event-id]
397 0 : (->>
398 0 : (jdbc/execute!
399 0 : ds
400 0 : ["SELECT *
401 : FROM telegram_match_result_alerts
402 : WHERE event_id = ?::uuid
403 : AND status IN ('pending', 'retrying')
404 : AND (last_attempt_at IS NULL OR last_attempt_at < now() - interval '30 seconds')
405 : ORDER BY created_datetime
406 : LIMIT 100"
407 0 : (str event-id)])
408 0 : (mapv row)))
409 :
410 :
411 4 : (defn mark-user-denied!
412 : [ds user-id]
413 4 : (jdbc/execute-one!
414 2 : ds
415 4 : ["UPDATE users
416 : SET telegram_bot_access_status = 'denied',
417 : telegram_bot_access_updated_at = now(),
418 : updated_by = NULL
419 : WHERE uuid = ?::uuid
420 : AND telegram_bot_access_status <> 'denied'"
421 6 : (str user-id)]))
|