forked from worknenjoy/gitpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.js
166 lines (158 loc) · 4.71 KB
/
cron.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const CronJob = require('cron').CronJob
const models = require('./models')
const moment = require('moment')
const i18n = require('i18n')
const DeadlineMail = require('./modules/mail/deadline')
const TaskMail = require('./modules/mail/task')
const OrderDetails = require('./modules/orders/orderDetails')
const OrderCancel = require('./modules/orders/orderCancel')
i18n.configure({
directory: process.env.NODE_ENV !== 'production' ? `${__dirname}/locales` : `${__dirname}/locales/result`,
locales: process.env.NODE_ENV !== 'production' ? ['en'] : ['en', 'br'],
defaultLocale: 'en',
updateFiles: false
})
i18n.init()
const TaskCron = {
weeklyBounties: async () => {
const tasks = await models.Task.findAll({ where: {
value: {
$gt: 0
},
assigned: {
$eq: null
},
status: {
$eq: 'open'
}
},
include: [ models.User ]
})
// eslint-disable-next-line no-console
console.log('tasks from cron job weekly bounties', tasks)
if (tasks[0]) {
TaskMail.weeklyBounties({ tasks })
}
return tasks
},
latestTasks: async () => {
const tasks = await models.Task.findAll({
where: {
assigned: {
$eq: null
},
status: {
$eq: 'open'
}
},
limit: 5,
order: [['id', 'DESC']],
include: [ models.User ]
})
// eslint-disable-next-line no-console
console.log('tasks from cron job latest tasks', tasks)
if (tasks[0]) {
TaskMail.weeklyLatest({ tasks })
}
return tasks
},
rememberDeadline: async () => {
const tasks = await models.Task.findAll({ where: {
status: 'in_progress',
deadline: {
$lt: moment(new Date()).format(),
$gt: moment(new Date()).subtract(2, 'days').format()
}
},
include: [ models.User ]
})
// eslint-disable-next-line no-console
console.log('tasks from cron job to remember deadline', tasks)
if (tasks[0]) {
tasks.map(async t => {
if (t.assigned) {
if (t.dataValues && t.assigned) {
const userAssigned = await models.Assign.findAll({ where: { id: t.assigned }, include: [models.User] })
if (userAssigned[0].dataValues) {
DeadlineMail.deadlineEndOwner(t.User.dataValues, t.dataValues, t.User.name || t.User.username)
DeadlineMail.deadlineEndAssigned(userAssigned[0].dataValues.User, t.dataValues, userAssigned[0].dataValues.User.dataValues.name)
}
}
}
})
}
return tasks
}
}
const OrderCron = {
verify: async () => {
const orders = await models.Order.findAll({ where: {
amount: {
$gt: 0
},
status: {
$eq: 'succeeded'
},
provider: {
$eq: 'paypal'
}
},
include: [ models.User, models.Task ]
})
// eslint-disable-next-line no-console
console.log('orders from cron daily check for paypal payments verification', orders)
if (orders.length) {
let invalids = []
orders.forEach(async order => {
const orderValues = order.dataValues
// eslint-disable-next-line no-console
console.log('order values id', orderValues)
if (orderValues.source_id) {
const orderWithDetails = await OrderDetails({ id: orderValues.id })
// eslint-disable-next-line no-console
console.log('orderStatus', orderWithDetails)
if (!orderWithDetails) {
const orderCanceled = await OrderCancel({ id: orderValues.id })
// eslint-disable-next-line no-console
console.log('return from order canceled', orderCanceled)
if (orderCanceled) {
invalids.push(order)
}
}
}
})
return invalids
}
return []
}
}
const dailyJob = new CronJob({
// Seconds: 0-59 Minutes: 0-59 Hours: 0-23 Day of Month: 1-31 Months: 0-11 (Jan-Dec) Day of Week: 0-6 (Sun-Sat)
cronTime: '0 0 0 * * *', // everyday at 12:00AM
onTick: () => {
const d = new Date()
// eslint-disable-next-line no-console
console.log('Log to confirm cron daily job run at', d)
TaskCron.rememberDeadline()
OrderCron.verify()
}
})
const weeklyJob = new CronJob({
cronTime: '5 8 * * 0',
onTick: () => {
const d = new Date()
// eslint-disable-next-line no-console
console.log('Log to confirm cron weekly job run at', d)
TaskCron.weeklyBounties()
}
})
const weeklyJobLatest = new CronJob({
cronTime: '5 8 * * 4',
onTick: () => {
const d = new Date()
// eslint-disable-next-line no-console
console.log('Log to confirm cron weekly job run at', d)
TaskCron.latestTasks()
}
})
module.exports = { dailyJob, weeklyJob, weeklyJobLatest, TaskCron, OrderCron }