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

added functionality of sending mail when new user is created and when… #1072

Open
wants to merge 1 commit into
base: main
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
5 changes: 5 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MONGO_DB_URL=<mongoDB_URL>

JWT_SECRET_KEY=<JWT_sceret>
JWT_EXPIRES_IN=6h
BASE_URL=https://community-website-backend.herokuapp.com
Expand All @@ -8,3 +9,7 @@ EMAIL_HOST=smtp.gmail.com
CLUSTER=no
JWT_RESET_PASSWORD_EXPIRES_IN=1h
LOCAL_DEV_ENV=http://localhost:3500/


EMAIL_PASS_FOR_NODEMAILER=<enter your gmail pass key>
PRANJALJAVIA marked this conversation as resolved.
Show resolved Hide resolved
EMAIL_ID_FOR_SENDING_MAIL=<enter mail id from which you have to send mail to user>
23 changes: 23 additions & 0 deletions backend/app/models/Subscriber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;

const SubscriberSchema = new Schema(
{
firstName: {
type: String,
trim: true,
},
lastName: {
type: String,
trim: true,
},
email: {
type: String,
trim: true,
unique: true,
},
},
{ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } }
);

module.exports = mongoose.model('Subscriber', SubscriberSchema);
9 changes: 9 additions & 0 deletions backend/app/routes/Subscriber/@validationSchema/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Joi = require('joi');

const SubscriberValidationSchema = Joi.object().keys({
firstName: Joi.string().trim().required(),
lastName: Joi.string().trim().required(),
email: Joi.string().trim().email().required(),
});

module.exports = SubscriberValidationSchema;
8 changes: 8 additions & 0 deletions backend/app/routes/Subscriber/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const router = require('express').Router({ mergeParams: true });
const SubscriberValidationSchema = require('./@validationSchema');
const validation = require('../../../helpers/middlewares/validation');
const postSubscriber = require('./post');

router.post('/', validation(SubscriberValidationSchema), postSubscriber);

module.exports = router;
61 changes: 61 additions & 0 deletions backend/app/routes/Subscriber/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const to = require('await-to-js').default;
const { ErrorHandler } = require('../../../helpers/error');
const constants = require('../../../constants');
const Subscriber = require('../../models/Subscriber');
const {SendEmailUsingNodeMailer} = require('../../../utility/sendEmail');
const {NewSubscriberMailTemplate} = require('../../../utility/emailTemplates');

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

const [findErr, existingSubscriber] = await to(Subscriber.findOne({ email: req.body.email }));

if(findErr) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: findErr,
});
return next(error);
}

if(existingSubscriber) {
const error = new ErrorHandler(constants.ERRORS.SUBSCRIBER_EXISTS, {
statusCode: 409,
message: 'Subscriber already exists',
});
return next(error);
}

const [createErr] = await to(Subscriber.create({ ...req.body }));
if(createErr){
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: createErr,
});
return next(error);
}

// sending email to newly added user using nodemailer
try{
await SendEmailUsingNodeMailer(
req.body.email,
'Welcome to HITE-TECH Community',
NewSubscriberMailTemplate(req.body.firstName),
);
}
catch(error){
const e = new ErrorHandler(constants.ERRORS.EMAIL, {
statusCode: 500,
message: 'NodeMailer Error',
errStack: error,
});
return next(e);
}

res.status(200).send({
message: 'New Subscriber has been added successfully',
});

return next();
}
32 changes: 32 additions & 0 deletions backend/app/routes/broadcast/postBroadcast.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const to = require('await-to-js').default;
const Broadcast = require('../../models/Broadcast');
const Subscriber = require('../../models/Subscriber');
const { ErrorHandler } = require('../../../helpers/error');
const constants = require('../../../constants');
const { SendEmailUsingNodeMailer } = require('../../../utility/sendEmail');
const { NewBroadCastMailTemplate } = require('../../../utility/emailTemplates');

module.exports = async (req, res, next) => {
const [err, { _id }] = await to(Broadcast.create({ ...req.body }));
Expand All @@ -13,6 +16,35 @@ module.exports = async (req, res, next) => {
});
return next(error);
}

const [err1, SubscriberList] = await to(Subscriber.find());
if(err1){
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Mongo Error: can not fetch subscriber list',
errStack: err,
});
return next(error);
}

try{
SubscriberList.map(async (subscriber) => {
await SendEmailUsingNodeMailer(
subscriber.email,
'New BroadCast Added',
NewBroadCastMailTemplate(subscriber),
)
})
}
catch(error){
const e = new ErrorHandler(constants.ERRORS.EMAIL, {
statusCode: 500,
message: 'NodeMailer Error',
errStack: error,
});
return next(e);
}

res.status(200).send({
message: 'Broadcast added successfully',
id: _id,
Expand Down
2 changes: 1 addition & 1 deletion backend/app/routes/contactUs/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { ErrorHandler } = require('../../../helpers/error');
const constants = require('../../../constants');
const contactUs = require('../../models/contactUs');
const Admin = require('../../models/Admin');
const sendEmail = require('../../../utility/sendEmail');
const {sendEmail} = require('../../../utility/sendEmail');
const { ContactUsMailTemplate } = require('../../../utility/emailTemplates');

module.exports = async (req, res, next) => {
Expand Down
2 changes: 2 additions & 0 deletions backend/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const question = require('./Q&A/question');
const answer = require('./Q&A/answers');
const teamMember = require('./teamMember');
const resource = require('./resources');
const subscriber = require('./Subscriber');

router.use('/admin', admin);
router.use('/auth', auth);
Expand All @@ -24,4 +25,5 @@ router.use('/joinUs', joinUs);
router.use('/teamMember', teamMember);
router.use('/', tinyURL);
router.use('/resources', resource);
router.use('/subscribers', subscriber);
module.exports = router;
3 changes: 2 additions & 1 deletion backend/constants/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"CRITICAL": "Critical Error",
"WARNING": "Warning",
"USER": "User ",
"UNEXPECTED": "Unexpected Error"
"UNEXPECTED": "Unexpected Error",
"SUBSCRIBER_EXISTS": "Subscriber already exists"
},
"PAGINATION_LIMIT": {
"GET_ADMINS": 3,
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"multer": "^1.4.4",
"nanoid": "^3.1.20",
"node-cron": "^3.0.0",
"nodemailer": "^6.5.0",
"nodemailer": "^6.9.13",
"response-time": "^2.3.2",
"url-exists": "^1.0.3"
},
Expand Down
Loading