properly making add email from landing page work
This commit is contained in:
@@ -1,28 +1,54 @@
|
|||||||
const db = require('../../models'),
|
const db = require('../../models'),
|
||||||
errorMaker = require('../helpers/error.maker');
|
errorMaker = require('../helpers/error.maker');
|
||||||
|
|
||||||
const { Grant, Cause, Region, Organization, User } = db;
|
const { Grant, Cause, Region, Organization, User, sequelize } = db;
|
||||||
|
|
||||||
// POST an email into user general model from given email
|
// POST an email into user general model from given email
|
||||||
exports.addEmail = (req, res, next) => {
|
exports.addEmail = async (req, res, next) => {
|
||||||
User.findAll({
|
try {
|
||||||
where: {
|
const { email } = req.body;
|
||||||
email: req.body.email
|
|
||||||
}
|
const users = await sequelize.query(
|
||||||
})
|
`
|
||||||
.then(users => {
|
SELECT id
|
||||||
if (users.length >= 1) {
|
FROM users
|
||||||
return next(errorMaker(409, `Email Exists: ${req.body.email}`));
|
WHERE email = :email`,
|
||||||
} else {
|
{
|
||||||
User.create({
|
type: sequelize.QueryTypes.SELECT,
|
||||||
email: req.body.email
|
replacements: {
|
||||||
})
|
email
|
||||||
.then(donor => {
|
}
|
||||||
// Send created user to client
|
|
||||||
return res.status(201).json(donor);
|
|
||||||
})
|
|
||||||
.catch(error => next(error));
|
|
||||||
}
|
}
|
||||||
})
|
);
|
||||||
.catch(error => next(error));
|
|
||||||
|
if (users.length > 0) return next(errorMaker(409, 'Email already exists!'));
|
||||||
|
|
||||||
|
const currDate = new Date();
|
||||||
|
await sequelize.query(
|
||||||
|
`
|
||||||
|
INSERT INTO users
|
||||||
|
(
|
||||||
|
email,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
:email,
|
||||||
|
:curr_date,
|
||||||
|
:curr_date
|
||||||
|
)`,
|
||||||
|
{
|
||||||
|
type: sequelize.QueryTypes.INSERT,
|
||||||
|
replacements: {
|
||||||
|
email,
|
||||||
|
curr_date: currDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.status(200).json({ message: 'Successfully signed up' });
|
||||||
|
} catch (e) {
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user