From a11ffaf6bb58841285fe89441fe3d7e1132f1a4e Mon Sep 17 00:00:00 2001
From: Chad Elliott <elliottc@objectcomputing.com>
Date: Fri, 10 Jan 2025 09:08:16 -0600
Subject: [PATCH] Handle required and optional values from Pulse submissions
 via Slack.

---
 .../SlackPulseResponseConverter.java          | 34 +++++++++++++++----
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/SlackPulseResponseConverter.java b/server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/SlackPulseResponseConverter.java
index b93c65541..42dd43679 100644
--- a/server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/SlackPulseResponseConverter.java
+++ b/server/src/main/java/com/objectcomputing/checkins/services/pulseresponse/SlackPulseResponseConverter.java
@@ -35,26 +35,46 @@ public static PulseResponseCreateDTO get(
                 PulseResponseCreateDTO response = new PulseResponseCreateDTO();
                 response.setTeamMemberId(lookupUser(memberProfileServices, map));
                 response.setSubmissionDate(LocalDate.now());
+
                 response.setInternalScore(Integer.parseInt(
-                    getMappedValue(values, "internalScore")));
+                    getMappedValue(values, "internalScore", true)));
                 response.setInternalFeelings(
-                    getMappedValue(values, "internalFeelings"));
-                response.setExternalScore(Integer.parseInt(
-                    getMappedValue(values, "externalScore")));
+                    getMappedValue(values, "internalFeelings", false));
+
+                String score = getMappedValue(values, "externalScore", false);
+                if (!score.isEmpty()) {
+                    response.setExternalScore(Integer.parseInt(score));
+                }
                 response.setExternalFeelings(
-                    getMappedValue(values, "externalFeelings"));
+                    getMappedValue(values, "externalFeelings", false));
 
                 return response;
             } catch(JsonProcessingException ex) {
                 throw new BadArgException(ex.getMessage());
+            } catch(NumberFormatException ex) {
+                throw new BadArgException("Pulse scores must be integers");
             }
         } else {
             throw new BadArgException("Invalid pulse response body");
         }
     }
 
-    private static String getMappedValue(Map<String, Object> map, String key) {
-        return (String)((Map<String, Object>)map.get(key)).get("value");
+    private static String getMappedValue(Map<String, Object> map,
+                                         String key, boolean required) {
+        final String valueKey = "value";
+        if (map.containsKey(key)) {
+            final Map<String, Object> other = (Map<String, Object>)map.get(key);
+            if (other.containsKey(valueKey)) {
+                return (String)other.get(valueKey);
+            }
+        }
+
+        if (required) {
+            throw new BadArgException(
+                String.format("Expected %s.%s was not found", key, valueKey));
+        } else {
+            return "";
+        }
     }
 
     private static UUID lookupUser(MemberProfileServices memberProfileServices,