1
1
const to = require ( 'await-to-js' ) . default ;
2
2
const Broadcast = require ( '../../models/Broadcast' ) ;
3
+ const Subscribers = require ( '../../models/Subscriber' ) ;
3
4
const { ErrorHandler } = require ( '../../../helpers/error' ) ;
4
5
const constants = require ( '../../../constants' ) ;
6
+ const nodemailer = require ( 'nodemailer' )
7
+ const config = require ( '../../../config' )
8
+ const { broadcastPublishMailTemplate } = require ( '../../../utility/emailTemplates' )
5
9
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 ) {
8
12
return res . status ( 200 ) . send ( {
9
- message : "Not Sufficient Data"
13
+ message : "Not Sufficient Data"
10
14
} )
11
15
}
12
16
@@ -15,11 +19,13 @@ module.exports = async (req, res, next) => {
15
19
} ;
16
20
17
21
delete data . id ;
22
+ let approving = data ?. approving
23
+ delete data ?. approving
18
24
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 } ) ) ;
20
26
21
27
// error occured due to the some problem
22
- if ( err ) {
28
+ if ( err ) {
23
29
const error = new ErrorHandler ( constants . ERRORS . DATABASE , {
24
30
statusCode : 500 ,
25
31
message : 'Database Error' ,
@@ -28,21 +34,65 @@ module.exports = async (req, res, next) => {
28
34
29
35
return next ( error ) ;
30
36
}
31
-
37
+
32
38
// if result is null that means broadcast with given id is not exist in collection
33
- if ( result === null ) {
39
+ if ( result === null ) {
34
40
const broadcastNotExistsError = new ErrorHandler ( constants . ERRORS . INPUT , {
35
41
statusCode : 400 ,
36
42
message : 'Broadcast Not Exist...' ,
37
43
} ) ;
38
44
39
45
return next ( broadcastNotExistsError ) ;
40
46
}
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
+ }
45
86
} ) ;
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 ( ) ;
48
98
}
0 commit comments