2023-05-26 02:45:25 +08:00
|
|
|
import json
|
2023-11-22 17:13:16 +08:00
|
|
|
from typing import Annotated
|
|
|
|
|
|
|
|
from fastapi import APIRouter, HTTPException, Depends, Request
|
|
|
|
|
|
|
|
from api.deps import get_current_user
|
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-09-11 13:02:05 +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-11-22 17:13:16 +08:00
|
|
|
from apps.user.schemas import User
|
2023-05-17 16:54:05 +08:00
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
fhir_v4 = APIRouter(tags=["fhir-v4"], 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(
|
2023-11-22 17:13:16 +08:00
|
|
|
request: Request,
|
|
|
|
resource_type: str,
|
|
|
|
current_user: Annotated[User, Depends(get_current_user)],
|
2023-09-11 13:02:05 +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-11-22 17:13:16 +08:00
|
|
|
user_auth = await user_models.UserAuth.get_by_username(current_user.username)
|
2023-07-09 23:10:41 +08:00
|
|
|
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-11-22 17:13:16 +08:00
|
|
|
raise HTTPException(417, f"{resource_type} Resource not supported")
|
2023-09-11 13:02: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}")
|
2023-09-11 13:02:05 +08:00
|
|
|
async def get_resource(
|
2023-11-22 17:13:16 +08:00
|
|
|
resource: str,
|
|
|
|
resource_id: int,
|
|
|
|
current_user: Annotated[User, Depends(get_current_user)],
|
2023-09-11 13:02:05 +08:00
|
|
|
):
|
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-11-22 17:13:16 +08:00
|
|
|
raise HTTPException(417, f"{resource} Resource not supported")
|
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-11-22 17:13:16 +08:00
|
|
|
raise HTTPException(404,
|
|
|
|
f"{resource} with id {resource_id} not found"
|
|
|
|
)
|
2023-05-17 16:54:05 +08:00
|
|
|
return item
|