Add validateAccountID middleware to account routes
This commit is contained in:
parent
79a8a2fb75
commit
e8d8d9e42f
@ -3,7 +3,9 @@ const router = express.Router();
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const { saltingRounds } = require('../../config/config');
|
||||
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||
const { authenticate } = require('../../middlewares/authenticate');
|
||||
const { validateAccountID } = require('../../middlewares/validateAccountID');
|
||||
|
||||
const Account = require('../models/accountModel');
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
@ -20,7 +22,7 @@ router.post('/', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:id', authenticate, async (req, res) => {
|
||||
router.put('/:id', authenticate, validateAccountID, async (req, res) => {
|
||||
const data = { ...req.body };
|
||||
const id = req.params.id;
|
||||
|
||||
@ -39,7 +41,7 @@ router.put('/:id', authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:id', authenticate, async (req, res) => {
|
||||
router.delete('/:id', authenticate, validateAccountID, async (req, res) => {
|
||||
const id = req.params.id;
|
||||
|
||||
try {
|
||||
@ -55,7 +57,7 @@ router.delete('/:id', authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/:id/meetings', authenticate, async (req, res) => {
|
||||
router.get('/:id/meetings', authenticate, validateAccountID, async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
|
@ -1,7 +1,7 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||
const { authenticate } = require('../../middlewares/authenticate');
|
||||
const Availibility = require('../models/availibilityModel');
|
||||
|
||||
router.post('/', authenticate, async (req, res) => {
|
||||
|
@ -3,7 +3,7 @@ const router = express.Router();
|
||||
const bcrypt = require('bcryptjs');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||
const { authenticate } = require('../../middlewares/authenticate');
|
||||
const Meeting = require('../models/meetingModel');
|
||||
|
||||
router.post('/', authenticate, async (req, res) => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||
const { authenticate } = require('../../middlewares/authenticate');
|
||||
const Participant = require('../models/participantModel');
|
||||
|
||||
router.post('/', authenticate, async (req, res) => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const { authenticate } = require('../../middlewares/authMiddleware');
|
||||
const { authenticate } = require('../../middlewares/authenticate');
|
||||
const PossibleDate = require('../models/possibleDateModel');
|
||||
|
||||
router.post('/', authenticate, async (req, res) => {
|
||||
|
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;
|
Loading…
Reference in New Issue
Block a user