Skip to content

Commit

Permalink
Adding more embedded data and properly formatting the time
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-moore-97 committed Feb 2, 2024
1 parent 697b9c0 commit 58e8d7f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
44 changes: 36 additions & 8 deletions qualtrix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime, timedelta
import logging
import time
from zoneinfo import ZoneInfo

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

class RedirectModel(SurveyModel):
targetSurveyId: str
RulesConsentID: str # Client dependent
SurveyswapID: str # Client dependent
utm_campaign: str
utm_medium: str
utm_source: str
email: str
firstName: str
lastName: str
rulesConsentId: str


@router.post("/bulk-responses")
Expand Down Expand Up @@ -70,6 +75,7 @@ async def intake_redirect(request: RedirectModel):
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
Expand All @@ -78,10 +84,16 @@ async def intake_redirect(request: RedirectModel):
add_user_to_contact_list(
link["link"],
directory_entry["id"],
request.rulesConsentId,
request.RulesConsentID,
request.SurveyswapID,
request.utm_campaign,
request.utm_medium,
request.utm_source,
request.firstName,
request.lastName,
datetime.utcnow(),
# https://stackoverflow.com/questions/10997577/python-timezone-conversion
# Consumers to this data require mountain time
datetime.now(tz=ZoneInfo("MST")),
)
)

Expand All @@ -98,27 +110,43 @@ async def create_reminder_distributions(distribution_id: str):
settings.LIBRARY_ID,
settings.REMINDER_MESSAGE_ID,
distribution_id,
(datetime.utcnow() + timedelta(days=1)),
(datetime.utcnow() + timedelta(minutes=1)),
)

distribution = client.create_reminder_distribution(
settings.LIBRARY_ID,
settings.REMINDER_MESSAGE_ID,
distribution_id,
(datetime.utcnow() + timedelta(minutes=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_utc: datetime,
timestamp: datetime,
):
return client.add_participant_to_contact_list(
settings.DEMOGRAPHICS_SURVEY_LABEL,
settings.RULES_CONSENT_ID_LABEL,
survey_link,
contact_id,
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_utc,
timestamp,
)


Expand Down
28 changes: 15 additions & 13 deletions qualtrix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import requests
import time
import pytz
import datetime
from datetime import datetime, timedelta

Expand Down Expand Up @@ -118,15 +119,6 @@ def create_directory_entry(
"firstName": first_name,
"lastName": last_name,
"email": email,
"embeddedData": {
"RulesConsentID": "test-rules",
"Date": "test-date",
"time": "test-time",
"SurveyswapID": "",
"utm_source": "test-utmsource",
"utm_medium": "test-utmmedium",
"utm_campaign": "test-utmcampaign",
},
}

# Create contact
Expand Down Expand Up @@ -201,13 +193,18 @@ def modify_prefix(current: str, desired: str, content: str) -> str:

def add_participant_to_contact_list(
survey_label: str,
rules_consent_id_label,
survey_link: str,
contact_id: str,
rules_consent_id_label,
rules_consent_id: str,
survey_swap_id_label: str,
survey_swap_id,
contact_id: str,
utm_campaign: str,
utm_medium: str,
utm_source: str,
first_name: str,
last_name: str,
timestamp_utc: datetime,
timestamp: datetime,
):
header = copy.deepcopy(auth_header)
header["Accept"] = "application/json"
Expand All @@ -216,9 +213,14 @@ def add_participant_to_contact_list(
"embeddedData": {
survey_label: survey_link,
rules_consent_id_label: rules_consent_id,
survey_swap_id_label: survey_swap_id,
"utm_campaign": utm_campaign,
"utm_medium": utm_medium,
"utm_source": utm_source,
"firstName": first_name,
"lastName": last_name,
"timestamp": timestamp_utc.isoformat() + "Z",
"Date": timestamp.strftime("%m/%d/%Y"),
"time": timestamp.strftime("%H:%M:%S"),
}
}

Expand Down
15 changes: 0 additions & 15 deletions qualtrix/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@

app = fastapi.FastAPI()

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

# app = FastAPI()

origins = ["*"]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.add_middleware(starlette_prometheus.PrometheusMiddleware)
app.add_route("/metrics/", starlette_prometheus.metrics)

Expand Down
3 changes: 3 additions & 0 deletions qualtrix/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
SURVEY_LINK_TYPE = None
DEMOGRAPHICS_SURVEY_LABEL = None
RULES_CONSENT_ID_LABEL = None
SURVEY_SWAP_ID_LABEL = None

try:
vcap_services = os.getenv("VCAP_SERVICES")
Expand All @@ -62,6 +63,7 @@
SURVEY_LINK_TYPE = config["survey_link_type"]
DEMOGRAPHICS_SURVEY_LABEL = config["demographics_survey_label"]
RULES_CONSENT_ID_LABEL = config["rules_consent_id_label"]
SURVEY_SWAP_ID_LABEL = config["survey_swap_id_label"]

else:
API_TOKEN = os.getenv("QUALTRIX_API_TOKEN")
Expand All @@ -79,6 +81,7 @@
SURVEY_LINK_TYPE = os.getenv("QUALTRIX_SURVEY_LINK_TYPE")
DEMOGRAPHICS_SURVEY_LABEL = os.getenv("QUALTRIX_DEMOGRAPHICS_SURVEY_LABEL")
RULES_CONSENT_ID_LABEL = os.getenv("QUALTRIX_RULES_CONSENT_ID_LABEL")
SURVEY_SWAP_ID_LABEL = os.getenv("QUALTRIX_SURVEY_SWAP_ID_LABEL")

except (json.JSONDecodeError, KeyError, FileNotFoundError) as err:
log.warning("Unable to load credentials from VCAP_SERVICES")
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ starlette-prometheus==0.9.0
google-api-python-client==2.113.0
google-auth-httplib2==0.2.0
google-auth-oauthlib==1.2.0
pytz==2024.1

0 comments on commit 58e8d7f

Please sign in to comment.