StMarkSchoolApplication / app / controller / usersListController.js
usersListController.js
Raw
const db = require('../config/db.config.js');
const User = db.user;
const Role = db.role;

/** TESTED */
exports.allStudents = (req, res) => {
	User.findAll({
        attributes: ['username', 'phonenumber','email'],
        include: [{
            model: Role,
            where: { name: 'STUDENT'},
            through:{
                attributes:[]
            }
        }]
	}).then(users => {
		res.status(200).json({
			"description": "User Content Page",
			"users": users    // retern [] // Doesn't retern any user
		});
	}).catch(err => {
		res.status(500).json({
			"description": "Can not access User Page",
			"error": err
		});
	})
}

/** TESTED */
exports.adminBoard = (req, res) => {
	User.findOne({
		where: {id: req.userId},
		attributes: ['username', 'email'],
		include: [{
			model: Role,
			attributes: ['id', 'name'],
			through: {
				attributes: ['userId', 'roleId'],
			}
		}]
	}).then(user => {
		res.status(200).json({
			"description": "Admin Board",
			"user": user
		});
	}).catch(err => {
		res.status(500).json({
			"description": "Can not access Admin Board",
			"error": err
		});
	})
}
/** TESTED */
exports.managementBoard = (req, res) => {
	User.findOne({
		where: {id: req.userId},
		attributes: ['username', 'email'],
		include: [{
			model: Role,
			attributes: ['id', 'name'],
			through: {
				attributes: ['userId', 'roleId'],
			}
		}]
	}).then(user => {
		res.status(200).json({
			"description": "Management Board",
			"user": user
		});
	}).catch(err => {
		res.status(500).json({
			"description": "Can not access Management Board",
			"error": err
		});
	})
}