Skip to content

Commit

Permalink
Fixed ABHA linking issue (#2458)
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar committed Sep 17, 2024
1 parent ce345a2 commit 1a3c4d3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
5 changes: 5 additions & 0 deletions care/abdm/api/serializers/healthid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion care/abdm/api/viewsets/abha_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_object(self):
Q(abha_number=id) | Q(health_id=id) | Q(patient__external_id=id)
).first()

if not instance or get_patient_queryset(self.request.user).contains(
if not instance or not get_patient_queryset(self.request.user).contains(
instance.patient
):
raise Http404

Check warning on line 34 in care/abdm/api/viewsets/abha_number.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/abha_number.py#L34

Added line #L34 was not covered by tests
Expand Down
60 changes: 60 additions & 0 deletions care/abdm/api/viewsets/healthid.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
GenerateMobileOtpRequestPayloadSerializer,
HealthIdAuthSerializer,
HealthIdSerializer,
LinkPatientSerializer,
QRContentSerializer,
VerifyDemographicsRequestPayloadSerializer,
VerifyOtpRequestPayloadSerializer,
Expand Down Expand Up @@ -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

Check warning on line 426 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L426

Added line #L426 was not covered by tests

serializer = LinkPatientSerializer(data=data)
serializer.is_valid(raise_exception=True)

Check warning on line 429 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L428-L429

Added lines #L428 - L429 were not covered by tests

patient_queryset = get_patient_queryset(request.user)
patient = patient_queryset.filter(external_id=data.get("patient")).first()

Check warning on line 432 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L431-L432

Added lines #L431 - L432 were not covered by tests

if not patient:
return Response(

Check warning on line 435 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L435

Added line #L435 was not covered by tests
{
"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(

Check warning on line 443 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L443

Added line #L443 was not covered by tests
{
"detail": "Patient already linked to an ABHA Number",
},
status=status.HTTP_400_BAD_REQUEST,
)

abha_number = AbhaNumber.objects.filter(

Check warning on line 450 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L450

Added line #L450 was not covered by tests
external_id=data.get("abha_number")
).first()

if not abha_number:
return Response(

Check warning on line 455 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L455

Added line #L455 was not covered by tests
{
"detail": "ABHA Number not found",
},
status=status.HTTP_400_BAD_REQUEST,
)

if abha_number.patient is not None:
return Response(

Check warning on line 463 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L463

Added line #L463 was not covered by tests
{
"detail": "ABHA Number already linked to a patient",
},
status=status.HTTP_400_BAD_REQUEST,
)

abha_number.patient = patient
abha_number.save()

Check warning on line 471 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L470-L471

Added lines #L470 - L471 were not covered by tests

return Response(

Check warning on line 473 in care/abdm/api/viewsets/healthid.py

View check run for this annotation

Codecov / codecov/patch

care/abdm/api/viewsets/healthid.py#L473

Added line #L473 was not covered by tests
AbhaNumberSerializer(abha_number).data,
status=status.HTTP_200_OK,
)

@extend_schema(
operation_id="get_new_linking_token",
responses={"200": "{'status': 'boolean'}"},
Expand Down

0 comments on commit 1a3c4d3

Please sign in to comment.