Skip to content

Commit

Permalink
Merge ffce63b into 617775b
Browse files Browse the repository at this point in the history
  • Loading branch information
higustave-ops committed Jan 13, 2020
2 parents 617775b + ffce63b commit fbec582
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 371 deletions.
2 changes: 1 addition & 1 deletion src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AuthController {
* @returns {object} ResponseService
*/
static async verifyAccount(req, res) {
const userToken = JwtService.verifyToken(req.token);
const userToken = req.userData;
const isUserRegistered = await UserService.findUserByProperty({ email: userToken.email });
if (isUserRegistered && (isUserRegistered.isVerified) === true) {
ResponseService.setError(400, 'Can\'t reverify this account. Account already verified.');
Expand Down
58 changes: 0 additions & 58 deletions src/controllers/profile-settings.controller.js

This file was deleted.

33 changes: 33 additions & 0 deletions src/controllers/settings.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import ResponseService from '../services/response.service';
import UserService from '../services/user.service';
/**
*
*
* @class SettingsController
*/
class SettingsController {
/**
* super_admin can assign a role to user by email
* @static
* @description PATCH /api/admin/reset-user-role
* @param {object} req request object
* @param {object} res response object
* @memberof UserRoleSettingsController
* @returns {object} ResponseService
*/
static async changeUserRole(req, res) {
const { userEmail, userRole } = req.body;
const findUser = await UserService.findUserByProperty({ email: userEmail.trim() });
if (findUser && findUser.role !== 'super_admin') {
await UserService.updateUser({ email: userEmail.trim() }, { role: userRole.trim() });
const data = await UserService.findUserByProperty({ email: userEmail.trim() });
const { role } = data;
ResponseService.setSuccess(200, 'User role successfully updated', { role });
ResponseService.send(res);
} else {
ResponseService.setError(404, `User role not updated. ${userEmail.trim()} is either a super admin or Not Registered`);
ResponseService.send(res);
}
}
}
export default SettingsController;
10 changes: 10 additions & 0 deletions src/middlewares/auth.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const authMiddleware = {
ResponseService.setError(401, 'No Token supplied');
return ResponseService.send(res);
}
},

verifyIfUserIsAdmin: async (req, res, next) => {
const userToken = req.userData;
const userData = await UserService.findUserByProperty({ email: userToken.email });
if (userData.role !== 'super_admin') {
ResponseService.setError(403, 'Only super admin can reset user role');
return ResponseService.send(res);
}
next();
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/migrations/20191218075931-create-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ export function up(queryInterface, Sequelize) {
defaultValue: 'requester',
},
profilePicture: {
type: Sequelize.STRING(1234),
allowNull: true
allowNull: true,
type: Sequelize.STRING(1234)
},
isVerified: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
token: {
allowNull: true,
type: Sequelize.STRING
type: Sequelize.STRING(1234)
},
createdAt: {
allowNull: false,
Expand Down
4 changes: 2 additions & 2 deletions src/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default (sequelize, DataTypes) => {
lineManagerId: DataTypes.INTEGER,
role: DataTypes.ENUM('super_admin', 'travel_admin', 'travel_team_member', 'manager', 'requester'),
profilePicture: DataTypes.STRING(1234),
token: DataTypes.STRING,
isVerified: DataTypes.BOOLEAN,
token: DataTypes.STRING(1234),
isVerified: DataTypes.BOOLEAN
}, {});
Users.associate = (models) => {
// associations can be defined here
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ router.get('/facebook/redirect', passport.authenticate('facebook', { session: fa
router.get('/protected', checkUserLoggedIn, (req, res) => {
res.status(200).send('Welcome to the protected route');
});
router.patch('/user/verify', RouteAccessMiddleware.checkRouteAccess, validateToken, AuthController.verifyAccount);
router.patch('/user/verify', RouteAccessMiddleware.checkRouteAccess, validateToken, authMiddleware.checkUserLoggedIn, AuthController.verifyAccount);
router.patch('/user/resendLink', validateResendVerificationLink, AuthController.resendAccVerificationLink);
export default router;
4 changes: 2 additions & 2 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import express from 'express';
import authRoute from './auth.route';
import tripRoute from './trip.route';
import profileSettingsRoute from './profile-settings.route';
import accommodationRoute from './accommodation.route';
import notificationRoute from './notification.route';
import settingsRoute from './settings.route';

const app = express();

app.use('/api/auth', authRoute);
app.use('/api/', tripRoute);
app.use('/api/accommodations', accommodationRoute);
app.use('/api/users', profileSettingsRoute);
app.use('/api/notifications', notificationRoute);
app.use('/api/users/settings', settingsRoute);

export default app;
11 changes: 0 additions & 11 deletions src/routes/profile-settings.route.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/routes/settings.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from 'express';
import authMiddleware from '../middlewares/auth.middleware';
import SettingsController from '../controllers/settings.controller';
import { validateUserRole } from '../validations/role.validation';

const router = express.Router();

router.patch('/reset-roles', authMiddleware.checkUserLoggedIn, validateUserRole, authMiddleware.verifyIfUserIsAdmin, SettingsController.changeUserRole);

export default router;
53 changes: 53 additions & 0 deletions src/swagger/settings.swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @swagger
* definitions:
* Super Admin Set or Reset User Role:
* type: object
* properties:
* userEmail:
* type: string
* format: email
* userRole:
* type: string
*/

/**
* @swagger
* /api/users/settings/reset-roles:
* patch:
* tags:
* - User
* name: Set or Reset User Role
* summary: Edit User Role By Super Admin
* produces:
* - application/json
* consumes:
* - application/json
* parameters:
* - name: authorization
* in: header
* schema:
* type: string
* - name: body
* in: body
* schema:
* $ref: '#/definitions/Super Admin Set or Reset User Role'
* type: object
* properties:
* userEmail:
* type: string
* userRole:
* type: string
* responses:
* '200':
* description: User Role Updated successfully.
* '400':
* description: Bad request.
* '401':
* description: No Token Supplied.
* '403':
* description: Forbiden.
* '404':
* description: User Not Found
*
*/
92 changes: 0 additions & 92 deletions src/swagger/user-profile-settings.swagger.js

This file was deleted.

Loading

0 comments on commit fbec582

Please sign in to comment.