-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathOperationModelStore.ts
More file actions
115 lines (103 loc) · 4.13 KB
/
OperationModelStore.ts
File metadata and controls
115 lines (103 loc) · 4.13 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
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
import type { IDBStoreName } from 'src/shared/database/types';
import Log from 'src/shared/libraries/Log';
import { OPERATION_NAME } from '../constants';
import { CreateSubscriptionOperation } from '../operations/CreateSubscriptionOperation';
import { DeleteAliasOperation } from '../operations/DeleteAliasOperation';
import { DeleteSubscriptionOperation } from '../operations/DeleteSubscriptionOperation';
import { LoginUserOperation } from '../operations/LoginUserOperation';
import { Operation } from '../operations/Operation';
import { RefreshUserOperation } from '../operations/RefreshUserOperation';
import { SetAliasOperation } from '../operations/SetAliasOperation';
import { SetPropertyOperation } from '../operations/SetPropertyOperation';
import { TrackCustomEventOperation } from '../operations/TrackCustomEventOperation';
import { TransferSubscriptionOperation } from '../operations/TransferSubscriptionOperation';
import { UpdateSubscriptionOperation } from '../operations/UpdateSubscriptionOperation';
import { ModelStore } from './ModelStore';
// Implements logic similar to Android SDK's OperationModelStore
// Reference: https://github.com/OneSignal/OneSignal-Android-SDK/blob/5.1.31/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl/OperationModelStore.kt
export class OperationModelStore extends ModelStore<Operation> {
constructor() {
super('operations' satisfies IDBStoreName);
}
async _loadOperations(): Promise<void> {
return this._load();
}
_create(jsonObject?: { name?: string } | null): Operation | null {
if (jsonObject === null) {
Log._error('null jsonObject in OpModelStore.create');
return null;
}
if (!this._isValidOperation(jsonObject)) {
return null;
}
// Determine the type of operation based on the name property in the json
const operationName = jsonObject.name;
let operation: Operation;
switch (operationName) {
case OPERATION_NAME._SetAlias:
operation = new SetAliasOperation();
break;
case OPERATION_NAME._DeleteAlias:
operation = new DeleteAliasOperation();
break;
case OPERATION_NAME._CreateSubscription:
operation = new CreateSubscriptionOperation();
break;
case OPERATION_NAME._UpdateSubscription:
operation = new UpdateSubscriptionOperation();
break;
case OPERATION_NAME._DeleteSubscription:
operation = new DeleteSubscriptionOperation();
break;
case OPERATION_NAME._TransferSubscription:
operation = new TransferSubscriptionOperation();
break;
case OPERATION_NAME._LoginUser:
operation = new LoginUserOperation();
break;
case OPERATION_NAME._RefreshUser:
operation = new RefreshUserOperation();
break;
case OPERATION_NAME._SetProperty:
operation = new SetPropertyOperation();
break;
case OPERATION_NAME._CustomEvent:
operation = new TrackCustomEventOperation();
break;
default:
throw new Error(`Unrecognized operation: ${operationName}`);
}
// populate the operation with the data
operation._initializeFromJson(jsonObject);
return operation;
}
/**
* Checks if a JSON object is a valid Operation. Contains a check for onesignalId.
* This is a rare case that a cached Operation is missing the onesignalId,
* which would continuously cause crashes when the Operation is processed.
*
* @param object The JSON object that represents an Operation
*/
private _isValidOperation(object?: {
name?: string;
onesignalId?: string;
}): object is {
name: string;
onesignalId?: string;
} {
const operationName = object?.name;
if (!operationName) {
Log._error("Missing 'name' attribute");
return false;
}
const excluded = new Set<string>([OPERATION_NAME._LoginUser]);
// Must have onesignalId if it is not one of the excluded operations above
if (!object.onesignalId && !excluded.has(operationName)) {
Log._error(
`${operationName} jsonObject must have 'onesignalId' attribute`,
);
return false;
}
return true;
}
}