Created
January 17, 2018 00:09
-
-
Save broccolinisoup/935cdd0ab9a47e77fe39f03f2766fa3d 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
app.post('/api/procedure', (req, res) => { | |
console.log('Received POST request for PROCEDURE:', req.body); | |
let procedure = new Procedure({ | |
name: req.body.name, | |
items: req.body.items | |
}); | |
// save the procedure | |
procedure.save((err, procedure) => { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(`Procedure created: ${procedure}`); | |
} | |
res.status(200).send(procedure); | |
}); | |
const deducted_items = req.body.items; | |
deducted_items.forEach(function(item){ | |
// Get the current number of the item in the stock | |
const current_number = Item.findOne({ _id: item.id }, (err, item) => { | |
if (err) { | |
console.log(err); | |
} else { | |
return item.should_have; | |
} | |
}); | |
// deduct the number that used in the procedure and update the item again, | |
const new_number = current_number - item.need; | |
Item.findOneAndUpdate( | |
{ _id: item.id }, | |
{ $set: { should_have: (new_number ) } }, | |
) | |
.then(item => { | |
res.status(200).send(item); | |
}) | |
.catch(err => { | |
res.status(500).send(err); | |
}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment