-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubscriptionsvc.js
More file actions
42 lines (33 loc) · 2.09 KB
/
Copy pathsubscriptionsvc.js
File metadata and controls
42 lines (33 loc) · 2.09 KB
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
var telegramBot = require('./util/telegramBot').bot
var markdown = require('./util/telegramBot').nopreview_markdown_opts
var database = require('./database')
database.connect()
var UserModel = require('./models/User')
var moment = require('moment')
var run = () => {
console.log('Running Subscription Service')
checkSubscriptionExpDate().then(() => {
process.exit()
})
}
function checkSubscriptionExpDate() {
var days5Ago = moment().add(-5, 'days').format('YYYY-MM-DD')
return UserModel.find({ $and: [{ $where: "this.settings.ittTransactions.length > 0" }, { 'settings.subscriptions.paid': { $gte: days5Ago } }, { 'settings.staking.diecimila': false }] })
.then(users => {
var notifications = []
users.filter(u => {
var daysLeft = Math.abs(moment(u.settings.subscriptions.paid).diff(moment(), 'days'))
return (daysLeft <= 5 && daysLeft % 2 == 1) || daysLeft == 0
}).map(u => {
var inNDays = moment(u.settings.subscriptions.paid).diff(moment(), 'days') == 0 ? 'today' : moment(u.settings.subscriptions.paid).fromNow()
var expiringSubscriptionMessage = ''
if (inNDays.includes('ago'))
expiringSubscriptionMessage = '❌ *Your subscription has expired!*\n\n/subscribe to regain access to your premium alerts.\n[Visit the pricing page](https://intelligenttrading.org/pricing/) to view all plan features.'
else
expiringSubscriptionMessage = `⚠️ *Your plan expires ${(inNDays == 'in a day' ? 'tomorrow' : inNDays)}.*\n\/subscribe now to extend your subscription${inNDays == 'today' || inNDays == 'tomorrow' ? ', or you will lose access to all of your alerts' : ''}.\n[Visit the pricing page](https://intelligenttrading.org/pricing/) to view all plan features.`
notifications.push(telegramBot.sendMessage(u.telegram_chat_id, expiringSubscriptionMessage, markdown))
})
return Promise.all(notifications).catch(err => console.log(err))
}).catch(err => console.log(err))
}
run()