Skip to content

Commit

Permalink
πŸ€ Fixed: Every New Broadcast should be Broadcasted to Subscribers (#1128
Browse files Browse the repository at this point in the history
)

* Upvote and Downvotes fixed

* Footer social icons adjusted

* Sent mail on approving every broadcast to the subscribers
  • Loading branch information
BHS-Harish authored Aug 10, 2024
1 parent 34c3189 commit 6692aa6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
3 changes: 2 additions & 1 deletion backend/app/routes/broadcast/@validationSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
76 changes: 63 additions & 13 deletions backend/app/routes/broadcast/updateBroadcast.js
Original file line number Diff line number Diff line change
@@ -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"
})
}

Expand All @@ -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',
Expand All @@ -28,21 +34,65 @@ 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...',
});

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: "[email protected]",
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();
}
18 changes: 18 additions & 0 deletions backend/utility/emailTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,22 @@ module.exports.welcomeSubscriberMailTemplate=()=>{
The HITK Tech Community Team
`
return emailContent
}

module.exports.broadcastPublishMailTemplate=(data)=>{
const emailContent=`
<h2>Hello there</h2>
<h3>${data?.title}</h3>
${data?.content}
<a href="${data?.link}" target="_blank">Click here</a>
<br/>
For more resource <a href="https://hitk-tech-community.netlify.app/broadcasts" target="_blank">See all broadcasts</a>
<br/>
<br/>
Best regards<br/>
<span style="font-weight:bold;">The HITK Tech Community</span>
`;

return emailContent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function Card(props) {
tags: project.tags,
isApproved: true,
title: project.title,
approving: true,
};
const res = await UpdateBoardCast(data, setToast, toast);
if (res) {
Expand Down Expand Up @@ -167,9 +168,9 @@ export function Card(props) {
>
View Details
</button>

<div className={style["button-group"]}>
{!props?.project?.isApproved && (
{!props?.project?.isApproved && (
<button
className={style["button-approve"]}
onClick={handleApprove}
Expand Down

0 comments on commit 6692aa6

Please sign in to comment.