forked from worknenjoy/gitpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
132 lines (125 loc) · 4.35 KB
/
scripts.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
const models = require('./models')
const Promise = require('bluebird')
const url = require('url')
const requestPromise = require('request-promise')
const i18n = require('i18n')
const stripe = require('stripe')(process.env.STRIPE_KEY)
const SendMail = require('./modules/mail/mail')
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 scripts = {
accountInfo: () => {
return models.User
.findAll(
{}
)
.then(users => {
if (!users) return false
if (users.length <= 0) return false
const accountInfo = users.map(u => {
if (!u.account_id) {
return {
user: u.email,
active_account: false
}
}
const accountDetails = stripe.accounts.retrieve(u.account_id).then(account => {
return {
user: u.email,
active_account: true,
account: account
}
})
return accountDetails
})
// eslint-disable-next-line no-console
console.log('users', accountInfo)
return accountInfo
}).catch(error => {
// eslint-disable-next-line no-console
console.log('error when search user: ', error)
return false
})
},
deleteInvalidTasks: () => {
return models.Task
.findAll(
{
where: {
provider: 'github'
},
include: [ models.User ]
}
)
.then(tasks => {
const tasksPromises = tasks.map(t => {
const uri = t.url
const splitIssueUrl = url.parse(uri).path.split('/')
const userOrCompany = splitIssueUrl[1]
const projectName = splitIssueUrl[2]
const issueId = splitIssueUrl[4]
if (!userOrCompany || !projectName || !issueId || isNaN(issueId)) return t
return requestPromise({
uri,
resolveWithFullResponse: true
}).then(response => {
// eslint-disable-next-line no-console
// console.log('response status code for issue', response.statusCode, t.url)
// eslint-disable-next-line no-console
// console.log('task successfull from Github', t.url)
return false
}).catch(e => {
// eslint-disable-next-line no-console
// console.log('task with error from Github', t.url)
return t
})
})
return Promise.all(tasksPromises).then(results => {
// eslint-disable-next-line no-console
// console.log('results from tasksPromises', results)
const invalidTasksToDelete = results.filter(t => t.id)
const invalidTasksDeleted = invalidTasksToDelete.map(invalidTask => {
return Promise.all([
models.History.destroy({ where: { TaskId: invalidTask.id } }),
models.Order.destroy({ where: { TaskId: invalidTask.id } }),
models.Assign.destroy({ where: { TaskId: invalidTask.id } }),
models.Offer.destroy({ where: { taskId: invalidTask.id } }),
models.Member.destroy({ where: { taskId: invalidTask.id } }),
]).then(result => {
return models.Task.destroy({
where: {
id: invalidTask.id
}
}).then(task => {
if (task) {
const user = invalidTask.User
const language = user.language || 'en'
i18n.setLocale(language)
SendMail.success(
user,
i18n.__('task.invalid.script.subject'),
i18n.__('task.invalid.script.message', {
url: invalidTask.url
})
)
}
return invalidTask
})
})
})
return Promise.all(invalidTasksDeleted).then(result => result)
})
}).catch(error => {
// eslint-disable-next-line no-console
console.log('error when search task: ', error)
return false
})
}
}
// if (process.argv[2]) scripts[process.argv[2]]()
module.exports = { scripts }