-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to map care facilities with abdm health facility (#1550)
* added health facility in abdm * link health facility on create and update * get hip_id from health facility * added authorization * use external_id instead of id --------- Co-authored-by: Vignesh Hari <[email protected]>
- Loading branch information
1 parent
df1244b
commit 1ece5cc
Showing
8 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from rest_framework import serializers | ||
|
||
from care.abdm.models import HealthFacility | ||
|
||
|
||
class HealthFacilitySerializer(serializers.ModelSerializer): | ||
id = serializers.CharField(source="external_id", read_only=True) | ||
|
||
class Meta: | ||
model = HealthFacility | ||
exclude = ("deleted",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework.mixins import ( | ||
CreateModelMixin, | ||
ListModelMixin, | ||
RetrieveModelMixin, | ||
UpdateModelMixin, | ||
) | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import GenericViewSet | ||
|
||
from care.abdm.api.serializers.health_facility import HealthFacilitySerializer | ||
from care.abdm.models import HealthFacility | ||
from care.abdm.utils.api_call import Bridge | ||
from care.utils.queryset.facility import get_facility_queryset | ||
|
||
|
||
class HealthFacilityViewSet( | ||
GenericViewSet, | ||
CreateModelMixin, | ||
ListModelMixin, | ||
RetrieveModelMixin, | ||
UpdateModelMixin, | ||
): | ||
serializer_class = HealthFacilitySerializer | ||
model = HealthFacility | ||
queryset = HealthFacility.objects.all() | ||
permission_classes = (IsAuthenticated,) | ||
lookup_field = "facility__external_id" | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
facilities = get_facility_queryset(self.request.user) | ||
return queryset.filter(facility__in=facilities) | ||
|
||
def get_facility(self, facility_external_id): | ||
facilities = get_facility_queryset(self.request.user) | ||
return get_object_or_404(facilities.filter(external_id=facility_external_id)) | ||
|
||
def link_health_facility(self, hf_id, facility_id): | ||
facility = self.get_facility(facility_id) | ||
return Bridge().add_update_service( | ||
{ | ||
"id": hf_id, | ||
"name": facility.name, | ||
"type": "HIP", | ||
"active": True, | ||
"alias": ["CARE_HIP"], | ||
} | ||
) | ||
|
||
def create(self, request, *args, **kwargs): | ||
if ( | ||
self.link_health_facility( | ||
request.data["hf_id"], request.data["facility"] | ||
).status_code | ||
== 200 | ||
): | ||
return super().create(request, *args, **kwargs) | ||
|
||
return Response({"message": "Error linking health facility"}, status=400) | ||
|
||
def update(self, request, *args, **kwargs): | ||
if ( | ||
self.link_health_facility( | ||
request.data["hf_id"], kwargs["facility__external_id"] | ||
).status_code | ||
== 200 | ||
): | ||
return super().update(request, *args, **kwargs) | ||
|
||
return Response({"message": "Error linking health facility"}, status=400) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Generated by Django 4.2.2 on 2023-08-21 09:53 | ||
|
||
import uuid | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0378_consultationbedasset_consultationbed_assets"), | ||
("abdm", "0008_abhanumber_new"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="HealthFacility", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"external_id", | ||
models.UUIDField(db_index=True, default=uuid.uuid4, unique=True), | ||
), | ||
( | ||
"created_date", | ||
models.DateTimeField(auto_now_add=True, db_index=True, null=True), | ||
), | ||
( | ||
"modified_date", | ||
models.DateTimeField(auto_now=True, db_index=True, null=True), | ||
), | ||
("deleted", models.BooleanField(db_index=True, default=False)), | ||
("hf_id", models.CharField(max_length=50, unique=True)), | ||
( | ||
"facility", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to="facility.facility", | ||
to_field="external_id", | ||
), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters