initial flow of sending email
This commit is contained in:
@@ -2,9 +2,12 @@ const db = require('../../models'),
|
||||
bcrypt = require('bcrypt-nodejs'),
|
||||
jwt = require('jsonwebtoken'),
|
||||
errorMaker = require('../helpers/error.maker'),
|
||||
roles = require('../helpers/roles');
|
||||
roles = require('../helpers/roles'),
|
||||
uuidv4 = require('uuid/v4');
|
||||
|
||||
const { Donor, Organization } = db;
|
||||
const sendgrid = require('./sendgrid.controller');
|
||||
|
||||
const { Donor, Organization, sequelize } = db;
|
||||
|
||||
// Find a Donor/Org by email + login with JWT
|
||||
exports.donorLogin = (req, res, next) => {
|
||||
@@ -77,3 +80,77 @@ exports.orgLogin = (req, res, next) => {
|
||||
})
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
exports.resetPasswordEmail = async (req, res, next) => {
|
||||
try {
|
||||
const { email, usertype } = req.query;
|
||||
|
||||
var user;
|
||||
if (usertype == 'donor') {
|
||||
user = await sequelize.query(
|
||||
`
|
||||
SELECT id FROM donors
|
||||
WHERE email = :email`,
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
replacements: { email }
|
||||
}
|
||||
);
|
||||
} else {
|
||||
user = await sequelize.query(
|
||||
`
|
||||
SELECT id FROM organizations
|
||||
WHERE email = :email`,
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
replacements: { email }
|
||||
}
|
||||
);
|
||||
}
|
||||
user = user[0];
|
||||
|
||||
if (!user)
|
||||
return res.status(400).json({
|
||||
message: 'User not found'
|
||||
});
|
||||
|
||||
const CODE = uuidv4();
|
||||
const currDate = new Date();
|
||||
|
||||
sequelize.query(
|
||||
`
|
||||
INSERT INTO password_reset
|
||||
(
|
||||
code,
|
||||
user_id,
|
||||
user_type,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
:code,
|
||||
:user_id,
|
||||
:user_type,
|
||||
:curr_date,
|
||||
:curr_date
|
||||
)`,
|
||||
{
|
||||
type: sequelize.QueryTypes.INSERT,
|
||||
replacements: {
|
||||
code: CODE,
|
||||
user_id: user.id,
|
||||
user_type: usertype,
|
||||
curr_date: currDate
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
sendgrid.passwordResetEmail(email, CODE);
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: 'Successfuly sent password reset email!' });
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user