-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsubscription.js
More file actions
43 lines (36 loc) · 972 Bytes
/
subscription.js
File metadata and controls
43 lines (36 loc) · 972 Bytes
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
const schedule = require('node-schedule');
const subscriptions = [
{
user_id: "12345",
next_payment_date: '2022-01-14 18:45',
credit_card_info: '**** 1234',
authorization_code: "43895743",
amount: 100,
retry_amount: 0
},
{
user_id: "123",
next_payment_date: '2022-01-15 18:45'
},
{
user_id: "12",
next_payment_date: '2022-01-16 18:45'
}
]
const job = schedule.scheduleJob('* * * * *', function(){
console.log('This is running!');
const subs = getAllSubscriptionsToday()
for (const sub of subs) {
const response = applySub(sub);
if (response.isSuccessful) {
sub.next_payment_date = thirtyDaysFromNow()
} else {
sub.next_payment_date = tomorrow()
sub.retry_amount += 1
if (sub.retry_amount > 5) {
removeFromPremiumAccount(sub.user_id);
sub.next_payment_date = null;
}
}
}
});