@itutoring/itutoring_application_js_api 1.11.0 → 1.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/iTutoring_Application_API-js.iml +11 -11
- package/.idea/modules.xml +7 -7
- package/.idea/vcs.xml +5 -5
- package/CookiesManager.js +47 -47
- package/LICENSE +21 -21
- package/README.md +6 -6
- package/apiController.js +436 -436
- package/cache.js +31 -31
- package/index.d.ts +116 -116
- package/index.js +114 -114
- package/modules/AiAssist.js +31 -31
- package/modules/Authentication.js +86 -86
- package/modules/Cart.js +83 -81
- package/modules/Checkout.js +60 -50
- package/modules/CustomerAuth.js +57 -57
- package/modules/CustomerLessonManager.js +29 -29
- package/modules/CustomerPortal.js +31 -31
- package/modules/FAQ.js +39 -39
- package/modules/GoogleReviews.js +36 -36
- package/modules/Inventory.js +51 -51
- package/modules/LectorDatabase.js +185 -185
- package/modules/LectorsCalendar.js +67 -67
- package/modules/LessonInventory.js +47 -47
- package/modules/MarketplaceController.js +86 -86
- package/modules/Models.js +380 -380
- package/modules/Payments.js +75 -75
- package/modules/Pricing.js +70 -70
- package/modules/ReservationSystem.js +48 -48
- package/modules/Reviews.js +53 -53
- package/modules/SubjectManager.js +139 -139
- package/modules/TableExplorer.js +246 -246
- package/modules/TeacherAuth.js +57 -57
- package/modules/TeacherProfileModule.js +299 -299
- package/modules/TeacherProfiles.js +60 -60
- package/modules/Version.js +24 -24
- package/modules/WebController.js +37 -37
- package/objects/AttendanceEvent.js +39 -39
- package/objects/AuthUser.js +24 -24
- package/objects/Course.js +38 -38
- package/objects/CourseReservation.js +22 -22
- package/objects/Customer.js +20 -20
- package/objects/Enums.js +62 -62
- package/objects/Event.js +125 -125
- package/objects/Reservation.js +42 -42
- package/objects/Subject.js +24 -24
- package/objects/Teacher.js +18 -18
- package/objects/TeacherProfile.js +17 -17
- package/objects/Webinar.js +25 -25
- package/objects/billingInfo.js +29 -29
- package/package.json +1 -1
- package/toolkit/Toolkit.jsx +51 -51
package/apiController.js
CHANGED
|
@@ -1,437 +1,437 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (C) 2024 iTutoring s.r.o.
|
|
3
|
-
* All rights reserved.
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import CookiesManager from "./CookiesManager";
|
|
11
|
-
import APICache from "./cache";
|
|
12
|
-
import { R_KEYs } from "./objects/Enums";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Main class for communicating with our backend REST api.
|
|
16
|
-
*/
|
|
17
|
-
class APIController
|
|
18
|
-
{
|
|
19
|
-
static #R_KEY = "-1";
|
|
20
|
-
static #CLIENT_KEY = "-1";
|
|
21
|
-
|
|
22
|
-
static UserSource = null;
|
|
23
|
-
|
|
24
|
-
static onErrorReceived = null
|
|
25
|
-
static onConfimationReceived = null;
|
|
26
|
-
static onWarningReceived = null;
|
|
27
|
-
|
|
28
|
-
static LastCall = null;
|
|
29
|
-
static ConfirmationToken = null;
|
|
30
|
-
|
|
31
|
-
static IsTestServer = null;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* R_KEY MUST be loaded before calling this method!
|
|
35
|
-
* @returns Returns appropriate rest url based on current server.
|
|
36
|
-
*/
|
|
37
|
-
static REST_URL()
|
|
38
|
-
{
|
|
39
|
-
if (location.hostname === 'localhost' || this.#R_KEY === R_KEYs.r_key_test)
|
|
40
|
-
{
|
|
41
|
-
return "https://api.test.itutoring.cz/";
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (this.#R_KEY === R_KEYs.r_key_live)
|
|
46
|
-
{
|
|
47
|
-
return "https://api.itutoring.cz/";
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
static TestServerCheck()
|
|
52
|
-
{
|
|
53
|
-
return this.IsTestServer;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
static async CreateVisitorSession()
|
|
57
|
-
{
|
|
58
|
-
var id = '';
|
|
59
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
60
|
-
if (urlParams.has("vid"))
|
|
61
|
-
{
|
|
62
|
-
if (CookiesManager.GetCookie("session_url") != null)
|
|
63
|
-
CookiesManager.RemoveCookie("session_url");
|
|
64
|
-
|
|
65
|
-
id = urlParams.get("vid");
|
|
66
|
-
const date = new Date();
|
|
67
|
-
date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
|
|
68
|
-
CookiesManager.SetCookie("session_url", id, date.toUTCString());
|
|
69
|
-
}
|
|
70
|
-
else
|
|
71
|
-
{
|
|
72
|
-
id = Date.now();
|
|
73
|
-
const date = new Date();
|
|
74
|
-
date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
|
|
75
|
-
CookiesManager.SetCookie("session", id, date.toUTCString());
|
|
76
|
-
}
|
|
77
|
-
this.ReadUserSource();
|
|
78
|
-
|
|
79
|
-
var rKey = await this.GetLocalRKey();
|
|
80
|
-
this.IsTestServer = rKey === R_KEYs.r_key_test || location.hostname === 'localhost';
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
static GetVisitorSessionID()
|
|
84
|
-
{
|
|
85
|
-
//console.log(CookiesManager.GetCookie("session"));
|
|
86
|
-
if (CookiesManager.GetCookie("session_url") != null)
|
|
87
|
-
return CookiesManager.GetCookie("session_url");
|
|
88
|
-
return CookiesManager.GetCookie("session");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
static ReadUserSource()
|
|
92
|
-
{
|
|
93
|
-
this.UserSource = CookiesManager.GetCookie("_o");
|
|
94
|
-
if (this.UserSource != null)
|
|
95
|
-
return;
|
|
96
|
-
|
|
97
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
98
|
-
if (urlParams.has("o"))
|
|
99
|
-
{
|
|
100
|
-
const source = urlParams.get("o");
|
|
101
|
-
if (this.UserSource == null)
|
|
102
|
-
{
|
|
103
|
-
const date = new Date();
|
|
104
|
-
date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
|
|
105
|
-
CookiesManager.SetCookie("_o", source, date.toUTCString());
|
|
106
|
-
this.UserSource = source;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
static async ConfirmRequest()
|
|
112
|
-
{
|
|
113
|
-
if (this.LastCall === null || this.ConfirmationToken === null)
|
|
114
|
-
return;
|
|
115
|
-
|
|
116
|
-
if (this.LastCall.data == null)
|
|
117
|
-
this.LastCall.data = {};
|
|
118
|
-
this.LastCall.data['conf_token'] = this.ConfirmationToken;
|
|
119
|
-
if (this.LastCall.type === "GET")
|
|
120
|
-
return await this.Get(this.LastCall.module, this.LastCall.method, this.LastCall.data);
|
|
121
|
-
else if (this.LastCall.type === "POST")
|
|
122
|
-
return await this.Post(this.LastCall.module, this.LastCall.method, this.LastCall.data);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
static ResolveResponse(request)
|
|
126
|
-
{
|
|
127
|
-
const json = JSON.parse(request.responseText);
|
|
128
|
-
if (json.code === 200)
|
|
129
|
-
{
|
|
130
|
-
// check for redirect
|
|
131
|
-
if (json.data === "redirect" && json.url !== undefined)
|
|
132
|
-
{
|
|
133
|
-
window.location.href = json.url;
|
|
134
|
-
}
|
|
135
|
-
if ((json.data != null || json.data != undefined) && json.data['conf_request'] !== undefined)
|
|
136
|
-
{
|
|
137
|
-
if (APIController.onConfimationReceived !== null && APIController.onConfimationReceived !== undefined)
|
|
138
|
-
{
|
|
139
|
-
APIController.ConfirmationToken = json.data['conf_request']['token'];
|
|
140
|
-
json.data['conf_request']["confirm"] = this.ConfirmRequest.bind(this);
|
|
141
|
-
APIController.onConfimationReceived(json.data['conf_request']);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (json.warnings != null && Object.entries(json.warnings).length > 0)
|
|
145
|
-
{
|
|
146
|
-
if (APIController.onWarningReceived !== null && APIController.onWarningReceived !== undefined)
|
|
147
|
-
{
|
|
148
|
-
APIController.onWarningReceived(json.warnings);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return (json.data);
|
|
152
|
-
}
|
|
153
|
-
if (json.code === 400)
|
|
154
|
-
{
|
|
155
|
-
console.log("API Error:");
|
|
156
|
-
console.log("Error: " + json.error_message);
|
|
157
|
-
console.log("ErrorCode: " + json.error_code);
|
|
158
|
-
console.log("StackTrace: " + json.stack_trace);
|
|
159
|
-
const error = {
|
|
160
|
-
error: {
|
|
161
|
-
message: json.error_message,
|
|
162
|
-
code: json.error_code,
|
|
163
|
-
stackTrace: json.stack_trace
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
if (APIController.onErrorReceived !== null && APIController.onErrorReceived !== undefined)
|
|
167
|
-
{
|
|
168
|
-
APIController.onErrorReceived(error);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return (JSON.stringify(error));
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
*
|
|
178
|
-
* @param module "Name of API module"
|
|
179
|
-
* @param method "Name of API method "
|
|
180
|
-
* @param data "data must be as array - key, value pair. They'll be passed into the request"
|
|
181
|
-
* @returns "Response from server"
|
|
182
|
-
*/
|
|
183
|
-
static async Get(module, method, data, useCache = false, cacheSuffix = "")
|
|
184
|
-
{
|
|
185
|
-
if (useCache && APICache.IsCached(module + method + cacheSuffix))
|
|
186
|
-
{
|
|
187
|
-
return APICache.Retrive(module + method + cacheSuffix);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
await APIController.GetLocalRKey();
|
|
191
|
-
await APIController.GetClientKey();
|
|
192
|
-
|
|
193
|
-
return new Promise(resolve =>
|
|
194
|
-
{
|
|
195
|
-
const args = APIController.GetArgsFromArray(data);
|
|
196
|
-
|
|
197
|
-
const request = new XMLHttpRequest();
|
|
198
|
-
request.withCredentials = true;
|
|
199
|
-
request.open("GET", APIController.REST_URL() + module + "/" + method + "?" + args, true);
|
|
200
|
-
request.onreadystatechange = function ()
|
|
201
|
-
{
|
|
202
|
-
if (request.readyState === 4)
|
|
203
|
-
{
|
|
204
|
-
var response = APIController.ResolveResponse(request);
|
|
205
|
-
if (useCache)
|
|
206
|
-
{
|
|
207
|
-
APICache.Cache(module + method + cacheSuffix, response);
|
|
208
|
-
}
|
|
209
|
-
if (response != null && response['conf_request'] !== undefined)
|
|
210
|
-
APIController.LastCall = {
|
|
211
|
-
"type": "GET",
|
|
212
|
-
"module": module,
|
|
213
|
-
"method": method,
|
|
214
|
-
"data": data,
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
resolve(response);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
222
|
-
request.setRequestHeader("r-key", APIController.#R_KEY);
|
|
223
|
-
request.setRequestHeader("sites", location.hostname);
|
|
224
|
-
request.setRequestHeader("key", APIController.#CLIENT_KEY);
|
|
225
|
-
request.setRequestHeader("visitor-session", this.GetVisitorSessionID());
|
|
226
|
-
if (this.UserSource != null)
|
|
227
|
-
request.setRequestHeader("visitor-source", this.UserSource);
|
|
228
|
-
request.send();
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
*
|
|
234
|
-
* @param module "Name of API module"
|
|
235
|
-
* @param method "Name of API method "
|
|
236
|
-
* @param data "data must be as array - key, value pair. They'll be passed into the request"
|
|
237
|
-
* @param file
|
|
238
|
-
* @returns "Response from server"
|
|
239
|
-
*/
|
|
240
|
-
static async Post(module, method, data, file = null, useCache = false)
|
|
241
|
-
{
|
|
242
|
-
if (useCache && APICache.IsCached(module + method))
|
|
243
|
-
{
|
|
244
|
-
return APICache.Retrive(module + method);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
await APIController.GetLocalRKey();
|
|
248
|
-
await APIController.GetClientKey();
|
|
249
|
-
|
|
250
|
-
return new Promise(resolve =>
|
|
251
|
-
{
|
|
252
|
-
let formData = new FormData();
|
|
253
|
-
if (file != null)
|
|
254
|
-
{
|
|
255
|
-
formData = new FormData();
|
|
256
|
-
formData.append("file", file);
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const args = APIController.GetArgsFromArray(data);
|
|
262
|
-
|
|
263
|
-
const request = new XMLHttpRequest();
|
|
264
|
-
request.open("POST", APIController.REST_URL() + module + "/" + method, true);
|
|
265
|
-
|
|
266
|
-
request.onreadystatechange = function ()
|
|
267
|
-
{
|
|
268
|
-
if (request.readyState === 4)
|
|
269
|
-
{
|
|
270
|
-
var response = APIController.ResolveResponse(request);
|
|
271
|
-
if (useCache)
|
|
272
|
-
{
|
|
273
|
-
APICache.Cache(module + method, response);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (response != null && response['conf_request'] !== undefined)
|
|
277
|
-
APIController.LastCall = {
|
|
278
|
-
"type": "POST",
|
|
279
|
-
"module": module,
|
|
280
|
-
"method": method,
|
|
281
|
-
"data": data,
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
resolve(response);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
request.setRequestHeader("r-key", APIController.#R_KEY);
|
|
289
|
-
request.setRequestHeader("sites", location.hostname);
|
|
290
|
-
request.setRequestHeader("key", APIController.#CLIENT_KEY);
|
|
291
|
-
request.setRequestHeader("visitor-session", this.GetVisitorSessionID());
|
|
292
|
-
if (this.UserSource != null)
|
|
293
|
-
request.setRequestHeader("visitor-source", this.UserSource);
|
|
294
|
-
|
|
295
|
-
if (file != null)
|
|
296
|
-
{
|
|
297
|
-
console.log(formData);
|
|
298
|
-
request.send(formData);
|
|
299
|
-
}
|
|
300
|
-
else
|
|
301
|
-
{
|
|
302
|
-
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
303
|
-
request.send(args);
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
static GetArgsFromArray(args)
|
|
309
|
-
{
|
|
310
|
-
let argsString = "";
|
|
311
|
-
|
|
312
|
-
if (args != null)
|
|
313
|
-
{
|
|
314
|
-
for (const [key, value] of Object.entries(args))
|
|
315
|
-
{
|
|
316
|
-
argsString += key + "=" + value + "&";
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return argsString;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
*
|
|
324
|
-
* @returns r_key for this server
|
|
325
|
-
*/
|
|
326
|
-
static GetLocalRKey()
|
|
327
|
-
{
|
|
328
|
-
return new Promise(resolve =>
|
|
329
|
-
{
|
|
330
|
-
if (APIController.#R_KEY !== "-1")
|
|
331
|
-
{
|
|
332
|
-
resolve(APIController.#R_KEY);
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
if (localStorage.getItem("r_key") !== null)
|
|
337
|
-
{
|
|
338
|
-
APIController.#R_KEY = localStorage.getItem("r_key");
|
|
339
|
-
resolve(APIController.#R_KEY);
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
// For localhost don't use xml config, but automatically
|
|
344
|
-
// return test key.
|
|
345
|
-
if (location.hostname === "localhost")
|
|
346
|
-
{
|
|
347
|
-
APIController.#R_KEY = R_KEYs.r_key_test;
|
|
348
|
-
resolve(APIController.#R_KEY);
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
else
|
|
352
|
-
{
|
|
353
|
-
fetch('/key_config.xml').then(response => response.text()).then((data) =>
|
|
354
|
-
{
|
|
355
|
-
const parser = new DOMParser();
|
|
356
|
-
const rKeyXML = parser.parseFromString(data, "text/xml");
|
|
357
|
-
const rKey = rKeyXML.getElementsByTagName("key")[0].childNodes[0].nodeValue;
|
|
358
|
-
|
|
359
|
-
APIController.#R_KEY = rKey;
|
|
360
|
-
localStorage.setItem("r_key", APIController.#R_KEY)
|
|
361
|
-
resolve(APIController.#R_KEY);
|
|
362
|
-
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
static GetClientKey()
|
|
369
|
-
{
|
|
370
|
-
return new Promise(resolve =>
|
|
371
|
-
{
|
|
372
|
-
if (APIController.#CLIENT_KEY !== "-1")
|
|
373
|
-
{
|
|
374
|
-
resolve(APIController.#CLIENT_KEY);
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
if (localStorage.getItem("client_key") !== null)
|
|
379
|
-
{
|
|
380
|
-
APIController.#CLIENT_KEY = localStorage.getItem("client_key");
|
|
381
|
-
resolve(APIController.#CLIENT_KEY);
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
let keyPath = '/client_key.xml';
|
|
386
|
-
// For localhost use different xml file (to make suer we won't rewrite the server key when uploading websites)
|
|
387
|
-
if (location.hostname === "localhost")
|
|
388
|
-
{
|
|
389
|
-
keyPath = '/local_client_key.xml'
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
fetch(keyPath).then(response => response.text()).then((data) =>
|
|
393
|
-
{
|
|
394
|
-
const parser = new DOMParser();
|
|
395
|
-
const keyXML = parser.parseFromString(data, "text/xml");
|
|
396
|
-
const key = keyXML.getElementsByTagName("key")[0].childNodes[0].nodeValue;
|
|
397
|
-
|
|
398
|
-
APIController.#CLIENT_KEY = key;
|
|
399
|
-
localStorage.setItem("client_key", APIController.#CLIENT_KEY)
|
|
400
|
-
resolve(APIController.#CLIENT_KEY);
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
static async GetCurrentHostURl()
|
|
407
|
-
{
|
|
408
|
-
if (location.hostname === 'localhost')
|
|
409
|
-
{
|
|
410
|
-
return "http://localhost:3000";
|
|
411
|
-
}
|
|
412
|
-
else
|
|
413
|
-
{
|
|
414
|
-
var r_key = await this.GetLocalRKey();
|
|
415
|
-
if (r_key === R_KEYs.r_key_test)
|
|
416
|
-
{
|
|
417
|
-
return "https://test.itutoring.cz";
|
|
418
|
-
}
|
|
419
|
-
else
|
|
420
|
-
{
|
|
421
|
-
return "https://itutoring.cz";
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
static IntToBool(value)
|
|
427
|
-
{
|
|
428
|
-
return value === 1;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
static BoolToInt(value)
|
|
432
|
-
{
|
|
433
|
-
return value ? 1 : 0;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2024 iTutoring s.r.o.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import CookiesManager from "./CookiesManager";
|
|
11
|
+
import APICache from "./cache";
|
|
12
|
+
import { R_KEYs } from "./objects/Enums";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Main class for communicating with our backend REST api.
|
|
16
|
+
*/
|
|
17
|
+
class APIController
|
|
18
|
+
{
|
|
19
|
+
static #R_KEY = "-1";
|
|
20
|
+
static #CLIENT_KEY = "-1";
|
|
21
|
+
|
|
22
|
+
static UserSource = null;
|
|
23
|
+
|
|
24
|
+
static onErrorReceived = null
|
|
25
|
+
static onConfimationReceived = null;
|
|
26
|
+
static onWarningReceived = null;
|
|
27
|
+
|
|
28
|
+
static LastCall = null;
|
|
29
|
+
static ConfirmationToken = null;
|
|
30
|
+
|
|
31
|
+
static IsTestServer = null;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* R_KEY MUST be loaded before calling this method!
|
|
35
|
+
* @returns Returns appropriate rest url based on current server.
|
|
36
|
+
*/
|
|
37
|
+
static REST_URL()
|
|
38
|
+
{
|
|
39
|
+
if (location.hostname === 'localhost' || this.#R_KEY === R_KEYs.r_key_test)
|
|
40
|
+
{
|
|
41
|
+
return "https://api.test.itutoring.cz/";
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (this.#R_KEY === R_KEYs.r_key_live)
|
|
46
|
+
{
|
|
47
|
+
return "https://api.itutoring.cz/";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static TestServerCheck()
|
|
52
|
+
{
|
|
53
|
+
return this.IsTestServer;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static async CreateVisitorSession()
|
|
57
|
+
{
|
|
58
|
+
var id = '';
|
|
59
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
60
|
+
if (urlParams.has("vid"))
|
|
61
|
+
{
|
|
62
|
+
if (CookiesManager.GetCookie("session_url") != null)
|
|
63
|
+
CookiesManager.RemoveCookie("session_url");
|
|
64
|
+
|
|
65
|
+
id = urlParams.get("vid");
|
|
66
|
+
const date = new Date();
|
|
67
|
+
date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
|
|
68
|
+
CookiesManager.SetCookie("session_url", id, date.toUTCString());
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
{
|
|
72
|
+
id = Date.now();
|
|
73
|
+
const date = new Date();
|
|
74
|
+
date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
|
|
75
|
+
CookiesManager.SetCookie("session", id, date.toUTCString());
|
|
76
|
+
}
|
|
77
|
+
this.ReadUserSource();
|
|
78
|
+
|
|
79
|
+
var rKey = await this.GetLocalRKey();
|
|
80
|
+
this.IsTestServer = rKey === R_KEYs.r_key_test || location.hostname === 'localhost';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static GetVisitorSessionID()
|
|
84
|
+
{
|
|
85
|
+
//console.log(CookiesManager.GetCookie("session"));
|
|
86
|
+
if (CookiesManager.GetCookie("session_url") != null)
|
|
87
|
+
return CookiesManager.GetCookie("session_url");
|
|
88
|
+
return CookiesManager.GetCookie("session");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static ReadUserSource()
|
|
92
|
+
{
|
|
93
|
+
this.UserSource = CookiesManager.GetCookie("_o");
|
|
94
|
+
if (this.UserSource != null)
|
|
95
|
+
return;
|
|
96
|
+
|
|
97
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
98
|
+
if (urlParams.has("o"))
|
|
99
|
+
{
|
|
100
|
+
const source = urlParams.get("o");
|
|
101
|
+
if (this.UserSource == null)
|
|
102
|
+
{
|
|
103
|
+
const date = new Date();
|
|
104
|
+
date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
|
|
105
|
+
CookiesManager.SetCookie("_o", source, date.toUTCString());
|
|
106
|
+
this.UserSource = source;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static async ConfirmRequest()
|
|
112
|
+
{
|
|
113
|
+
if (this.LastCall === null || this.ConfirmationToken === null)
|
|
114
|
+
return;
|
|
115
|
+
|
|
116
|
+
if (this.LastCall.data == null)
|
|
117
|
+
this.LastCall.data = {};
|
|
118
|
+
this.LastCall.data['conf_token'] = this.ConfirmationToken;
|
|
119
|
+
if (this.LastCall.type === "GET")
|
|
120
|
+
return await this.Get(this.LastCall.module, this.LastCall.method, this.LastCall.data);
|
|
121
|
+
else if (this.LastCall.type === "POST")
|
|
122
|
+
return await this.Post(this.LastCall.module, this.LastCall.method, this.LastCall.data);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static ResolveResponse(request)
|
|
126
|
+
{
|
|
127
|
+
const json = JSON.parse(request.responseText);
|
|
128
|
+
if (json.code === 200)
|
|
129
|
+
{
|
|
130
|
+
// check for redirect
|
|
131
|
+
if (json.data === "redirect" && json.url !== undefined)
|
|
132
|
+
{
|
|
133
|
+
window.location.href = json.url;
|
|
134
|
+
}
|
|
135
|
+
if ((json.data != null || json.data != undefined) && json.data['conf_request'] !== undefined)
|
|
136
|
+
{
|
|
137
|
+
if (APIController.onConfimationReceived !== null && APIController.onConfimationReceived !== undefined)
|
|
138
|
+
{
|
|
139
|
+
APIController.ConfirmationToken = json.data['conf_request']['token'];
|
|
140
|
+
json.data['conf_request']["confirm"] = this.ConfirmRequest.bind(this);
|
|
141
|
+
APIController.onConfimationReceived(json.data['conf_request']);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (json.warnings != null && Object.entries(json.warnings).length > 0)
|
|
145
|
+
{
|
|
146
|
+
if (APIController.onWarningReceived !== null && APIController.onWarningReceived !== undefined)
|
|
147
|
+
{
|
|
148
|
+
APIController.onWarningReceived(json.warnings);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return (json.data);
|
|
152
|
+
}
|
|
153
|
+
if (json.code === 400)
|
|
154
|
+
{
|
|
155
|
+
console.log("API Error:");
|
|
156
|
+
console.log("Error: " + json.error_message);
|
|
157
|
+
console.log("ErrorCode: " + json.error_code);
|
|
158
|
+
console.log("StackTrace: " + json.stack_trace);
|
|
159
|
+
const error = {
|
|
160
|
+
error: {
|
|
161
|
+
message: json.error_message,
|
|
162
|
+
code: json.error_code,
|
|
163
|
+
stackTrace: json.stack_trace
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
if (APIController.onErrorReceived !== null && APIController.onErrorReceived !== undefined)
|
|
167
|
+
{
|
|
168
|
+
APIController.onErrorReceived(error);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return (JSON.stringify(error));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
*
|
|
178
|
+
* @param module "Name of API module"
|
|
179
|
+
* @param method "Name of API method "
|
|
180
|
+
* @param data "data must be as array - key, value pair. They'll be passed into the request"
|
|
181
|
+
* @returns "Response from server"
|
|
182
|
+
*/
|
|
183
|
+
static async Get(module, method, data, useCache = false, cacheSuffix = "")
|
|
184
|
+
{
|
|
185
|
+
if (useCache && APICache.IsCached(module + method + cacheSuffix))
|
|
186
|
+
{
|
|
187
|
+
return APICache.Retrive(module + method + cacheSuffix);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
await APIController.GetLocalRKey();
|
|
191
|
+
await APIController.GetClientKey();
|
|
192
|
+
|
|
193
|
+
return new Promise(resolve =>
|
|
194
|
+
{
|
|
195
|
+
const args = APIController.GetArgsFromArray(data);
|
|
196
|
+
|
|
197
|
+
const request = new XMLHttpRequest();
|
|
198
|
+
request.withCredentials = true;
|
|
199
|
+
request.open("GET", APIController.REST_URL() + module + "/" + method + "?" + args, true);
|
|
200
|
+
request.onreadystatechange = function ()
|
|
201
|
+
{
|
|
202
|
+
if (request.readyState === 4)
|
|
203
|
+
{
|
|
204
|
+
var response = APIController.ResolveResponse(request);
|
|
205
|
+
if (useCache)
|
|
206
|
+
{
|
|
207
|
+
APICache.Cache(module + method + cacheSuffix, response);
|
|
208
|
+
}
|
|
209
|
+
if (response != null && response['conf_request'] !== undefined)
|
|
210
|
+
APIController.LastCall = {
|
|
211
|
+
"type": "GET",
|
|
212
|
+
"module": module,
|
|
213
|
+
"method": method,
|
|
214
|
+
"data": data,
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
resolve(response);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
222
|
+
request.setRequestHeader("r-key", APIController.#R_KEY);
|
|
223
|
+
request.setRequestHeader("sites", location.hostname);
|
|
224
|
+
request.setRequestHeader("key", APIController.#CLIENT_KEY);
|
|
225
|
+
request.setRequestHeader("visitor-session", this.GetVisitorSessionID());
|
|
226
|
+
if (this.UserSource != null)
|
|
227
|
+
request.setRequestHeader("visitor-source", this.UserSource);
|
|
228
|
+
request.send();
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
*
|
|
234
|
+
* @param module "Name of API module"
|
|
235
|
+
* @param method "Name of API method "
|
|
236
|
+
* @param data "data must be as array - key, value pair. They'll be passed into the request"
|
|
237
|
+
* @param file
|
|
238
|
+
* @returns "Response from server"
|
|
239
|
+
*/
|
|
240
|
+
static async Post(module, method, data, file = null, useCache = false)
|
|
241
|
+
{
|
|
242
|
+
if (useCache && APICache.IsCached(module + method))
|
|
243
|
+
{
|
|
244
|
+
return APICache.Retrive(module + method);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
await APIController.GetLocalRKey();
|
|
248
|
+
await APIController.GetClientKey();
|
|
249
|
+
|
|
250
|
+
return new Promise(resolve =>
|
|
251
|
+
{
|
|
252
|
+
let formData = new FormData();
|
|
253
|
+
if (file != null)
|
|
254
|
+
{
|
|
255
|
+
formData = new FormData();
|
|
256
|
+
formData.append("file", file);
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const args = APIController.GetArgsFromArray(data);
|
|
262
|
+
|
|
263
|
+
const request = new XMLHttpRequest();
|
|
264
|
+
request.open("POST", APIController.REST_URL() + module + "/" + method, true);
|
|
265
|
+
|
|
266
|
+
request.onreadystatechange = function ()
|
|
267
|
+
{
|
|
268
|
+
if (request.readyState === 4)
|
|
269
|
+
{
|
|
270
|
+
var response = APIController.ResolveResponse(request);
|
|
271
|
+
if (useCache)
|
|
272
|
+
{
|
|
273
|
+
APICache.Cache(module + method, response);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (response != null && response['conf_request'] !== undefined)
|
|
277
|
+
APIController.LastCall = {
|
|
278
|
+
"type": "POST",
|
|
279
|
+
"module": module,
|
|
280
|
+
"method": method,
|
|
281
|
+
"data": data,
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
resolve(response);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
request.setRequestHeader("r-key", APIController.#R_KEY);
|
|
289
|
+
request.setRequestHeader("sites", location.hostname);
|
|
290
|
+
request.setRequestHeader("key", APIController.#CLIENT_KEY);
|
|
291
|
+
request.setRequestHeader("visitor-session", this.GetVisitorSessionID());
|
|
292
|
+
if (this.UserSource != null)
|
|
293
|
+
request.setRequestHeader("visitor-source", this.UserSource);
|
|
294
|
+
|
|
295
|
+
if (file != null)
|
|
296
|
+
{
|
|
297
|
+
console.log(formData);
|
|
298
|
+
request.send(formData);
|
|
299
|
+
}
|
|
300
|
+
else
|
|
301
|
+
{
|
|
302
|
+
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
303
|
+
request.send(args);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
static GetArgsFromArray(args)
|
|
309
|
+
{
|
|
310
|
+
let argsString = "";
|
|
311
|
+
|
|
312
|
+
if (args != null)
|
|
313
|
+
{
|
|
314
|
+
for (const [key, value] of Object.entries(args))
|
|
315
|
+
{
|
|
316
|
+
argsString += key + "=" + value + "&";
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return argsString;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
*
|
|
324
|
+
* @returns r_key for this server
|
|
325
|
+
*/
|
|
326
|
+
static GetLocalRKey()
|
|
327
|
+
{
|
|
328
|
+
return new Promise(resolve =>
|
|
329
|
+
{
|
|
330
|
+
if (APIController.#R_KEY !== "-1")
|
|
331
|
+
{
|
|
332
|
+
resolve(APIController.#R_KEY);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (localStorage.getItem("r_key") !== null)
|
|
337
|
+
{
|
|
338
|
+
APIController.#R_KEY = localStorage.getItem("r_key");
|
|
339
|
+
resolve(APIController.#R_KEY);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// For localhost don't use xml config, but automatically
|
|
344
|
+
// return test key.
|
|
345
|
+
if (location.hostname === "localhost")
|
|
346
|
+
{
|
|
347
|
+
APIController.#R_KEY = R_KEYs.r_key_test;
|
|
348
|
+
resolve(APIController.#R_KEY);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
else
|
|
352
|
+
{
|
|
353
|
+
fetch('/key_config.xml').then(response => response.text()).then((data) =>
|
|
354
|
+
{
|
|
355
|
+
const parser = new DOMParser();
|
|
356
|
+
const rKeyXML = parser.parseFromString(data, "text/xml");
|
|
357
|
+
const rKey = rKeyXML.getElementsByTagName("key")[0].childNodes[0].nodeValue;
|
|
358
|
+
|
|
359
|
+
APIController.#R_KEY = rKey;
|
|
360
|
+
localStorage.setItem("r_key", APIController.#R_KEY)
|
|
361
|
+
resolve(APIController.#R_KEY);
|
|
362
|
+
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
static GetClientKey()
|
|
369
|
+
{
|
|
370
|
+
return new Promise(resolve =>
|
|
371
|
+
{
|
|
372
|
+
if (APIController.#CLIENT_KEY !== "-1")
|
|
373
|
+
{
|
|
374
|
+
resolve(APIController.#CLIENT_KEY);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (localStorage.getItem("client_key") !== null)
|
|
379
|
+
{
|
|
380
|
+
APIController.#CLIENT_KEY = localStorage.getItem("client_key");
|
|
381
|
+
resolve(APIController.#CLIENT_KEY);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let keyPath = '/client_key.xml';
|
|
386
|
+
// For localhost use different xml file (to make suer we won't rewrite the server key when uploading websites)
|
|
387
|
+
if (location.hostname === "localhost")
|
|
388
|
+
{
|
|
389
|
+
keyPath = '/local_client_key.xml'
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
fetch(keyPath).then(response => response.text()).then((data) =>
|
|
393
|
+
{
|
|
394
|
+
const parser = new DOMParser();
|
|
395
|
+
const keyXML = parser.parseFromString(data, "text/xml");
|
|
396
|
+
const key = keyXML.getElementsByTagName("key")[0].childNodes[0].nodeValue;
|
|
397
|
+
|
|
398
|
+
APIController.#CLIENT_KEY = key;
|
|
399
|
+
localStorage.setItem("client_key", APIController.#CLIENT_KEY)
|
|
400
|
+
resolve(APIController.#CLIENT_KEY);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
static async GetCurrentHostURl()
|
|
407
|
+
{
|
|
408
|
+
if (location.hostname === 'localhost')
|
|
409
|
+
{
|
|
410
|
+
return "http://localhost:3000";
|
|
411
|
+
}
|
|
412
|
+
else
|
|
413
|
+
{
|
|
414
|
+
var r_key = await this.GetLocalRKey();
|
|
415
|
+
if (r_key === R_KEYs.r_key_test)
|
|
416
|
+
{
|
|
417
|
+
return "https://test.itutoring.cz";
|
|
418
|
+
}
|
|
419
|
+
else
|
|
420
|
+
{
|
|
421
|
+
return "https://itutoring.cz";
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
static IntToBool(value)
|
|
427
|
+
{
|
|
428
|
+
return value === 1;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
static BoolToInt(value)
|
|
432
|
+
{
|
|
433
|
+
return value ? 1 : 0;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
437
|
export default APIController;
|