Skip to content

Commit

Permalink
upsert does not modify author upon update. fixes #256
Browse files Browse the repository at this point in the history
  • Loading branch information
remyla committed Jan 7, 2021
1 parent e42a426 commit f8b9fe0
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions server-nodejs/routes/cruds.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,26 @@ module.exports = function (app, routes) {
*/
upsert = function (req, res) {
var nodes = Array.isArray(req.body) ? req.body : [req.body];
var controlProperties = {
author: req.user.username,
time: Date.now()
}
for (var i = 0; i < nodes.length; ++i) {
if ('object' !== typeof nodes[i] || null === nodes[i]) {
return httpStatus(res, 400, 'Upsert');
}
nodes[i] = Object.assign({}, controlProperties, nodes[i]);
}
nodes = unfoldIds(nodes);
for(var i in nodes) {
if(nodes[i]._id === 'null') {
delete nodes[i]._id;
}
}

db.create(nodes, function(err, result) {
var array = [];
var controlProperties = {
author: req.user.username,
time: Date.now()
}
for (var i = 0; i < nodes.length; ++i) {
array.push(Object.assign({}, controlProperties, nodes[i]));
}
db.create(array, function(err, result) {
if(err) {
return httpStatus(res, 500, 'Upsert');
}
Expand All @@ -232,7 +234,7 @@ module.exports = function (app, routes) {
toUpdate.push(nodes[i]);
}
else {
created.push(nodes[i]);
created.push(array[i]);
}
}
var response = getMultipleResponse(result);
Expand Down

0 comments on commit f8b9fe0

Please sign in to comment.