Skip to content
Snippets Groups Projects
Commit d9b3938e authored by pjookim's avatar pjookim
Browse files

Refactor collaborator management in TripController

parent 287d7a3c
No related branches found
No related tags found
1 merge request!15Hi
......@@ -66,29 +66,38 @@ const addCollaborator = async (req, res) => {
try {
const { collaboratorEmail } = req.body;
// Verify if the collaboratorEmail corresponds to a valid user
const user = await User.findOne({ email: collaboratorEmail });
if (!user) {
return res.status(404).json({ message: 'User not found' });
const collaborator = await User.findOne({ email: collaboratorEmail });
if (!collaborator) {
return res.status(404).json({ message: '해당 이메일의 사용자를 찾을 수 없습니다.' });
}
const trip = await Trip.findById(req.params.id).populate('creaate_by');
if (!trip) return res.status(404).json({ message: 'Trip not found' });
const trip = await Trip.findById(req.params.id).populate('create_by');
if (!trip) {
return res.status(404).json({ message: '여행을 찾을 수 없습니다.' });
}
const isAlreadyCollaborator = trip.collaborators.some(
collab => collab.toString() === collaborator._id.toString()
);
// Check if the user is already a collaborator
if (trip.collaborators.some(collaborator => collaborator.email === collaboratorEmail)) {
return res.status(400).json({ message: 'User is already a collaborator' });
if (isAlreadyCollaborator) {
return res.status(400).json({ message: '이미 공동작업자로 등록된 사용자입니다.' });
}
// Check if the email is the creator's email
if (trip.create_by.email === collaboratorEmail) {
return res.status(400).json({ message: 'Creator cannot be added as a collaborator' });
if (trip.create_by._id.toString() === collaborator._id.toString()) {
return res.status(400).json({ message: '생성자는 공동작업자로 추가할 수 없습니다.' });
}
trip.collaborators.push({ email: collaboratorEmail });
trip.collaborators.push(collaborator._id);
await trip.save();
res.status(200).json(trip);
const updatedTrip = await Trip.findById(trip._id)
.populate('create_by')
.populate('collaborators');
res.status(200).json(updatedTrip);
} catch (err) {
console.error('공동작업자 추가 오류:', err);
res.status(500).json({ error: err.message });
}
};
......
......@@ -27,7 +27,8 @@ const tripSchema = new mongoose.Schema({
location: { type: String, required: true },
create_by: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
collaborators: [{
email: { type: String, required: true }
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
plans: {
type: Map, // Object 대신 Map 사용
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment