diff --git a/backend/migrations/072_chat_room_type_length.sql b/backend/migrations/072_chat_room_type_length.sql
new file mode 100644
index 0000000..18278e7
--- /dev/null
+++ b/backend/migrations/072_chat_room_type_length.sql
@@ -0,0 +1,6 @@
+-- Increase type column length to fit 'notifications'
+ALTER TABLE chat_rooms ALTER COLUMN type TYPE VARCHAR(20);
+
+-- Partial unique index for general room per hotel (needed for ON CONFLICT)
+CREATE UNIQUE INDEX IF NOT EXISTS uniq_chat_rooms_general_hotel
+ ON chat_rooms(hotel_id) WHERE type = 'general';
diff --git a/backend/src/routes/chat.ts b/backend/src/routes/chat.ts
index 5652bcc..5b87e48 100644
--- a/backend/src/routes/chat.ts
+++ b/backend/src/routes/chat.ts
@@ -13,39 +13,29 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
const canAccess = (userSlug: string | null, role: string, slug: string) =>
role === 'super_admin' || userSlug === slug
- // Ensure general room exists for hotel
- const ensureGeneralRoom = async (hotelId: string) => {
+ // Ensure a room of given type exists for hotel (idempotent)
+ const ensureRoom = async (hotelId: string, type: string, name: string) => {
+ const { rows: existing } = await db.query(
+ `SELECT id FROM chat_rooms WHERE hotel_id = $1 AND type = $2 LIMIT 1`,
+ [hotelId, type],
+ )
+ if (existing[0]) return existing[0].id as string
const { rows } = await db.query(
- `INSERT INTO chat_rooms (hotel_id, type, name)
- VALUES ($1, 'general', 'Общий чат')
- ON CONFLICT (hotel_id) WHERE type = 'general' DO NOTHING
- RETURNING id`,
- [hotelId],
+ `INSERT INTO chat_rooms (hotel_id, type, name) VALUES ($1, $2, $3)
+ ON CONFLICT DO NOTHING RETURNING id`,
+ [hotelId, type, name],
)
if (rows[0]) return rows[0].id as string
- const { rows: existing } = await db.query(
- `SELECT id FROM chat_rooms WHERE hotel_id = $1 AND type = 'general'`,
- [hotelId],
+ // Concurrent insert — fetch again
+ const { rows: r2 } = await db.query(
+ `SELECT id FROM chat_rooms WHERE hotel_id = $1 AND type = $2 LIMIT 1`,
+ [hotelId, type],
)
- return existing[0]?.id as string
+ return r2[0]?.id as string
}
- // Ensure notifications room exists for hotel
- const ensureNotificationsRoom = async (hotelId: string) => {
- const { rows } = await db.query(
- `INSERT INTO chat_rooms (hotel_id, type, name)
- VALUES ($1, 'notifications', 'Уведомления')
- ON CONFLICT DO NOTHING
- RETURNING id`,
- [hotelId],
- )
- if (rows[0]) return rows[0].id as string
- const { rows: existing } = await db.query(
- `SELECT id FROM chat_rooms WHERE hotel_id = $1 AND type = 'notifications'`,
- [hotelId],
- )
- return existing[0]?.id as string
- }
+ const ensureGeneralRoom = (hotelId: string) => ensureRoom(hotelId, 'general', 'Общий чат')
+ const ensureNotificationsRoom = (hotelId: string) => ensureRoom(hotelId, 'notifications', 'Уведомления')
// GET /api/hotels/:slug/chat/rooms — list rooms (general + notifications + directs for current user)
fastify.get
{visibleRooms.length} чатов
)} {view === 'messages' && activeRoom?.type === 'notifications' && ( @@ -348,10 +348,12 @@ export function ChatWidget() { {/* ── Rooms list ── */} {view === 'rooms' && ({label}
+ +