33import com .objectcomputing .checkins .exceptions .BadArgException ;
44import com .objectcomputing .checkins .services .memberprofile .MemberProfile ;
55import com .objectcomputing .checkins .services .memberprofile .MemberProfileServices ;
6+ import com .objectcomputing .checkins .notifications .social_media .SlackSearch ;
7+
8+ import jakarta .inject .Singleton ;
69
710import org .slf4j .Logger ;
811import org .slf4j .LoggerFactory ;
1518import java .util .UUID ;
1619import java .time .LocalDate ;
1720
21+ @ Singleton
1822public class SlackPulseResponseConverter {
1923 private static final Logger LOG = LoggerFactory .getLogger (SlackPulseResponseConverter .class );
2024
21- public static PulseResponseCreateDTO get (
25+ private final SlackSearch slackSearch ;
26+
27+ public SlackPulseResponseConverter (SlackSearch slackSearch ) {
28+ this .slackSearch = slackSearch ;
29+ }
30+
31+ public PulseResponseCreateDTO get (
2232 MemberProfileServices memberProfileServices , String body ) {
23- final String key = "payload=" ;
24- final int start = body .indexOf (key );
25- if (start >= 0 ) {
26- try {
27- // Get the map of values from the string body
28- final ObjectMapper mapper = new ObjectMapper ();
29- final Map <String , Object > map =
30- mapper .readValue (body .substring (start + key .length ()),
31- new TypeReference <>() {});
33+ try {
34+ // Get the map of values from the string body
35+ final ObjectMapper mapper = new ObjectMapper ();
36+ final Map <String , Object > map =
37+ mapper .readValue (body , new TypeReference <>() {});
38+ final String type = (String )map .get ("type" );
39+
40+ if (type .equals ("view_submission" )) {
3241 final Map <String , Object > view =
3342 (Map <String , Object >)map .get ("view" );
3443 final Map <String , Object > state =
@@ -41,59 +50,77 @@ public static PulseResponseCreateDTO get(
4150 response .setTeamMemberId (lookupUser (memberProfileServices , map ));
4251 response .setSubmissionDate (LocalDate .now ());
4352
44- response .setInternalScore (Integer .parseInt (
45- getMappedValue (values , "internalScore" , true )));
46- response .setInternalFeelings (
47- getMappedValue (values , "internalFeelings" , false ));
53+ // Internal Score
54+ Map <String , Object > internalBlock =
55+ (Map <String , Object >)values .get ("internalNumber" );
56+ response .setInternalScore (Integer .parseInt (getMappedValue (
57+ internalBlock , "internalScore" , "selected_option" , true )));
58+ // Internal Feelings
59+ response .setInternalFeelings (getMappedValue (
60+ values , "internalText" , "internalFeelings" , false ));
4861
49- String score = getMappedValue (values , "externalScore" , false );
50- if (!score .isEmpty ()) {
62+ // External Score
63+ Map <String , Object > externalBlock =
64+ (Map <String , Object >)values .get ("externalNumber" );
65+ String score = getMappedValue (externalBlock , "externalScore" ,
66+ "selected_option" , false );
67+ if (score != null && !score .isEmpty ()) {
5168 response .setExternalScore (Integer .parseInt (score ));
5269 }
53- response .setExternalFeelings (
54- getMappedValue (values , "externalFeelings" , false ));
70+ // External Feelings
71+ response .setExternalFeelings (getMappedValue (
72+ values , "externalText" , "externalFeelings" , false ));
5573
5674 return response ;
57- } catch (JsonProcessingException ex ) {
58- LOG .error (ex .getMessage ());
59- throw new BadArgException (ex .getMessage ());
60- } catch (NumberFormatException ex ) {
61- LOG .error (ex .getMessage ());
62- throw new BadArgException ("Pulse scores must be integers" );
75+ } else {
76+ // If it's not a view submission, we need to return null so
77+ // the the caller knows that this is not the full pulse
78+ // response.
79+ return null ;
6380 }
64- } else {
65- LOG .error (body );
66- throw new BadArgException ("Invalid pulse response body" );
81+ } catch (JsonProcessingException ex ) {
82+ LOG .error (ex .getMessage ());
83+ throw new BadArgException (ex .getMessage ());
84+ } catch (NumberFormatException ex ) {
85+ LOG .error (ex .getMessage ());
86+ throw new BadArgException ("Pulse scores must be integers" );
6787 }
6888 }
6989
70- private static String getMappedValue (Map <String , Object > map ,
71- String key , boolean required ) {
90+ private String getMappedValue (Map <String , Object > map , String key1 ,
91+ String key2 , boolean required ) {
7292 final String valueKey = "value" ;
73- if (map .containsKey (key )) {
74- final Map <String , Object > other = (Map <String , Object >)map .get (key );
75- if (other .containsKey (valueKey )) {
76- return (String )other .get (valueKey );
93+ if (map != null && map .containsKey (key1 )) {
94+ Map <String , Object > firstMap = (Map <String , Object >)map .get (key1 );
95+ if (firstMap != null && firstMap .containsKey (key2 )) {
96+ final Map <String , Object > secondMap =
97+ (Map <String , Object >)firstMap .get (key2 );
98+ if (secondMap != null && secondMap .containsKey (valueKey )) {
99+ return (String )secondMap .get (valueKey );
100+ }
77101 }
78102 }
79103
80104 if (required ) {
81- LOG .error ("Expected {}.{} was not found" , key , valueKey );
105+ LOG .error ("Expected {}.{}.{} was not found" , key1 , key2 , valueKey );
82106 throw new BadArgException (
83- String .format ("Expected %s.%s was not found" , key , valueKey ));
107+ String .format ("Expected %s.%s.%s was not found" ,
108+ key1 , key2 , valueKey ));
84109 } else {
85- return "" ;
110+ return null ;
86111 }
87112 }
88113
89- private static UUID lookupUser (MemberProfileServices memberProfileServices ,
90- Map <String , Object > map ) {
114+ private UUID lookupUser (MemberProfileServices memberProfileServices ,
115+ Map <String , Object > map ) {
91116 // Get the user's profile map.
92117 Map <String , Object > user = (Map <String , Object >)map .get ("user" );
93- Map <String , Object > profile = (Map <String , Object >)user .get ("profile" );
94118
95119 // Lookup the user based on the email address.
96- String email = (String )profile .get ("email" );
120+ String email = slackSearch .findUserEmail ((String )user .get ("id" ));
121+ if (email == null ) {
122+ throw new BadArgException ("Unable to find the user email address" );
123+ }
97124 MemberProfile member = memberProfileServices .findByWorkEmail (email );
98125 return member .getId ();
99126 }
0 commit comments