Last active
August 29, 2015 14:07
-
-
Save catorda/31b895a77cb6fa8321f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateGoals(user, goal, callback) { | |
// Check if mainGoal has an ID | |
// If no id was found, create a new mainGoal | |
var goalId = goal._id; | |
if(!goalId) { | |
// Before creating the goal, check if user has goal with that name | |
Goal.findOne({name: goal.name, owner: user.id}, function(err, g){ | |
if(err) return callback(err); | |
if(g) return callback(new Error('There is another goal with that name')); | |
console.log('new goal'); | |
var newGoal = new Goal(); | |
newGoal.owner = user.id; | |
newGoal.name = goal.name; | |
newGoal.created = new Date(); | |
newGoal.save(function(err) { | |
if(err) return callback(err); | |
return callback(null, newGoal); | |
}); | |
}); | |
} else { | |
// If an ID was found, lookup that main goal | |
Goal.findOne({_id : goal._id}, function(err, g) { | |
if(err) return callback(err); | |
console.log("goal found"); | |
if(g) { | |
console.log("id: " + g._id); | |
//mg = mainGoal; | |
if(goal.name) g.name = goal.name; | |
if(goal.motivation) g.motivation = goal.motivation; | |
if(goal.achievements) g.achievements = goal.achievements; | |
if(goal.tasks) g.tasks = goal.tasks; | |
g.owner = user._id; | |
g.save(function(err) { | |
if(err) return callback(err); | |
else return callback(null, g); | |
}); | |
} else { | |
var error = new Error("No goal passed with request"); | |
return callback(error); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment