diff --git a/caddy_chatbot/src/caddy_core/services/survey.py b/caddy_chatbot/src/caddy_core/services/survey.py index 9c8e44a..bd62695 100644 --- a/caddy_chatbot/src/caddy_core/services/survey.py +++ b/caddy_chatbot/src/caddy_core/services/survey.py @@ -28,7 +28,7 @@ def get_survey(user: str) -> Tuple[List[str], List[str]]: user (str): The email of the user Returns: - Tuple[List[str], List[str]]: The questions for the survey and the values for the survey + List[dict[str, List[str]]]: A list of questions and values for the survey """ user_workspace_variables = get_user_workspace_variables(user) @@ -37,9 +37,8 @@ def get_survey(user: str) -> Tuple[List[str], List[str]]: ] post_call_survey_questions = post_call_module["questions"] - post_call_survey_values = post_call_module["values"] - return post_call_survey_questions, post_call_survey_values + return post_call_survey_questions def get_user_workspace_variables(user_email: str): diff --git a/caddy_chatbot/src/integrations/google_chat/structures.py b/caddy_chatbot/src/integrations/google_chat/structures.py index c47187a..5fe6ef9 100644 --- a/caddy_chatbot/src/integrations/google_chat/structures.py +++ b/caddy_chatbot/src/integrations/google_chat/structures.py @@ -249,30 +249,28 @@ def handle_survey_response(self, event): survey_response = [{question: response}] - evaluation_entry = evaluation_table.get_item(Key={"threadId": str(threadId)}) - - if "Item" in evaluation_entry and "surveyResponse" in evaluation_entry["Item"]: - evaluation_table.update_item( - Key={"threadId": str(threadId)}, - UpdateExpression="set surveyResponse = list_append(surveyResponse, :surveyResponse)", - ExpressionAttributeValues={":surveyResponse": survey_response}, - ReturnValues="UPDATED_NEW", - ) - else: - evaluation_table.update_item( - Key={"threadId": str(threadId)}, - UpdateExpression="set surveyResponse = :surveyResponse", - ExpressionAttributeValues={":surveyResponse": survey_response}, - ReturnValues="UPDATED_NEW", - ) + evaluation_table.update_item( + Key={"threadId": str(threadId)}, + UpdateExpression="set surveyResponse = list_append(if_not_exists(surveyResponse, :empty_list), :surveyResponse)", + ExpressionAttributeValues={ + ":surveyResponse": survey_response, + ":empty_list": [], + }, + ReturnValues="UPDATED_NEW", + ) + + remaining_sections = len(card[0]["card"]["sections"]) - card[0]["card"]["sections"] = [ - section - for section in card[0]["card"]["sections"] - if section["widgets"][0]["textParagraph"]["text"] != question - ] + for i in range(remaining_sections): + if ( + card[0]["card"]["sections"][i]["widgets"][1]["textParagraph"]["text"] + == f"{question}" + ): + del card[0]["card"]["sections"][i] + remaining_sections -= 1 + break - if len(card[0]["card"]["sections"]) == 0: + if remaining_sections == 0: card[0]["card"]["sections"].append(self.messages.SURVEY_COMPLETE_WIDGET) self.update_survey_card_in_adviser_space( @@ -443,11 +441,9 @@ def run_new_survey(self, user: str, thread_id: str, user_space: str) -> None: Returns: None """ - post_call_survey_questions, post_call_survey_values = get_survey(user) + post_call_survey_questions = get_survey(user) - survey_card = self.get_post_call_survey_card( - post_call_survey_questions, post_call_survey_values - ) + survey_card = self.get_post_call_survey_card(post_call_survey_questions) self.send_dynamic_to_adviser_space( response_type="cardsV2", @@ -457,14 +453,14 @@ def run_new_survey(self, user: str, thread_id: str, user_space: str) -> None: ) def get_post_call_survey_card( - self, post_call_survey_questions: List[str], post_call_survey_values: List[str] + self, + post_call_survey_questions: List[dict[str, List[str]]], ) -> dict: """ Create a post call survey card with the given questions and values Args: - post_call_survey_questions (List[str]): The questions for the survey - post_call_survey_values (List[str]): The values for the survey + post_call_survey_questions (List[dict[str, List[str]]]): The questions and values for the survey Returns: dict: The survey card @@ -480,14 +476,24 @@ def get_post_call_survey_card( ], } - for question in post_call_survey_questions: + i = 0 + for question_dict in post_call_survey_questions: + i += 1 + question = question_dict["question"] + values = question_dict["values"] section = {"widgets": []} - question_section = {"textParagraph": {"text": question}} + label_section = { + "decoratedText": { + "topLabel": f"Question {i}", + } + } + + question_section = {"textParagraph": {"text": f"{question}"}} button_section = {"buttonList": {"buttons": []}} - for value in post_call_survey_values: + for value in values: button_section["buttonList"]["buttons"].append( { "text": value, @@ -503,6 +509,7 @@ def get_post_call_survey_card( } ) + section["widgets"].append(label_section) section["widgets"].append(question_section) section["widgets"].append(button_section) @@ -1051,11 +1058,9 @@ def get_survey(self, user: str) -> dict: Returns: dict: The survey card """ - post_call_survey_questions, post_call_survey_values = get_survey(user) + post_call_survey_questions = get_survey(user) - survey_card = self.get_post_call_survey_card( - post_call_survey_questions, post_call_survey_values - ) + survey_card = self.get_post_call_survey_card(post_call_survey_questions) return survey_card