diff --git a/aws/backend.json b/aws/backend.json index d7a02a832f..594e0fe214 100644 --- a/aws/backend.json +++ b/aws/backend.json @@ -117,6 +117,10 @@ "name": "HEALTH_SERVICE_API_URL", "value": "https://healthidsbx.abdm.gov.in/api" }, + { + "name": "ABDM_FACILITY_URL", + "value": "https://facilitysbx.abdm.gov.in" + }, { "name": "X_CM_ID", "value": "sbx" diff --git a/aws/celery.json b/aws/celery.json index 3b0aa109e3..51c2fd0c56 100644 --- a/aws/celery.json +++ b/aws/celery.json @@ -104,6 +104,10 @@ "name": "HEALTH_SERVICE_API_URL", "value": "https://healthidsbx.abdm.gov.in/api" }, + { + "name": "ABDM_FACILITY_URL", + "value": "https://facilitysbx.abdm.gov.in" + }, { "name": "X_CM_ID", "value": "sbx" diff --git a/care/abdm/api/viewsets/health_facility.py b/care/abdm/api/viewsets/health_facility.py index 5fb41b5b4d..0b85b9b2a6 100644 --- a/care/abdm/api/viewsets/health_facility.py +++ b/care/abdm/api/viewsets/health_facility.py @@ -1,4 +1,5 @@ from celery import shared_task +from django.conf import settings from dry_rest_permissions.generics import DRYPermissions from rest_framework.decorators import action from rest_framework.mixins import ( @@ -13,7 +14,7 @@ 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.abdm.utils.api_call import Facility from care.utils.queryset.facility import get_facility_queryset @@ -24,27 +25,39 @@ def register_health_facility_as_service(facility_external_id): ).first() if not health_facility: - return False + return [False, "Health Facility Not Found"] if health_facility.registered: - return True + return [True, None] - response = Bridge().add_update_service( + response = Facility().add_update_service( { - "id": health_facility.hf_id, - "name": health_facility.facility.name, - "type": "HIP", - "active": True, - "alias": ["CARE_HIP"], + "facilityId": health_facility.hf_id, + "facilityName": health_facility.facility.name, + "HRP": [ + { + "bridgeId": settings.ABDM_CLIENT_ID, + "hipName": 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 + data = response.json()[0] - return False + if "error" in data: + return [False, data["error"]["message"]] + + if "servicesLinked" in data: + health_facility.registered = True + health_facility.save() + return [True, None] + + return [False, None] class HealthFacilityViewSet( @@ -67,7 +80,10 @@ def get_queryset(self): @action(detail=True, methods=["POST"]) def register_service(self, request, facility__external_id): - registered = register_health_facility_as_service(facility__external_id) + [registered, error] = register_health_facility_as_service(facility__external_id) + + if error: + return Response({"error": error}, status=400) return Response({"registered": registered}) diff --git a/care/abdm/utils/api_call.py b/care/abdm/utils/api_call.py index e955ab0142..db3f0142dd 100644 --- a/care/abdm/utils/api_call.py +++ b/care/abdm/utils/api_call.py @@ -20,6 +20,7 @@ ABDM_GATEWAY_URL = GATEWAY_API_URL + "/gateway" ABDM_TOKEN_URL = ABDM_GATEWAY_URL + "/v0.5/sessions" ABDM_TOKEN_CACHE_KEY = "abdm_token" +ABDM_FACILITY_URL = settings.ABDM_FACILITY_URL # TODO: Exception handling for all api calls, need to gracefully handle known exceptions @@ -45,6 +46,8 @@ def __init__(self, gateway, token): self.url = ABDM_GATEWAY_URL elif gateway == "abdm_devservice": self.url = ABDM_DEVSERVICE_URL + elif gateway == "facility": + self.url = ABDM_FACILITY_URL else: self.url = GATEWAY_API_URL self.token = token @@ -790,3 +793,13 @@ def add_update_service(self, data): path = "/v1/bridges/addUpdateServices" response = self.api.post(path, data, method="PUT") return response + + +class Facility: + def __init__(self) -> None: + self.api = APIGateway("facility", None) + + def add_update_service(self, data): + path = "/v1/bridges/MutipleHRPAddUpdateServices" + response = self.api.post(path, data, method="POST") + return response diff --git a/config/settings/base.py b/config/settings/base.py index c0a9932ff4..09b1cd140a 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -545,6 +545,7 @@ HEALTH_SERVICE_API_URL = env( "HEALTH_SERVICE_API_URL", default="https://healthidsbx.abdm.gov.in/api" ) +ABDM_FACILITY_URL = env("ABDM_FACILITY_URL", default="https://facilitysbx.abdm.gov.in") ABDM_USERNAME = env("ABDM_USERNAME", default="abdm_user_internal") X_CM_ID = env("X_CM_ID", default="sbx") FIDELIUS_URL = env("FIDELIUS_URL", default="http://fidelius:8090")