Skip to content
Snippets Groups Projects

Changes to idea controller to toggle and get collaboration

parent 1c5c318a
No related branches found
No related tags found
1 merge request!9Changes to idea controller to toggle and get collaboration
const Idea = require('../models/idea'); const Idea = require('../models/idea');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
// Helper function for validating required fields // Helper function for validating required fields
const validateFields = (fields, res) => { const validateFields = (fields, res) => {
for (const field of fields) { for (const field of fields) {
...@@ -102,4 +101,58 @@ exports.getLikedPosts = async (req, res) => { ...@@ -102,4 +101,58 @@ exports.getLikedPosts = async (req, res) => {
console.error('Error fetching liked posts:', err.message); console.error('Error fetching liked posts:', err.message);
res.status(500).json({ msg: 'Failed to fetch liked posts' }); res.status(500).json({ msg: 'Failed to fetch liked posts' });
} }
};
// Add or remove a collaborator
exports.toggleCollaboration = async (req, res) => {
try {
//console.log('Request body:', req.body);
//console.log('Authenticated user:', req.user);
const { ideaId } = req.body;
const userId = req.user.id;
const idea = await Idea.findById(ideaId);
if (!idea) {
console.error('Idea not found:', ideaId);
return res.status(404).json({ msg: 'Idea not found' });
}
const isCollaborating = idea.collaborators.includes(userId);
if (isCollaborating) {
// Remove collaborator
idea.collaborators = idea.collaborators.filter(id => id.toString() !== userId);
} else {
// Add collaborator
idea.collaborators.push(userId);
}
await idea.save();
res.status(200).json({
msg: isCollaborating ? 'Collaboration reverted' : 'Collaboration requested',
collaborators: idea.collaborators,
});
} catch (error) {
console.error('Error toggling collaboration:', error.message);
res.status(500).json({ msg: 'Server error' });
}
};
// Get collaborators for an idea
exports.getCollaborators = async (req, res) => {
try {
const { ideaId } = req.params;
const idea = await Idea.findById(ideaId).populate('collaborators', 'name email');
if (!idea) {
return res.status(404).json({ msg: 'Idea not found' });
}
res.status(200).json(idea.collaborators);
} catch (error) {
console.error('Error fetching collaborators:', error.message);
res.status(500).json({ msg: 'Server error' });
}
}; };
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment