-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (125 loc) · 4.15 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
"use strict";
const unirest = require("unirest");
const path = require("path");
const fs = require("fs");
const log = require("electron-log");
const { app, BrowserWindow, ipcMain } = require("electron");
const AWT =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJuYmYiOjE2NjI0MjI0MDAsImFwaV9zdWIiOiIzNzU5NzA2ZjQxMGMyMTdjODYyMTZhOGZhZGIzMDY1NDc0YmRmZjNkNDdiNDg5ZGM1OTk1NzQyYTE0ZTY5ZDQxMTY3NTEyMzIwMDAwMCIsInBsYyI6IjVkY2VjNzRhZTk3NzAxMGUwM2FkNjQ5NSIsImV4cCI6MTY3NTEyMzIwMCwiZGV2ZWxvcGVyX2lkIjoiMzc1OTcwNmY0MTBjMjE3Yzg2MjE2YThmYWRiMzA2NTQ3NGJkZmYzZDQ3YjQ4OWRjNTk5NTc0MmExNGU2OWQ0MSJ9.Mijhmrl5GOeD6qrSpj4NHZlT3hx6DT8WE-OqP-5kwcS7EIA_DScaaRVpqCflKJraLbFaNXz7DJR6lliN9VrQJvnu-4aLQonFfazLk8Nf8Fujd2C81Z-BenNSTY1PApFS18WVHaS2VNolC_UufXNfCigBDkr5emEB1Q52gLdQBPdGb_YMO04g0ln5YNIe0YARzDniT37NjF6rux6iguUbajGF35hVJkzIfpvMPz-FCXcehBsocPAhUyhakeBS-0cjeCcTTRMAgB_O3zKPSQ6QyOtVjQsqxIS6rRThTk1hu9pbRvR4amoe73ZYt_yjFYQM68IYdpRldZi9kjnGhdrUKQ";
const NODE_ENV = "development";
const DATA_DIR = path.join(__dirname + "/SAM_Data");
//file logging
log.transports.file.resolvePath = () => path.join(DATA_DIR, "mainlog.txt");
Object.assign(console, log.functions);
// if (NODE_ENV == "production") {
// Object.assign(console, log.functions)
// }
//create user data file if not exists (or if it fails to parse)
const userDataFilePath = path.join(DATA_DIR, "UserSoleStats.json");
var GlobalUserData = {};
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR);
}
if (!fs.existsSync(userDataFilePath)) {
fs.writeFileSync(userDataFilePath, "{}");
} else {
try {
GlobalUserData = JSON.parse(
fs.readFileSync(userDataFilePath, { encoding: "utf8", flag: "r" })
);
} catch {
fs.writeFileSync(userDataFilePath, "{}");
}
}
/**
* Saves the user data object to the user data path
* @param {object} data
*/
function saveUserData(data) {
fs.writeFileSync(userDataFilePath, JSON.stringify(data));
}
const createWindow = () => {
const win = new BrowserWindow({
width: 1000,
height: 600,
minWidth: 1000,
minHeight: 500,
icon: "assets/images/logo.svg",
titleBarStyle: "hidden",
titleBarOverlay: {
color: "#989898",
symbolColor: "#00",
height: 30,
},
autoHideMenuBar: true,
webPreferences: {
enableBlinkFeatures: false,
contextIsolation: true,
backgroundThrottling: false,
preload: path.join(__dirname, "preload.js"),
devTools: NODE_ENV == "development" ? true : false,
},
show: false,
});
ipcMain.on("saveUserData", (event, data) => {
saveUserData(data);
win.webContents.send("dataSaved", {});
console.log("User data saved");
});
ipcMain.handle("getUserData", async (event, data) => {
return GlobalUserData;
});
ipcMain.on("getUserPurchases", (event, data) => {
console.log(data);
let req = unirest
.get(
"https://sandbox.capitalone.co.uk/developer-services-platform-pr/api/data/transactions/accounts/" +
data.id +
"/transactions"
)
.headers({
"Content-Type": "application/json",
Authorization: "Bearer " + AWT,
version: "1.0",
})
.end((transaction_res) => {
var req = unirest(
"GET",
"https://sandbox.capitalone.co.uk/developer-services-platform-pr/api/data/accounts/" +
data.id
)
.headers({
"Content-Type": "application/json",
Authorization: "Bearer " + AWT,
version: "1.0",
})
.end((account_res) => {
win.webContents.send("transactionData", {
account: account_res.raw_body,
transactions: transaction_res.raw_body,
});
});
});
});
ipcMain.on("saveUserData", (event, data) => {
saveUserData(data);
console.log("User data saved");
});
win.once("ready-to-show", () => {
win.show();
});
win.loadFile("web/intro.html");
};
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", () => {
try {
if (process.platform !== "darwin") app.quit();
} catch (error) {
console.log(error);
}
});