From e496e6f809e8e92e6a542f4dfcbbc77ea44e1c0b Mon Sep 17 00:00:00 2001 From: nathan-moore-97 Date: Wed, 31 Jan 2024 10:56:15 -0500 Subject: [PATCH] Redirect --- qualtrix/api.py | 17 ++++++++++++----- qualtrix/client.py | 22 ++++++++++++++-------- qualtrix/settings.py | 2 ++ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/qualtrix/api.py b/qualtrix/api.py index c3efd72..35fe21f 100644 --- a/qualtrix/api.py +++ b/qualtrix/api.py @@ -36,7 +36,6 @@ class RedirectModel(SurveyModel): email: str first_name: str last_name: str - auth_token: str @router.post("/bulk-responses") @@ -56,7 +55,6 @@ async def get_response(request: ResponseModel): async def intake_redirect(request: RedirectModel): start_time = time.time() try: - # participant = client.get_participant(request.surveyId, request.responseId) directory_entry = client.create_directory_entry( request.email, request.first_name, @@ -64,10 +62,9 @@ async def intake_redirect(request: RedirectModel): settings.DIRECTORY_ID, settings.MAILING_LIST_ID, ) - # TODO: Abstract this into a general create_distribution with a type argument + email_distribution = client.create_email_distribution( directory_entry["contactLookupId"], - settings.DIRECTORY_ID, settings.LIBRARY_ID, settings.INVITE_MESSAGE_ID, settings.MAILING_LIST_ID, @@ -77,6 +74,7 @@ async def intake_redirect(request: RedirectModel): # If link creation succeeds, create reminders while the link is returned create_task(create_reminder_distributions(email_distribution["id"])) + create_task(add_user_to_contact_list(link["link"], directory_entry["id"])) log.info("Redirect link created in %.2f seconds" % (time.time() - start_time)) return link @@ -87,13 +85,22 @@ async def intake_redirect(request: RedirectModel): async def create_reminder_distributions(distribution_id: str): - client.create_reminder_distribution( + distribution = client.create_reminder_distribution( settings.LIBRARY_ID, settings.REMINDER_MESSAGE_ID, distribution_id, (datetime.utcnow() + timedelta(minutes=1)), ) + logging.info(f"created reminder {distribution['distributionId']}") + + +async def add_user_to_contact_list(survey_link: str, contact_id: str): + contact = client.add_participant_to_contact_list( + settings.DEMOGRAPHICS_SURVEY_LABEL, survey_link, contact_id + ) + logging.info(f"add contact {contact} to contact list") + @router.post("/survey-schema") async def get_schema(request: SurveyModel): diff --git a/qualtrix/client.py b/qualtrix/client.py index 1d83ae1..026c8d9 100644 --- a/qualtrix/client.py +++ b/qualtrix/client.py @@ -193,30 +193,36 @@ def create_reminder_distribution( if reminder_distribution is None: raise error.QualtricsError("Something went wrong creating the distribution") + return reminder_distribution + def add_participant_to_contact_list( - auth_token: str, survey_label: str, survey_link: str + survey_label: str, survey_link: str, contact_id: str ): header = copy.deepcopy(auth_header) header["Accept"] = "application/json" logging.info("Add participant to the contact list") - add_particpant_payload = { - "embeddedData": {survey_label: survey_link, "auth_token": auth_token} - } + add_particpant_payload = {"embeddedData": {survey_label: survey_link}} - r = requests.post( - settings.BASE_URL + f"/distributions/{distribution_id}/reminders", + r = requests.put( + settings.BASE_URL + + f"/directories/{settings.DIRECTORY_ID}/mailinglists/{settings.MAILING_LIST_ID}/contacts/{contact_id}", headers=header, - json=create_reminder_distribution_payload, + json=add_particpant_payload, timeout=settings.TIMEOUT, ) + add_to_contact_list_response = r.json() + if "error" in add_to_contact_list_response["meta"]: + raise error.QualtricsError(add_to_contact_list_response["meta"]["error"]) + + return contact_id + def create_email_distribution( contact_id: str, - distribution_id: str, library_id: str, message_id: str, mailing_list_id: str, diff --git a/qualtrix/settings.py b/qualtrix/settings.py index 7cd4563..a1af691 100644 --- a/qualtrix/settings.py +++ b/qualtrix/settings.py @@ -59,6 +59,7 @@ INVITE_SUBJECT = config["invite_subject"] REMINDER_SUBJECT = config["reminder_subject"] SURVEY_LINK_TYPE = config["survey_link_type"] + DEMOGRAPHICS_SURVEY_LABEL = config["demographics_survey_label"] else: API_TOKEN = os.getenv("QUALTRIX_API_TOKEN") @@ -74,6 +75,7 @@ INVITE_SUBJECT = os.getenv("QUALTRIX_INVITE_SUBJECT") REMINDER_SUBJECT = os.getenv("QUALTRIX_REMINDER_SUBJECT") SURVEY_LINK_TYPE = os.getenv("QUALTRIX_SURVEY_LINK_TYPE") + DEMOGRAPHICS_SURVEY_LABEL = os.getenv("QUALTRIX_DEMOGRAPHICS_SURVEY_LABEL") except (json.JSONDecodeError, KeyError, FileNotFoundError) as err: log.warning("Unable to load credentials from VCAP_SERVICES")