Skip to content

Commit

Permalink
Merge pull request #1807 from coronasafe/hf_id-error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gigincg authored Jan 4, 2024
2 parents c0740a2 + 7f6abcd commit ef73d93
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 95 deletions.
46 changes: 30 additions & 16 deletions care/abdm/api/viewsets/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

return Response({}, status=status.HTTP_202_ACCEPTED)

Expand Down Expand Up @@ -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),
},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
status=status.HTTP_400_BAD_REQUEST,
)

return Response({}, status=status.HTTP_202_ACCEPTED)
105 changes: 66 additions & 39 deletions care/abdm/api/viewsets/healthid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
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(
Expand Down Expand Up @@ -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)},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
status=status.HTTP_400_BAD_REQUEST,
)

return Response({}, status=status.HTTP_200_OK)

Expand All @@ -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)},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
status=status.HTTP_400_BAD_REQUEST,
)

return Response(status=status.HTTP_202_ACCEPTED)

Expand All @@ -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)},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
status=status.HTTP_400_BAD_REQUEST,
)

return Response(response, status=status.HTTP_202_ACCEPTED)

Expand Down
40 changes: 25 additions & 15 deletions care/abdm/api/viewsets/hip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 12 additions & 15 deletions care/abdm/utils/api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down
23 changes: 13 additions & 10 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ef73d93

Please sign in to comment.