feat: Add internal chat system and network access support

- Add messages table schema with soft delete support
- Add message service, controller and routes
- Update CORS to allow local network IPs
- Update server to listen on 0.0.0.0
- Fix cookie sameSite for local network development

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
richardtekula
2026-01-15 10:13:14 +01:00
parent 70fa080455
commit 2a9377ce3d
7 changed files with 470 additions and 9 deletions

View File

@@ -29,17 +29,18 @@ export const login = async (req, res, next) => {
await logLogin(result.user.id, username, ipAddress, userAgent);
// Nastav cookie s access tokenom (httpOnly, secure)
const isProduction = process.env.NODE_ENV === 'production';
res.cookie('accessToken', result.tokens.accessToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
secure: isProduction,
sameSite: isProduction ? 'strict' : 'lax',
maxAge: 60 * 60 * 1000, // 1 hodina
});
res.cookie('refreshToken', result.tokens.refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
secure: isProduction,
sameSite: isProduction ? 'strict' : 'lax',
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 dní
});

View File

@@ -0,0 +1,110 @@
import * as messageService from '../services/message.service.js';
/**
* Get all conversations for current user
* GET /api/messages/conversations
*/
export const getConversations = async (req, res, next) => {
try {
const conversations = await messageService.getConversations(req.userId);
res.status(200).json({
success: true,
data: conversations,
});
} catch (error) {
next(error);
}
};
/**
* Get messages with a specific user
* GET /api/messages/conversation/:userId
*/
export const getConversation = async (req, res, next) => {
const { userId: partnerId } = req.params;
try {
const conversation = await messageService.getConversation(req.userId, partnerId);
res.status(200).json({
success: true,
data: conversation,
});
} catch (error) {
next(error);
}
};
/**
* Send a message to another user
* POST /api/messages/send
*/
export const sendMessage = async (req, res, next) => {
const { receiverId, content } = req.body;
try {
const message = await messageService.sendMessage(req.userId, receiverId, content);
res.status(201).json({
success: true,
data: message,
message: 'Správa bola odoslaná',
});
} catch (error) {
next(error);
}
};
/**
* Delete conversation with a user
* DELETE /api/messages/conversation/:userId
*/
export const deleteConversation = async (req, res, next) => {
const { userId: partnerId } = req.params;
try {
await messageService.deleteConversation(req.userId, partnerId);
res.status(200).json({
success: true,
message: 'Konverzácia bola odstránená',
});
} catch (error) {
next(error);
}
};
/**
* Get unread message count
* GET /api/messages/unread-count
*/
export const getUnreadCount = async (req, res, next) => {
try {
const count = await messageService.getUnreadCount(req.userId);
res.status(200).json({
success: true,
data: { unreadCount: count },
});
} catch (error) {
next(error);
}
};
/**
* Get all CRM users available for chat
* GET /api/messages/users
*/
export const getChatUsers = async (req, res, next) => {
try {
const users = await messageService.getChatUsers(req.userId);
res.status(200).json({
success: true,
data: users,
});
} catch (error) {
next(error);
}
};