Skip to content

Commit

Permalink
add expired handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobasco99 committed Jun 27, 2024
1 parent 46079c2 commit 844bb1d
Showing 1 changed file with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,16 @@ private static void deleteParticipant(Survey currSurvey, String email) {
}
}

@NotNull
private static JSONObject getDoneButton() {
JSONObject button = new JSONObject();
button.put("intent", "Fertig");
button.put("label", "Fertig");
button.put("description", "Fertig");
button.put("isFile", false);
return button;
}

@POST
@Path("/nextQuestion")
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -1341,24 +1351,14 @@ public Response nextQuestion(@FormDataParam("msg") String msg, @FormDataParam("c
bodyInput.put("adminmail", adminmail);

String input = bodyInput.toString();
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);

try{
LocalDate dateNow = LocalDate.now();
LocalTime timeNow = LocalTime.now();

System.out.println("received message: " + bodyInput);
//System.out.println("received message: " + bodyInput);

String beginningTextEN = "";
String beginningTextDE = "";
if(bodyInput.containsKey("beginningText")){
System.out.println("has beginningText");
beginningTextEN = bodyInput.getAsString("beginningText");
beginningTextDE = bodyInput.getAsString("beginningText");
} else if(bodyInput.containsKey("beginningTextDE") && bodyInput.containsKey("beginningTextEN")){
beginningTextEN = bodyInput.getAsString("beginningTextEN");
beginningTextDE = bodyInput.getAsString("beginningTextDE");
}
String senderEmail = "";

messenger = Messenger.RESTFUL;
Expand Down Expand Up @@ -1417,21 +1417,18 @@ public Response nextQuestion(@FormDataParam("msg") String msg, @FormDataParam("c
}

if (isExpired(response, dateNow, timeNow, ls, currSurvey)) {
JSONObject doneButton = getDoneButton();
response.put("channel", channel);
response.put("message", "Die Umfrage ist beendet.");
response.put("closeContext", true);
response.put("interactiveElements", doneButton);
return Response.ok().entity(response).build();
}

String messageId = bodyInput.getAsString("message_id");
JSONObject currMessage = new JSONObject();
JSONObject prevMessage = new JSONObject();

if(bodyInput.containsKey("currMessage") && bodyInput.containsKey("previousMessage")){
currMessage = (JSONObject) p.parse(bodyInput.getAsString("currMessage"));
prevMessage = (JSONObject) p.parse(bodyInput.getAsString("previousMessage"));
}

// Check if message was sent by someone we only knew the channel of, but now also the email
if(Objects.nonNull(currSurvey.findParticipant(channel))){
// after setting the channel last time now we can set email, since the email gets send the second time a participants sents something
Expand Down Expand Up @@ -1484,13 +1481,8 @@ public Response nextQuestion(@FormDataParam("msg") String msg, @FormDataParam("c
if(currParticipant.isCompletedsurvey()){

System.out.println("Participant has completed survey");
// no unfinished survey left
JSONArray interactiveElements = new JSONArray();
JSONObject button = new JSONObject();
button.put("intent", "Fertig");
button.put("label", "Fertig");
button.put("description", "Fertig");
button.put("isFile", false);
JSONObject button = getDoneButton();
interactiveElements.add(button);
String completedSurvey = SurveyHandlerService.texts.get("completedSurveyDE");
response.put("message", completedSurvey);
Expand All @@ -1515,7 +1507,7 @@ public Response nextQuestion(@FormDataParam("msg") String msg, @FormDataParam("c
// Get the next action
return currParticipant.calculateNextAction(intent, message, messageId, buttonIntent, messageTs, currMessage, prevMessage, token, false, beginningTextEN, beginningTextDE, channel);

} catch (ParseException e) {
} catch (Exception e) {
e.printStackTrace();
}
response.put("message", "Something went wrong in Next Question try block.");
Expand Down Expand Up @@ -2120,7 +2112,7 @@ public Response sendResultsToLimesurvey(@FormDataParam("channel") String channel
ClientResponse minires2 = mini.sendRequest("POST", uri, responseData, MediaType.APPLICATION_JSON, "", head);
JSONObject minire2 = (JSONObject) p.parse(minires2.getResponse());
String response2 = minire2.getAsString("result");
System.out.println("Response updated: " + response2);
//System.out.println("Response updated: " + response2);
} else{
// New response, add new response and save id at participant
String contentFilled = "{" + content + "}";
Expand All @@ -2132,7 +2124,7 @@ public Response sendResultsToLimesurvey(@FormDataParam("channel") String channel
Integer.parseInt(surveyResponseID);
pa.setSurveyResponseID(surveyResponseID);
SurveyHandlerServiceQueries.updateParticipantInDB(pa, currSurvey.database);
System.out.println("New response added: " + pa.getSurveyResponseID());
//System.out.println("New response added: " + pa.getSurveyResponseID());
} catch (Exception e){
System.out.println("ERROR in sending results to LimeSurvey");
response.put("message", surveyResponseID);
Expand Down

0 comments on commit 844bb1d

Please sign in to comment.