Skip to content

Commit 1ad17fc

Browse files
committed
Sent mail on approving every broadcast to the subscribers
1 parent 2973a13 commit 1ad17fc

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
lines changed

β€Žbackend/app/routes/broadcast/@validationSchema/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const updateBroadcastValidationSchema = Joi.object().keys({
2020
imageUrl: Joi.array().min(1).items(Joi.string().uri()),
2121
tags: Joi.array().min(1).items(Joi.string()),
2222
isApproved: Joi.boolean().required(),
23-
id : Joi.string().min(24).max(24).required()
23+
id : Joi.string().min(24).max(24).required(),
24+
approving:Joi.boolean()
2425
});
2526

2627
const getBroadcastsValidationSchema = Joi.object().keys({
Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const to = require('await-to-js').default;
22
const Broadcast = require('../../models/Broadcast');
3+
const Subscribers = require('../../models/Subscriber');
34
const { ErrorHandler } = require('../../../helpers/error');
45
const constants = require('../../../constants');
6+
const nodemailer = require('nodemailer')
7+
const config = require('../../../config')
8+
const { broadcastPublishMailTemplate } = require('../../../utility/emailTemplates')
59

6-
module.exports = async (req, res, next) => {
7-
if(Object.keys(req.body).length <= 1) {
10+
module.exports = async (req, res, next) => {
11+
if (Object.keys(req.body).length <= 1) {
812
return res.status(200).send({
9-
message : "Not Sufficient Data"
13+
message: "Not Sufficient Data"
1014
})
1115
}
1216

@@ -15,11 +19,13 @@ module.exports = async (req, res, next) => {
1519
};
1620

1721
delete data.id;
22+
let approving = data?.approving
23+
delete data?.approving
1824

19-
const [err, result] = await to(Broadcast.findOneAndUpdate({ _id : req.body.id }, { $set : data }));
25+
const [err, result] = await to(Broadcast.findOneAndUpdate({ _id: req.body.id }, { $set: data }));
2026

2127
// error occured due to the some problem
22-
if(err) {
28+
if (err) {
2329
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
2430
statusCode: 500,
2531
message: 'Database Error',
@@ -28,21 +34,65 @@ module.exports = async (req, res, next) => {
2834

2935
return next(error);
3036
}
31-
37+
3238
// if result is null that means broadcast with given id is not exist in collection
33-
if(result === null) {
39+
if (result === null) {
3440
const broadcastNotExistsError = new ErrorHandler(constants.ERRORS.INPUT, {
3541
statusCode: 400,
3642
message: 'Broadcast Not Exist...',
3743
});
3844

3945
return next(broadcastNotExistsError);
4046
}
41-
42-
// success response
43-
res.status(200).send({
44-
message : "Broadcast Updated..."
47+
var subscribers;
48+
if (approving && data?.isApproved == true) {
49+
const transporter = nodemailer.createTransport({
50+
type: 'SMTP',
51+
host: config.EMAIL_HOST,
52+
secure: true,
53+
debug: true,
54+
port: 465,
55+
auth: {
56+
user: config.EMAIL_USER,
57+
pass: config.EMAIL_PASS,
58+
},
59+
});
60+
subscribers = await Subscribers.find();
61+
subscribers = subscribers.map((subscriber) => { return subscriber?.email })
62+
63+
const mailOptions = {
64+
from: `HITK TECH Community <${config.EMAIL_USER}>`,
65+
66+
subject: `New Broadcast: ${data?.title} 😍`,
67+
html: broadcastPublishMailTemplate(data),
68+
bcc: subscribers,
69+
attachments: data?.imageUrl.map((image, index) => {
70+
return {
71+
filename: `${data?.title}${index+1}`,
72+
path: image
73+
}
74+
})
75+
};
76+
await transporter.sendMail(mailOptions).catch((err) => {
77+
if (err) {
78+
const error = new ErrorHandler(constants.ERRORS.UNEXPECTED, {
79+
statusCode: 500,
80+
message: 'The server encountered an unexpected condition which prevented it from fulfilling the request.',
81+
errStack: err,
82+
user: req.body.email,
83+
});
84+
throw error;
85+
}
4586
});
46-
47-
return next();
87+
}
88+
89+
90+
91+
92+
// success response
93+
res.status(200).send({
94+
message: "Broadcast Updated...",
95+
});
96+
97+
return next();
4898
}

β€Žbackend/utility/emailTemplates.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,22 @@ module.exports.welcomeSubscriberMailTemplate=()=>{
119119
The HITK Tech Community Team
120120
`
121121
return emailContent
122+
}
123+
124+
module.exports.broadcastPublishMailTemplate=(data)=>{
125+
const emailContent=`
126+
<h2>Hello there</h2>
127+
<h3>${data?.title}</h3>
128+
${data?.content}
129+
<a href="${data?.link}" target="_blank">Click here</a>
130+
<br/>
131+
For more resource <a href="https://hitk-tech-community.netlify.app/broadcasts" target="_blank">See all broadcasts</a>
132+
<br/>
133+
<br/>
134+
Best regards<br/>
135+
<span style="font-weight:bold;">The HITK Tech Community</span>
136+
137+
`;
138+
139+
return emailContent;
122140
}

β€Žfrontend/src/pages/Admin/Components/Broadcast/ManageBroadcasts/Card/Card.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function Card(props) {
6060
tags: project.tags,
6161
isApproved: true,
6262
title: project.title,
63+
approving: true,
6364
};
6465
const res = await UpdateBoardCast(data, setToast, toast);
6566
if (res) {
@@ -167,9 +168,9 @@ export function Card(props) {
167168
>
168169
View Details
169170
</button>
170-
171+
171172
<div className={style["button-group"]}>
172-
{!props?.project?.isApproved && (
173+
{!props?.project?.isApproved && (
173174
<button
174175
className={style["button-approve"]}
175176
onClick={handleApprove}

0 commit comments

Comments
Β (0)