Skip to content

Commit

Permalink
Merge branch 'abdm-m2' into abdm
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar committed Jul 20, 2023
2 parents e4347bb + 3605b8b commit 5da359e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 83 deletions.
15 changes: 8 additions & 7 deletions care/abdm/api/viewsets/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DiscoverView(GenericAPIView):

def post(self, request, *args, **kwargs):
data = request.data
print(data)

patients = PatientRegistration.objects.all()
verified_identifiers = data["patient"]["verifiedIdentifiers"]
Expand All @@ -120,11 +121,14 @@ def post(self, request, *args, **kwargs):
)
else:
for identifier in verified_identifiers:
if identifier["value"] is None:
continue

if identifier["type"] == "MOBILE":
matched_by.append(identifier["value"])
mobile = identifier["value"].replace("+91", "").replace("-", "")
patients = patients.filter(
Q(phone_number=f"+91{identifier['value']}")
| Q(phone_number=identifier["value"])
Q(phone_number=f"+91{mobile}") | Q(phone_number=mobile)
)

if identifier["type"] == "NDHM_HEALTH_NUMBER":
Expand All @@ -139,11 +143,8 @@ def post(self, request, *args, **kwargs):
abha_number__health_id=identifier["value"]
)

patient = patients.filter(
abha_number__name=data["patient"]["name"],
abha_number__gender=data["patient"]["gender"],
# TODO: check date also
).last()
# TODO: also filter by demographics
patient = patients.last()

if not patient:
return Response(
Expand Down
116 changes: 41 additions & 75 deletions care/abdm/api/viewsets/healthid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from care.abdm.api.serializers.abhanumber import AbhaNumberSerializer
from care.abdm.api.serializers.healthid import (
AadharOtpGenerateRequestPayloadSerializer,
AadharOtpResendRequestPayloadSerializer,
Expand All @@ -25,7 +26,6 @@
from care.abdm.models import AbhaNumber
from care.abdm.utils.api_call import AbdmGateway, HealthIdGateway
from care.facility.api.serializers.patient import PatientDetailSerializer
from care.facility.models.facility import Facility
from care.facility.models.patient import PatientConsultation, PatientRegistration
from care.utils.queryset.patient import get_patient_queryset
from config.auth_views import CaptchaRequiredException
Expand Down Expand Up @@ -332,61 +332,45 @@ def link_via_qr(self, request):

dob = datetime.strptime(data["dob"], "%d-%m-%Y").date()

if "patientId" not in data or data["patientId"] is None:
patient = PatientRegistration.objects.filter(
abha_number__abha_number=data["hidn"]
).first()
if patient:
return Response(
{
"message": "A patient is already associated with the provided Abha Number"
},
status=status.HTTP_400_BAD_REQUEST,
)

if (
"facilityId" not in data
or not Facility.objects.filter(external_id=data["facilityId"]).first()
):
return Response(
{"message": "Enter a valid facilityId"},
status=status.HTTP_400_BAD_REQUEST,
)
patient = PatientRegistration.objects.filter(
abha_number__abha_number=data["hidn"]
).first()
if patient:
return Response(
{
"message": "A patient is already associated with the provided Abha Number"
},
status=status.HTTP_400_BAD_REQUEST,
)

if not HealthIdGateway().verify_demographics(
data["phr"] or data["hidn"],
data["name"],
data["gender"],
str(dob.year),
):
return Response(
{"message": "Please enter valid data"},
status=status.HTTP_403_FORBIDDEN,
)
abha_number = AbhaNumber.objects.filter(abha_number=data["hidn"]).first()

patient = PatientRegistration.objects.create(
facility=Facility.objects.get(external_id=data["facilityId"]),
if not abha_number:
abha_number = AbhaNumber.objects.create(
abha_number=data["hidn"],
health_id=data["phr"],
name=data["name"],
gender=1
if data["gender"] == "M"
else 2
if data["gender"] == "F"
else 3,
is_antenatal=False,
phone_number=data["mobile"],
emergency_phone_number=data["mobile"],
date_of_birth=dob,
blood_group="UNK",
nationality="India",
gender=data["gender"],
date_of_birth=str(dob)[0:10],
address=data["address"],
pincode=None,
created_by=None,
state=None,
district=None,
local_body=None,
ward=None,
district=data["dist name"],
state=data["state name"],
)
else:

abha_number.save()

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
],
}
)

if "patientId" in data and data["patientId"] is not None:
patient = PatientRegistration.objects.filter(
external_id=data["patientId"]
).first()
Expand All @@ -397,33 +381,15 @@ def link_via_qr(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

abha_number = AbhaNumber.objects.create(
abha_number=data["hidn"],
health_id=data["phr"],
name=data["name"],
gender=data["gender"],
date_of_birth=str(dob)[0:10],
address=data["address"],
district=data["dist name"],
state=data["state name"],
)

abha_number.save()
patient.abha_number = abha_number
patient.save()
patient.abha_number = abha_number
patient.save()

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_serialized = AbhaNumberSerializer(abha_number).data
return Response(
{"id": abha_serialized["external_id"], "abha_profile": abha_serialized},
status=status.HTTP_200_OK,
)

patient_serialized = PatientDetailSerializer(patient).data
return Response(patient_serialized, status=status.HTTP_200_OK)

@extend_schema(
operation_id="get_new_linking_token",
responses={"200": "{'status': 'boolean'}"},
Expand Down
2 changes: 1 addition & 1 deletion care/abdm/utils/api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def verify_mobile_otp(self, data):
def create_health_id(self, data):
path = "/v1/registration/aadhaar/createHealthIdWithPreVerified"
print("Creating Health ID with data: {}".format(data))
data.pop("healthId", None)
# data.pop("healthId", None)
response = self.api.post(path, data)
return response.json()

Expand Down

0 comments on commit 5da359e

Please sign in to comment.