File size: 4,103 Bytes
e3fefe8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | const ChatModel = require('../models/chatModel');
const UserModel = require('../models/userModel');
const ChatController = {
/**
* GET /api/chat/:otherUserId
* Get chat messages between current user and otherUserId.
* Only allowed if a confirmed booking exists between them.
*/
async getMessages(req, res) {
try {
const uid = req.user.uid;
const { otherUserId } = req.params;
const chatId = ChatModel.getChatId(uid, otherUserId);
// Check if confirmed booking exists between them
const isAllowed = await ChatModel.hasActiveBooking(uid, otherUserId);
if (!isAllowed) {
return res.status(403).json({
success: false,
message: 'Chat is only available after booking is confirmed (payment verified).'
});
}
// Get or create chat
await ChatModel.getOrCreate(uid, otherUserId);
// Get messages
const messages = await ChatModel.getMessages(chatId);
return res.status(200).json({
success: true,
data: {
chatId,
messages,
},
});
} catch (error) {
console.error('getMessages error:', error);
return res.status(500).json({
success: false,
message: 'Failed to fetch messages.'
});
}
},
/**
* POST /api/chat/send
* Send a message to professional or customer.
*/
async sendMessage(req, res) {
try {
const uid = req.user.uid;
const { receiverId, text } = req.body;
if (!receiverId || !text || text.trim().length === 0) {
return res.status(400).json({
success: false,
message: 'receiverId and text are required.'
});
}
if (text.length > 1000) {
return res.status(400).json({
success: false,
message: 'Message too long. Maximum 1000 characters.'
});
}
// Check if confirmed booking exists between them
const isAllowed = await ChatModel.hasActiveBooking(uid, receiverId);
if (!isAllowed) {
return res.status(403).json({
success: false,
message: 'Chat is only available after booking is confirmed (payment verified).'
});
}
const chatId = ChatModel.getChatId(uid, receiverId);
await ChatModel.getOrCreate(uid, receiverId);
const message = await ChatModel.sendMessage(chatId, uid, text.trim());
return res.status(201).json({
success: true,
data: {
messageId: message.id,
timestamp: message.timestamp,
},
});
} catch (error) {
console.error('sendMessage error:', error);
return res.status(500).json({
success: false,
message: 'Failed to send message.'
});
}
},
/**
* GET /api/chat/conversations
* Get all conversations for current user.
*/
async getConversations(req, res) {
try {
const uid = req.user.uid;
const chats = await ChatModel.getConversationsForUser(uid);
const conversations = await Promise.all(
chats.map(async (chat) => {
const participants = chat.participants || [];
const otherUserId = participants.find(p => p !== uid);
const otherUser = await UserModel.getById(otherUserId, true);
return {
chatId: chat.chatId,
otherUserId: otherUserId || '',
otherUserName: otherUser?.displayName || 'User',
otherUserPhoto: otherUser?.photoURL || '',
lastMessage: chat.lastMessage || '',
lastUpdated: chat.lastUpdated || 0,
};
})
);
return res.status(200).json({
success: true,
data: conversations
});
} catch (error) {
console.error('getConversations error:', error);
return res.status(500).json({
success: false,
message: 'Failed to fetch conversations.'
});
}
}
};
module.exports = ChatController; |