-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (100 loc) · 3.01 KB
/
index.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
#!/usr/bin/env node
import axios from 'axios';
import chalk from 'chalk';
import inquirer from 'inquirer';
import SearchBox from 'inquirer-search-list';
import {
getAuthTokenFromFile,
getUserFile,
questionsForLogin,
} from './utils/util.js';
import cliFiglet from './utils/figlet.js';
import { authWithResponse } from './scripts/AuthWithResponse.js';
import fs from 'fs';
import applyIpoList from './scripts/IpoList.js';
import applyForIpo from './scripts/applyIpo.js';
import { homeScreenPrompt, ipoSelectPrompt } from './utils/prompts.js';
const greeting = chalk.green.bold('Welcome to Auto IPO Apply!');
inquirer.registerPrompt('search-list', SearchBox);
console.log(greeting);
/**
* @TODO auto login on single click on second login attempt
*/
const login = async function () {
try {
const answers = await inquirer.prompt(questionsForLogin);
const data = await authWithResponse(
parseInt(answers.clientId),
answers.username,
answers.password,
answers.pincode,
answers.crn
);
fs.writeFileSync('.store.bin', JSON.stringify(data));
run(true);
} catch (error) {
if (error.isTtyError) {
console.log(`Prompt couldn't be rendered in the current environment`);
} else {
console.log('Error');
console.log(error.response.data);
}
}
};
const run = async (errorRun) => {
try {
if (!errorRun) await cliFiglet();
const { options } = await inquirer.prompt(homeScreenPrompt);
if (options.includes('Login to Meroshare')) await login();
else if (options.includes('Apply From List of Available IPO')) {
const availableList = await applyIpoList();
availableList?.length !== 0
? availableList
: [{ key: '-1', value: 'No IPO available' }];
const userChoice = await inquirer.prompt([
{
type: 'checkbox',
name: 'list',
message: 'Which IPO you want to apply. Select Multiple From List?',
choices:
availableList?.length !== 0
? availableList
: [{ key: '1', value: 'No IPO available' }],
},
]);
console.log(availableList);
const { authToken, partialApplyObject } = getUserFile();
for (let [index, val] of userChoice.list.entries()) {
const res = await applyForIpo(authToken, {
...partialApplyObject,
companyShareId: val,
appliedKitta: '10',
});
console.log(
chalk.red(
`Hurray!!!! Applied ${10} units of share in ${
availableList[index].name
}${res.message} `
)
);
}
} else {
process.exit(1);
}
} catch (error) {
if (error.isTtyError) {
console.log(
chalk.redBright(
`Prompt couldn't be rendered in the current environment`
)
);
} else if (error.response) {
console.log(error.response);
console.log(chalk.redBright(error.response));
} else {
console.log(chalk.redBright(error));
}
run(true);
}
};
run(false);