HH/test_simulator_email_integration.py

119 lines
4.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
"""
Test script to verify HIS simulator email integration
"""
import os
import sys
import django
# Add project root to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')
django.setup()
from apps.simulator.his_simulator import generate_his_patient_journey
from apps.integrations.services.his_adapter import HISAdapter
from apps.organizations.models import Patient
def test_simulator_email_integration():
"""Test that HIS simulator generates emails and surveys are delivered via email"""
print("="*70)
print("🧪 TESTING HIS SIMULATOR EMAIL INTEGRATION")
print("="*70)
# Generate 3 test patient journeys
for i in range(1, 4):
print(f"\n{'='*70}")
print(f"Test {i}: Generating HIS patient journey...")
print('='*70)
# Generate HIS data
his_data, summary = generate_his_patient_journey()
# Extract patient info
patient_data = his_data['FetchPatientDataTimeStampList'][0]
print(f"\nPatient Name: {patient_data['PatientName']}")
print(f"Admission ID: {summary['admission_id']}")
print(f"Email: {patient_data.get('Email', 'N/A')}")
print(f"Phone: {patient_data.get('MobileNo')}")
print(f"Discharged: {summary['is_discharged']}")
print(f"Visits: {summary['visits_completed']}/{summary['total_possible_visits']}")
# Process HIS data
print(f"\n🔄 Processing HIS data through HISAdapter...")
result = HISAdapter.process_his_data(his_data)
if result['success']:
print(f"✅ Processing successful!")
print(f" Patient ID: {result['patient'].id}")
print(f" Journey ID: {result['journey'].id}")
# Check patient has email
patient = result['patient']
print(f" Patient email: {patient.email if patient.email else '❌ None'}")
# Check if survey was triggered
if result['survey_triggered']:
print(f" ✅ Survey triggered!")
# Check survey details
from apps.surveys.models import SurveyInstance
survey = SurveyInstance.objects.filter(
patient=patient,
journey_instance=result['journey']
).first()
if survey:
print(f" Survey ID: {survey.id}")
print(f" Delivery Channel: {survey.delivery_channel}")
print(f" Recipient Email: {survey.recipient_email if survey.delivery_channel == 'EMAIL' else 'N/A'}")
print(f" Survey Status: {survey.status}")
print(f" Sent At: {survey.sent_at}")
# Check notification log
from apps.notifications.models import NotificationLog
from django.contrib.contenttypes.models import ContentType
survey_ct = ContentType.objects.get_for_model(SurveyInstance)
logs = NotificationLog.objects.filter(
object_id=str(survey.id),
content_type=survey_ct
)
if logs.exists():
log = logs.first()
print(f"\n Notification Log:")
print(f" - ID: {log.id}")
print(f" - Channel: {log.channel}")
print(f" - Status: {log.status}")
print(f" - Provider: {log.provider}")
print(f" - Recipient: {log.recipient}")
print(f" - Created At: {log.created_at}")
else:
print(f" ⚠️ No notification log found")
else:
print(f" Survey not triggered (patient not discharged or survey not enabled)")
if not summary['is_discharged']:
print(f" Reason: Patient not discharged yet")
else:
print(f"❌ Processing failed: {result['message']}")
print(f"\n{'='*70}")
print("📊 SUMMARY")
print('='*70)
# Check total patients with emails
total_patients = Patient.objects.count()
patients_with_email = Patient.objects.filter(email__isnull=False).exclude(email='').count()
print(f"Total patients in database: {total_patients}")
print(f"Patients with email: {patients_with_email}")
print(f"Percentage: {(patients_with_email/total_patients*100):.1f}%")
print(f"\n✅ Integration test complete!")
if __name__ == "__main__":
test_simulator_email_integration()