Add validateAccountID middleware to account routes

This commit is contained in:
2020-05-11 00:09:16 +02:00
parent 79a8a2fb75
commit e8d8d9e42f
7 changed files with 33 additions and 8 deletions

View File

@@ -0,0 +1,23 @@
const Account = require('../api/models/accountModel');
async function validateAccountID(req, res, next) {
const { id } = req.params;
try {
const account = await Account.getAccountById(id);
if (typeof account == 'undefined') {
return res.status(404).json({
message: `Account with id ${id} doesn't exist.`,
});
} else {
next();
}
} catch (error) {
res.status(500).json({
message: `Failed to fetch account with id ${id}.`,
error,
});
}
}
exports.validateAccountID = validateAccountID;