From 5499f83782a97a387569415251a3d6d022ec70d3 Mon Sep 17 00:00:00 2001 From: Lucas Berger Date: Tue, 9 Jun 2026 10:39:28 -0400 Subject: [PATCH] fix(03): WR-02 make default-calendar selection deterministic (orderBy id, limit 1) --- apps/api/src/routes/events.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/api/src/routes/events.ts b/apps/api/src/routes/events.ts index 3a795cc..e85c19c 100644 --- a/apps/api/src/routes/events.ts +++ b/apps/api/src/routes/events.ts @@ -256,11 +256,17 @@ eventsRouter.post('/create', zValidator('json', eventFieldsSchema), async (c) => } targetCalendarUrl = calRow.url } else { - // Default to first personal calendar (D-01) + // Default to the member's first personal calendar (D-01). + // WR-02: add a deterministic ORDER BY + LIMIT. Without them, a member with + // multiple personal calendars gets an arbitrary, query-to-query-unstable "first" + // row. Ordering by id (insertion order) gives a stable default; limit(1) avoids + // fetching the whole set just to take [0]. const [calRow] = await db .select({ url: calendars.url }) .from(calendars) .where(eq(calendars.userId, currentUserId)) + .orderBy(calendars.id) + .limit(1) if (!calRow) { return c.json({ error: 'No writable calendar found for user' }, 422)