Line data Source code
1 4 : (ns matchscore.realtime.events.match-results
2 : (:require
3 : [cats.core :as cats]
4 : [cats.monad.either :as either]
5 : [matchscore.access.match-results :as match-result-access]
6 : [matchscore.domain.match-result.command :as command]
7 : [matchscore.jobs.advancements :as advancements-jobs]
8 : [matchscore.jobs.scheduler :as scheduler]
9 : [matchscore.jobs.telegram-match-notifications :as match-notification-jobs]
10 : [matchscore.realtime.core :as realtime]
11 : [matchscore.realtime.events.core :refer [-event-msg-handler]]
12 : [taoensso.telemere :as t]))
13 :
14 :
15 4 : (defn- schedule-reconcile!
16 : [goose match-uuid]
17 2 : (when goose
18 0 : (try
19 0 : (scheduler/schedule-job
20 0 : goose
21 : `advancements-jobs/reconcile-for-match-job
22 0 : {:args [{:match-uuid (str match-uuid)}]})
23 : (catch Exception e
24 0 : (t/log!
25 : :error
26 0 : {:message "Failed to schedule advancement reconcile job"
27 0 : :match-uuid (str match-uuid)
28 0 : :exception e})))))
29 :
30 :
31 12 : (defmethod -event-msg-handler :match-result/add
32 : [{:keys [?data uid ctx]}]
33 2 : (let [{:keys [repos pg sente goose redis]} ctx
34 1 : {:keys [match-uuid client-id]} ?data]
35 98 : (t/log! :info (str "User " uid " adding match result for match " match-uuid))
36 6 : (let [authorization (match-result-access/authorize ctx uid match-uuid)
37 4 : result (if (either/right? authorization)
38 3 : (command/save-match-result!
39 4 : {:repos repos
40 3 : :pg {:datasource pg}}
41 5 : (assoc ?data :match-id match-uuid)
42 : :user-uuid
43 1 : uid)
44 0 : authorization)]
45 4 : (if (either/right? result)
46 4 : (let [{:keys [data event-uuid]} (cats/extract result)]
47 4 : (schedule-reconcile! goose match-uuid)
48 5 : (match-notification-jobs/record-result-alerts! ctx match-uuid uid)
49 6 : (match-notification-jobs/schedule-result-sync! goose match-uuid uid :add)
50 2 : (realtime/send!
51 1 : sente
52 1 : uid
53 6 : [:match-result/add-ok {:match-uuid match-uuid
54 4 : :data (into {} data)}])
55 2 : (when event-uuid
56 0 : (realtime/relay-to-event!
57 0 : sente
58 0 : redis
59 0 : event-uuid
60 0 : [:match-result/updated {:match-uuid (str match-uuid)
61 0 : :event-uuid event-uuid
62 0 : :data (into {} data)
63 0 : :client-id client-id}])))
64 1 : (do
65 92 : (t/log!
66 : :error
67 5 : {:message "Failed to save match result"
68 3 : :match-uuid (str match-uuid)
69 3 : :error (cats/extract result)})
70 2 : (realtime/send!
71 1 : sente
72 1 : uid
73 6 : [:match-result/add-error {:match-uuid match-uuid
74 5 : :error (str (cats/extract result))}]))))))
75 :
76 :
77 12 : (defmethod -event-msg-handler :match-result/edit
78 : [{:keys [?data uid ctx]}]
79 2 : (let [{:keys [repos pg sente goose redis]} ctx
80 1 : {:keys [match-uuid client-id]} ?data]
81 98 : (t/log! :info (str "User " uid " editing match result for match " match-uuid))
82 6 : (let [authorization (match-result-access/authorize ctx uid match-uuid)
83 4 : result (if (either/right? authorization)
84 3 : (command/save-match-result!
85 4 : {:repos repos
86 3 : :pg {:datasource pg}}
87 5 : (assoc ?data :match-id match-uuid)
88 : :user-uuid
89 1 : uid)
90 0 : authorization)]
91 4 : (if (either/right? result)
92 4 : (let [{:keys [data event-uuid]} (cats/extract result)]
93 4 : (schedule-reconcile! goose match-uuid)
94 5 : (match-notification-jobs/record-result-alerts! ctx match-uuid uid)
95 6 : (match-notification-jobs/schedule-result-sync! goose match-uuid uid :edit)
96 2 : (realtime/send!
97 1 : sente
98 1 : uid
99 6 : [:match-result/edit-ok {:match-uuid match-uuid
100 4 : :data (into {} data)}])
101 3 : (when event-uuid
102 2 : (realtime/relay-to-event!
103 1 : sente
104 1 : redis
105 1 : event-uuid
106 10 : [:match-result/updated {:match-uuid (str match-uuid)
107 1 : :event-uuid event-uuid
108 4 : :data (into {} data)
109 1 : :client-id client-id}])))
110 1 : (do
111 92 : (t/log!
112 : :error
113 5 : {:message "Failed to edit match result"
114 3 : :match-uuid (str match-uuid)
115 3 : :error (cats/extract result)})
116 2 : (realtime/send!
117 1 : sente
118 1 : uid
119 6 : [:match-result/edit-error {:match-uuid match-uuid
120 5 : :error (str (cats/extract result))}]))))))
121 :
122 :
123 12 : (defmethod -event-msg-handler :match-result/remove
124 : [{:keys [?data uid ctx]}]
125 2 : (let [{:keys [repos pg sente goose redis]} ctx
126 1 : {:keys [match-uuid client-id]} ?data]
127 98 : (t/log! :info (str "User " uid " removing match result for match " match-uuid))
128 6 : (let [authorization (match-result-access/authorize ctx uid match-uuid)
129 4 : result (if (either/right? authorization)
130 3 : (command/delete-match-result!
131 4 : {:repos repos
132 3 : :pg {:datasource pg}}
133 3 : {:match-id match-uuid}
134 : :user-uuid
135 1 : uid)
136 0 : authorization)]
137 4 : (if (either/right? result)
138 4 : (let [{:keys [event-uuid]} (cats/extract result)]
139 4 : (schedule-reconcile! goose match-uuid)
140 5 : (match-notification-jobs/record-result-alerts! ctx match-uuid uid)
141 6 : (match-notification-jobs/schedule-result-sync! goose match-uuid uid :delete)
142 2 : (realtime/send!
143 1 : sente
144 1 : uid
145 5 : [:match-result/remove-ok {:match-uuid match-uuid}])
146 2 : (when event-uuid
147 0 : (realtime/relay-to-event!
148 0 : sente
149 0 : redis
150 0 : event-uuid
151 0 : [:match-result/deleted {:match-uuid (str match-uuid)
152 0 : :event-uuid event-uuid
153 0 : :client-id client-id}])))
154 2 : (realtime/send!
155 1 : sente
156 1 : uid
157 6 : [:match-result/remove-error {:match-uuid match-uuid
158 5 : :error (str (cats/extract result))}])))))
|