Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (redirect): Create contact to reduce latency #91

Merged
merged 11 commits into from
Feb 2, 2024
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
repos:
- repo: https://github.com/psf/black
rev: 22.3.0 # Update with 'pre-commit autoupdate'
rev: 24.1.1 # Update with 'pre-commit autoupdate'
hooks:
- id: black

- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
rev: 1.7.7
hooks:
- id: bandit
exclude: tests
107 changes: 100 additions & 7 deletions qualtrix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
qualtrix rest api
"""

from asyncio import create_task
from datetime import datetime, timedelta
import logging
import time
from zoneinfo import ZoneInfo

import fastapi
from fastapi import HTTPException
Expand All @@ -29,7 +33,14 @@ class SessionModel(SurveyModel):

class RedirectModel(SurveyModel):
targetSurveyId: str
responseId: str
nathan-moore-97 marked this conversation as resolved.
Show resolved Hide resolved
RulesConsentID: str # Client dependent
SurveyswapID: str # Client dependent
utm_campaign: str
utm_medium: str
utm_source: str
email: str
firstName: str
lastName: str


@router.post("/bulk-responses")
Expand All @@ -46,15 +57,97 @@ async def get_response(request: ResponseModel):


@router.post("/redirect")
async def get_redirect(request: RedirectModel):
async def intake_redirect(request: RedirectModel):
nathan-moore-97 marked this conversation as resolved.
Show resolved Hide resolved
start_time = time.time()
try:
email = client.get_email(request.surveyId, request.responseId)
contact = client.get_contact(settings.DIRECTORY_ID, email)
distribution = client.get_distribution(settings.DIRECTORY_ID, contact["id"])
return client.get_link(request.targetSurveyId, distribution["distributionId"])
directory_entry = client.create_directory_entry(
request.email,
request.firstName,
request.lastName,
settings.DIRECTORY_ID,
settings.MAILING_LIST_ID,
)

email_distribution = client.create_email_distribution(
directory_entry["contactLookupId"],
settings.LIBRARY_ID,
settings.INVITE_MESSAGE_ID,
settings.MAILING_LIST_ID,
request.targetSurveyId,
)

link = client.get_link(request.targetSurveyId, email_distribution["id"])

# 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"],
request.RulesConsentID,
request.SurveyswapID,
request.utm_campaign,
request.utm_medium,
request.utm_source,
request.firstName,
request.lastName,
# https://stackoverflow.com/questions/10997577/python-timezone-conversion
# Consumers to this data require mountain time
datetime.now(tz=ZoneInfo("MST")),
)
)

log.info("Redirect link created in %.2f seconds" % (time.time() - start_time))
return link
except error.QualtricsError as e:
logging.error(e)
raise HTTPException(status_code=400, detail=e.args)
# the next time any client side changes are required update this to 422
raise HTTPException(status_code=422, detail=e.args)


async def create_reminder_distributions(distribution_id: str):
distribution = client.create_reminder_distribution(
settings.LIBRARY_ID,
settings.REMINDER_MESSAGE_ID,
distribution_id,
(datetime.utcnow() + timedelta(days=1)),
)

distribution = client.create_reminder_distribution(
settings.LIBRARY_ID,
settings.REMINDER_MESSAGE_ID,
distribution_id,
(datetime.utcnow() + timedelta(days=3)),
)


async def add_user_to_contact_list(
survey_link: str,
contact_id: str,
rules_consent_id: str,
survey_swap_id: str,
utm_campaign: str,
utm_medium: str,
utm_source: str,
first_name: str,
last_name: str,
timestamp: datetime,
):
return client.add_participant_to_contact_list(
settings.DEMOGRAPHICS_SURVEY_LABEL,
survey_link,
settings.RULES_CONSENT_ID_LABEL,
client.modify_prefix("FS", "R", rules_consent_id),
settings.SURVEY_SWAP_ID_LABEL,
survey_swap_id,
contact_id,
utm_campaign,
utm_medium,
utm_source,
first_name,
last_name,
timestamp,
)


@router.post("/survey-schema")
Expand Down
Loading