Add validateAccountID middleware to account routes
This commit is contained in:
23
middlewares/validateAccountID.js
Normal file
23
middlewares/validateAccountID.js
Normal 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;
|
||||
Reference in New Issue
Block a user