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

Fixed: Every New Broadcast should be Broadcasted to Subscribers #1128

Merged
merged 10 commits into from
Aug 10, 2024
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 @@
};

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 }));

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

// 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 @@

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),

Check failure

Code scanning / CodeQL

Client-side cross-site scripting High

HTML injection vulnerability due to
user-provided value
.
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;
}
3 changes: 1 addition & 2 deletions frontend/src/components/Footer/footer.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@
color: #dd2a7b;
}


.fa-envelope:hover,
.fa-envelope-own:hover {
color: #c71610;
Expand Down Expand Up @@ -703,7 +702,7 @@ a > span {
}
}

@media screen and (max-width: 510px) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change shouldn't have come here, make sure to pull the latest master, rest looks fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its okay, we are all learning 😊

@media screen and (max-width: 1124px) {
.col .social {
display: grid;
grid-template-columns: 50% 50%;
Expand Down
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
Loading