-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
246 lines (194 loc) · 7.04 KB
/
main.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
const electron = require('electron');
const fs = require('fs');
const url = require('url');
const path = require('path');
const robinhood = require(path.join(__dirname, 'assets/robinhood'));
const td = require(path.join(__dirname, 'assets/tradingdive'));
const Store = require('./store.js');
const dialog = electron.dialog;
const isDev = require('electron-is-dev'); // this is required to check if the app is running in development mode.
const { app, BrowserWindow, ipcMain, ipcRenderer, shell } = electron;
let mainWindow;
// Funtion to check the current OS. As of now there is no proper method to add auto-updates to linux platform.
function isWindowsOrmacOS() {
return process.platform === 'darwin' || process.platform === 'win32';
}
// Set environment
process.env.NODE_ENV = "production";
// Instantiate Store class for authentication tokens.
// DO NOT store any usernames or passwords here
const store = new Store({
configName: 'user-authentication',
defaults: {
rhToken: '',
tdToken: ''
}
});
// Store CSV Data
var _csvContent = null;
// Store current balance
var currentBalance = null;
// Listen for app to be ready
app.on('ready', function () {
// Create new window
mainWindow = new BrowserWindow({ width: 1024, height: 750, resizable: false});
// load html into window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'main.html'),
protocol: 'file:',
slashes: true
}));
mainWindow.webContents.on('did-finish-load', function () {
mainWindow.show();
if (store.get('rhToken')) {
// Set RH token for future API requests
robinhood.setToken(store.get('rhToken'));
// Send authentication success to main window
mainWindow.webContents.send("authenticated", true);
}
if (store.get('tdToken')) {
// Let main window know we're authenticated with Trading Dive
mainWindow.webContents.send("tdAuthenticated", true);
// Get User's portfolios
td.getPortfolios(store.get('tdToken'), function (err, portfolios) {
mainWindow.webContents.send("tdPortfolios", portfolios);
});
}
mainWindow.webContents.send("finishedLoad", true);
// const checkOS = isWindowsOrmacOS();
// if (checkOS && !isDev) {
// // Initate auto-updates on macOs and windows
// appUpdater();
// }
});
});
// Catch login events
ipcMain.on('robinhoodLogin', function (e, login) {
// Authenticate with Robinhood and get trade history
robinhood.authenticate(login, function (err, token) {
if (err) {
// Return Error
mainWindow.webContents.send("error", err);
} else {
// Store RH token locally
store.set('rhToken', token);
// Set RH token for future API requests
robinhood.setToken(store.get('rhToken'));
// Send authentication success to main window
mainWindow.webContents.send("authenticated", true);
}
});
});
ipcMain.on('tradingDiveLogin', function (e, login) {
// Authenticate with Trading Dive
td.authenticate(login.username, login.password, function (err, token) {
if (err) {
mainWindow.webContents.send("tdLoginError", err);
} else {
// Let main window know we're authenticated with Trading Dive
mainWindow.webContents.send("tdAuthenticated", true);
// Store TD token locally
store.set('tdToken', token);
// Get User's portfolios
td.getPortfolios(token, function (err, portfolios) {
mainWindow.webContents.send("tdPortfolios", portfolios);
});
}
});
});
// Listen for get data events
ipcMain.on('getAll', function () {
// Populate trade data
_populateCSVData('all');
});
ipcMain.on('getRecent', function () {
// Populate trade data
_populateCSVData('recent');
});
// Listen for download CVS button click event
ipcMain.on('downloadCSV', function (e, download) {
_saveFile();
});
ipcMain.on('tdSync', function (e, portfolio) {
portfolio.currentValue =
td.import(store.get('tdToken'), portfolio, _csvContent, function (e, status) {
// Sync balance with portfolio
robinhood.getBalance(store.get('rhToken'), function (err, balance) {
portfolio.currentValue = parseFloat(balance);
td.updatePortfolioBalance(store.get('tdToken'), portfolio, function () {
});
});
mainWindow.webContents.send("syncFinish", status);
});
});
// Listen for logouts
ipcMain.on('rhLogout', function (e, download) {
store.set('rhToken', '');
});
ipcMain.on('tdLogout', function (e, download) {
store.set('tdToken', '');
});
function _populateCSVData(filter) {
if (filter === 'all') {
robinhood.getAllOrders(mainWindow, function (err, data) {
_generateCSV(data);
// Let main window know when file is finished generating
mainWindow.webContents.send("csvReady", true);
});
} else {
robinhood.getRecentOrders(mainWindow, function (err, data) {
_generateCSV(data);
// Let main window know when file is finished generating
mainWindow.webContents.send("csvReady", true);
});
}
}
ipcMain.on('openRegisterPage', function () {
shell.openExternal('https://app.tradingdive.com/users/register');
});
ipcMain.on('loadDashboardPage', function () {
shell.openExternal('https://app.tradingdive.com/dashboard/#/trades');
});
function _saveFile() {
dialog.showSaveDialog({ filters: [{
name: 'csv',
extensions: ['csv']
}] }, (fileName) => {
if (fileName === undefined) {
console.log("You didn't save the file");
return;
}
// fileName is a string that contains the path and filename created in the save file dialog.
fs.writeFile(fileName, _csvContent, (err) => {
if (err) {
dialog.showMessageBox({
message: "There was an error saving your file.",
buttons: ["OK"]
});
}
dialog.showMessageBox({
message: "The file has been saved! :-)",
buttons: ["OK"]
});
});
});
}
function _generateCSV(data) {
// Create CSV headers
var csvContent = 'created,symbol,side,quantity,price,fees \r\n';
// Loop through trades and create csv structure
var count = 0;
for (var i = 0; i < data.length; i++) {
if (data[i].cumulative_quantity > 0) {
count++
csvContent += data[i].created_at + ','
+ data[i].symbol + ','
+ data[i].side + ','
+ data[i].cumulative_quantity + ','
+ data[i].average_price + ','
+ data[i].fees
+ '\r\n';
}
}
_csvContent = csvContent;
}