Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Production Release; Sep Week 2 #1587

Merged
merged 10 commits into from
Sep 9, 2023
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "pip" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "Weekly"
56 changes: 56 additions & 0 deletions aws/celery.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@
{
"name": "USE_S3",
"value": "True"
},
{
"name": "ENABLE_ABDM",
"value": "True"
},
{
"name": "ABDM_URL",
"value": "https://dev.abdm.gov.in"
},
{
"name": "HEALTH_SERVICE_API_URL",
"value": "https://healthidsbx.abdm.gov.in/api"
},
{
"name": "X_CM_ID",
"value": "sbx"
},
{
"name": "FIDELIUS_URL",
"value": "https://fidelius.ohc.network"
}
],
"repositoryCredentials": {
Expand Down Expand Up @@ -215,6 +235,14 @@
{
"valueFrom": "/care/backend/HCX_IG_URL",
"name": "HCX_IG_URL"
},
{
"valueFrom": "/care/backend/ABDM_CLIENT_ID",
"name": "ABDM_CLIENT_ID"
},
{
"valueFrom": "/care/backend/ABDM_CLIENT_SECRET",
"name": "ABDM_CLIENT_SECRET"
}
],
"name": "care-celery-beat"
Expand Down Expand Up @@ -310,6 +338,26 @@
{
"name": "USE_S3",
"value": "True"
},
{
"name": "ENABLE_ABDM",
"value": "True"
},
{
"name": "ABDM_URL",
"value": "https://dev.abdm.gov.in"
},
{
"name": "HEALTH_SERVICE_API_URL",
"value": "https://healthidsbx.abdm.gov.in/api"
},
{
"name": "X_CM_ID",
"value": "sbx"
},
{
"name": "FIDELIUS_URL",
"value": "https://fidelius.ohc.network"
}
],
"repositoryCredentials": {
Expand Down Expand Up @@ -432,6 +480,14 @@
{
"valueFrom": "/care/backend/HCX_IG_URL",
"name": "HCX_IG_URL"
},
{
"valueFrom": "/care/backend/ABDM_CLIENT_ID",
"name": "ABDM_CLIENT_ID"
},
{
"valueFrom": "/care/backend/ABDM_CLIENT_SECRET",
"name": "ABDM_CLIENT_SECRET"
}
],
"name": "care-celery-worker"
Expand Down
1 change: 1 addition & 0 deletions care/abdm/api/serializers/health_facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class HealthFacilitySerializer(serializers.ModelSerializer):
id = serializers.CharField(source="external_id", read_only=True)
registered = serializers.BooleanField(read_only=True)

class Meta:
model = HealthFacility
Expand Down
81 changes: 45 additions & 36 deletions care/abdm/api/viewsets/health_facility.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.shortcuts import get_object_or_404
from celery import shared_task
from dry_rest_permissions.generics import DRYPermissions
from rest_framework.decorators import action
from rest_framework.mixins import (
CreateModelMixin,
ListModelMixin,
Expand All @@ -15,6 +17,36 @@
from care.utils.queryset.facility import get_facility_queryset


@shared_task
def register_health_facility_as_service(facility_external_id):
health_facility = HealthFacility.objects.filter(
facility__external_id=facility_external_id
).first()

if not health_facility:
return False

if health_facility.registered:
return True

response = Bridge().add_update_service(
{
"id": health_facility.hf_id,
"name": health_facility.facility.name,
"type": "HIP",
"active": True,
"alias": ["CARE_HIP"],
}
)

if response.status_code == 200:
health_facility.registered = True
health_facility.save()
return True

return False


class HealthFacilityViewSet(
GenericViewSet,
CreateModelMixin,
Expand All @@ -25,48 +57,25 @@ class HealthFacilityViewSet(
serializer_class = HealthFacilitySerializer
model = HealthFacility
queryset = HealthFacility.objects.all()
permission_classes = (IsAuthenticated,)
permission_classes = (IsAuthenticated, DRYPermissions)
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)
@action(detail=True, methods=["POST"])
def register_service(self, request, facility__external_id):
registered = register_health_facility_as_service(facility__external_id)

return Response({"message": "Error linking health facility"}, status=400)
return Response({"registered": registered})

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)
def perform_create(self, serializer):
instance = serializer.save()
register_health_facility_as_service.delay(instance.facility.external_id)

return Response({"message": "Error linking health facility"}, status=400)
def perform_update(self, serializer):
serializer.validated_data["registered"] = False
instance = serializer.save()
register_health_facility_as_service.delay(instance.facility.external_id)
17 changes: 17 additions & 0 deletions care/abdm/migrations/0010_healthfacility_registered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.2 on 2023-09-05 06:42

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("abdm", "0009_healthfacility"),
]

operations = [
migrations.AddField(
model_name="healthfacility",
name="registered",
field=models.BooleanField(default=False),
),
]
4 changes: 3 additions & 1 deletion care/abdm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.db import models

from care.abdm.permissions import HealthFacilityPermissions
from care.utils.models.base import BaseModel


Expand Down Expand Up @@ -37,8 +38,9 @@ def __str__(self):
return self.abha_number


class HealthFacility(BaseModel):
class HealthFacility(BaseModel, HealthFacilityPermissions):
hf_id = models.CharField(max_length=50, unique=True)
registered = models.BooleanField(default=False)
facility = models.OneToOneField(
"facility.Facility", on_delete=models.PROTECT, to_field="external_id"
)
Expand Down
29 changes: 29 additions & 0 deletions care/abdm/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from care.facility.models.mixins.permissions.base import BasePermissionMixin
from care.users.models import User


class HealthFacilityPermissions(BasePermissionMixin):
"""
Permissions for HealthFacilityViewSet
"""

def has_object_read_permission(self, request):
return self.facility.has_object_read_permission(request)

def has_object_write_permission(self, request):
allowed_user_types = [
User.TYPE_VALUE_MAP["WardAdmin"],
User.TYPE_VALUE_MAP["LocalBodyAdmin"],
User.TYPE_VALUE_MAP["DistrictAdmin"],
User.TYPE_VALUE_MAP["StateAdmin"],
]
return request.user.is_superuser or (
request.user.user_type in allowed_user_types
and self.facility.has_object_write_permission(request)
)

def has_object_update_permission(self, request):
return self.has_object_write_permission(request)

def has_object_destroy_permission(self, request):
return self.has_object_write_permission(request)
6 changes: 5 additions & 1 deletion care/abdm/utils/fhir.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ def _practioner(self):

id = str(uuid())
name = (
self.consultation.verified_by
(
self.consultation.verified_by
and f"{self.consultation.verified_by.first_name} {self.consultation.verified_by.last_name}"
)
or self.consultation.deprecated_verified_by
or f"{self.consultation.created_by.first_name} {self.consultation.created_by.last_name}"
)
self._practitioner_profile = Practitioner(
Expand Down
Loading