Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#168429357 get a specific request #72

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions src/controllers/requestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
serverError, unauthorizedUserRequest, noRequests, acceptedTripRequest, rejectedTripRequest
} = messages;
const {
create, getAll, bulkCreate, update
create, getAll, bulkCreate, update, getByOptions, getById
} = DbServices;

/**
Expand Down Expand Up @@ -94,12 +94,18 @@ const requestTrip = async (req, res) => {
*/
const getUserRequest = async (req, res) => {
try {
const { decoded: { id, roleId }, query: { page, perPage, userId, approvalStatus } } = req;
const {
decoded: { id, roleId }, query: {
page, perPage, userId, approvalStatus
}
} = req;
if (userId && (userId !== id && roleId !== roles.SUPER_ADMIN)) {
return response(res, 403, 'error', { message: unauthorizedUserRequest });
}
const { limit, offset } = calculateLimitAndOffset(page, perPage);
const options = { where: { userId: userId || id }, order: [['updatedAt', 'ASC']], limit, offset };
const options = {
where: { userId: userId || id }, order: [['updatedAt', 'ASC']], limit, offset
};
if (approvalStatus) {
options.where.approvalStatus = approvalStatus;
}
Expand Down Expand Up @@ -236,11 +242,51 @@ const updateTripRequest = async (req, res) => {
}
};

/**
* @function getSpecificRequest
* @param {Object} req - server request
* @param {Object} res - server response
* @returns {Object} - custom response
*/
const getSpecificRequest = async (req, res) => {
try {
const {
decoded: { id, roleId }, params: { requestId }
} = req;

const options = {
where: { id: requestId },
include: [{
model: Subrequest,
as: 'Subrequests'
}]
};
const { dataValues, dataValues: { userId } } = await getByOptions(Request, options);
const data = dataValues;

const authorizedUser = id === userId;
const superAdmin = roleId === roles.SUPER_ADMIN;
const manager = roleId === roles.MANAGER;

if (!(authorizedUser || superAdmin || manager)) return response(res, 401, 'error', { message: messages.unauthorized });

if (manager) {
const { dataValues: { lineManager } } = await getById(User, userId, { attributes: ['lineManager'] });
if (id !== lineManager) return response(res, 401, 'error', { message: messages.unauthorized });
}
return response(res, 200, 'success', data);
} catch (error) {
return response(res, 500, 'error', serverError);
}
};


export default {
requestTrip,
getUserRequest,
searchRequest,
updateApprovalStatus,
getManagerRequest,
updateTripRequest
updateTripRequest,
getSpecificRequest
};
10 changes: 10 additions & 0 deletions src/database/seeders/20190819153902-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ export default {
phoneNo: '2347032444332',
lineManager: '38eb202c-3f67-4eed-b7ac-9c31bc226e0c',
verified: true
},
{
id: '942d754a-e723-4f28-9238-402559925135',
firstName: 'line',
lastName: 'manager2',
email: 'linemanager2@gmail.com',
password: hashPassword('linemanager2'),
phoneNo: '2347033553727',
verified: true,
roleId: roles.MANAGER,
}
], {});
},
Expand Down
5 changes: 4 additions & 1 deletion src/models/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'userId',
as: 'User',
});
Request.hasMany(models.Subrequest);
Request.hasMany(models.Subrequest, {
foreignKey: 'requestId',
as: 'Subrequests',
});
};

return Request;
Expand Down
Loading