-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (70 loc) · 2.03 KB
/
app.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
91
require('dotenv').config();
require('./db/conn');
const cron = require('node-cron');
const cors = require('cors');
const express = require('express');
const router = require('./routes/router');
const authRouter = require('./routes/authRouter');
const nodemailer = require('nodemailer');
const { getMailSubject } = require('./models/mailSubjects');
const { getMailBody } = require('./models/mailBody');
const bd = require('./models/birthdaySchema');
const app = express();
app.use(cors());
app.use(express.json());
app.use('/api/auth', authRouter);
app.use('/api', router);
const port = process.env.PORT;
const transporter = nodemailer.createTransport({
service:"gmail",
auth:{
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
const sendBirthdayWishMail = async(email, name, senderName) => {
const subject = getMailSubject(name);
const mailBody = getMailBody();
await transporter.sendMail({
from: '[email protected]',
to: email,
subject: subject,
html: `<p>Dear ${name},</p>
<br />
<p>${mailBody}</p>
<br>
<p>Warmest regards,</p>
<p>${senderName}</p>`
})
};
const checkTodaysBirthday = async () => {
// get todays date
const today = new Date();
const t_dd = today.getDate();
const t_mm = today.getMonth();
const birthdays = await bd.find({
$expr: {
$and: [
{ $eq: [{ $dayOfMonth: "$dob" }, t_dd] },
{ $eq: [{ $month: "$dob" }, t_mm + 1] }
]
}}).populate("userId");
const length = Object.keys(birthdays).length;
for(let i=0; i<length; i++) {
const { name, email, userId } = birthdays[i];
sendBirthdayWishMail(email, name, userId.name);
}
};
// checkTodaysBirthday();
// setInterval(() => {
// checkTodaysBirthday();
// }, 24 * 60 * 60 * 1000);
cron.schedule('0 0 2 * * *', function() {
checkTodaysBirthday();
});
// cron.schedule('0 */8 */2 * * *', function() {
// console.log("Running");
// });
app.listen(port, ()=>{
console.log(`Server running on port ::: ${port}`);
});