feat: chat — search, notifications room, settings
- Migration 071: nullable sender_id, is_system flag, notifications room type - Backend: notifications room auto-created per hotel, search messages endpoint, notify endpoint (manager/admin posts system messages), read-only enforcement - API: ChatSearchResult type, chat.search(), chat.notify(), updated ChatMessage type - Frontend ChatWidget: search view (people + messages tabs), settings panel (notifications visibility toggle, sound toggle), notifications room (bell icon, read-only banner, amber styling for system messages), new-chat button Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -30,7 +30,24 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
return existing[0]?.id as string
|
||||
}
|
||||
|
||||
// GET /api/hotels/:slug/chat/rooms — list rooms (general + directs for current user)
|
||||
// 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
|
||||
}
|
||||
|
||||
// GET /api/hotels/:slug/chat/rooms — list rooms (general + notifications + directs for current user)
|
||||
fastify.get<SlugParam>(
|
||||
'/api/hotels/:slug/chat/rooms',
|
||||
{ onRequest: [fastify.authenticate] },
|
||||
@@ -41,10 +58,10 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
const hotelId = await getHotelId(slug)
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
|
||||
const generalRoomId = await ensureGeneralRoom(hotelId)
|
||||
await ensureGeneralRoom(hotelId)
|
||||
await ensureNotificationsRoom(hotelId)
|
||||
const userId = request.user.sub
|
||||
|
||||
// Get all rooms this user belongs to (general + directs)
|
||||
const { rows } = await db.query(
|
||||
`SELECT r.id, r.type, r.name,
|
||||
(SELECT COUNT(*) FROM chat_messages m
|
||||
@@ -53,23 +70,22 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
(SELECT rs.last_read FROM chat_read_status rs WHERE rs.room_id = r.id AND rs.user_id = $2),
|
||||
'1970-01-01'
|
||||
)
|
||||
AND m.sender_id != $2
|
||||
AND (m.sender_id != $2 OR m.sender_id IS NULL)
|
||||
) AS unread_count,
|
||||
(SELECT m.text FROM chat_messages m WHERE m.room_id = r.id ORDER BY m.created_at DESC LIMIT 1) AS last_message,
|
||||
(SELECT m.created_at FROM chat_messages m WHERE m.room_id = r.id ORDER BY m.created_at DESC LIMIT 1) AS last_message_at,
|
||||
(SELECT u.name FROM chat_messages m JOIN users u ON u.id = m.sender_id WHERE m.room_id = r.id ORDER BY m.created_at DESC LIMIT 1) AS last_sender,
|
||||
(SELECT COALESCE(m.system_name, u.name) FROM chat_messages m LEFT JOIN users u ON u.id = m.sender_id WHERE m.room_id = r.id ORDER BY m.created_at DESC LIMIT 1) AS last_sender,
|
||||
-- for direct rooms: get the other user's name
|
||||
(SELECT u.name FROM chat_room_members crm JOIN users u ON u.id = crm.user_id WHERE crm.room_id = r.id AND crm.user_id != $2 LIMIT 1) AS other_user_name,
|
||||
(SELECT crm.user_id FROM chat_room_members crm WHERE crm.room_id = r.id AND crm.user_id != $2 LIMIT 1) AS other_user_id
|
||||
FROM chat_rooms r
|
||||
WHERE r.hotel_id = $1
|
||||
AND (r.type = 'general' OR EXISTS (
|
||||
AND (r.type IN ('general', 'notifications') OR EXISTS (
|
||||
SELECT 1 FROM chat_room_members crm WHERE crm.room_id = r.id AND crm.user_id = $2
|
||||
))
|
||||
ORDER BY last_message_at DESC NULLS LAST, r.type = 'general' DESC`,
|
||||
[hotelId, userId],
|
||||
)
|
||||
void generalRoomId
|
||||
return rows
|
||||
},
|
||||
)
|
||||
@@ -88,9 +104,11 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
const limit = Math.min(Number(request.query.limit ?? 50), 100)
|
||||
const { rows } = await db.query(
|
||||
`SELECT m.id, m.room_id, m.sender_id, m.text, m.created_at,
|
||||
u.name AS sender_name, u.role AS sender_role
|
||||
m.is_system, m.system_name,
|
||||
COALESCE(m.system_name, u.name) AS sender_name,
|
||||
COALESCE(u.role, 'system') AS sender_role
|
||||
FROM chat_messages m
|
||||
JOIN users u ON u.id = m.sender_id
|
||||
LEFT JOIN users u ON u.id = m.sender_id
|
||||
WHERE m.room_id = $1 AND m.hotel_id = $2
|
||||
ORDER BY m.created_at DESC
|
||||
LIMIT $3`,
|
||||
@@ -118,6 +136,11 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
const hotelId = await getHotelId(slug)
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
|
||||
// Disallow posting to notifications room
|
||||
const { rows: roomRows } = await db.query('SELECT type FROM chat_rooms WHERE id = $1', [roomId])
|
||||
if (roomRows[0]?.type === 'notifications')
|
||||
return reply.code(403).send({ error: 'Cannot post to notifications room' })
|
||||
|
||||
const { text } = request.body
|
||||
if (!text?.trim()) return reply.code(400).send({ error: 'Text required' })
|
||||
|
||||
@@ -128,9 +151,8 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
[roomId, hotelId, request.user.sub, text.trim()],
|
||||
)
|
||||
const msg = rows[0]
|
||||
// Get sender name
|
||||
const { rows: uRows } = await db.query('SELECT name, role FROM users WHERE id = $1', [request.user.sub])
|
||||
const result = { ...msg, sender_name: uRows[0]?.name, sender_role: uRows[0]?.role }
|
||||
const result = { ...msg, sender_name: uRows[0]?.name, sender_role: uRows[0]?.role, is_system: false, system_name: null }
|
||||
return reply.code(201).send(result)
|
||||
},
|
||||
)
|
||||
@@ -147,7 +169,6 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
|
||||
const userId = request.user.sub
|
||||
// Check if direct room already exists between these two users
|
||||
const { rows: existing } = await db.query(
|
||||
`SELECT r.id FROM chat_rooms r
|
||||
JOIN chat_room_members m1 ON m1.room_id = r.id AND m1.user_id = $1
|
||||
@@ -158,7 +179,6 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
)
|
||||
if (existing[0]) return { room_id: existing[0].id }
|
||||
|
||||
// Create new direct room
|
||||
const { rows: [room] } = await db.query(
|
||||
`INSERT INTO chat_rooms (hotel_id, type) VALUES ($1, 'direct') RETURNING id`,
|
||||
[hotelId],
|
||||
@@ -185,6 +205,71 @@ const chatRoutes: FastifyPluginAsync = async (fastify) => {
|
||||
return { ok: true }
|
||||
},
|
||||
)
|
||||
|
||||
// GET /api/hotels/:slug/chat/search?q= — search messages across accessible rooms
|
||||
fastify.get<SlugParam & { Querystring: { q?: string } }>(
|
||||
'/api/hotels/:slug/chat/search',
|
||||
{ onRequest: [fastify.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { slug } = request.params
|
||||
if (!canAccess(request.user.hotelSlug, request.user.role, slug))
|
||||
return reply.code(403).send({ error: 'Forbidden' })
|
||||
const hotelId = await getHotelId(slug)
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
|
||||
const q = (request.query.q ?? '').trim()
|
||||
if (!q) return []
|
||||
|
||||
const userId = request.user.sub
|
||||
const { rows } = await db.query(
|
||||
`SELECT m.id, m.room_id, m.text, m.created_at,
|
||||
COALESCE(m.system_name, u.name) AS sender_name,
|
||||
r.type AS room_type, r.name AS room_name,
|
||||
(SELECT u2.name FROM chat_room_members crm2 JOIN users u2 ON u2.id = crm2.user_id
|
||||
WHERE crm2.room_id = r.id AND crm2.user_id != $2 LIMIT 1) AS other_user_name
|
||||
FROM chat_messages m
|
||||
JOIN chat_rooms r ON r.id = m.room_id
|
||||
LEFT JOIN users u ON u.id = m.sender_id
|
||||
WHERE m.hotel_id = $1
|
||||
AND m.text ILIKE $3
|
||||
AND (r.type IN ('general', 'notifications') OR EXISTS (
|
||||
SELECT 1 FROM chat_room_members crm WHERE crm.room_id = r.id AND crm.user_id = $2
|
||||
))
|
||||
ORDER BY m.created_at DESC
|
||||
LIMIT 30`,
|
||||
[hotelId, userId, `%${q}%`],
|
||||
)
|
||||
return rows
|
||||
},
|
||||
)
|
||||
|
||||
// POST /api/hotels/:slug/chat/notify — post a system notification (manager/admin only)
|
||||
fastify.post<SlugParam & { Body: { text: string; system_name?: string } }>(
|
||||
'/api/hotels/:slug/chat/notify',
|
||||
{ onRequest: [fastify.authenticate] },
|
||||
async (request, reply) => {
|
||||
const { slug } = request.params
|
||||
if (!canAccess(request.user.hotelSlug, request.user.role, slug))
|
||||
return reply.code(403).send({ error: 'Forbidden' })
|
||||
if (!['super_admin', 'hotel_admin', 'manager'].includes(request.user.role))
|
||||
return reply.code(403).send({ error: 'Insufficient permissions' })
|
||||
|
||||
const hotelId = await getHotelId(slug)
|
||||
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
|
||||
|
||||
const { text, system_name = 'Система' } = request.body
|
||||
if (!text?.trim()) return reply.code(400).send({ error: 'Text required' })
|
||||
|
||||
const notifRoomId = await ensureNotificationsRoom(hotelId)
|
||||
const { rows } = await db.query(
|
||||
`INSERT INTO chat_messages (room_id, hotel_id, sender_id, text, is_system, system_name)
|
||||
VALUES ($1, $2, NULL, $3, true, $4)
|
||||
RETURNING id, room_id, text, created_at, is_system, system_name`,
|
||||
[notifRoomId, hotelId, text.trim(), system_name],
|
||||
)
|
||||
return reply.code(201).send({ ...rows[0], sender_name: system_name, sender_role: 'system' })
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
export default chatRoutes
|
||||
|
||||
Reference in New Issue
Block a user