-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsplit.js
More file actions
92 lines (81 loc) · 2.48 KB
/
Copy pathsplit.js
File metadata and controls
92 lines (81 loc) · 2.48 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
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
const {Command, Flags} = require('@oclif/core')
const helpers = require('../lib/helpers')
const APIs = require('../lib/paystack/APIs.json')
const Paystack = require('../lib/paystack')
const db = require('../lib/db')
let key = 'split';
let API = APIs[key];
class SplitCommand extends Command {
async run() {
const {args, flags} = await this.parse(SplitCommand)
let selected_integration = db.read('selected_integration.id')
let user = db.read('user.id')
if (!selected_integration || !user) {
this.error("You're not signed in, please run the `paystack login` command before you begin")
}
let token = ''
let expiry = parseInt(db.read('token_expiry'), 10) * 1000
let now = parseFloat(Date.now().toString())
if (expiry > now) {
token = db.read('token')
} else {
await helpers.promiseWrapper(Paystack.refreshIntegration())
token = db.read('token')
}
let schema = helpers.findSchema(key, args.endpoint, flags)
if(!schema){
helpers.errorLog(`ValidationError: Invalid endpoint 'split'`)
return;
}
let [err, result] = await helpers.promiseWrapper(helpers.executeSchema(schema, flags))
if (err) {
if (err.response) {
helpers.errorLog(err.response.data.message)
return
}
helpers.errorLog(err)
return
}
helpers.successLog(result.message)
helpers.jsonLog(result.data)
}
}
SplitCommand.description = helpers.getDescription(API, key)
SplitCommand.flags = {
domain: Flags.string(),
}
let addedFlags = ['domain']
let endpoints = [];
API.forEach(path => {
endpoints.push(path.api);
path.params.forEach(param => {
if (addedFlags.indexOf(param.parameter) < 0) {
switch (param.type) {
case 'String':
SplitCommand.flags[param.parameter] = Flags.string()
break
case 'Number':
SplitCommand.flags[param.parameter] = Flags.integer()
break
case 'Boolean':
SplitCommand.flags[param.parameter] = Flags.boolean()
break
default:
SplitCommand.flags[param.parameter] = Flags.string()
}
addedFlags.push(param.parameter)
}
})
if(path.variables){
path.variables.forEach((variable)=>{
if(addedFlags.indexOf(variable.key) < 0){
SplitCommand.flags[variable.key] = Flags.string();
addedFlags.push(variable.key);
}
})
}
})
SplitCommand.args = [
{name: 'endpoint', required: true, options: endpoints}
]
module.exports = SplitCommand;