2023-05-26 02:45:25 +08:00
|
|
|
import json
|
2023-08-14 02:08:08 +08:00
|
|
|
from sanic import Blueprint
|
|
|
|
from sanic.request import Request
|
|
|
|
from sanic.exceptions import SanicException
|
2023-05-17 16:54:05 +08:00
|
|
|
from apps.iol.fhir.schema import (
|
2023-05-26 02:45:25 +08:00
|
|
|
BundleResource,
|
2023-05-17 16:54:05 +08:00
|
|
|
DiagnosticReportResource,
|
|
|
|
PatientResource,
|
|
|
|
ServiceRequestResource,
|
|
|
|
)
|
2023-05-26 02:45:25 +08:00
|
|
|
from apps.iol.fhir.utils import get_diagnostic_report_resource, get_patient_resource, create_resource
|
2023-07-09 23:10:41 +08:00
|
|
|
from apps.user import models as user_models
|
2023-05-17 16:54:05 +08:00
|
|
|
|
2023-08-14 02:08:08 +08:00
|
|
|
fhir_v4 = Blueprint('fhir-v4', url_prefix="/fhir")
|
2023-05-17 16:54:05 +08:00
|
|
|
|
|
|
|
|
2023-08-14 02:08:08 +08:00
|
|
|
@fhir_v4.post("/{resource_type}")
|
2023-07-09 23:10:41 +08:00
|
|
|
async def add_resource(
|
|
|
|
request: Request,
|
2023-08-14 02:08:08 +08:00
|
|
|
resource_type: str,
|
|
|
|
current_user: user_models.User,
|
2023-07-09 23:10:41 +08:00
|
|
|
):
|
2023-05-17 16:54:05 +08:00
|
|
|
"""
|
|
|
|
Add a fhir resource
|
2023-05-26 02:45:25 +08:00
|
|
|
Supported Resources are Bundle, ServiceRequest and Patient
|
2023-05-17 16:54:05 +08:00
|
|
|
"""
|
2023-07-09 23:10:41 +08:00
|
|
|
if not "authenticated" in request.auth.scopes:
|
2023-08-14 02:08:08 +08:00
|
|
|
return SanicException("You are not authenticated", status_code=400)
|
2023-07-09 23:10:41 +08:00
|
|
|
|
|
|
|
user_auth = await user_models.UserAuth.get_by_username(request.user.username)
|
|
|
|
current_user = await user_models.User.get(auth_uid=user_auth.uid)
|
|
|
|
|
2023-06-24 18:28:27 +08:00
|
|
|
data = json.loads(await request.json())
|
2023-07-09 23:10:41 +08:00
|
|
|
|
2023-05-26 02:45:25 +08:00
|
|
|
resources = {
|
|
|
|
"Bundle": BundleResource,
|
2023-07-09 23:10:41 +08:00
|
|
|
"DiagnosticReport": DiagnosticReportResource,
|
2023-05-26 02:45:25 +08:00
|
|
|
"ServiceRequest": ServiceRequestResource,
|
|
|
|
"Patient": PatientResource,
|
|
|
|
}
|
|
|
|
if resource_type not in resources:
|
2023-08-14 02:08:08 +08:00
|
|
|
raise SanicException(
|
|
|
|
f"{resource_type} Resource not supported",
|
|
|
|
status_code=417
|
2023-05-17 16:54:05 +08:00
|
|
|
)
|
2023-05-26 02:45:25 +08:00
|
|
|
|
|
|
|
mapped_data = resources[resource_type](**data)
|
2023-07-09 23:10:41 +08:00
|
|
|
return await create_resource(resource_type, mapped_data, request, current_user)
|
2023-05-17 16:54:05 +08:00
|
|
|
|
|
|
|
|
2023-08-14 02:08:08 +08:00
|
|
|
@fhir_v4.get("/{resource}/{resource_id}")
|
|
|
|
async def get_resource(request, resource: str, resource_id: int,
|
|
|
|
current_user: user_models.User,):
|
2023-05-17 16:54:05 +08:00
|
|
|
"""
|
|
|
|
Supported Resources are DiagnosticReport and Patient
|
|
|
|
|
|
|
|
- **resource_id** A Fhir Resource ID
|
|
|
|
"""
|
|
|
|
if resource not in ["Patient", "DiagnosticReport"]:
|
2023-08-14 02:08:08 +08:00
|
|
|
raise SanicException(
|
|
|
|
f"{resource} Resource not supported",
|
|
|
|
status_code=417
|
2023-05-17 16:54:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
item = None
|
|
|
|
if resource == "Patient":
|
|
|
|
item = await get_patient_resource(resource_id)
|
|
|
|
|
|
|
|
if resource == "DiagnosticReport":
|
|
|
|
item = await get_diagnostic_report_resource(resource_id)
|
|
|
|
|
|
|
|
if not item:
|
2023-08-14 02:08:08 +08:00
|
|
|
raise SanicException(
|
|
|
|
f"{resource} with id {resource_id} not found",
|
|
|
|
status_code=404
|
2023-05-17 16:54:05 +08:00
|
|
|
)
|
|
|
|
return item
|