Skip to content

Commit

Permalink
Merge pull request #293 from biocore/st_dh_hotfix_vioscreen
Browse files Browse the repository at this point in the history
Vioscreen Hotfix - Registry Table
  • Loading branch information
wasade authored Feb 1, 2021
2 parents e4b7b3a + 93e67ef commit 0f6c862
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
16 changes: 13 additions & 3 deletions microsetta_private_api/api/_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def read_survey_templates(account_id, source_id, language_tag, token_info):


def read_survey_template(account_id, source_id, survey_template_id,
language_tag, token_info, survey_redirect_url=None):
language_tag, token_info, survey_redirect_url=None,
vioscreen_ext_sample_id=None):
_validate_account_access(token_info, account_id)

# TODO: can we get rid of source_id? I don't have anything useful to do
Expand All @@ -58,14 +59,23 @@ def read_survey_template(account_id, source_id, survey_template_id,

# For external surveys, we generate links pointing out
if survey_template_id == SurveyTemplateRepo.VIOSCREEN_ID:

if vioscreen_ext_sample_id:
# User is about to start a vioscreen survey for this sample
# record this in the database.
db_vioscreen_id = survey_template_repo.create_vioscreen_id(
account_id, source_id, vioscreen_ext_sample_id
)
else:
raise ValueError("Vioscreen Template requires "
"vioscreen_ext_sample_id parameter.")
url = vioscreen.gen_survey_url(
language_tag, survey_redirect_url
db_vioscreen_id, language_tag, survey_redirect_url
)
# TODO FIXME HACK: This field's contents are not specified!
info.survey_template_text = {
"url": url
}
t.commit()
return jsonify(info), 200

# For local surveys, we generate the json representing the survey
Expand Down
9 changes: 9 additions & 0 deletions microsetta_private_api/api/microsetta_private_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ paths:
- $ref: '#/components/parameters/survey_template_id'
- $ref: '#/components/parameters/language_tag'
- $ref: '#/components/parameters/survey_redirect_url'
- $ref: '#/components/parameters/vioscreen_ext_sample_id'
responses:
'200':
description: Successfully returned a specific survey template
Expand Down Expand Up @@ -1540,6 +1541,14 @@ components:
schema:
type: string
example: "https://www.microsetta.org/accounts/72d2cc55-8522-4528-a85b-78be2ec0933f/sources/bfed3a1b-0855-4dce-9398-7c54f5b4ac8f"
vioscreen_ext_sample_id:
name: vioscreen_ext_sample_id
in: query
description: Identifies sample for vioscreen templates, indicates the user is being redirected to the response url and so vioscreen id should be logged
schema:
$ref: '#/components/schemas/sample_id'
required: False

source_type:
name: source_type
in: query
Expand Down
24 changes: 23 additions & 1 deletion microsetta_private_api/db/migration_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,32 @@ def migrate_70(TRN):
if len(offending_ids) != 0:
raise Exception("Couldn't resolve vioscreen/primary split :(")

@staticmethod
def migrate_74(TRN):
TRN.add("SELECT DISTINCT "
"ag_login_id, "
"ag_login_surveys.source_id, "
"ag_kit_barcode_id, "
"ag_login_surveys.survey_id "
"FROM "
"ag_login_surveys LEFT JOIN "
"source_barcodes_surveys USING (survey_id) LEFT JOIN "
"ag_kit_barcodes USING (barcode) "
"WHERE vioscreen_status is not null "
)
rows = TRN.execute()[-1]
for r in rows:
TRN.add("INSERT INTO vioscreen_registry("
"account_id, source_id, sample_id, vio_id) "
"VALUES(%s, %s, %s, %s)",
(r[0], r[1], r[2], r[3]))
TRN.execute()

MIGRATION_LOOKUP = {
"0048.sql": migrate_48.__func__,
"0050.sql": migrate_50.__func__,
"0070.sql": migrate_70.__func__
"0070.sql": migrate_70.__func__,
"0074.sql": migrate_74.__func__,
# ...
}

Expand Down
14 changes: 14 additions & 0 deletions microsetta_private_api/db/patches/0074.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- create table to track vioscreen survey ids
CREATE TABLE ag.vioscreen_registry
(
account_id uuid NOT NULL,
source_id uuid NOT NULL,
sample_id uuid,
vio_id varchar NOT NULL,
CONSTRAINT fk_account FOREIGN KEY (account_id) REFERENCES ag.account( id ),
CONSTRAINT fk_source FOREIGN KEY (source_id) REFERENCES ag.source( id ),
CONSTRAINT fk_sample FOREIGN KEY (sample_id) REFERENCES ag.ag_kit_barcodes( ag_kit_barcode_id )
);

CREATE INDEX vio_reg_by_vio_id ON ag.vioscreen_registry (vio_id);
CREATE INDEX vio_reg_by_sample ON ag.vioscreen_registry (account_id, source_id, sample_id);
21 changes: 21 additions & 0 deletions microsetta_private_api/repo/survey_template_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from microsetta_private_api.model.survey_template_trigger import \
SurveyTemplateTrigger
import copy
import secrets


class SurveyTemplateRepo(BaseRepo):
Expand Down Expand Up @@ -209,3 +210,23 @@ def _get_question_triggers(self, survey_question_id):

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

def create_vioscreen_id(self, account_id, source_id,
vioscreen_ext_sample_id):
with self._transaction.cursor() as cur:
cur.execute("SELECT vio_id FROM vioscreen_registry WHERE "
"account_id=%s AND "
"source_id=%s AND "
"sample_id=%s",
(account_id, source_id, vioscreen_ext_sample_id))
rows = cur.fetchall()
if rows is None or len(rows) == 0:
vioscreen_id = secrets.token_hex(8)
cur.execute("INSERT INTO vioscreen_registry("
"account_id, source_id, sample_id, vio_id) "
"VALUES(%s, %s, %s, %s)",
(account_id, source_id, vioscreen_ext_sample_id,
vioscreen_id))
else:
vioscreen_id = rows[0][0]
return vioscreen_id
5 changes: 2 additions & 3 deletions microsetta_private_api/util/vioscreen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import base64
import secrets
import uuid
from urllib.parse import urljoin

Expand All @@ -16,7 +15,7 @@
from microsetta_private_api.config_manager import SERVER_CONFIG


def gen_survey_url(language_tag, survey_redirect_url):
def gen_survey_url(user_id, language_tag, survey_redirect_url):
if not survey_redirect_url:
raise BadRequest("Food Frequency Questionnaire Requires "
"survey_redirect_url")
Expand All @@ -25,7 +24,7 @@ def gen_survey_url(language_tag, survey_redirect_url):
url = SERVER_CONFIG["vioscreen_endpoint"] + "/remotelogin.aspx?%s" % \
url_encode(
{
b"Key": encrypt_key(secrets.token_hex(8),
b"Key": encrypt_key(user_id,
language_tag,
survey_redirect_url),
b"RegCode": regcode.encode()
Expand Down

0 comments on commit 0f6c862

Please sign in to comment.