Skip to content

Commit ad84f7c

Browse files
authored
Merge pull request #8 from helpwave/patch/read-functionality
implement read capabilities
2 parents a98297e + 778d5d1 commit ad84f7c

File tree

22 files changed

+1029
-382
lines changed

22 files changed

+1029
-382
lines changed

backend/api/inputs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
@strawberry.enum
8-
class Gender(Enum):
8+
class Sex(Enum):
99
MALE = "MALE"
1010
FEMALE = "FEMALE"
1111
UNKNOWN = "UNKNOWN"
@@ -56,7 +56,7 @@ class CreatePatientInput:
5656
firstname: str
5757
lastname: str
5858
birthdate: date
59-
gender: Gender
59+
sex: Sex
6060
assigned_location_id: strawberry.ID | None = None
6161
properties: list[PropertyValueInput] | None = None
6262

@@ -66,7 +66,7 @@ class UpdatePatientInput:
6666
firstname: str | None = None
6767
lastname: str | None = None
6868
birthdate: date | None = None
69-
gender: Gender | None = None
69+
sex: Sex | None = None
7070
assigned_location_id: strawberry.ID | None = None
7171
properties: list[PropertyValueInput] | None = None
7272

backend/api/resolvers/location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LocationQuery:
1616
async def location_roots(self, info: Info) -> list[LocationNodeType]:
1717
result = await info.context.db.execute(
1818
select(models.LocationNode).where(
19-
models.LocationNode.parent_id == None, # noqa: E711
19+
models.LocationNode.parent_id == None,
2020
),
2121
)
2222
return result.scalars().all()

backend/api/resolvers/patient.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,32 @@ async def patients(
3232
) -> list[PatientType]:
3333
query = select(models.Patient)
3434
if location_node_id:
35+
# Recursive CTE to find the selected node and all its descendants
36+
cte = (
37+
select(models.LocationNode.id)
38+
.where(models.LocationNode.id == location_node_id)
39+
.cte(name="location_descendants", recursive=True)
40+
)
41+
42+
parent = select(models.LocationNode.id).join(
43+
cte,
44+
models.LocationNode.parent_id == cte.c.id,
45+
)
46+
cte = cte.union_all(parent)
47+
48+
# Filter patients who are assigned to any of these locations
3549
query = query.where(
36-
models.Patient.assigned_location_id == location_node_id,
50+
models.Patient.assigned_location_id.in_(select(cte.c.id)),
3751
)
52+
3853
result = await info.context.db.execute(query)
3954
return result.scalars().all()
4055

4156
@strawberry.field
4257
async def recent_patients(
43-
self, info: Info, limit: int = 5
58+
self,
59+
info: Info,
60+
limit: int = 5,
4461
) -> list[PatientType]:
4562
query = select(models.Patient).limit(limit)
4663
result = await info.context.db.execute(query)
@@ -59,7 +76,7 @@ async def create_patient(
5976
firstname=data.firstname,
6077
lastname=data.lastname,
6178
birthdate=data.birthdate,
62-
gender=data.gender.value,
79+
sex=data.sex.value,
6380
assigned_location_id=data.assigned_location_id,
6481
)
6582
info.context.db.add(new_patient)
@@ -97,8 +114,8 @@ async def update_patient(
97114
patient.lastname = data.lastname
98115
if data.birthdate is not None:
99116
patient.birthdate = data.birthdate
100-
if data.gender is not None:
101-
patient.gender = data.gender.value
117+
if data.sex is not None:
118+
patient.sex = data.sex.value
102119
if data.assigned_location_id is not None:
103120
patient.assigned_location_id = data.assigned_location_id
104121

backend/api/types/patient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import strawberry
55
from api.context import Info
6-
from api.inputs import Gender
6+
from api.inputs import Sex
77
from api.types.property import PropertyValueType
88
from database import models
99
from sqlalchemy import select
@@ -20,7 +20,7 @@ class PatientType:
2020
firstname: str
2121
lastname: str
2222
birthdate: date
23-
gender: Gender
23+
sex: Sex
2424
assigned_location_id: strawberry.ID | None
2525

2626
@strawberry.field

backend/database/migrations/versions/ac24ef41bd41_.py renamed to backend/database/migrations/versions/a0b4916b75c6_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""empty message
22
3-
Revision ID: ac24ef41bd41
3+
Revision ID: a0b4916b75c6
44
Revises:
5-
Create Date: 2025-12-12 19:00:58.216109
5+
Create Date: 2025-12-12 20:54:09.779104
66
77
"""
88
from typing import Sequence, Union
@@ -12,7 +12,7 @@
1212

1313

1414
# revision identifiers, used by Alembic.
15-
revision: str = 'ac24ef41bd41'
15+
revision: str = 'a0b4916b75c6'
1616
down_revision: Union[str, Sequence[str], None] = None
1717
branch_labels: Union[str, Sequence[str], None] = None
1818
depends_on: Union[str, Sequence[str], None] = None
@@ -53,7 +53,7 @@ def upgrade() -> None:
5353
sa.Column('firstname', sa.String(), nullable=False),
5454
sa.Column('lastname', sa.String(), nullable=False),
5555
sa.Column('birthdate', sa.Date(), nullable=False),
56-
sa.Column('gender', sa.String(), nullable=False),
56+
sa.Column('sex', sa.String(), nullable=False),
5757
sa.Column('assigned_location_id', sa.String(), nullable=True),
5858
sa.ForeignKeyConstraint(['assigned_location_id'], ['location_nodes.id'], ),
5959
sa.PrimaryKeyConstraint('id')

backend/database/models/patient.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@ class Patient(Base):
1818
__tablename__ = "patients"
1919

2020
id: Mapped[str] = mapped_column(
21-
String, primary_key=True, default=lambda: str(uuid.uuid4())
21+
String,
22+
primary_key=True,
23+
default=lambda: str(uuid.uuid4()),
2224
)
2325
firstname: Mapped[str] = mapped_column(String)
2426
lastname: Mapped[str] = mapped_column(String)
2527
birthdate: Mapped[date] = mapped_column()
26-
gender: Mapped[str] = mapped_column(String)
28+
sex: Mapped[str] = mapped_column(String)
2729
assigned_location_id: Mapped[str | None] = mapped_column(
28-
ForeignKey("location_nodes.id"), nullable=True
30+
ForeignKey("location_nodes.id"),
31+
nullable=True,
2932
)
3033

3134
assigned_location: Mapped[LocationNode | None] = relationship(
32-
"LocationNode", back_populates="patients"
35+
"LocationNode",
36+
back_populates="patients",
3337
)
3438
tasks: Mapped[list[Task]] = relationship(
35-
"Task", back_populates="patient", cascade="all, delete-orphan"
39+
"Task",
40+
back_populates="patient",
41+
cascade="all, delete-orphan",
3642
)
3743
properties: Mapped[list[PropertyValue]] = relationship(
38-
"PropertyValue", back_populates="patient", cascade="all, delete-orphan"
44+
"PropertyValue",
45+
back_populates="patient",
46+
cascade="all, delete-orphan",
3947
)

backend/database/models/task.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,34 @@ class Task(Base):
2525
__tablename__ = "tasks"
2626

2727
id: Mapped[str] = mapped_column(
28-
String, primary_key=True, default=lambda: str(uuid.uuid4())
28+
String,
29+
primary_key=True,
30+
default=lambda: str(uuid.uuid4()),
2931
)
3032
title: Mapped[str] = mapped_column(String)
3133
description: Mapped[str | None] = mapped_column(String, nullable=True)
3234
done: Mapped[bool] = mapped_column(Boolean, default=False)
3335
creation_date: Mapped[datetime] = mapped_column(default=datetime.now)
3436
update_date: Mapped[datetime | None] = mapped_column(
35-
nullable=True, onupdate=datetime.now
37+
nullable=True,
38+
default=datetime.now,
39+
onupdate=datetime.now,
3640
)
3741
assignee_id: Mapped[str | None] = mapped_column(
38-
ForeignKey("users.id"), nullable=True
42+
ForeignKey("users.id"),
43+
nullable=True,
3944
)
4045
patient_id: Mapped[str] = mapped_column(ForeignKey("patients.id"))
4146

4247
assignee: Mapped[User | None] = relationship(
43-
"User", back_populates="tasks"
48+
"User",
49+
back_populates="tasks",
4450
)
4551
patient: Mapped[Patient] = relationship("Patient", back_populates="tasks")
4652
properties: Mapped[list[PropertyValue]] = relationship(
47-
"PropertyValue", back_populates="task", cascade="all, delete-orphan"
53+
"PropertyValue",
54+
back_populates="task",
55+
cascade="all, delete-orphan",
4856
)
4957

5058
previous_tasks: Mapped[list[Task]] = relationship(

0 commit comments

Comments
 (0)