94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
"""
|
|
API views for package integration.
|
|
|
|
This module provides API endpoints for package-related operations.
|
|
"""
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import JsonResponse
|
|
from django.views import View
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from .package_integration_service import PackageIntegrationService
|
|
|
|
|
|
class AvailablePackagesAPIView(LoginRequiredMixin, View):
|
|
"""
|
|
API endpoint to get available packages for a patient.
|
|
|
|
Returns JSON list of packages with remaining sessions.
|
|
"""
|
|
|
|
def get(self, request):
|
|
"""Get available packages for patient."""
|
|
patient_id = request.GET.get('patient')
|
|
clinic_id = request.GET.get('clinic')
|
|
|
|
if not patient_id:
|
|
return JsonResponse({
|
|
'success': False,
|
|
'error': 'Patient ID is required'
|
|
}, status=400)
|
|
|
|
try:
|
|
from core.models import Patient, Clinic
|
|
|
|
# Get patient
|
|
patient = Patient.objects.get(
|
|
id=patient_id,
|
|
tenant=request.user.tenant
|
|
)
|
|
|
|
# Get clinic if provided
|
|
clinic = None
|
|
if clinic_id:
|
|
clinic = Clinic.objects.get(
|
|
id=clinic_id,
|
|
tenant=request.user.tenant
|
|
)
|
|
|
|
# Get available packages
|
|
packages = PackageIntegrationService.get_available_packages_for_patient(
|
|
patient=patient,
|
|
clinic=clinic
|
|
)
|
|
|
|
# Build response
|
|
packages_data = []
|
|
for pkg in packages:
|
|
packages_data.append({
|
|
'id': str(pkg.id),
|
|
'package_id': str(pkg.package.id),
|
|
'package_name': pkg.package.name_en,
|
|
'package_name_ar': pkg.package.name_ar if pkg.package.name_ar else '',
|
|
'total_sessions': pkg.total_sessions,
|
|
'sessions_used': pkg.sessions_used,
|
|
'sessions_remaining': pkg.sessions_remaining,
|
|
'purchase_date': pkg.purchase_date.isoformat(),
|
|
'expiry_date': pkg.expiry_date.isoformat(),
|
|
'is_expired': pkg.is_expired,
|
|
'status': pkg.status,
|
|
})
|
|
|
|
return JsonResponse({
|
|
'success': True,
|
|
'packages': packages_data,
|
|
'count': len(packages_data)
|
|
})
|
|
|
|
except Patient.DoesNotExist:
|
|
return JsonResponse({
|
|
'success': False,
|
|
'error': 'Patient not found'
|
|
}, status=404)
|
|
except Clinic.DoesNotExist:
|
|
return JsonResponse({
|
|
'success': False,
|
|
'error': 'Clinic not found'
|
|
}, status=404)
|
|
except Exception as e:
|
|
return JsonResponse({
|
|
'success': False,
|
|
'error': str(e)
|
|
}, status=500)
|