1414 ReminderPublic ,
1515 RemindersPublic ,
1616 ReminderUpdate ,
17+ ReminderWithContactPublic ,
18+ RemindersWithContactPublic ,
1719)
1820
1921router = APIRouter (prefix = "/reminders" , tags = ["reminders" ])
@@ -58,7 +60,7 @@ def list_reminders(
5860 )
5961
6062
61- @router .get ("/due" , response_model = RemindersPublic )
63+ @router .get ("/due" , response_model = RemindersWithContactPublic )
6264def list_due_reminders (
6365 session : SessionDep ,
6466 current_user : CurrentUser ,
@@ -72,30 +74,62 @@ def list_due_reminders(
7274 - snoozed_until is NULL or snoozed_until <= now (not snoozed)
7375 - is_active is True
7476 - owned by current user or tied to visible contacts
77+
78+ Also joins with Contact to include contact_name.
7579 """
7680 now = datetime .now (timezone .utc )
7781
78- statement = select (Reminder ).where (
79- Reminder .remind_at <= now ,
80- or_ (
81- Reminder .snoozed_until .is_ (None ),
82- Reminder .snoozed_until <= now ,
83- ),
84- Reminder .is_active == True ,
85- or_ (
86- Reminder .owner_id == current_user .id ,
87- Reminder .contact_id .in_ (visible_contact_ids (current_user )),
88- ),
82+ statement = (
83+ select (Reminder , Contact .first_name , Contact .last_name )
84+ .outerjoin (Contact , Reminder .contact_id == Contact .id )
85+ .where (
86+ Reminder .remind_at <= now ,
87+ or_ (
88+ Reminder .snoozed_until .is_ (None ),
89+ Reminder .snoozed_until <= now ,
90+ ),
91+ Reminder .is_active == True ,
92+ or_ (
93+ Reminder .owner_id == current_user .id ,
94+ Reminder .contact_id .in_ (visible_contact_ids (current_user )),
95+ ),
96+ )
8997 )
9098
91- count_statement = select (func .count ()).select_from (statement .subquery ())
99+ count_statement = select (func .count ()).select_from (
100+ select (Reminder .id )
101+ .outerjoin (Contact , Reminder .contact_id == Contact .id )
102+ .where (
103+ Reminder .remind_at <= now ,
104+ or_ (
105+ Reminder .snoozed_until .is_ (None ),
106+ Reminder .snoozed_until <= now ,
107+ ),
108+ Reminder .is_active == True ,
109+ or_ (
110+ Reminder .owner_id == current_user .id ,
111+ Reminder .contact_id .in_ (visible_contact_ids (current_user )),
112+ ),
113+ )
114+ .subquery ()
115+ )
92116 count = session .exec (count_statement ).one ()
93117
94118 statement = statement .order_by (Reminder .remind_at .asc ()).offset (skip ).limit (limit )
95- reminders = session .exec (statement ).all ()
96-
97- return RemindersPublic (
98- data = [ReminderPublic .model_validate (r ) for r in reminders ],
119+ results = session .exec (statement ).all ()
120+
121+ reminders_with_contact = []
122+ for row in results :
123+ reminder = row [0 ]
124+ first_name = row [1 ]
125+ last_name = row [2 ]
126+ reminder_data = ReminderWithContactPublic .model_validate (reminder )
127+ if first_name :
128+ reminder_data .contact_name = f"{ first_name } { last_name or '' } " .strip ()
129+ reminders_with_contact .append (reminder_data )
130+
131+ return RemindersWithContactPublic (
132+ data = reminders_with_contact ,
99133 count = count ,
100134 )
101135
0 commit comments