forked from F3riko/redeployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
364 lines (323 loc) · 10.7 KB
/
renderer.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
class TokenError extends Error {
constructor(message) {
super(message);
this.name = "TokenError";
}
}
class VehicleError extends Error {
constructor(message) {
super(message);
this.name = "VehicleError";
}
}
class TravelSheetError extends Error {
constructor(message) {
super(message);
this.name = "TravelSheetError";
}
}
class ExitCommand extends Error {
constructor(message) {
super(message);
this.name = "ExitCommand";
}
}
function logMessage(message) {
const log = document.getElementById("log");
log.innerHTML += `${new Date().toTimeString().slice(0, 8)}: ${message}<br>`;
log.scrollTop = log.scrollHeight;
}
async function promptWithExit(message) {
const input = await showPrompt(message);
const normalizedInput = input ? input.trim().toLowerCase() : "";
if (normalizedInput === "exit") {
throw new ExitCommand("User initiated exit.");
}
return input;
}
async function confirmWithExit(message) {
const input = await showConfirm(message);
const normalizedInput =
typeof input === "string" ? input.trim().toLowerCase() : "";
if (normalizedInput === "exit") {
throw new ExitCommand("User initiated exit.");
}
return input;
}
function showPrompt(message) {
return new Promise((resolve) => {
const modal = document.getElementById("modal");
const modalMessage = document.getElementById("modalMessage");
const modalInput = document.getElementById("modalInput");
const modalOk = document.getElementById("modalOk");
const modalCancel = document.getElementById("modalCancel");
const modalClose = document.getElementById("modalClose");
modalMessage.textContent = message;
modalInput.value = "";
modal.style.display = "block";
modalOk.onclick = function () {
const value = modalInput.value.trim();
modal.style.display = "none";
resolve(value);
};
modalCancel.onclick = function () {
modal.style.display = "none";
resolve(null);
};
modalClose.onclick = function () {
modal.style.display = "none";
resolve("exit");
};
});
}
function showConfirm(message) {
return new Promise((resolve) => {
const confirmModal = document.getElementById("confirmModal");
const confirmMessage = document.getElementById("confirmMessage");
const confirmYes = document.getElementById("confirmYes");
const confirmNo = document.getElementById("confirmNo");
const confirmClose = document.getElementById("confirmClose");
confirmMessage.textContent = message;
confirmModal.style.display = "block";
confirmYes.onclick = function () {
confirmModal.style.display = "none";
resolve(true);
};
confirmNo.onclick = function () {
confirmModal.style.display = "none";
resolve(false);
};
confirmClose.onclick = function () {
confirmModal.style.display = "none";
resolve("exit");
};
});
}
async function retrieveRefreshToken() {
try {
logMessage("Attempting to retrieve refresh token...");
const refreshTokenValue = await window.api.refreshToken();
if (!refreshTokenValue) {
logMessage("No refresh token found.");
return null;
}
logMessage("Refresh token retrieved successfully.");
return refreshTokenValue;
} catch (error) {
logMessage(`Error retrieving refresh token: ${error.message}`);
throw error;
}
}
async function retrieveAccessToken(refreshToken) {
logMessage("Attempting to retrieve access token...");
const accessToken = await window.api.getAccessToken(refreshToken);
if (!accessToken) {
throw new TokenError("Failed to retrieve access token.");
}
logMessage("Access token retrieved successfully.");
return accessToken;
}
async function addRefreshToken() {
try {
let refreshTokenValue = await window.api.refreshToken();
if (refreshTokenValue) {
logMessage(`Refresh Token already exists: ${refreshTokenValue}`);
const overwrite = await confirmWithExit(
"A refresh token already exists. Do you want to overwrite it? (yes/no)"
);
if (!overwrite) {
logMessage("Operation cancelled by the user.");
return;
}
}
} catch (error) {
logMessage("No existing refresh token found.");
}
try {
const newRefreshToken = await promptWithExit(
"Please enter the new REFRESH_TOKEN:"
);
if (!newRefreshToken) {
logMessage("No refresh token entered. Operation cancelled.");
return;
}
await window.api.saveRefreshToken(newRefreshToken);
logMessage("REFRESH_TOKEN has been updated and saved.");
} catch (error) {
logMessage(`An unexpected error occurred: ${error.message}`);
throw error;
}
}
async function travelSheet() {
try {
const refreshToken = await retrieveRefreshToken();
const accessToken = await retrieveAccessToken(refreshToken);
let continueTravelSheet = true;
while (continueTravelSheet) {
try {
const userInput = await promptWithExit(
'Please enter the reservation number (or type "exit" to quit):'
);
if (!userInput) {
logMessage("No reservation number entered. Operation cancelled.");
continue;
}
if (userInput.toLowerCase() === "exit") {
logMessage("Exiting the travel sheet retrieval process.");
continueTravelSheet = false;
continue;
}
logMessage(`Reservation number entered: ${userInput}`);
logMessage("Attempting to retrieve travel sheet...");
await window.api.getTravelSheet(
accessToken,
async () => userInput,
logMessage
);
const continueInput = await confirmWithExit(
"Do you want to retrieve another travel sheet? (yes/no)"
);
if (!continueInput) {
logMessage("Exiting travel sheet retrieval loop.");
continueTravelSheet = false;
}
} catch (error) {
if (error instanceof ExitCommand) {
logMessage("User requested exit. Exiting process.");
continueTravelSheet = false;
} else {
logMessage(
`An error occurred while processing travel sheet retrieval: ${
error.message || error
}`
);
const retryInput = await confirmWithExit(
"An error occurred. Do you want to try again? (yes/no)"
);
if (!retryInput) {
logMessage("Exiting travel sheet retrieval loop.");
continueTravelSheet = false;
}
}
}
}
} catch (error) {
if (error instanceof TokenError) {
logMessage(`Authentication Error: ${error.message}`);
logMessage(
"Please use 'Add New Refresh Token' to provide a valid refresh token."
);
} else if (error instanceof ExitCommand) {
logMessage("User requested exit. Exiting process.");
} else {
console.error(`An unexpected error occurred: ${error.stack}`);
logMessage("An unexpected error occurred. Please try again later.");
}
}
}
async function redeployVehicle() {
try {
const refreshToken = await retrieveRefreshToken();
const accessToken = await retrieveAccessToken(refreshToken);
let continueRedeploy = true;
while (continueRedeploy) {
try {
const vehicleVIN = await getVehicleVIN();
const vehicleData = await fetchVehicleData(vehicleVIN, accessToken);
const currentBM =
window.api.businessModelsReverse[vehicleData.activeBusinessModelId];
logMessage(`Vehicle ${vehicleVIN} is currently in ${currentBM}.`);
await validateAndRedeployVehicle(
vehicleData,
vehicleVIN,
currentBM,
accessToken
);
const continueInput = await confirmWithExit(
"Do you want to continue with another vehicle? (yes/no)"
);
if (!continueInput) {
logMessage("Exiting vehicle redeployment loop.");
continueRedeploy = false;
}
} catch (error) {
if (error instanceof VehicleError) {
logMessage(`Vehicle Error: ${error.message}`);
const retryInput = await confirmWithExit(
"An error occurred. Do you want to try again? (yes/no)"
);
if (!retryInput) {
logMessage("Exiting vehicle redeployment loop.");
continueRedeploy = false;
}
} else if (error instanceof ExitCommand) {
logMessage("User requested exit. Exiting process.");
continueRedeploy = false;
} else {
throw error;
}
}
}
} catch (error) {
if (error instanceof TokenError) {
logMessage(`Authentication Error: ${error.message}`);
logMessage(
"Please use 'Add New Refresh Token' to provide a valid refresh token."
);
} else if (error instanceof ExitCommand) {
logMessage("User requested exit. Exiting process.");
} else {
console.error(`An unexpected error occurred: ${error.stack}`);
logMessage("An unexpected error occurred. Please try again later.");
}
}
}
async function getVehicleVIN() {
const vehicleVIN = await promptWithExit("Enter vehicle's VIN:");
if (!vehicleVIN) {
throw new VehicleError("No vehicle VIN provided.");
}
logMessage(`Vehicle VIN entered: ${vehicleVIN}`);
return vehicleVIN;
}
async function fetchVehicleData(vehicleVIN, accessToken) {
logMessage("Fetching vehicle data from the server...");
const response = await window.api.getVehicleData(vehicleVIN, accessToken);
if (!response.rows || response.rows.length === 0) {
throw new VehicleError("No vehicle data found for the given VIN.");
}
logMessage("Vehicle data received successfully.");
return response.rows[0];
}
async function validateAndRedeployVehicle(
vehicleData,
vehicleVIN,
currentBM,
accessToken
) {
const targetBM = await promptWithExit("Enter target Business Model name:");
if (!targetBM) {
throw new VehicleError("No target Business Model name was entered.");
}
logMessage(`Target Business Model name entered: ${targetBM}`);
logMessage("Validating vehicle for redeployment...");
window.api.validateVehicleForRedeployment(vehicleData, targetBM, currentBM);
logMessage("Vehicle is valid for redeployment.");
logMessage("Attempting to redeploy vehicle...");
const redeployResponse = await window.api.redeployVehicle(
vehicleData.id,
targetBM,
accessToken
);
if (!redeployResponse) {
throw new VehicleError("Vehicle redeployment failed.");
}
const redeployedBM =
window.api.businessModelsReverse[redeployResponse.activeBusinessModelId];
logMessage(
`Vehicle redeployed successfully: ${vehicleVIN} is now in ${redeployedBM}.`
);
}
window.addRefreshToken = addRefreshToken;
window.redeployVehicle = redeployVehicle;
window.travelSheet = travelSheet;