From 019dd8c4da4895484f5984b7e2eb76e211030086 Mon Sep 17 00:00:00 2001 From: rxmox Date: Sun, 1 Mar 2026 15:00:04 -0700 Subject: [PATCH] Fix guest join 409 error caused by duplicate email index Remove redundant non-sparse index on user email field that prevented multiple guest users (who have no email) from being created. The sparse unique index from unique+sparse is sufficient. Also improve E11000 error handling in joinEventAsUser and joinEventAsGuest to distinguish between duplicate name vs duplicate email errors instead of assuming all duplicates are name conflicts. --- .../src/controllers/event_controller.ts | 32 ++++++++++++++----- shatter-backend/src/models/user_model.ts | 1 - 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/shatter-backend/src/controllers/event_controller.ts b/shatter-backend/src/controllers/event_controller.ts index f884d3a..122968c 100644 --- a/shatter-backend/src/controllers/event_controller.ts +++ b/shatter-backend/src/controllers/event_controller.ts @@ -206,10 +206,18 @@ export async function joinEventAsUser(req: Request, res: Response) { }); } catch (e: any) { if (e.code === 11000) { - return res.status(409).json({ - success: false, - msg: "This name is already taken in this event", - }); + if (e.keyPattern?.name && e.keyPattern?.eventId) { + return res.status(409).json({ + success: false, + msg: "This name is already taken in this event", + }); + } + if (e.keyPattern?.email) { + return res.status(409).json({ + success: false, + msg: "A user with this email already exists", + }); + } } console.error("JOIN EVENT ERROR:", e); return res.status(500).json({ success: false, msg: "Internal error" }); @@ -297,10 +305,18 @@ export async function joinEventAsGuest(req: Request, res: Response) { }); } catch (e: any) { if (e.code === 11000) { - return res.status(409).json({ - success: false, - msg: "This name is already taken in this event", - }); + if (e.keyPattern?.name && e.keyPattern?.eventId) { + return res.status(409).json({ + success: false, + msg: "This name is already taken in this event", + }); + } + if (e.keyPattern?.email) { + return res.status(409).json({ + success: false, + msg: "A user with this email already exists", + }); + } } console.error("JOIN GUEST ERROR:", e); return res.status(500).json({ success: false, msg: "Internal error" }); diff --git a/shatter-backend/src/models/user_model.ts b/shatter-backend/src/models/user_model.ts index 521553e..78e76c9 100644 --- a/shatter-backend/src/models/user_model.ts +++ b/shatter-backend/src/models/user_model.ts @@ -45,7 +45,6 @@ const UserSchema = new Schema( lowercase: true, unique: true, sparse: true, // allows multiple users without email (guests) - index: true, match: [ /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/, "Please provide a valid email address",