-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add middleware for consultant authorization
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const Consultant = require('../models/consultant.js'); | ||
|
||
module.exports = async (req, res, next) => { | ||
//check if the username is a consultant's username | ||
const consultant = await Consultant.findOne({ | ||
where: { | ||
Username: req.username | ||
} | ||
}); | ||
if (!consultant) return res.status(401).send('Access denied.'); | ||
else { | ||
req.consultant = consultant; | ||
next(); | ||
} | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const express = require('express'); | ||
const router = express(); | ||
router.use(express.json()); | ||
|
||
const Sequelize = require('sequelize'); | ||
|
||
const Consultant = require('../models/consultant.js'); | ||
const Customer = require('../models/customer.js'); | ||
const Owner = require('../models/owner.js'); | ||
const Search= require('../models/search.js') | ||
|
||
const isConsultant = require('../middleware/checkIfConsultantMiddleware.js'); | ||
const auth = require('../middleware/authorizationMiddleware.js'); | ||
|
||
|
||
|
||
router.get('/', auth, isConsultant, async (req, res) => { | ||
console.log('In GET /api/consultant/statistics'); | ||
try { | ||
console.log('consultant is ' + req.username) | ||
|
||
const customers= await Customer.findAll(); | ||
console.log('customers are ' + JSON.stringify(customers)); | ||
|
||
const customersByNationality=await Customer.findAll({ | ||
attributes: ['Nationality', [Sequelize.fn('count', Sequelize.col('Nationality')), 'numberOfCustomers']], | ||
group: ['Nationality'], | ||
raw: true | ||
}) | ||
|
||
const customers2=await Customer.count('Nationality', 'count', { | ||
g | ||
}); | ||
console.log('customers2 are ' + JSON.stringify(customers2)) | ||
|
||
console.log('customers by nationality ' + JSON.stringify(customersByNationality)) | ||
|
||
res.status(200).send(customers2); | ||
// await Search.create({ | ||
// Username: 'marinco', | ||
// SearchedWord: 'word2', | ||
// NumberOfSearches: 1 | ||
// }); | ||
// res.status(200).send('ok'); | ||
|
||
|
||
}catch (error) { | ||
console.log(error) | ||
res.status(500).send('Internal server error'); | ||
} | ||
}); | ||
|
||
module.exports = router; |