From 07a3d1a4b70b889275cc914ee4575d58d4462d4f Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Tue, 17 Sep 2024 09:04:26 +0530 Subject: [PATCH] added link patient api --- care/abdm/api/serializers/healthid.py | 5 +++ care/abdm/api/viewsets/healthid.py | 60 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/care/abdm/api/serializers/healthid.py b/care/abdm/api/serializers/healthid.py index aa2b7cc1fd..2c1910b823 100644 --- a/care/abdm/api/serializers/healthid.py +++ b/care/abdm/api/serializers/healthid.py @@ -59,3 +59,8 @@ class CreateHealthIdSerializer(Serializer): healthId = CharField(max_length=64, min_length=1, required=False) txnId = CharField(max_length=64, min_length=1, required=True) patientId = UUIDField(required=False) + + +class LinkPatientSerializer(Serializer): + abha_number = UUIDField(required=True) + patient = UUIDField(required=True) diff --git a/care/abdm/api/viewsets/healthid.py b/care/abdm/api/viewsets/healthid.py index 3a280503be..e435c1614f 100644 --- a/care/abdm/api/viewsets/healthid.py +++ b/care/abdm/api/viewsets/healthid.py @@ -20,6 +20,7 @@ GenerateMobileOtpRequestPayloadSerializer, HealthIdAuthSerializer, HealthIdSerializer, + LinkPatientSerializer, QRContentSerializer, VerifyDemographicsRequestPayloadSerializer, VerifyOtpRequestPayloadSerializer, @@ -415,6 +416,65 @@ def link_via_qr(self, request): status=status.HTTP_200_OK, ) + @extend_schema( + operation_id="search_by_health_id", + request=LinkPatientSerializer, + tags=["ABDM HealthID"], + ) + @action(detail=False, methods=["post"]) + def link_patient(self, request): + data = request.data + + serializer = LinkPatientSerializer(data=data) + serializer.is_valid(raise_exception=True) + + patient_queryset = get_patient_queryset(request.user) + patient = patient_queryset.filter(external_id=data.get("patient")).first() + + if not patient: + return Response( + { + "detail": "Patient not found or you do not have permission to access the patient", + }, + status=status.HTTP_400_BAD_REQUEST, + ) + + if hasattr(patient, "abha_number"): + return Response( + { + "detail": "Patient already linked to an ABHA Number", + }, + status=status.HTTP_400_BAD_REQUEST, + ) + + abha_number = AbhaNumber.objects.filter( + external_id=data.get("abha_number") + ).first() + + if not abha_number: + return Response( + { + "detail": "ABHA Number not found", + }, + status=status.HTTP_400_BAD_REQUEST, + ) + + if abha_number.patient is not None: + return Response( + { + "detail": "ABHA Number already linked to a patient", + }, + status=status.HTTP_400_BAD_REQUEST, + ) + + abha_number.patient = patient + abha_number.save() + + return Response( + AbhaNumberSerializer(abha_number).data, + status=status.HTTP_200_OK, + ) + @extend_schema( operation_id="get_new_linking_token", responses={"200": "{'status': 'boolean'}"},