fix(backend): auto-create event ChatRoom on first check-in
Problem:
- User got "Chat room not found" error when trying to send messages
- Event ChatRooms were only created by seed script, not for manually
created events
- Event "Another Dance Event" (ID: 420) was missing its ChatRoom
Root Cause:
- Seed script (seed.js:179-188) correctly creates ChatRooms for events
- But events created outside of seed (CLI, manual DB insert) didn't
create ChatRooms
- Socket handler requires ChatRoom to exist before accepting messages
Solution:
1. Added defensive check in check-in handler (POST /api/events/checkin/:token)
2. Automatically creates ChatRoom if missing when first user checks in
3. Logs creation for debugging: "Created missing chat room for event: {slug}"
Impact:
- Existing events without ChatRooms will get them on next check-in
- Future manually-created events will work correctly
- No breaking changes - all 223 tests pass
Changes:
- backend/src/routes/events.js: Added ChatRoom existence check and
auto-creation logic (lines 256-272)
Note: Manually created ChatRoom for event ID 420 to fix immediate issue
This commit is contained in:
@@ -253,6 +253,24 @@ router.post('/checkin/:token', authenticate, async (req, res, next) => {
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure event chat room exists (create if missing)
|
||||
const existingChatRoom = await prisma.chatRoom.findFirst({
|
||||
where: {
|
||||
eventId: event.id,
|
||||
type: 'event',
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingChatRoom) {
|
||||
await prisma.chatRoom.create({
|
||||
data: {
|
||||
eventId: event.id,
|
||||
type: 'event',
|
||||
},
|
||||
});
|
||||
console.log(`✅ Created missing chat room for event: ${event.slug}`);
|
||||
}
|
||||
|
||||
// Add user to event participants
|
||||
const participant = await prisma.eventParticipant.create({
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user