diff --git a/backend/app/routes/broadcast/@validationSchema/index.js b/backend/app/routes/broadcast/@validationSchema/index.js index 934ef07f..45962dad 100644 --- a/backend/app/routes/broadcast/@validationSchema/index.js +++ b/backend/app/routes/broadcast/@validationSchema/index.js @@ -20,7 +20,8 @@ const updateBroadcastValidationSchema = Joi.object().keys({ imageUrl: Joi.array().min(1).items(Joi.string().uri()), tags: Joi.array().min(1).items(Joi.string()), isApproved: Joi.boolean().required(), - id : Joi.string().min(24).max(24).required() + id : Joi.string().min(24).max(24).required(), + approving:Joi.boolean() }); const getBroadcastsValidationSchema = Joi.object().keys({ diff --git a/backend/app/routes/broadcast/updateBroadcast.js b/backend/app/routes/broadcast/updateBroadcast.js index be508474..ba3e6b83 100644 --- a/backend/app/routes/broadcast/updateBroadcast.js +++ b/backend/app/routes/broadcast/updateBroadcast.js @@ -1,12 +1,16 @@ const to = require('await-to-js').default; const Broadcast = require('../../models/Broadcast'); +const Subscribers = require('../../models/Subscriber'); const { ErrorHandler } = require('../../../helpers/error'); const constants = require('../../../constants'); +const nodemailer = require('nodemailer') +const config = require('../../../config') +const { broadcastPublishMailTemplate } = require('../../../utility/emailTemplates') -module.exports = async (req, res, next) => { - if(Object.keys(req.body).length <= 1) { +module.exports = async (req, res, next) => { + if (Object.keys(req.body).length <= 1) { return res.status(200).send({ - message : "Not Sufficient Data" + message: "Not Sufficient Data" }) } @@ -15,11 +19,13 @@ module.exports = async (req, res, next) => { }; delete data.id; + let approving = data?.approving + delete data?.approving - const [err, result] = await to(Broadcast.findOneAndUpdate({ _id : req.body.id }, { $set : data })); + const [err, result] = await to(Broadcast.findOneAndUpdate({ _id: req.body.id }, { $set: data })); // error occured due to the some problem - if(err) { + if (err) { const error = new ErrorHandler(constants.ERRORS.DATABASE, { statusCode: 500, message: 'Database Error', @@ -28,9 +34,9 @@ module.exports = async (req, res, next) => { return next(error); } - + // if result is null that means broadcast with given id is not exist in collection - if(result === null) { + if (result === null) { const broadcastNotExistsError = new ErrorHandler(constants.ERRORS.INPUT, { statusCode: 400, message: 'Broadcast Not Exist...', @@ -38,11 +44,55 @@ module.exports = async (req, res, next) => { return next(broadcastNotExistsError); } - - // success response - res.status(200).send({ - message : "Broadcast Updated..." + var subscribers; + if (approving && data?.isApproved == true) { + const transporter = nodemailer.createTransport({ + type: 'SMTP', + host: config.EMAIL_HOST, + secure: true, + debug: true, + port: 465, + auth: { + user: config.EMAIL_USER, + pass: config.EMAIL_PASS, + }, + }); + subscribers = await Subscribers.find(); + subscribers = subscribers.map((subscriber) => { return subscriber?.email }) + + const mailOptions = { + from: `HITK TECH Community <${config.EMAIL_USER}>`, + to: "hitktechcommunity@gmail.com", + subject: `New Broadcast: ${data?.title} 😍`, + html: broadcastPublishMailTemplate(data), + bcc: subscribers, + attachments: data?.imageUrl.map((image, index) => { + return { + filename: `${data?.title}${index+1}`, + path: image + } + }) + }; + await transporter.sendMail(mailOptions).catch((err) => { + if (err) { + const error = new ErrorHandler(constants.ERRORS.UNEXPECTED, { + statusCode: 500, + message: 'The server encountered an unexpected condition which prevented it from fulfilling the request.', + errStack: err, + user: req.body.email, + }); + throw error; + } }); - - return next(); + } + + + + + // success response + res.status(200).send({ + message: "Broadcast Updated...", + }); + + return next(); } \ No newline at end of file diff --git a/backend/utility/emailTemplates.js b/backend/utility/emailTemplates.js index 118e8382..6c7c4118 100644 --- a/backend/utility/emailTemplates.js +++ b/backend/utility/emailTemplates.js @@ -119,4 +119,22 @@ module.exports.welcomeSubscriberMailTemplate=()=>{ The HITK Tech Community Team ` return emailContent +} + +module.exports.broadcastPublishMailTemplate=(data)=>{ + const emailContent=` +