11package com .objectcomputing .checkins .services .pulseresponse ;
22
33import com .objectcomputing .checkins .exceptions .NotFoundException ;
4- import com .objectcomputing .checkins .util . form . FormUrlEncodedDecoder ;
4+ import com .objectcomputing .checkins .services . slack . SlackSubmissionHandler ;
55import com .objectcomputing .checkins .services .memberprofile .MemberProfileServices ;
66
77import io .micronaut .http .MediaType ;
2929import java .time .LocalDate ;
3030import java .util .Set ;
3131import java .util .UUID ;
32- import java .util .Map ;
33- import java .nio .charset .StandardCharsets ;
3432
3533@ Controller ("/services/pulse-responses" )
3634@ ExecuteOn (TaskExecutors .BLOCKING )
3735@ Tag (name = "pulse-responses" )
3836public class PulseResponseController {
3937 private final PulseResponseService pulseResponseServices ;
4038 private final MemberProfileServices memberProfileServices ;
41- private final SlackSignatureVerifier slackSignatureVerifier ;
42- private final PulseSlackCommand pulseSlackCommand ;
43- private final SlackPulseResponseConverter slackPulseResponseConverter ;
39+ private final SlackSubmissionHandler slackSubmissionHandler ;
4440
4541 public PulseResponseController (PulseResponseService pulseResponseServices ,
4642 MemberProfileServices memberProfileServices ,
47- SlackSignatureVerifier slackSignatureVerifier ,
48- PulseSlackCommand pulseSlackCommand ,
49- SlackPulseResponseConverter slackPulseResponseConverter ) {
43+ SlackSubmissionHandler slackSubmissionHandler ) {
5044 this .pulseResponseServices = pulseResponseServices ;
5145 this .memberProfileServices = memberProfileServices ;
52- this .slackSignatureVerifier = slackSignatureVerifier ;
53- this .pulseSlackCommand = pulseSlackCommand ;
54- this .slackPulseResponseConverter = slackPulseResponseConverter ;
46+ this .slackSubmissionHandler = slackSubmissionHandler ;
5547 }
5648
5749 /**
@@ -120,25 +112,8 @@ public HttpResponse commandPulseResponse(
120112 @ Header ("X-Slack-Signature" ) String signature ,
121113 @ Header ("X-Slack-Request-Timestamp" ) String timestamp ,
122114 @ Body String requestBody ) {
123- // Validate the request
124- if (slackSignatureVerifier .verifyRequest (signature ,
125- timestamp , requestBody )) {
126- // Convert the request body to a map of values.
127- FormUrlEncodedDecoder formUrlEncodedDecoder = new FormUrlEncodedDecoder ();
128- Map <String , Object > body =
129- formUrlEncodedDecoder .decode (requestBody ,
130- StandardCharsets .UTF_8 );
131-
132- // Respond to the slack command.
133- String triggerId = (String )body .get ("trigger_id" );
134- if (pulseSlackCommand .send (triggerId )) {
135- return HttpResponse .ok ();
136- } else {
137- return HttpResponse .status (HttpStatus .INTERNAL_SERVER_ERROR );
138- }
139- } else {
140- return HttpResponse .unauthorized ();
141- }
115+ return slackSubmissionHandler .commandResponse (signature ,
116+ timestamp , requestBody );
142117 }
143118
144119 @ Secured (SecurityRule .IS_ANONYMOUS )
@@ -148,53 +123,7 @@ public HttpResponse externalPulseResponse(
148123 @ Header ("X-Slack-Request-Timestamp" ) String timestamp ,
149124 @ Body String requestBody ,
150125 HttpRequest <?> request ) {
151- // Validate the request
152- if (slackSignatureVerifier .verifyRequest (signature ,
153- timestamp , requestBody )) {
154- // Convert the request body to a map of values.
155- FormUrlEncodedDecoder formUrlEncodedDecoder =
156- new FormUrlEncodedDecoder ();
157- Map <String , Object > body =
158- formUrlEncodedDecoder .decode (requestBody ,
159- StandardCharsets .UTF_8 );
160-
161- final String key = "payload" ;
162- if (body .containsKey (key )) {
163- PulseResponseCreateDTO pulseResponseDTO =
164- slackPulseResponseConverter .get (memberProfileServices ,
165- (String )body .get (key ));
166-
167- // If we receive a null DTO, that means that this is not the
168- // actual submission of the form. We can just return 200 so
169- // that Slack knows to continue without error.
170- if (pulseResponseDTO == null ) {
171- return HttpResponse .ok ();
172- }
173-
174- // Create the pulse response
175- PulseResponse pulseResponse =
176- pulseResponseServices .unsecureSave (
177- new PulseResponse (
178- pulseResponseDTO .getInternalScore (),
179- pulseResponseDTO .getExternalScore (),
180- pulseResponseDTO .getSubmissionDate (),
181- pulseResponseDTO .getTeamMemberId (),
182- pulseResponseDTO .getInternalFeelings (),
183- pulseResponseDTO .getExternalFeelings ()
184- )
185- );
186-
187- if (pulseResponse == null ) {
188- return HttpResponse .status (HttpStatus .CONFLICT ,
189- "Already submitted today" );
190- } else {
191- return HttpResponse .ok ();
192- }
193- } else {
194- return HttpResponse .unprocessableEntity ();
195- }
196- } else {
197- return HttpResponse .unauthorized ();
198- }
126+ return slackSubmissionHandler .externalResponse (signature , timestamp ,
127+ requestBody , request );
199128 }
200129}
0 commit comments