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)