|
| 1 | +""" |
| 2 | +recordlinker.routes.patient_router |
| 3 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | +This module implements the patient router for the RecordLinker API. Exposing |
| 6 | +the patient API endpoints. |
| 7 | +""" |
| 8 | + |
| 9 | +import uuid |
| 10 | + |
| 11 | +import fastapi |
| 12 | +import sqlalchemy.orm as orm |
| 13 | + |
| 14 | +from recordlinker import schemas |
| 15 | +from recordlinker.database import get_session |
| 16 | +from recordlinker.linking import mpi_service as service |
| 17 | + |
| 18 | +router = fastapi.APIRouter() |
| 19 | + |
| 20 | + |
| 21 | +@router.post( |
| 22 | + "/{patient_reference_id}/person", |
| 23 | + summary="Assign Patient to new Person", |
| 24 | + status_code=fastapi.status.HTTP_201_CREATED, |
| 25 | +) |
| 26 | +def create_person( |
| 27 | + patient_reference_id: uuid.UUID, session: orm.Session = fastapi.Depends(get_session) |
| 28 | +) -> schemas.PatientPersonRef: |
| 29 | + """ |
| 30 | + Create a new Person in the MPI database and link the Patient to them. |
| 31 | + """ |
| 32 | + patient = service.get_patient_by_reference_id(session, patient_reference_id) |
| 33 | + if patient is None: |
| 34 | + raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND) |
| 35 | + |
| 36 | + person = service.update_person_cluster(session, patient, commit=False) |
| 37 | + return schemas.PatientPersonRef( |
| 38 | + patient_reference_id=patient.reference_id, person_reference_id=person.reference_id |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@router.patch( |
| 43 | + "/{patient_reference_id}/person", |
| 44 | + summary="Assign Patient to existing Person", |
| 45 | + status_code=fastapi.status.HTTP_200_OK, |
| 46 | +) |
| 47 | +def update_person( |
| 48 | + patient_reference_id: uuid.UUID, |
| 49 | + data: schemas.PersonRef, |
| 50 | + session: orm.Session = fastapi.Depends(get_session), |
| 51 | +) -> schemas.PatientPersonRef: |
| 52 | + """ |
| 53 | + Update the Person linked on the Patient. |
| 54 | + """ |
| 55 | + patient = service.get_patient_by_reference_id(session, patient_reference_id) |
| 56 | + if patient is None: |
| 57 | + raise fastapi.HTTPException(status_code=fastapi.status.HTTP_404_NOT_FOUND) |
| 58 | + |
| 59 | + person = service.get_person_by_reference_id(session, data.person_reference_id) |
| 60 | + if person is None: |
| 61 | + raise fastapi.HTTPException(status_code=fastapi.status.HTTP_400_BAD_REQUEST) |
| 62 | + |
| 63 | + person = service.update_person_cluster(session, patient, person, commit=False) |
| 64 | + return schemas.PatientPersonRef( |
| 65 | + patient_reference_id=patient.reference_id, person_reference_id=person.reference_id |
| 66 | + ) |
0 commit comments