-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
91 lines (70 loc) · 2.75 KB
/
mailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const mongoose = require('mongoose')
const moment = require('moment')
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const msg91 = require('msg91')('247111AI4S9E1P5bea6b3a', 'OFAJMA', '4')
const DBPORT = process.env.MONGODB_URI || 'mongodb://heroku_glpd1cfw:[email protected]:57574/heroku_glpd1cfw'
mongoose.connect(DBPORT)
mongoose.Promise = global.Promise
var db = mongoose.connection
db.on('error', console.error.bind(console, 'MongoDB connection error:'))
// get list of all messgaes
var Machine = require('./models/machine')
function splitMulti(str, tokens){
var tempChar = tokens[0]; // We can use the first token as a temporary join character
for(var i = 1; i < tokens.length; i++){
str = str.split(tokens[i]).join(tempChar);
}
str = str.split(tempChar);
return str;
}
Machine.find({}).exec((err, result) => {
if (err) return console.error(err)
// filter messages for today
let todayMachines = result.filter(machine => moment(machine.reminder.next, 'DD MMM YYYY').isSame(moment(), 'day'))
// console.log(todayMachines)
let emails = []
todayMachines.forEach(machine => {
let email = {
// to: ['[email protected]'],
to: splitMulti(machine.supplier.email, [';',',']),
from: 'M.M. OFAJ Nagpur <[email protected]>', //
subject: `B.Q. for calibration of ${machine.name} from OFAJ`,
html: `<p>Kindly give B.Q. for calibration of ${machine.name}. \
<br>Thanking you! \
<br>With regards, \
<br>${machine.incharge.name} \
<br>${inchagephone}
<br>M.M. OFAJ Nagpur<p>`
}
emails.push(email)
})
console.log('Sending...', emails)
sgMail
.sendMultiple(emails)
.then(result => {
//Celebrate
console.log('Sent mails.')
console.log('Sending messages...')
todayMachines.forEach(machine => {
let mobileNo = splitMulti(machine.incharge.phone, [';',','])
// let mobileNo = ['+918208396310','+917767060939','+919011792002', '+919021735821']
let message = `Email sent for calibration of ${machine.name} to ${machine.supplier.name}. ${moment(machine.reminder.next, 'DD MMM YYYY').fromNow('days')} days left.`
msg91.send(mobileNo, message, function(err, response) {
if (err) console.log(err)
console.log('Sent message.')
})
})
})
.catch(error => {
//Log friendly error
console.error(error.toString())
//Extract error msg
// const { message, code, response } = error
//Extract response msg
// const { headers, body } = response
})
mongoose.disconnect()
})
// create array of objects as shonwn below
let sparesnotificationtext = `${name.spare} is waiting for ${nextstage} since the last ${timeout} days. Please check.`