""" Verification Script - Confirm NO Journeys are Created This script verifies that: 1. HISAdapter.process_his_data() does NOT create PatientJourneyInstance 2. HISAdapter.process_his_data() does NOT create PatientJourneyStageInstance 3. Only SurveyInstance is created 4. Survey is sent via SMS Usage: python verify_no_journeys.py """ import os import django import sys # Setup Django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev') sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) django.setup() from apps.integrations.services.his_adapter import HISAdapter from apps.surveys.models import SurveyInstance from apps.journeys.models import PatientJourneyInstance, PatientJourneyStageInstance from apps.organizations.models import Hospital from datetime import datetime def test_no_journeys_created(): """Test that no journey instances are created""" print("=" * 80) print("VERIFICATION: NO JOURNEYS CREATED TEST") print("=" * 80) # Count existing records before surveys_before = SurveyInstance.objects.count() journeys_before = PatientJourneyInstance.objects.count() stages_before = PatientJourneyStageInstance.objects.count() print(f"\nšŸ“Š Before Test:") print(f" Survey Instances: {surveys_before}") print(f" Journey Instances: {journeys_before}") print(f" Stage Instances: {stages_before}") # Create test HIS data (discharged patient) his_data = { "FetchPatientDataTimeStampList": [{ "PatientID": "TEST-VERIFY-001", "AdmissionID": "ADM-VERIFY-001", "HospitalID": "1", "HospitalName": "Al Hammadi Hospital - Main", "PatientType": "2", # OPD "AdmitDate": "29-Jan-2026 10:00", "DischargeDate": "29-Jan-2026 14:00", # Discharged "PatientName": "Verify Test Patient", "MobileNo": "0512345678", "Email": "verify@test.com", "Gender": "M", "DOB": "01-Jan-1990", "SSN": "1234567890" }], "FetchPatientDataTimeStampVisitDataList": [], "Code": 200, "Status": "Success" } print(f"\nšŸ“„ Processing HIS Data:") print(f" PatientID: {his_data['FetchPatientDataTimeStampList'][0]['PatientID']}") print(f" PatientType: {his_data['FetchPatientDataTimeStampList'][0]['PatientType']}") print(f" DischargeDate: {his_data['FetchPatientDataTimeStampList'][0]['DischargeDate']}") # Process HIS data print(f"\nāš™ļø Processing...") result = HISAdapter.process_his_data(his_data) print(f"\nāœ… Result:") print(f" Success: {result['success']}") print(f" Message: {result['message']}") if result['patient']: print(f" Patient: {result['patient'].get_full_name()}") if result['survey']: print(f" Survey ID: {result['survey'].id}") print(f" Survey Status: {result['survey'].status}") print(f" Survey URL: {result.get('survey_url', 'N/A')}") print(f" Survey Sent: {result.get('survey_sent', False)}") print(f" Patient Type: {result.get('patient_type', 'N/A')}") # Count records after surveys_after = SurveyInstance.objects.count() journeys_after = PatientJourneyInstance.objects.count() stages_after = PatientJourneyStageInstance.objects.count() print(f"\nšŸ“Š After Test:") print(f" Survey Instances: {surveys_after} (change: +{surveys_after - surveys_before})") print(f" Journey Instances: {journeys_after} (change: +{journeys_after - journeys_before})") print(f" Stage Instances: {stages_after} (change: +{stages_after - stages_before})") # Verify results print(f"\nšŸ” Verification:") survey_created = surveys_after > surveys_before journey_created = journeys_after > journeys_before stage_created = stages_after > stages_before print(f" āœ… Survey Created: {survey_created}") print(f" āŒ Journey Created: {journey_created}") print(f" āŒ Stage Created: {stage_created}") # Check for journeys with this admission ID admission_id = his_data['FetchPatientDataTimeStampList'][0]['AdmissionID'] matching_journeys = PatientJourneyInstance.objects.filter( metadata__admission_id=admission_id ) print(f"\nšŸ” Direct Check for Admission ID '{admission_id}':") print(f" Matching Journeys: {matching_journeys.count()}") # Check survey metadata if result['survey']: survey = result['survey'] print(f"\nšŸ“‹ Survey Details:") print(f" Survey ID: {survey.id}") print(f" Survey Template: {survey.survey_template.name}") print(f" Hospital: {survey.hospital.name}") print(f" Patient: {survey.patient.get_full_name()}") print(f" Status: {survey.status}") print(f" Delivery Channel: {survey.delivery_channel}") print(f" Recipient Phone: {survey.recipient_phone}") print(f" Metadata: {survey.metadata}") # Check if survey has journey_instance has_journey = survey.journey_instance is not None print(f" Has Journey Instance: {has_journey}") # Final verdict print(f"\n{'=' * 80}") print("FINAL VERDICT:") print(f"{'=' * 80}") if survey_created and not journey_created and not stage_created: print("āœ… SUCCESS: Only survey was created (NO journeys, NO stages)") print("āœ… System correctly creates only surveys from HIS data") return True else: print("āŒ FAILURE: Unexpected behavior detected") if not survey_created: print(" - Survey was NOT created") if journey_created: print(" - Journey was created (should NOT happen)") if stage_created: print(" - Stage was created (should NOT happen)") return False def test_non_discharged_patient(): """Test that non-discharged patients don't get surveys""" print(f"\n{'=' * 80}") print("VERIFICATION: NON-DISCHARGED PATIENT TEST") print(f"{'=' * 80}") # Create test HIS data (NOT discharged) his_data = { "FetchPatientDataTimeStampList": [{ "PatientID": "TEST-VERIFY-002", "AdmissionID": "ADM-VERIFY-002", "HospitalID": "1", "HospitalName": "Al Hammadi Hospital - Main", "PatientType": "2", # OPD "AdmitDate": "29-Jan-2026 10:00", "DischargeDate": None, # NOT discharged "PatientName": "Non-Discharged Patient", "MobileNo": "0512345679", "Email": "nondischarge@test.com", "Gender": "M", "DOB": "01-Jan-1990", "SSN": "2345678901" }], "FetchPatientDataTimeStampVisitDataList": [], "Code": 200, "Status": "Success" } print(f"\nšŸ“„ Processing Non-Discharged HIS Data:") print(f" PatientID: {his_data['FetchPatientDataTimeStampList'][0]['PatientID']}") print(f" PatientType: {his_data['FetchPatientDataTimeStampList'][0]['PatientType']}") print(f" DischargeDate: {his_data['FetchPatientDataTimeStampList'][0]['DischargeDate']}") # Process HIS data print(f"\nāš™ļø Processing...") result = HISAdapter.process_his_data(his_data) print(f"\nāœ… Result:") print(f" Success: {result['success']}") print(f" Message: {result['message']}") print(f" Survey: {result.get('survey')}") if result['message'] == 'Patient not discharged - no survey sent': print(f"\nāœ… SUCCESS: Non-discharged patient correctly did NOT receive survey") return True else: print(f"\nāŒ FAILURE: Unexpected behavior for non-discharged patient") return False def test_all_patient_types(): """Test all patient types (OPD, INPATIENT, EMS)""" print(f"\n{'=' * 80}") print("VERIFICATION: ALL PATIENT TYPES TEST") print(f"{'=' * 80}") test_cases = [ ("OPD", "2"), ("OPD (alternative)", "O"), ("INPATIENT", "1"), ("EMS", "3"), ("EMS (alternative)", "E"), ] all_passed = True for type_name, patient_type in test_cases: ssn_counter = 3000000000 + list(test_cases).index((type_name, patient_type)) his_data = { "FetchPatientDataTimeStampList": [{ "PatientID": f"TEST-TYPES-{patient_type}", "AdmissionID": f"ADM-TYPES-{patient_type}", "HospitalID": "1", "HospitalName": "Al Hammadi Hospital - Main", "PatientType": patient_type, "AdmitDate": "29-Jan-2026 10:00", "DischargeDate": "29-Jan-2026 14:00", "PatientName": f"Patient {type_name}", "MobileNo": "0512345678", "Email": f"type{patient_type}@test.com", "Gender": "M", "DOB": "01-Jan-1990", "SSN": str(ssn_counter) }], "FetchPatientDataTimeStampVisitDataList": [], "Code": 200, "Status": "Success" } print(f"\nšŸ“„ Testing {type_name} (PatientType={patient_type})") result = HISAdapter.process_his_data(his_data) if result['success'] and result['survey']: expected_type = HISAdapter.map_patient_type_to_survey_type(patient_type) actual_type = result['survey'].survey_template.name survey_type_in_template = expected_type in actual_type.upper() print(f" āœ… Survey created") print(f" Expected Type: {expected_type}") print(f" Template Name: {actual_type}") print(f" Match: {survey_type_in_template}") if not survey_type_in_template: print(f" āš ļø WARNING: Template doesn't match patient type") all_passed = False else: print(f" āŒ Survey NOT created: {result.get('message', 'Unknown error')}") all_passed = False if all_passed: print(f"\nāœ… SUCCESS: All patient types work correctly") else: print(f"\nāŒ FAILURE: Some patient types failed") return all_passed if __name__ == "__main__": print("\n" + "=" * 80) print("PX360 SIMPLIFIED SURVEY INTEGRATION - VERIFICATION") print("=" * 80) print("\nThis script verifies that:") print("1. NO patient journeys are created") print("2. ONLY surveys are created from HIS data") print("3. Surveys are sent to patients via SMS") print("=" * 80) try: # Run all tests test1_passed = test_no_journeys_created() test2_passed = test_non_discharged_patient() test3_passed = test_all_patient_types() # Summary print(f"\n{'=' * 80}") print("TEST SUMMARY") print(f"{'=' * 80}") print(f"Test 1 - No Journeys Created: {'āœ… PASS' if test1_passed else 'āŒ FAIL'}") print(f"Test 2 - Non-Discharged Patient: {'āœ… PASS' if test2_passed else 'āŒ FAIL'}") print(f"Test 3 - All Patient Types: {'āœ… PASS' if test3_passed else 'āŒ FAIL'}") if test1_passed and test2_passed and test3_passed: print(f"\n{'=' * 80}") print("āœ… ALL TESTS PASSED") print(f"{'=' * 80}") print("\nāœ… CONFIRMED: System correctly creates ONLY surveys (no journeys)") print("āœ… CONFIRMED: Surveys are sent to patients based on HIS data") print("āœ… CONFIRMED: PatientType mapping works correctly") sys.exit(0) else: print(f"\n{'=' * 80}") print("āŒ SOME TESTS FAILED") print(f"{'=' * 80}") sys.exit(1) except Exception as e: print(f"\nāŒ ERROR: {str(e)}") import traceback traceback.print_exc() sys.exit(1)