1+ from pathlib import Path
2+
13from flask import Flask , json , render_template , request , jsonify
24import requests
5+ import yaml
36
47from new_ldot_workflows .b_1_get_new_subjects import get_new_subjects
58from new_ldot_workflows .b_2_get_qualtrics_links import add_individuals_to_survey
69from new_ldot_workflows .b_2_send_links_to_ldot import send_links_to_ldot
10+ from new_ldot_workflows .b_3_get_incomplete_subjects import get_incomplete_subjects
711from new_ldot_workflows .b_4_get_individual_progress import get_individual_progress
812
913app = Flask (__name__ )
1014
11- STUDIES = json .load (open ("ldot_study_configs.json" ))
15+ CONFIG_PATH = Path (__file__ ).resolve ().with_name ("study_configs.yaml" )
16+ STUDIES = yaml .safe_load (CONFIG_PATH .read_text (encoding = "utf-8" ))
17+
18+ def get_study_settings (study_key : str ):
19+ study = STUDIES .get (study_key )
20+ if not study :
21+ return None , None , None
22+
23+ return study , study .get ("ldot_variables" , {}), study .get ("qualtrics_variables" , {})
1224
1325@app .route ("/" )
1426def index ():
@@ -17,12 +29,19 @@ def index():
1729
1830@app .route ("/api/button1" , methods = ["POST" ])
1931def button1 ():
20- """First API call """
32+ """Get the participants who have not yet been added to Qualtrics """
2133 data = request .json
2234 study_id = data .get ("study_id" )
35+ print (f"Received study_id: { study_id } " )
2336
24- # TODO: Replace this with your actual API call that returns subject IDs
25- subject_ids = get_new_subjects (study_id )
37+ study , ldot_vars , qualtrics_vars = get_study_settings (study_id )
38+ if not study :
39+ return jsonify ({"success" : False , "message" : f"Unknown study_id: { study_id } " }), 400
40+
41+ ldot_study_id = ldot_vars .get ("ldot_study_id" )
42+ link_creation_eaid = ldot_vars .get ("link_creation_eaid" ) or ldot_vars .get ("survey_progress_wait_for_entry_eaid" )
43+
44+ subject_ids = get_new_subjects (ldot_study_id , link_creation_eaid )
2645
2746 message = f"Found { len (subject_ids )} subject IDs for study { study_id } " if study_id else f"Found { len (subject_ids )} subject IDs"
2847 return jsonify ({"success" : True , "message" : message , "subject_ids" : subject_ids })
@@ -43,18 +62,24 @@ def button2():
4362 return jsonify ({"success" : False , "message" : "Missing subject_ids in request" }), 400
4463
4564 # Lookup study variables from STUDIES config
46- study = STUDIES . get (study_id )
65+ study , ldot_vars , qualtrics_vars = get_study_settings (study_id )
4766 if not study :
4867 return jsonify ({"success" : False , "message" : f"Unknown study_id: { study_id } " }), 400
4968
50- vars = study .get ("variables" , {})
51- survey_id = vars .get ("survey_id" )
52- mailing_list_id = vars .get ("mailing_list_id" )
53- embedded_data_field = vars .get ("embedded_data_field" )
54- directory_id = vars .get ("directory_id" )
55- distribution_id = vars .get ("distribution_id" )
69+ ldot_study_id = ldot_vars .get ("ldot_study_id" )
70+ survey_id = qualtrics_vars .get ("survey_id" )
71+ mailing_list_id = qualtrics_vars .get ("mailing_list_id" )
72+ embedded_data_field = qualtrics_vars .get ("embedded_data_field" )
73+ directory_id = qualtrics_vars .get ("directory_id" )
74+ distribution_id = qualtrics_vars .get ("distribution_id" )
75+ link_creation_eaid = ldot_vars .get ("link_creation_eaid" ) or ldot_vars .get ("survey_progress_wait_for_entry_eaid" )
76+
77+ if not ldot_study_id :
78+ return jsonify ({"success" : False , "message" : f"Missing ldot_study_id for study_id: { study_id } " }), 400
79+
5680 debug_inputs = {
5781 "study_id" : study_id ,
82+ "ldot_study_id" : ldot_study_id ,
5883 "subject_ids" : subject_ids ,
5984 "survey_id" : survey_id ,
6085 "mailing_list_id" : mailing_list_id ,
@@ -64,29 +89,38 @@ def button2():
6489 }
6590
6691 participant_to_link_dict = add_individuals_to_survey (subject_ids , embedded_data_field , distribution_id , survey_id , mailing_list_id , directory_id )
67- send_links_to_ldot (participant_to_link_dict )
68-
92+ send_links_to_ldot (ldot_study_id , link_creation_eaid )
6993
70- # return jsonify({
71- # "success": True,
72- # "message": "Button 2 inputs resolved successfully ",
73- # "debug_inputs": debug_inputs,
74- # "participant_to_link_dict": participant_to_link_dict
75- # })
94+ return jsonify ({
95+ "success" : True ,
96+ "message" : f"Processed { len ( subject_ids ) } subject IDs for study { study_id } " ,
97+ "debug_inputs" : debug_inputs ,
98+ "participant_to_link_dict" : participant_to_link_dict ,
99+ })
76100
77101
78102@app .route ("/api/button3" , methods = ["POST" ])
79103def button3 ():
80104 """Third API call - returns list of subjectIDs"""
81105 data = request .json
82106 study_id = data .get ("study_id" )
107+ study , ldot_vars , qualtrics_vars = get_study_settings (study_id )
108+
109+ if not study :
110+ return jsonify ({"success" : False , "message" : f"Unknown study_id: { study_id } " }), 400
83111
84- # TODO: Add your API call logic here - should return a list of subjectIDs
112+ ldot_study_id = ldot_vars .get ("ldot_study_id" )
113+ survey_progress_wait_for_entry_eaid = ldot_vars .get ("survey_progress_wait_for_entry_eaid" )
114+ survey_progress_completed_eaid = ldot_vars .get ("survey_progress_completed_eaid" )
115+
116+ if not ldot_study_id :
117+ return jsonify ({"success" : False , "message" : f"Missing ldot_study_id for study_id: { study_id } " }), 400
118+
85119 try :
86120 result = f"Button 3 executed for study { study_id } "
87121 # Return both message and subject_ids list
88- subject_ids = [] # TODO: Fill this with actual subjectIDs from your API call
89- return jsonify ({"success" : True , "message" : result , "subject_ids" : subject_ids })
122+ incomplete_subjects = get_incomplete_subjects ( ldot_study_id , survey_progress_wait_for_entry_eaid , survey_progress_completed_eaid )
123+ return jsonify ({"success" : True , "message" : result , "subject_ids" : incomplete_subjects })
90124 except Exception as e :
91125 return jsonify ({"success" : False , "message" : str (e )})
92126
@@ -104,11 +138,10 @@ def button4():
104138 if not subject_ids :
105139 return jsonify ({"success" : False , "message" : "Missing subject_ids in request" }), 400
106140
107- study = STUDIES . get (study_id )
141+ study , vars = get_study_settings (study_id )
108142 if not study :
109143 return jsonify ({"success" : False , "message" : f"Unknown study_id: { study_id } " }), 400
110144
111- vars = study .get ("variables" , {})
112145 survey_id = vars .get ("survey_id" )
113146 embedded_data_field = vars .get ("embedded_data_field" )
114147
0 commit comments