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

API Support for Prompt to Update Surveys #518

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion microsetta_private_api/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ._source import (
create_source, read_source, update_source, read_sources,
create_human_source_from_consent, check_duplicate_source_name,
scrub_source, check_source_ffq_prereqs
scrub_source, check_source_ffq_prereqs, check_prompt_survey_update
)
from ._survey import (
read_survey_template, read_survey_templates, read_answered_survey,
Expand Down Expand Up @@ -83,6 +83,7 @@
'create_source',
'read_source',
'check_source_ffq_prereqs',
'check_prompt_survey_update',
'update_source',
'scrub_source',
'read_sources',
Expand Down
9 changes: 9 additions & 0 deletions microsetta_private_api/api/_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,12 @@ def check_duplicate_source_name(account_id, body, token_info):
source = source_repo.get_duplicate_source_name(
account_id, source_name)
return jsonify(source), 200


def check_prompt_survey_update(account_id, source_id, token_info):
_validate_account_access(token_info, account_id)

with Transaction() as t:
s_t_r = SurveyTemplateRepo(t)
prompt_update = s_t_r.check_prompt_survey_update(source_id)
return jsonify({"prompt": prompt_update}), 200
26 changes: 26 additions & 0 deletions microsetta_private_api/api/microsetta_private_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,32 @@ paths:
'404':
$ref: '#/components/responses/404NotFound'

'/accounts/{account_id}/sources/{source_id}/check_prompt_survey_update':
get:
operationId: microsetta_private_api.api.check_prompt_survey_update
tags:
- Source
summary: Check if the user should be prompted to update their survey responses
description: Check if the user should be prompted to update their survey responses
parameters:
- $ref: '#/components/parameters/account_id'
- $ref: '#/components/parameters/source_id'
- $ref: '#/components/parameters/language_tag'
responses:
'200':
description: True/False as to whether the user should be prompted
content:
application/json:
schema:
type: "object"
properties:
prompt:
type: boolean
'401':
$ref: '#/components/responses/401Unauthorized'
'403':
$ref: '#/components/responses/403Forbidden'

'/accounts/{account_id}/sources/{source_id}/survey_templates':
get:
operationId: microsetta_private_api.api.read_survey_templates
Expand Down
36 changes: 36 additions & 0 deletions microsetta_private_api/repo/survey_template_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,3 +1351,39 @@ def get_template_ids_from_survey_ids(self, survey_ids):
(tuple(survey_ids),))

return [(x[0], x[1]) for x in cur.fetchall()]

def check_prompt_survey_update(
self, source_id, days_threshold=7
):
""" Checks how long it has been since a source last updated any survey
as a means of determining whether to prompt them to update their
profile after logging a sample. We add the survey_template_id <
10000 clause to ignore Vioscreen/remote surveys

Parameters
----------
source_id : str
The source we're checking
days_threshold : int
The number of maximum number of days to allow since last survey
update. Defaults to 7.
Returns
-------
bool
If the user should be prompted, returns True
If the user should not be prompted, return False
"""
with self._transaction.cursor() as cur:
cur.execute(
"SELECT COUNT(survey_id) "
"FROM ag.ag_login_surveys "
"WHERE source_id = %s "
"AND creation_time >= current_date - %s "
"AND survey_template_id < 10000",
(source_id, days_threshold)
)
row = cur.fetchone()
if row[0] > 0:
return False

return True
50 changes: 50 additions & 0 deletions microsetta_private_api/repo/tests/test_survey_template_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,56 @@ def test_get_template_ids_from_survey_ids(self):
exp = [('6d16832b84358c93', 1), ('001e19ab6ea2f7de', 1)]
self.assertEqual(obs, exp)

def test_check_prompt_survey_update(self):
with Transaction() as t:
s_t_r = SurveyTemplateRepo(t)

with t.cursor() as cur:
# 6d16832b84358c93 is a stable survey_id used for testing so
# we'll use that for our purposes. We need to grab the
# source_id for our purposes.
cur.execute(
"SELECT source_id "
"FROM ag_login_surveys "
"WHERE survey_id = '6d16832b84358c93'"
)
source_id = cur.fetchone()[0]

# And we're going to delete any other surveys associated with
# that source_id to make testing easier.
cur.execute(
"DELETE FROM ag.ag_login_surveys "
"WHERE source_id = %s "
"AND survey_id != '6d16832b84358c93'",
(source_id, )
)

# First, we're going to pretend they took the survey at
# midnight this morning, which falls within the past week.
cur.execute(
"UPDATE ag.ag_login_surveys "
"SET creation_time = CURRENT_DATE "
"WHERE survey_id = '6d16832b84358c93'"
)

# We should observe a response of False, as they do not need
# to be prompted to update their surveys.
obs = s_t_r.check_prompt_survey_update(source_id)
self.assertFalse(obs)

# Now, we'll pretend they took the survey on January 1, 2023
# which falls well outside of the past week.
cur.execute(
"UPDATE ag.ag_login_surveys "
"SET creation_time = '2023-01-01 09:00:00' "
"WHERE survey_id = '6d16832b84358c93'"
)

# We should observe a response of True, as they need to be
# prompted to update their surveys.
obs = s_t_r.check_prompt_survey_update(source_id)
self.assertTrue(obs)

filled_surveys = {
"10": {
"22": "I am right handed",
Expand Down