-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.ts
More file actions
90 lines (74 loc) · 2.88 KB
/
app.ts
File metadata and controls
90 lines (74 loc) · 2.88 KB
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
import { AnalyticsController } from './controllers/Analytics.controller'
import { NetworkController } from './controllers/Network.controller'
import { SessionController } from './controllers/Session.controller'
import { BatchService } from "./services/Batch.service";
import { HumanProofService } from "./services/HumanProof.service";
import {InvoicePayload} from "../declaration";
import {Events} from "./constants";
export class App {
private sessionController: SessionController;
private networkController: NetworkController;
private analyticsController: AnalyticsController;
private batchService: BatchService;
private humanProofService: HumanProofService;
private readonly apiToken: string;
private readonly appName: string;
public taskParams: string | undefined | null = null;
public env: 'STG' | 'PROD';
constructor(apiToken: string, appName: string, env: 'STG' | 'PROD') {
this.env = env;
this.apiToken = apiToken;
this.appName = appName;
this.humanProofService = new HumanProofService(this);
this.sessionController = new SessionController(this);
this.networkController = new NetworkController(this);
this.analyticsController = new AnalyticsController(this);
this.batchService = new BatchService(this);
}
public async init() {
this.sessionController.init();
await this.analyticsController.init();
await this.humanProofService.init().catch(e => console.error(e));
this.networkController.init();
this.batchService.init();
}
public assembleEventSession() {
return this.sessionController.assembleEventSession();
}
public recordEvent(
event_name: string,
data?: Record<string, any>,
attributes?: Record<string, any>,
) {
return this.networkController.recordEvent(event_name, data, attributes);
}
public recordEvents(
data: Record<string, any>[],
) {
return this.networkController.recordEvents(data);
}
public collectEvent(event_name: string, requestBody?: Record<string, any>){
this.batchService.collect(event_name, {
...requestBody,
...this.assembleEventSession(),
});
}
public registerInvoice(invoicePayload: InvoicePayload) {
this.batchService.collect(Events.INVOICE_REGISTERED, {
...invoicePayload,
...this.assembleEventSession(),
});
}
public getApiToken() {
return this.apiToken;
}
public getAppName() {
return this.appName;
}
public async solveHumanProofTask(): Promise<string | undefined> {
if (this.taskParams === null) {
this.taskParams = await this.humanProofService.getInitialParams().catch(_err => undefined);
}
return await this.humanProofService.solveTask(this.taskParams).catch(_err => undefined);
}
}