File size: 4,366 Bytes
4888678 |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
const Conversation = require('../models/Conversation');
const Message = require('../models/Message');
const Joi = require('joi');
// Validation schemas
const createConversationSchema = Joi.object({
title: Joi.string().min(1).max(100).required()
});
const updateConversationSchema = Joi.object({
title: Joi.string().min(1).max(100),
isArchived: Joi.boolean(),
tags: Joi.array().items(Joi.string().trim())
});
// Create new conversation
exports.createConversation = async (req, res) => {
try {
// Validate input
const { error } = createConversationSchema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
const { title } = req.body;
const userId = req.userId;
const conversation = new Conversation({
title,
userId
});
await conversation.save();
res.status(201).json({
message: 'Conversation created successfully',
conversation
});
} catch (error) {
console.error('Create conversation error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
// Get user conversations
exports.getConversations = async (req, res) => {
try {
const { page = 1, limit = 20, search, archived } = req.query;
const userId = req.userId;
// Build query
const query = { userId };
if (archived !== undefined) {
query.isArchived = archived === 'true';
}
if (search) {
query.title = { $regex: search, $options: 'i' };
}
// Get conversations with pagination
const conversations = await Conversation.find(query)
.sort({ lastMessageAt: -1 })
.limit(limit * 1)
.skip((page - 1) * limit);
const total = await Conversation.countDocuments(query);
res.json({
conversations,
pagination: {
page: parseInt(page),
limit: parseInt(limit),
total,
pages: Math.ceil(total / limit)
}
});
} catch (error) {
console.error('Get conversations error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
// Get single conversation
exports.getConversation = async (req, res) => {
try {
const { conversationId } = req.params;
const userId = req.userId;
const conversation = await Conversation.findOne({
_id: conversationId,
userId
});
if (!conversation) {
return res.status(404).json({ error: 'Conversation not found' });
}
res.json({ conversation });
} catch (error) {
console.error('Get conversation error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
// Update conversation
exports.updateConversation = async (req, res) => {
try {
// Validate input
const { error } = updateConversationSchema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
const { conversationId } = req.params;
const userId = req.userId;
const updates = req.body;
const conversation = await Conversation.findOneAndUpdate(
{ _id: conversationId, userId },
updates,
{ new: true, runValidators: true }
);
if (!conversation) {
return res.status(404).json({ error: 'Conversation not found' });
}
res.json({
message: 'Conversation updated successfully',
conversation
});
} catch (error) {
console.error('Update conversation error:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
// Delete conversation
exports.deleteConversation = async (req, res) => {
try {
const { conversationId } = req.params;
const userId = req.userId;
const conversation = await Conversation.findOneAndDelete({
_id: conversationId,
userId
});
if (!conversation) {
return res.status(404).json({ error: 'Conversation not found' });
}
// Delete all messages in the conversation
await Message.deleteMany({ conversationId });
res.json({ message: 'Conversation deleted successfully' });
} catch (error) {
console.error('Delete conversation error:', error);
res.status(500).json({ error: 'Internal server error' });
}
}; |