-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π Fixed: Every New Broadcast should be Broadcasted to Subscribers (#1128
) * Upvote and Downvotes fixed * Footer social icons adjusted * Sent mail on approving every broadcast to the subscribers
- Loading branch information
1 parent
34c3189
commit 6692aa6
Showing
4 changed files
with
86 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}) | ||
} | ||
|
||
|
@@ -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,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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters