diff --git a/care/abdm/api/viewsets/auth.py b/care/abdm/api/viewsets/auth.py index ab7c88a1d4..5625824126 100644 --- a/care/abdm/api/viewsets/auth.py +++ b/care/abdm/api/viewsets/auth.py @@ -22,7 +22,10 @@ class OnFetchView(GenericAPIView): def post(self, request, *args, **kwargs): data = request.data - AbdmGateway().init(data["resp"]["requestId"]) + try: + AbdmGateway().init(data["resp"]["requestId"]) + except Exception as e: + return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST) return Response({}, status=status.HTTP_202_ACCEPTED) @@ -324,20 +327,31 @@ def post(self, request, *args, **kwargs): } ) - AbdmGateway().data_notify( - { - "health_id": consent["notification"]["consentDetail"]["patient"]["id"], - "consent_id": data["hiRequest"]["consent"]["id"], - "transaction_id": data["transactionId"], - "care_contexts": list( - map( - lambda context: {"id": context["careContextReference"]}, - consent["notification"]["consentDetail"]["careContexts"][ - :-2:-1 - ], - ) - ), - } - ) + try: + AbdmGateway().data_notify( + { + "health_id": consent["notification"]["consentDetail"]["patient"][ + "id" + ], + "consent_id": data["hiRequest"]["consent"]["id"], + "transaction_id": data["transactionId"], + "care_contexts": list( + map( + lambda context: {"id": context["careContextReference"]}, + consent["notification"]["consentDetail"]["careContexts"][ + :-2:-1 + ], + ) + ), + } + ) + except Exception as e: + return Response( + { + "detail": "Failed to notify (health-information/notify)", + "error": str(e), + }, + status=status.HTTP_400_BAD_REQUEST, + ) return Response({}, status=status.HTTP_202_ACCEPTED) diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index 1090c12d98..2957f93914 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -351,18 +351,24 @@ def link_via_qr(self, request): state=data["state name"], ) - abha_number.save() + try: + AbdmGateway().fetch_modes( + { + "healthId": data["phr"] or data["hidn"], + "name": data["name"], + "gender": data["gender"], + "dateOfBirth": str(datetime.strptime(data["dob"], "%d-%m-%Y"))[ + 0:10 + ], + } + ) + except Exception as e: + return Response( + {"detail": "Failed to fetch modes", "error": str(e)}, + status=status.HTTP_400_BAD_REQUEST, + ) - AbdmGateway().fetch_modes( - { - "healthId": data["phr"] or data["hidn"], - "name": data["name"], - "gender": data["gender"], - "dateOfBirth": str(datetime.strptime(data["dob"], "%d-%m-%Y"))[ - 0:10 - ], - } - ) + abha_number.save() if "patientId" in data and data["patientId"] is not None: patient = PatientRegistration.objects.filter( @@ -403,14 +409,20 @@ def get_new_linking_token(self, request): PatientRegistration.objects.get(external_id=data["patient"]) ).data - AbdmGateway().fetch_modes( - { - "healthId": patient["abha_number_object"]["abha_number"], - "name": patient["abha_number_object"]["name"], - "gender": patient["abha_number_object"]["gender"], - "dateOfBirth": str(patient["abha_number_object"]["date_of_birth"]), - } - ) + try: + AbdmGateway().fetch_modes( + { + "healthId": patient["abha_number_object"]["abha_number"], + "name": patient["abha_number_object"]["name"], + "gender": patient["abha_number_object"]["gender"], + "dateOfBirth": str(patient["abha_number_object"]["date_of_birth"]), + } + ) + except Exception as e: + return Response( + {"detail": "Failed to fetch modes", "error": str(e)}, + status=status.HTTP_400_BAD_REQUEST, + ) return Response({}, status=status.HTTP_200_OK) @@ -432,23 +444,29 @@ def add_care_context(self, request, *args, **kwargs): status=status.HTTP_404_NOT_FOUND, ) - AbdmGateway().fetch_modes( - { - "healthId": consultation.patient.abha_number.health_id, - "name": request.data["name"] - if "name" in request.data - else consultation.patient.abha_number.name, - "gender": request.data["gender"] - if "gender" in request.data - else consultation.patient.abha_number.gender, - "dateOfBirth": request.data["dob"] - if "dob" in request.data - else str(consultation.patient.abha_number.date_of_birth), - "consultationId": consultation_id, - # "authMode": "DIRECT", - "purpose": "LINK", - } - ) + try: + AbdmGateway().fetch_modes( + { + "healthId": consultation.patient.abha_number.health_id, + "name": request.data["name"] + if "name" in request.data + else consultation.patient.abha_number.name, + "gender": request.data["gender"] + if "gender" in request.data + else consultation.patient.abha_number.gender, + "dateOfBirth": request.data["dob"] + if "dob" in request.data + else str(consultation.patient.abha_number.date_of_birth), + "consultationId": consultation_id, + # "authMode": "DIRECT", + "purpose": "LINK", + } + ) + except Exception as e: + return Response( + {"detail": "Failed to add care context", "error": str(e)}, + status=status.HTTP_400_BAD_REQUEST, + ) return Response(status=status.HTTP_202_ACCEPTED) @@ -470,9 +488,18 @@ def patient_sms_notify(self, request, *args, **kwargs): status=status.HTTP_404_NOT_FOUND, ) - response = AbdmGateway().patient_sms_notify( - {"phone": patient.phone_number, "healthId": patient.abha_number.health_id} - ) + try: + response = AbdmGateway().patient_sms_notify( + { + "phone": patient.phone_number, + "healthId": patient.abha_number.health_id, + } + ) + except Exception as e: + return Response( + {"detail": "Failed to send SMS", "error": str(e)}, + status=status.HTTP_400_BAD_REQUEST, + ) return Response(response, status=status.HTTP_202_ACCEPTED) diff --git a/care/abdm/api/viewsets/hip.py b/care/abdm/api/viewsets/hip.py index ac93ef17de..0decf29745 100644 --- a/care/abdm/api/viewsets/hip.py +++ b/care/abdm/api/viewsets/hip.py @@ -87,25 +87,35 @@ def share(self, request, *args, **kwargs): pincode=patient_data["address"]["pincode"], ) + try: + self.get_linking_token( + { + "healthId": patient_data["healthId"] + or patient_data["healthIdNumber"], + "name": patient_data["name"], + "gender": patient_data["gender"], + "dateOfBirth": str( + datetime.strptime( + f"{patient_data['yearOfBirth']}-{patient_data['monthOfBirth']}-{patient_data['dayOfBirth']}", + "%Y-%m-%d", + ) + )[0:10], + } + ) + except Exception: + return Response( + { + "status": "FAILED", + "healthId": patient_data["healthId"] + or patient_data["healthIdNumber"], + }, + status=status.HTTP_400_BAD_REQUEST, + ) + abha_number.save() patient.abha_number = abha_number patient.save() - self.get_linking_token( - { - "healthId": patient_data["healthId"] - or patient_data["healthIdNumber"], - "name": patient_data["name"], - "gender": patient_data["gender"], - "dateOfBirth": str( - datetime.strptime( - f"{patient_data['yearOfBirth']}-{patient_data['monthOfBirth']}-{patient_data['dayOfBirth']}", - "%Y-%m-%d", - ) - )[0:10], - } - ) - payload = { "requestId": str(uuid.uuid4()), "timestamp": str( diff --git a/care/abdm/utils/api_call.py b/care/abdm/utils/api_call.py index db3f0142dd..6c060d1835 100644 --- a/care/abdm/utils/api_call.py +++ b/care/abdm/utils/api_call.py @@ -349,11 +349,17 @@ def __init__(self): self.api = APIGateway("abdm_gateway", None) def get_hip_id_by_health_id(self, health_id): - return ( - AbhaNumber.objects.filter(Q(abha_number=health_id) | Q(health_id=health_id)) - .first() - .patientregistration.facility.healthfacility.hf_id - ) + abha_number = AbhaNumber.objects.filter( + Q(abha_number=health_id) | Q(health_id=health_id) + ).first() + if not abha_number: + raise Exception("No ABHA Number found") + + patient_facility = abha_number.patientregistration.last_consultation.facility + if not hasattr(patient_facility, "healthfacility"): + raise Exception("Health Facility not linked") + + return patient_facility.healthfacility.hf_id def add_care_context(self, access_token, request_id): if request_id not in self.temp_memory: @@ -404,16 +410,7 @@ def fetch_modes(self, data): additional_headers = {"X-CM-ID": settings.X_CM_ID} request_id = str(uuid.uuid4()) - """ - data = { - healthId, - name, - gender, - dateOfBirth, - } - """ self.temp_memory[request_id] = data - if "authMode" in data and data["authMode"] == "DIRECT": self.init(request_id) return @@ -722,7 +719,7 @@ def data_notify(self, data): ), "statusNotification": { "sessionStatus": "TRANSFERRED", - "hipId": self.get_hip_id_by_health_id(data["health_id"]), + "hipId": self.get_hip_id_by_health_id(data["healthId"]), "statusResponses": list( map( lambda context: { diff --git a/care/facility/api/serializers/patient_consultation.py b/care/facility/api/serializers/patient_consultation.py index 712d85882d..401d9f3539 100644 --- a/care/facility/api/serializers/patient_consultation.py +++ b/care/facility/api/serializers/patient_consultation.py @@ -668,16 +668,19 @@ def save(self, **kwargs): ).update(end_date=now()) if patient.abha_number: abha_number = patient.abha_number - AbdmGateway().fetch_modes( - { - "healthId": abha_number.abha_number, - "name": abha_number.name, - "gender": abha_number.gender, - "dateOfBirth": str(abha_number.date_of_birth), - "consultationId": abha_number.external_id, - "purpose": "LINK", - } - ) + try: + AbdmGateway().fetch_modes( + { + "healthId": abha_number.abha_number, + "name": abha_number.name, + "gender": abha_number.gender, + "dateOfBirth": str(abha_number.date_of_birth), + "consultationId": abha_number.external_id, + "purpose": "LINK", + } + ) + except Exception: + pass return instance def create(self, validated_data):