-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (61 loc) · 2.33 KB
/
index.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
const express = require('express');
const app = express();
const cron = require('node-cron');
const routes = require('./routes/api');
const authRoutes = require('./routes/auth-routes');
const axios=require('axios');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
dotenv.config();
const cors = require('cors');
const User=require('./models/user-model');
//Connect to DB
mongoose.connect(process.env.DB_CONNECTION_STRING,
{ useNewUrlParser: true },
() => console.log('Connected to db'));
cron.schedule('00 22 01 * * *', ()=> {
/*
* Runs every day
* at 12:00:00 AM.
*/
console.log("Cron triggered");
User.find({},(err, users)=>{
users.forEach((user)=>{
let peopleList=user.peopleList;
peopleList.forEach((person)=>{
let cDob=new Date(person.dob);
let today=new Date();
if(cDob.getDate()===today.getDate()&& cDob.getMonth()===today.getMonth()){
console.log(`Happy Birthday ${person.name}`);
try {
let msg = `Happy Birthday ${person.name}`;
axios.get(`https://api.telegram.org/bot${process.env.BOT_TOKEN}/sendMessage?chat_id=@${user.channelId}&text=${msg}`).then(response => {
console.log("Telegram message sent");
person.lastSentDate=today;
console.log(peopleList);
User.updateOne({ email: user.email }, { $set: { peopleList: peopleList } }).then(() => {
console.log(peopleList);
});
}).catch(error => {
});
} catch {
next();
}
}
})
});
})
}
);
//Middlewares
app.use(express.json());
app.use(cors());
app.use('/api', routes);
app.use('/auth', authRoutes);
app.use((err, req, res, next) => {
res.status(422).send({ error: err.message });
})
app.listen(process.env.PORT || 5000, () => {
console.log("now listening to requests");
console.log(`${process.env.PORT || 5000}`);
});