Skip to content

Commit

Permalink
Redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-moore-97 committed Jan 31, 2024
1 parent 2e0c18f commit e496e6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
17 changes: 12 additions & 5 deletions qualtrix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class RedirectModel(SurveyModel):
email: str
first_name: str
last_name: str
auth_token: str


@router.post("/bulk-responses")
Expand All @@ -56,18 +55,16 @@ 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,
request.last_name,
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,
Expand All @@ -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
Expand All @@ -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):
Expand Down
22 changes: 14 additions & 8 deletions qualtrix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions qualtrix/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit e496e6f

Please sign in to comment.