forked from worknenjoy/gitpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
311 lines (298 loc) · 9.63 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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 newOrExistingProject = async (userOrCompany, projectName, userId) => {
try {
const organizationExist = await models.Organization.find(
{
where: {
name: userOrCompany
},
include: [models.Project]
}
)
if (organizationExist) {
const projectFromOrg = await models.Project.find(
{
where: {
name: projectName,
OrganizationId: organizationExist.id
},
include: [models.Organization]
}
)
if (projectFromOrg) {
return projectFromOrg
}
else {
const newProject = await organizationExist.createProject({ name: projectName })
return newProject
}
}
else {
const organization = await models.Organization.create({ name: userOrCompany, UserId: userId })
const project = await organization.createProject({ name: projectName })
return project
}
}
catch (e) {
// eslint-disable-next-line no-console
console.log('error', e)
throw new Error(e)
}
}
const calculateTotal = async () => {
const orders = await models.Order.findAll(
{},
{
where: {
paid: true
}
}
) || []
const total = orders.reduce(async (accPromise, order) => {
const acc = await accPromise;
if(order.provider === 'stripe') {
if(!order.source) return acc;
try {
const charge = await stripe.charges.retrieve(order.source);
const balanceTransaction = await stripe.balanceTransactions.retrieve(charge.balance_transaction);
if (balanceTransaction) {
return acc + ((balanceTransaction.net / 100) - parseFloat(order.amount));
}
return acc;
} catch (e) {
console.log('error on balance script for stripe', e);
return acc;
}
}
}, Promise.resolve(0));
return total;
}
const calculateTotalTransfers = async () => {
const transfers = await models.Payout.findAll();
return transfers.reduce((acc, payout) => {
return (acc + (parseFloat(payout.amount) - parseFloat(payout.amount) * 0.92));
}, 0);
}
const scripts = {
balance: async () => {
try {
const totalFromOrders = await calculateTotal();
const totalFromTransfers = await calculateTotalTransfers();
console.log('totalFromOrders', totalFromOrders)
console.log('totalFromTransfers', totalFromTransfers)
return {
payments_fee: totalFromOrders.toFixed(2),
payouts: totalFromTransfers.toFixed(2),
};
} catch (e) {
console.log('error on balance script', e);
return 0;
}
},
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
})
},
createProjects: () => {
return models.Task
.findAll(
{
where: {
provider: 'github'
},
include: [models.User, models.Project]
}
)
.then(tasks => {
const tasksPromises = tasks.filter(t => {
if (t.ProjectId) return false
return true
})
return Promise.all(tasksPromises).then(results => {
// eslint-disable-next-line no-console
console.log('results from tasksPromises', results)
return results.map(task => {
const uri = task.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 task
return newOrExistingProject(userOrCompany, projectName, task.userId).then((project) => {
return task.update({ ProjectId: project.id }).then(u => u)
})
})
})
}).catch(error => {
// eslint-disable-next-line no-console
console.log('error when search task: ', error)
return false
})
},
updateAssignsStatus: () => {
return models.Assign
.findAll({
attributes: ['id', 'TaskId'],
where: {
status: null
}
}).then(assigns => {
return assigns.map(a => {
return models.Task
.findOne({
attributes: ['status'],
where: {
id: a.dataValues.TaskId
}
}).then(task => {
if (task.dataValues.status === 'closed' || task.dataValues.status === 'in_progress') {
return {
id: a.dataValues.id,
status: 'accepted'
}
}
else if (task.dataValues.status === 'open') {
return {
id: a.dataValues.id,
status: 'pending'
}
}
})
// eslint-disable-next-line no-console
.catch(err => console.log(`error occured in assigns.map: ${err}`))
})
}).all().then(updateFields => {
return updateFields.forEach(uf => {
return models.Assign
.update({
status: uf.status
}, {
where: {
id: uf.id
}
})
})
// eslint-disable-next-line no-console
}).then(console.log('Assigns successfully updated.'))
// eslint-disable-next-line no-console
.catch(err => console.log(`error while updating assigns status: ${err}`))
}
}
// if (process.argv[2]) scripts[process.argv[2]]()
module.exports = { scripts }