Skip to content

Commit

Permalink
Bug: Reset password fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
BHS-Harish committed Jul 28, 2024
1 parent 0a946e5 commit 8058502
Show file tree
Hide file tree
Showing 52 changed files with 854 additions and 748 deletions.
1 change: 1 addition & 0 deletions backend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ EMAIL_HOST=smtp.gmail.com
CLUSTER=no
JWT_RESET_PASSWORD_EXPIRES_IN=1h
LOCAL_DEV_ENV=http://localhost:3500/
FRONTEND_URL=https://hitk-tech-community.netlify.app/
2 changes: 1 addition & 1 deletion backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const responseTime = require('response-time');
const cookieParser = require('cookie-parser');
const routes = require('./app/routes');
const { errorHandler } = require('./helpers/error');
const cookieParser = require('cookie-parser');
require('colors');
require('./helpers/dbConnection');

Expand Down
6 changes: 3 additions & 3 deletions backend/app/routes/Q&A/answers/@validationSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const getAnswerValidationSchema = Joi.object().keys({
});

const updateAnswerStatusSchema = Joi.object().keys({
id : Joi.string().min(24).max(24).required(),
status : Joi.boolean().required()
id: Joi.string().min(24).max(24).required(),
status: Joi.boolean().required(),
});

module.exports = {
answerValidationSchema,
getAnswerValidationSchema,
updateAnswerStatusSchema
updateAnswerStatusSchema,
};
2 changes: 1 addition & 1 deletion backend/app/routes/Q&A/answers/deleteAnswer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require('mongoose');
const Answer = require('../../../models/answers');

module.exports = async (req, res, next) => {
module.exports = async (req, res) => {
try {
const payload = res.locals.decode;
const { answerId } = req.body;
Expand Down
2 changes: 1 addition & 1 deletion backend/app/routes/Q&A/answers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const router = require('express').Router({ mergeParams: true });
const { answerValidationSchema, getAnswerValidationSchema, updateAnswerStatusSchema } = require('./@validationSchema');
const { answerValidationSchema, updateAnswerStatusSchema } = require('./@validationSchema');
const validation = require('../../../../helpers/middlewares/validation');

const postAnswer = require('./post');
Expand Down
74 changes: 37 additions & 37 deletions backend/app/routes/Q&A/answers/updateAnswerStatus.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
const to = require("await-to-js").default;
const to = require('await-to-js').default;

const constants = require("../../../../constants");
const { ErrorHandler } = require("../../../../helpers/error");
const constants = require('../../../../constants');
const { ErrorHandler } = require('../../../../helpers/error');

const answers = require("../../../models/answers");
const answers = require('../../../models/answers');

module.exports = async (req, res, next) => {
// getting id and status from body
const id = req.body.id;
const status = req.body.status

// query fro updating
const [ err, result ] = await to(answers.findOneAndUpdate({ _id : id }, { $set : { isApproved : status } }));

// error occured due to the some problem
if(err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: err,
});

return next(error);
}

// if result is null that means answer with given id is not exist in collection
if(result === null) {
const answerNotExistError = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: 'Answer Not Exist...',
});

return next(answerNotExistError);
}

// success response
res.status(200).send({
message : "Status Updated..."
// getting id and status from body
const { id } = req.body;
const { status } = req.body;

// query fro updating
const [err, result] = await to(answers.findOneAndUpdate({ _id: id }, { $set: { isApproved: status } }));

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

// error occured due to the some problem
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: err,
});

return next();
}
return next(error);
}

// if result is null that means answer with given id is not exist in collection
if (result === null) {
const answerNotExistError = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: 'Answer Not Exist...',
});

return next(answerNotExistError);
}

// success response
res.status(200).send({
message: 'Status Updated...',
});

return next();
};
4 changes: 2 additions & 2 deletions backend/app/routes/Q&A/question/@validationSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const QuestionValidationSchema = Joi.object().keys({
});

const updateQuestionStatusSchema = Joi.object().keys({
id : Joi.string().min(24).max(24).required(),
status : Joi.boolean().required()
id: Joi.string().min(24).max(24).required(),
status: Joi.boolean().required(),
});

module.exports = { QuestionValidationSchema, updateQuestionStatusSchema };
2 changes: 1 addition & 1 deletion backend/app/routes/Q&A/question/deleteQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mongoose = require('mongoose');
const Question = require('../../../models/question');
const Answer = require('../../../models/answers');

module.exports = async (req, res, next) => {
module.exports = async (req, res) => {
try {
const payload = res.locals.decode;
const { questionId } = req.body;
Expand Down
4 changes: 2 additions & 2 deletions backend/app/routes/Q&A/question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ router.get('/getallquestions', getAllQuestion);
router.get('/getQuestionById/:questionId', getQuestionById);

// This route will increase upvote by one.
router.patch('/upvote',checkVoteCookie, upvoteQuestion);
router.patch('/upvote', checkVoteCookie, upvoteQuestion);

// This route will decrease upvote by one.
router.patch('/downvote',checkVoteCookie, downvoteQuestion);
router.patch('/downvote', checkVoteCookie, downvoteQuestion);

// route for updating the question status
router.patch('/updateStatus', validation(updateQuestionStatusSchema), updateQuestionStatus);
Expand Down
74 changes: 37 additions & 37 deletions backend/app/routes/Q&A/question/updateQuestionStatus.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
const to = require("await-to-js").default;
const to = require('await-to-js').default;

const constants = require("../../../../constants");
const { ErrorHandler } = require("../../../../helpers/error");
const constants = require('../../../../constants');
const { ErrorHandler } = require('../../../../helpers/error');

const question = require("../../../models/question");
const question = require('../../../models/question');

module.exports = async (req, res, next) => {
// getting id and status from body
const id = req.body.id;
const status = req.body.status

// query fro updating
const [ err, result ] = await to(question.findOneAndUpdate({ _id : id }, { $set : { isApproved : status } }));

// error occured due to the some problem
if(err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: err,
});

return next(error);
}

// if result is null that means question with given id is not exist in collection
if(result === null) {
const questionNotExistsError = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: 'Question Not Exist...',
});

return next(questionNotExistsError);
}

// success response
res.status(200).send({
message : "Status Updated..."
// getting id and status from body
const { id } = req.body;
const { status } = req.body;

// query fro updating
const [err, result] = await to(question.findOneAndUpdate({ _id: id }, { $set: { isApproved: status } }));

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

// error occured due to the some problem
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: err,
});

return next();
}
return next(error);
}

// if result is null that means question with given id is not exist in collection
if (result === null) {
const questionNotExistsError = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: 'Question Not Exist...',
});

return next(questionNotExistsError);
}

// success response
res.status(200).send({
message: 'Status Updated...',
});

return next();
};
18 changes: 9 additions & 9 deletions backend/app/routes/admin/@validationSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ const forgotPasswordSchema = Joi.object({
const resetPasswordSchema = Joi.object({
newPassword: Joi.string().required(),
});
const updateAdminSchema =Joi.object({
firstName:Joi.string(),
lastName:Joi.string(),
contact:Joi.string().regex(/[+]91[6-9]{1}[0-9]{9}$/, 'phone'),
username:Joi.string(),
const updateAdminSchema = Joi.object({
firstName: Joi.string(),
lastName: Joi.string(),
contact: Joi.string().regex(/[+]91[6-9]{1}[0-9]{9}$/, 'phone'),
username: Joi.string(),
});

const deleteAdminSchema = Joi.object({
id : Joi.string().min(24).max(24).required()
})
id: Joi.string().min(24).max(24).required(),
});

module.exports = {
postSuperAdminSchema,
Expand All @@ -62,5 +62,5 @@ module.exports = {
forgotPasswordSchema,
resetPasswordSchema,
updateAdminSchema,
deleteAdminSchema
};
deleteAdminSchema,
};
68 changes: 34 additions & 34 deletions backend/app/routes/admin/deleteAdmin.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
const { default: to } = require("await-to-js");
const constants = require("../../../constants");
const { ErrorHandler } = require("../../../helpers/error");
const Admin = require("../../models/Admin");
const { default: to } = require('await-to-js');
const constants = require('../../../constants');
const { ErrorHandler } = require('../../../helpers/error');
const Admin = require('../../models/Admin');

module.exports = async (req, res, next) => {
const { isSuperAdmin } = res.locals.decode;
const { isSuperAdmin } = res.locals.decode;

if(!isSuperAdmin) {
const error = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 401,
message: 'Unauthorized Request: Not a superAdmin',
user: req.body.email,
});
if (!isSuperAdmin) {
const error = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 401,
message: 'Unauthorized Request: Not a superAdmin',
user: req.body.email,
});

return next(error);
}
return next(error);
}

const id = req.body.id;
const { id } = req.body;

const [err, admin] = await to(Admin.findByIdAndDelete(id));
const [err, admin] = await to(Admin.findByIdAndDelete(id));

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

if (!admin) {
const error = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: "Admin doesn't exist",
});
if (!admin) {
const error = new ErrorHandler(constants.ERRORS.INPUT, {
statusCode: 400,
message: "Admin doesn't exist",
});

return next(error);
}
return next(error);
}

if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Mongo Error: Deletion Failed',
errStack: err,
});
if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Mongo Error: Deletion Failed',
errStack: err,
});

return next(error);
}
return next(error);
}

return res.status(200).send({
message: 'Admin deleted successfully',
});
}
return res.status(200).send({
message: 'Admin deleted successfully',
});
};
Loading

0 comments on commit 8058502

Please sign in to comment.