[TASK] Create new profile document on DELETE /profile.
This commit is contained in:
parent
254274da56
commit
a0b426e7a2
|
@ -62,21 +62,71 @@ var constructor = function(db) {
|
|||
}
|
||||
|
||||
if(req.method == "DELETE") {
|
||||
var changeset = {
|
||||
"data": {}
|
||||
};
|
||||
db.merge(req.session.data.user.profile, changeset, function(err, result) {
|
||||
db.get(req.session.data.user.profile, function(err, doc) {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
res.send(200, JSON.stringify({
|
||||
"success": false,
|
||||
"error": "Something went wrong updating the profile document!"
|
||||
"error": "Could not fetch old profile document!"
|
||||
}));
|
||||
} else {
|
||||
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
|
||||
}));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue