Line data Source code
1 4 : (ns matchscore.api.telegram-notifications
2 : (:require
3 : [matchscore.domain.event.repository.postgres :as event-postgres-repo]
4 : [matchscore.jobs.scheduler :as scheduler]
5 : [matchscore.jobs.telegram-event-ended :as telegram-event-ended-jobs]
6 : [matchscore.jobs.telegram-notifications :as telegram-notifications-jobs]
7 : [ring.util.http-status :as status]
8 : [schemas.api.core :as api-schemas]))
9 :
10 :
11 4 : (defn- schedule-post-participants-job!
12 : [goose event-uuid]
13 3 : (scheduler/schedule-job
14 1 : goose
15 : `telegram-notifications-jobs/post-participants-job
16 4 : {:args [event-uuid]}))
17 :
18 :
19 4 : (defn- schedule-post-draws-job!
20 : [goose event-uuid]
21 8 : (scheduler/schedule-job goose `telegram-notifications-jobs/post-draws-job {:args [event-uuid]}))
22 :
23 :
24 4 : (defn- schedule-post-results-job!
25 : [goose event-uuid]
26 3 : (scheduler/schedule-job
27 1 : goose
28 : `telegram-event-ended-jobs/send-event-results-updated-job
29 4 : {:args [event-uuid]}))
30 :
31 :
32 4 : (def PostParticipantsJobAcceptedResponseSchema
33 28 : [:map {:swagger/title "PostParticipantsJobAcceptedResponseSchema"
34 : :swagger/description "Successful participants post job scheduling response"}
35 20 : [:status [:= status/accepted]]
36 8 : [:body
37 8 : [:map
38 12 : [:message string?]
39 8 : [:data
40 8 : [:map
41 20 : [:event-uuid [:ref :types/UuidString]]
42 20 : [:template-uuid {:optional true}
43 12 : [:ref :types/UuidString]]]]]]])
44 :
45 :
46 4 : (def PostParticipantsJobTemplateResponseSchema
47 28 : [:or {:swagger/title "PostParticipantsJobTemplateResponseSchema"
48 : :swagger/description "Template participants post job scheduling response"}
49 4 : PostParticipantsJobAcceptedResponseSchema
50 56 : [:map [:status [:= status/not-found]] [:body [:map [:message string?]]]]])
51 :
52 :
53 4 : (defn event-post-participants-job-handler
54 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PostParticipantsJobAcceptedResponseSchema]
55 : :malli/parameters {:path api-schemas/EventPathParamsSchema}
56 : :authz {:obj :event
57 : :act :update
58 : :id :route-params/event-uuid}
59 : :summary "Post participants to Telegram"
60 : :description "Schedules a fresh Telegram participants post for the event."}
61 : [{:keys [goose route-params]
62 : :as _request}]
63 6 : (schedule-post-participants-job! goose (:event-uuid route-params))
64 7 : {:body {:message "Participants post job scheduled."
65 5 : :data {:event-uuid (:event-uuid route-params)}}
66 1 : :status status/accepted})
67 :
68 :
69 4 : (def PostResultsJobAcceptedResponseSchema
70 28 : [:map {:swagger/title "PostResultsJobAcceptedResponseSchema"
71 : :swagger/description "Successful results post job scheduling response"}
72 20 : [:status [:= status/accepted]]
73 64 : [:body [:map [:message string?] [:data [:map [:event-uuid [:ref :types/UuidString]]]]]]])
74 :
75 :
76 4 : (defn event-post-results-job-handler
77 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PostResultsJobAcceptedResponseSchema]
78 : :malli/parameters {:path api-schemas/EventPathParamsSchema}
79 : :authz {:obj :event
80 : :act :update
81 : :id :route-params/event-uuid}
82 : :summary "Post results to Telegram"
83 : :description
84 : "Schedules a Telegram results re-post for the event. Intended for notifying linked groups
85 : after an already-completed event's results have changed."}
86 : [{:keys [goose ctx route-params]
87 : :as _request}]
88 4 : (let [event-uuid (:event-uuid route-params)
89 3 : event (event-postgres-repo/find-by-id
90 6 : (get-in ctx [:repos :event-postgres-repo])
91 1 : event-uuid
92 : nil)]
93 7 : (cond (nil? event)
94 0 : (throw
95 0 : (ex-info
96 : "Event not found"
97 0 : {:type :api-not-found
98 0 : :message (str "Event with uuid " event-uuid " not found")}))
99 :
100 6 : (not= "completed" (:state event))
101 1 : (throw
102 3 : (ex-info
103 : "Event is not completed"
104 4 : {:type :internal-validation-error
105 4 : :humanized-errors {:state ["Results can only be posted for completed events."]}}))
106 :
107 : :else
108 1 : (do
109 4 : (schedule-post-results-job! goose event-uuid)
110 7 : {:body {:message "Results post job scheduled."
111 3 : :data {:event-uuid event-uuid}}
112 1 : :status status/accepted}))))
113 :
114 :
115 4 : (def PostDrawsJobAcceptedResponseSchema
116 28 : [:map {:swagger/title "PostDrawsJobAcceptedResponseSchema"
117 : :swagger/description "Successful draws post job scheduling response"}
118 20 : [:status [:= status/accepted]]
119 64 : [:body [:map [:message string?] [:data [:map [:event-uuid [:ref :types/UuidString]]]]]]])
120 :
121 :
122 4 : (defn event-post-draws-job-handler
123 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PostDrawsJobAcceptedResponseSchema]
124 : :malli/parameters {:path api-schemas/EventPathParamsSchema}
125 : :authz {:obj :event
126 : :act :update
127 : :id :route-params/event-uuid}
128 : :summary "Post stage-1 draws to Telegram"
129 : :description "Schedules a Telegram stage-1 draws post for the event."}
130 : [{:keys [goose route-params]
131 : :as _request}]
132 6 : (schedule-post-draws-job! goose (:event-uuid route-params))
133 7 : {:body {:message "Draws post job scheduled."
134 5 : :data {:event-uuid (:event-uuid route-params)}}
135 1 : :status status/accepted})
136 :
137 :
138 4 : (defn template-post-participants-job-handler
139 : {:malli/schema [:=> [:cat api-schemas/RingRequest] PostParticipantsJobTemplateResponseSchema]
140 : :malli/parameters {:path api-schemas/TemplatePathParamsSchema}
141 : :authz {:obj :template
142 : :act :update
143 : :id :route-params/template-uuid}
144 : :summary "Post template event participants to Telegram"
145 : :description "Schedules a fresh Telegram participants post for the active event of the template."}
146 : [{:keys [goose ctx route-params]
147 : :as _request}]
148 3 : (let [event (event-postgres-repo/find-active-by-template-id
149 6 : (get-in ctx [:repos :event-postgres-repo])
150 3 : (:template-uuid route-params))]
151 2 : (if event
152 1 : (do
153 6 : (schedule-post-participants-job! goose (:uuid event))
154 7 : {:body {:message "Participants post job scheduled."
155 6 : :data {:event-uuid (:uuid event)
156 3 : :template-uuid (:template-uuid route-params)}}
157 1 : :status status/accepted})
158 0 : {:body {:message "Active event not found for template."}
159 0 : :status status/not-found})))
|