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

verifymentor api added #2

Merged
merged 1 commit into from
Nov 18, 2021
Merged
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
4 changes: 3 additions & 1 deletion constants/api-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ module.exports = {
"FILE_NOT_PROVIDED": "File not provided",
"FILE_UPLOADED_SUCCESSFULLY": "File uploaded successfully",
"SIGNED_URL_GENERATED_SUCCESSFULLY": "Signed Url Generated Successfully",
"INCORRECT_INTERNAL_ACCESS_TOKEN": "Invalid internal access token"
"INCORRECT_INTERNAL_ACCESS_TOKEN": "Invalid internal access token",
"USER_IS_A_MENTOR":"User has mentor access",
"USER_IS_NOT_A_MENTOR":"User does't have mentor access"
};
3 changes: 2 additions & 1 deletion constants/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
'/user/v1/systemUsers/login'
],
uploadUrls: [
'bulkCreateMentors'
'bulkCreateMentors',
'/user/v1/account/verifyMentor'
]
};
21 changes: 21 additions & 0 deletions controllers/v1/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,25 @@ module.exports = class Account {
return error;
}
}

/**
* Reset password
* @method
* @name verifyMentor
* @param {Object} req -request data.
* @returns {JSON} - verifies user is mentor or not
*/


async verifyMentor(req) {
try {
console.log("req.query",req.query);
const result = await accountHelper.verifyMentor(req.query.userId);
return result;
} catch (error) {
return error;
}
}


}
39 changes: 29 additions & 10 deletions middlewares/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,39 @@ const httpStatusCode = require('../generics/http-status');
const apiResponses = require('../constants/api-responses');
const common = require('../constants/common');

module.exports = (req, res, next) => {
module.exports = async function (req, res, next) {


if (!common.guestUrls.includes(req.url)) {

let internalAccess = false;
await Promise.all(common.uploadUrls.map(async function (path) {
if (req.path.includes(path)) {
if (req.headers.internal_access_token && process.env.INTERNAL_ACCESS_TOKEN == req.headers.internal_access_token) {
internalAccess =true;
}
}
}));
if (internalAccess == true) {
next();
return;
}
else if (!common.guestUrls.includes(req.url)) {



const authHeader = req.get('X-auth-token');
if (!authHeader) {
throw common.failureResponse({ message: apiResponses.UNAUTHORIZED_REQUEST, statusCode: httpStatusCode.unauthorized, responseCode: 'UNAUTHORIZED' });
}

let splittedUrl = req.url.split('/');

if (common.uploadUrls.includes(splittedUrl[splittedUrl.length - 1])) {
if (!req.headers.internal_access_token || process.env.INTERNAL_ACCESS_TOKEN !== req.headers.internal_access_token) {
throw common.failureResponse({ message: apiResponses.INCORRECT_INTERNAL_ACCESS_TOKEN, statusCode: httpStatusCode.unauthorized, responseCode: 'UNAUTHORIZED' });
}
}

// let splittedUrl = req.url.split('/');
// if (common.uploadUrls.includes(splittedUrl[splittedUrl.length - 1])) {
// if (!req.headers.internal_access_token || process.env.INTERNAL_ACCESS_TOKEN !== req.headers.internal_access_token) {
// throw common.failureResponse({ message: apiResponses.INCORRECT_INTERNAL_ACCESS_TOKEN, statusCode: httpStatusCode.unauthorized, responseCode: 'UNAUTHORIZED' });
// }
// }


const authHeaderArray = authHeader.split(' ');
if (authHeaderArray[0] !== 'bearer') {
Expand All @@ -45,6 +63,7 @@ module.exports = (req, res, next) => {
}

req.decodedToken = decodedToken.data;
}
}

next();
};
5 changes: 3 additions & 2 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ module.exports = (app) => {

}

app.all("/user/:version/:controller/:file/:method", validator, router);
app.all("/user/:version/:controller/:file/:method/:id", router);

app.all("/user/:version/:controller/:method", validator, router);
app.all("/user/:version/:controller/:method/:id", validator, router);
app.all("/user/:version/:controller/:file/:method", validator, router);
app.all("/user/:version/:controller/:file/:method/:id", router);

app.use((req, res, next) => {
res.status(404).json({
Expand Down
18 changes: 18 additions & 0 deletions services/helper/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,22 @@ module.exports = class AccountHelper {
}
})
}


static async verifyMentor(userId) {
try {

let user = await usersData.findOne({ '_id': userId },{ "isAMentor":1 });
if(!user){
return common.failureResponse({ message: apiResponses.USER_DOESNOT_EXISTS, statusCode: httpStatusCode.bad_request, responseCode: 'CLIENT_ERROR' });
} else if(user && user.isAMentor==true){
return common.successResponse({ statusCode: httpStatusCode.ok, message: apiResponses.USER_IS_A_MENTOR, result:user });
} else {
return common.successResponse({ statusCode: httpStatusCode.ok, message: apiResponses.USER_IS_NOT_A_MENTOR, result:user });
}

} catch(error) {
throw error;
}
}
}