""" Setup metadata on Healthie form modules for the intake micro app. Run once after form creation, or after structural form changes. Usage: cd intake_app python scripts/setup_metadata.py """ import json import urllib.request def main(): # Load env env = {} with open("../.env.local") as f: for line in f: line = line.strip() if line.startswith("#") or not line or "=" not in line: continue k, _, v = line.partition("=") env[k.strip()] = v.strip("'\"") api_key = env["HEALTHIE_API_KEY"] url = env["HEALTHIE_API_BASE_URL"] headers = { "Content-Type": "application/json", "Authorization": f"Basic {api_key}", "AuthorizationSource": "API", } mutation = """ mutation($input: updateCustomModuleInput!) { updateCustomModule(input: $input) { clientMutationId } } """ updates = { # Conditional sections (on label modules) "20245147": { "access_intake": { "section_label": True, "conditional": { "depends_on": "20244729", "rule": "equals", "value": "Female", }, } }, "20245148": { "access_intake": { "section_label": True, "conditional": { "depends_on": "20244729", "rule": "equals", "value": "Male", }, } }, "20245155": { "access_intake": { "section_label": True, "conditional": { "depends_on": "20244792", "rule": "contains", "value": "Vascular Plaque / Atherosclerosis / Vascular Narrowing|Coronary artery disease / history of heart attack", }, } }, "20245156": { "access_intake": { "section_label": True, "conditional": { "depends_on": "20244792", "rule": "contains", "value": "Fatty liver", }, } }, "20245157": { "access_intake": { "section_label": True, "conditional": { "depends_on": "20244792", "rule": "contains", "value": "Chronic kidney disease / CKD|Abnormal urine protein or abnormal uACR", }, } }, # File upload fields "20244752": { "access_intake": { "file_upload": { "max_size_mb": 10, "allowed_types": ["pdf", "jpg", "png"], } } }, "20244760": { "access_intake": { "file_upload": { "max_size_mb": 10, "allowed_types": ["pdf", "jpg", "png"], } } }, "20244796": { "access_intake": { "file_upload": { "max_size_mb": 10, "allowed_types": ["pdf"], } } }, "20244797": { "access_intake": { "file_upload": { "max_size_mb": 10, "allowed_types": ["pdf"], } } }, "20244798": { "access_intake": { "file_upload": { "max_size_mb": 10, "allowed_types": ["pdf"], } } }, "20244803": { "access_intake": { "file_upload": { "max_size_mb": 10, "allowed_types": ["pdf", "jpg", "png"], } } }, } fixed = 0 failed = [] for mid, metadata in updates.items(): req = urllib.request.Request( url, data=json.dumps({ "query": mutation, "variables": { "input": { "id": mid, "metadata": json.dumps(metadata), } }, }).encode(), headers=headers, method="POST", ) try: with urllib.request.urlopen(req) as resp: r = json.loads(resp.read()) if "errors" in r: failed.append((mid, r["errors"][0]["message"][:100])) else: fixed += 1 print(f"OK {mid}") except Exception as e: failed.append((mid, str(e))) print(f"\nUpdated: {fixed}/{len(updates)}") if failed: for mid, err in failed: print(f"FAIL {mid}: {err}") if __name__ == "__main__": main()