Skip to content

Commit a11ffaf

Browse files
committed
Handle required and optional values from Pulse submissions via Slack.
1 parent a8cb08f commit a11ffaf

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,46 @@ public static PulseResponseCreateDTO get(
3535
PulseResponseCreateDTO response = new PulseResponseCreateDTO();
3636
response.setTeamMemberId(lookupUser(memberProfileServices, map));
3737
response.setSubmissionDate(LocalDate.now());
38+
3839
response.setInternalScore(Integer.parseInt(
39-
getMappedValue(values, "internalScore")));
40+
getMappedValue(values, "internalScore", true)));
4041
response.setInternalFeelings(
41-
getMappedValue(values, "internalFeelings"));
42-
response.setExternalScore(Integer.parseInt(
43-
getMappedValue(values, "externalScore")));
42+
getMappedValue(values, "internalFeelings", false));
43+
44+
String score = getMappedValue(values, "externalScore", false);
45+
if (!score.isEmpty()) {
46+
response.setExternalScore(Integer.parseInt(score));
47+
}
4448
response.setExternalFeelings(
45-
getMappedValue(values, "externalFeelings"));
49+
getMappedValue(values, "externalFeelings", false));
4650

4751
return response;
4852
} catch(JsonProcessingException ex) {
4953
throw new BadArgException(ex.getMessage());
54+
} catch(NumberFormatException ex) {
55+
throw new BadArgException("Pulse scores must be integers");
5056
}
5157
} else {
5258
throw new BadArgException("Invalid pulse response body");
5359
}
5460
}
5561

56-
private static String getMappedValue(Map<String, Object> map, String key) {
57-
return (String)((Map<String, Object>)map.get(key)).get("value");
62+
private static String getMappedValue(Map<String, Object> map,
63+
String key, boolean required) {
64+
final String valueKey = "value";
65+
if (map.containsKey(key)) {
66+
final Map<String, Object> other = (Map<String, Object>)map.get(key);
67+
if (other.containsKey(valueKey)) {
68+
return (String)other.get(valueKey);
69+
}
70+
}
71+
72+
if (required) {
73+
throw new BadArgException(
74+
String.format("Expected %s.%s was not found", key, valueKey));
75+
} else {
76+
return "";
77+
}
5878
}
5979

6080
private static UUID lookupUser(MemberProfileServices memberProfileServices,

0 commit comments

Comments
 (0)