Skip to content

Commit 0c12da8

Browse files
committed
Moved slack functionality into a new slack directory.
1 parent 0663430 commit 0c12da8

File tree

5 files changed

+129
-88
lines changed

5 files changed

+129
-88
lines changed

server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/PulseResponseController.java

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.objectcomputing.checkins.services.pulseresponse;
22

33
import com.objectcomputing.checkins.exceptions.NotFoundException;
4-
import com.objectcomputing.checkins.util.form.FormUrlEncodedDecoder;
4+
import com.objectcomputing.checkins.services.slack.SlackSubmissionHandler;
55
import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices;
66

77
import io.micronaut.http.MediaType;
@@ -29,29 +29,21 @@
2929
import java.time.LocalDate;
3030
import java.util.Set;
3131
import 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")
3836
public 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
}

server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/PulseSlackCommand.java renamed to server/src/main/java/com/objectcomputing/checkins/services/slack/PulseSlackCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.objectcomputing.checkins.services.pulseresponse;
1+
package com.objectcomputing.checkins.services.slack;
22

33
import com.objectcomputing.checkins.configuration.CheckInsConfiguration;
44

server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/SlackPulseResponseConverter.java renamed to server/src/main/java/com/objectcomputing/checkins/services/slack/SlackPulseResponseConverter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.objectcomputing.checkins.services.pulseresponse;
1+
package com.objectcomputing.checkins.services.slack;
22

33
import com.objectcomputing.checkins.exceptions.BadArgException;
44
import com.objectcomputing.checkins.services.memberprofile.MemberProfile;
55
import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices;
66
import com.objectcomputing.checkins.notifications.social_media.SlackSearch;
7+
import com.objectcomputing.checkins.services.pulseresponse.PulseResponseCreateDTO;
78

89
import jakarta.inject.Singleton;
910

@@ -23,13 +24,15 @@ public class SlackPulseResponseConverter {
2324
private static final Logger LOG = LoggerFactory.getLogger(SlackPulseResponseConverter.class);
2425

2526
private final SlackSearch slackSearch;
27+
private final MemberProfileServices memberProfileServices;
2628

27-
public SlackPulseResponseConverter(SlackSearch slackSearch) {
29+
public SlackPulseResponseConverter(SlackSearch slackSearch,
30+
MemberProfileServices memberProfileServices) {
2831
this.slackSearch = slackSearch;
32+
this.memberProfileServices = memberProfileServices;
2933
}
3034

31-
public PulseResponseCreateDTO get(
32-
MemberProfileServices memberProfileServices, String body) {
35+
public PulseResponseCreateDTO get(String body) {
3336
try {
3437
// Get the map of values from the string body
3538
final ObjectMapper mapper = new ObjectMapper();
@@ -47,7 +50,7 @@ public PulseResponseCreateDTO get(
4750

4851
// Create the pulse DTO and fill in the values.
4952
PulseResponseCreateDTO response = new PulseResponseCreateDTO();
50-
response.setTeamMemberId(lookupUser(memberProfileServices, map));
53+
response.setTeamMemberId(lookupUser(map));
5154
response.setSubmissionDate(LocalDate.now());
5255

5356
// Internal Score
@@ -111,8 +114,7 @@ private String getMappedValue(Map<String, Object> map, String key1,
111114
}
112115
}
113116

114-
private UUID lookupUser(MemberProfileServices memberProfileServices,
115-
Map<String, Object> map) {
117+
private UUID lookupUser(Map<String, Object> map) {
116118
// Get the user's profile map.
117119
Map<String, Object> user = (Map<String, Object>)map.get("user");
118120

server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/SlackSignatureVerifier.java renamed to server/src/main/java/com/objectcomputing/checkins/services/slack/SlackSignatureVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.objectcomputing.checkins.services.pulseresponse;
1+
package com.objectcomputing.checkins.services.slack;
22

33
import com.objectcomputing.checkins.configuration.CheckInsConfiguration;
44

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.objectcomputing.checkins.services.slack;
2+
3+
import com.objectcomputing.checkins.util.form.FormUrlEncodedDecoder;
4+
import com.objectcomputing.checkins.services.pulseresponse.PulseResponse;
5+
import com.objectcomputing.checkins.services.pulseresponse.PulseResponseService;
6+
import com.objectcomputing.checkins.services.pulseresponse.PulseResponseCreateDTO;
7+
8+
import io.micronaut.http.HttpStatus;
9+
import io.micronaut.http.HttpRequest;
10+
import io.micronaut.http.HttpResponse;
11+
12+
import jakarta.inject.Singleton;
13+
14+
import java.util.Map;
15+
import java.nio.charset.StandardCharsets;
16+
17+
@Singleton
18+
public class SlackSubmissionHandler {
19+
private final PulseResponseService pulseResponseServices;
20+
private final SlackSignatureVerifier slackSignatureVerifier;
21+
private final PulseSlackCommand pulseSlackCommand;
22+
private final SlackPulseResponseConverter slackPulseResponseConverter;
23+
24+
public SlackSubmissionHandler(PulseResponseService pulseResponseServices,
25+
SlackSignatureVerifier slackSignatureVerifier,
26+
PulseSlackCommand pulseSlackCommand,
27+
SlackPulseResponseConverter slackPulseResponseConverter) {
28+
this.pulseResponseServices = pulseResponseServices;
29+
this.slackSignatureVerifier = slackSignatureVerifier;
30+
this.pulseSlackCommand = pulseSlackCommand;
31+
this.slackPulseResponseConverter = slackPulseResponseConverter;
32+
}
33+
34+
public HttpResponse commandResponse(String signature,
35+
String timestamp,
36+
String requestBody) {
37+
// Validate the request
38+
if (slackSignatureVerifier.verifyRequest(signature,
39+
timestamp, requestBody)) {
40+
// Convert the request body to a map of values.
41+
FormUrlEncodedDecoder formUrlEncodedDecoder = new FormUrlEncodedDecoder();
42+
Map<String, Object> body =
43+
formUrlEncodedDecoder.decode(requestBody,
44+
StandardCharsets.UTF_8);
45+
46+
// Respond to the slack command.
47+
String triggerId = (String)body.get("trigger_id");
48+
if (pulseSlackCommand.send(triggerId)) {
49+
return HttpResponse.ok();
50+
} else {
51+
return HttpResponse.status(HttpStatus.INTERNAL_SERVER_ERROR);
52+
}
53+
} else {
54+
return HttpResponse.unauthorized();
55+
}
56+
}
57+
58+
public HttpResponse externalResponse(String signature,
59+
String timestamp,
60+
String requestBody,
61+
HttpRequest<?> request) {
62+
// Validate the request
63+
if (slackSignatureVerifier.verifyRequest(signature,
64+
timestamp, requestBody)) {
65+
// Convert the request body to a map of values.
66+
FormUrlEncodedDecoder formUrlEncodedDecoder =
67+
new FormUrlEncodedDecoder();
68+
Map<String, Object> body =
69+
formUrlEncodedDecoder.decode(requestBody,
70+
StandardCharsets.UTF_8);
71+
72+
final String key = "payload";
73+
if (body.containsKey(key)) {
74+
PulseResponseCreateDTO pulseResponseDTO =
75+
slackPulseResponseConverter.get((String)body.get(key));
76+
77+
// If we receive a null DTO, that means that this is not the
78+
// actual submission of the form. We can just return 200 so
79+
// that Slack knows to continue without error.
80+
if (pulseResponseDTO == null) {
81+
return HttpResponse.ok();
82+
}
83+
84+
// Create the pulse response
85+
PulseResponse pulseResponse =
86+
pulseResponseServices.unsecureSave(
87+
new PulseResponse(
88+
pulseResponseDTO.getInternalScore(),
89+
pulseResponseDTO.getExternalScore(),
90+
pulseResponseDTO.getSubmissionDate(),
91+
pulseResponseDTO.getTeamMemberId(),
92+
pulseResponseDTO.getInternalFeelings(),
93+
pulseResponseDTO.getExternalFeelings()
94+
)
95+
);
96+
97+
if (pulseResponse == null) {
98+
return HttpResponse.status(HttpStatus.CONFLICT,
99+
"Already submitted today");
100+
} else {
101+
return HttpResponse.ok();
102+
}
103+
} else {
104+
return HttpResponse.unprocessableEntity();
105+
}
106+
} else {
107+
return HttpResponse.unauthorized();
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)