[TASK] Create new profile document on DELETE /profile.

This commit is contained in:
Jan Philipp Timme 2013-09-18 16:00:43 +02:00
parent 254274da56
commit a0b426e7a2
1 changed files with 59 additions and 9 deletions

View File

@ -62,21 +62,71 @@ var constructor = function(db) {
} }
if(req.method == "DELETE") { if(req.method == "DELETE") {
var changeset = { db.get(req.session.data.user.profile, function(err, doc) {
"data": {}
};
db.merge(req.session.data.user.profile, changeset, function(err, result) {
if(err) { if(err) {
console.log(err); console.log(err);
res.send(200, JSON.stringify({ res.send(200, JSON.stringify({
"success": false, "success": false,
"error": "Something went wrong updating the profile document!" "error": "Could not fetch old profile document!"
}));
} else {
res.send(200, JSON.stringify({
"success": true
})); }));
return;
} }
//delete profile document
db.remove(doc._id, doc._rev, function(err, result) {
if(err) {
console.log(err);
res.send(200, JSON.stringify({
"success": false,
"error": "Could not remove old profile document!"
}));
return;
}
//create new profile document
db.save({
"type": "profile",
"data": {}
}, function(err, result) {
if(err) {
console.log(err);
res.send(200, JSON.stringify({
"success": false,
"error": "Could not create new profile document!"
}));
return;
}
//update profile id value in user document
var newProfileDocument = result;
var changeset = {
"profile": newProfileDocument.id
};
db.merge(req.session.data.user._id, changeset, function(err, result) {
if(err) {
console.log(err);
res.send(200, JSON.stringify({
"success": false,
"error": "Could not update user document!"
}));
return;
}
db.get(req.session.data.user._id, function(err, doc) {
if(err) {
console.log(err);
res.send(200, JSON.stringify({
"success": false,
"error": "Could not re-read user document!"
}));
return;
}
//update profile id in session data.
req.session.data.user.profile = newProfileDocument.id;
//send response
res.send(200, JSON.stringify({
"success": true
}));
});
});
});
});
}); });
} }
}; };