231 lines
11 KiB
JavaScript
231 lines
11 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const express_1 = __importDefault(require("express"));
|
|
const AuthMiddleware_1 = require("../../Application/Services/AuthMiddleware");
|
|
const DIContainer_1 = require("../../Application/Services/DIContainer");
|
|
const ErrorResponseService_1 = require("../../Application/Services/ErrorResponseService");
|
|
const ValidationMiddleware_1 = require("../../Application/Services/ValidationMiddleware");
|
|
const Logger_1 = require("../../Application/Services/Logger");
|
|
const chatRouter = express_1.default.Router();
|
|
// Get user's chats
|
|
chatRouter.get('/user-chats', AuthMiddleware_1.authRequired, async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const includeArchived = req.query.includeArchived === 'true';
|
|
(0, Logger_1.logRequest)('Get user chats endpoint accessed', req, res, { userId, includeArchived });
|
|
const chats = await DIContainer_1.container.getUserChatsQueryHandler.execute({
|
|
userId,
|
|
includeArchived
|
|
});
|
|
(0, Logger_1.logRequest)('User chats retrieved successfully', req, res, {
|
|
userId,
|
|
chatCount: chats.length
|
|
});
|
|
res.json(chats);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Get user chats endpoint error', error, req, res);
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
// Get chat history
|
|
chatRouter.get('/history/:chatId', AuthMiddleware_1.authRequired, ValidationMiddleware_1.ValidationMiddleware.validateUUIDFormat(['chatId']), async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const chatId = req.params.chatId;
|
|
(0, Logger_1.logRequest)('Get chat history endpoint accessed', req, res, { userId, chatId });
|
|
const history = await DIContainer_1.container.getChatHistoryQueryHandler.execute({
|
|
chatId,
|
|
userId
|
|
});
|
|
if (!history) {
|
|
(0, Logger_1.logWarning)('Chat history not found or unauthorized', { userId, chatId }, req, res);
|
|
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, 'Chat not found or unauthorized');
|
|
}
|
|
(0, Logger_1.logRequest)('Chat history retrieved successfully', req, res, {
|
|
userId,
|
|
chatId,
|
|
messageCount: history.messages.length,
|
|
isArchived: history.isArchived
|
|
});
|
|
res.json(history);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Get chat history endpoint error', error, req, res);
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
// Create new chat (direct/group)
|
|
chatRouter.post('/create', AuthMiddleware_1.authRequired, ValidationMiddleware_1.ValidationMiddleware.combine([
|
|
ValidationMiddleware_1.ValidationMiddleware.validateRequiredFields(['type', 'userIds']),
|
|
ValidationMiddleware_1.ValidationMiddleware.validateAllowedValues({ type: ['direct', 'group'] }),
|
|
ValidationMiddleware_1.ValidationMiddleware.validateNonEmptyArrays(['userIds'])
|
|
]), async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const { type, name, userIds } = req.body;
|
|
(0, Logger_1.logRequest)('Create chat endpoint accessed', req, res, {
|
|
userId,
|
|
type,
|
|
targetUserCount: userIds?.length || 0
|
|
});
|
|
if (type === 'group' && !name?.trim()) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'Group name is required');
|
|
}
|
|
const chat = await DIContainer_1.container.createChatCommandHandler.execute({
|
|
type,
|
|
name: name?.trim(),
|
|
createdBy: userId,
|
|
userIds
|
|
});
|
|
if (!chat) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'Failed to create chat');
|
|
}
|
|
(0, Logger_1.logRequest)('Chat created successfully', req, res, {
|
|
userId,
|
|
chatId: chat.id,
|
|
chatType: chat.type
|
|
});
|
|
res.json({
|
|
id: chat.id,
|
|
type: chat.type,
|
|
name: chat.name,
|
|
users: chat.users,
|
|
messages: chat.messages
|
|
});
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Create chat endpoint error', error, req, res);
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('Premium subscription required')) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendForbidden(res, 'Premium subscription required to create groups');
|
|
}
|
|
if (error.message.includes('not found')) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, 'One or more users not found');
|
|
}
|
|
}
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
// Send message (REST endpoint - mainly for testing, real messaging is via WebSocket)
|
|
chatRouter.post('/message', AuthMiddleware_1.authRequired, ValidationMiddleware_1.ValidationMiddleware.combine([
|
|
ValidationMiddleware_1.ValidationMiddleware.validateRequiredFields(['chatId', 'message']),
|
|
ValidationMiddleware_1.ValidationMiddleware.validateUUIDFormat(['chatId']),
|
|
ValidationMiddleware_1.ValidationMiddleware.validateStringLength({ message: { min: 1, max: 2000 } })
|
|
]), async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const { chatId, message } = req.body;
|
|
(0, Logger_1.logRequest)('Send message endpoint accessed', req, res, {
|
|
userId,
|
|
chatId,
|
|
messageLength: message?.length || 0
|
|
});
|
|
const sentMessage = await DIContainer_1.container.sendMessageCommandHandler.execute({
|
|
chatId,
|
|
userId,
|
|
message
|
|
});
|
|
if (!sentMessage) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'Failed to send message');
|
|
}
|
|
(0, Logger_1.logRequest)('Message sent successfully', req, res, {
|
|
userId,
|
|
chatId,
|
|
messageId: sentMessage.id
|
|
});
|
|
res.json(sentMessage);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Send message endpoint error', error, req, res);
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('Chat not found')) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, 'Chat not found');
|
|
}
|
|
if (error.message.includes('not a member')) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendForbidden(res, 'Not authorized to send messages to this chat');
|
|
}
|
|
if (error.message.includes('non-empty string')) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'Message must be a non-empty string');
|
|
}
|
|
}
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
// Archive chat manually
|
|
chatRouter.post('/archive/:chatId', AuthMiddleware_1.authRequired, ValidationMiddleware_1.ValidationMiddleware.validateUUIDFormat(['chatId']), async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const chatId = req.params.chatId;
|
|
(0, Logger_1.logRequest)('Archive chat endpoint accessed', req, res, { userId, chatId });
|
|
// Check if user has access to this chat
|
|
const chat = await DIContainer_1.container.chatRepository.findById(chatId);
|
|
if (!chat) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, 'Chat not found');
|
|
}
|
|
if (!chat.users.includes(userId)) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendForbidden(res, 'Not authorized to archive this chat');
|
|
}
|
|
const success = await DIContainer_1.container.archiveChatCommandHandler.execute({ chatId });
|
|
if (!success) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'Failed to archive chat');
|
|
}
|
|
(0, Logger_1.logRequest)('Chat archived successfully', req, res, { userId, chatId });
|
|
res.json({ success: true, message: 'Chat archived successfully' });
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Archive chat endpoint error', error, req, res);
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
// Restore chat from archive
|
|
chatRouter.post('/restore/:chatId', AuthMiddleware_1.authRequired, ValidationMiddleware_1.ValidationMiddleware.validateUUIDFormat(['chatId']), async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const chatId = req.params.chatId;
|
|
(0, Logger_1.logRequest)('Restore chat endpoint accessed', req, res, { userId, chatId });
|
|
// Check if user has access to this archived chat
|
|
const archive = await DIContainer_1.container.chatArchiveRepository.findByChatId(chatId);
|
|
const userArchive = archive.find((a) => a.participants.includes(userId));
|
|
if (!userArchive) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendNotFound(res, 'Archived chat not found or unauthorized');
|
|
}
|
|
const success = await DIContainer_1.container.restoreChatCommandHandler.execute({ chatId });
|
|
if (!success) {
|
|
return ErrorResponseService_1.ErrorResponseService.sendBadRequest(res, 'Failed to restore chat (game chats cannot be restored)');
|
|
}
|
|
(0, Logger_1.logRequest)('Chat restored successfully', req, res, { userId, chatId });
|
|
res.json({ success: true, message: 'Chat restored successfully' });
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Restore chat endpoint error', error, req, res);
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
// Get archived chats for a game
|
|
chatRouter.get('/archived/game/:gameId', AuthMiddleware_1.authRequired, ValidationMiddleware_1.ValidationMiddleware.validateUUIDFormat(['gameId']), async (req, res) => {
|
|
try {
|
|
const userId = req.user.userId;
|
|
const gameId = req.params.gameId;
|
|
(0, Logger_1.logRequest)('Get archived game chats endpoint accessed', req, res, { userId, gameId });
|
|
const archivedChats = await DIContainer_1.container.getArchivedChatsQueryHandler.execute({
|
|
userId,
|
|
gameId
|
|
});
|
|
(0, Logger_1.logRequest)('Archived game chats retrieved successfully', req, res, {
|
|
userId,
|
|
gameId,
|
|
chatCount: archivedChats.length
|
|
});
|
|
res.json(archivedChats);
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('Get archived game chats endpoint error', error, req, res);
|
|
return ErrorResponseService_1.ErrorResponseService.sendInternalServerError(res);
|
|
}
|
|
});
|
|
exports.default = chatRouter;
|
|
//# sourceMappingURL=chatRouter.js.map
|