-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathOperationModelStore.ts
More file actions
122 lines (110 loc) · 4.37 KB
/
Copy pathOperationModelStore.ts
File metadata and controls
122 lines (110 loc) · 4.37 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
116
117
118
119
120
121
122
import Log from 'src/shared/libraries/Log';
import { IPreferencesService } from 'src/types/preferences';
import { OPERATION_NAME } from '../executors/constants';
import { CreateSubscriptionOperation } from '../operations/CreateSubscriptionOperation';
import { DeleteAliasOperation } from '../operations/DeleteAliasOperation';
import { DeleteSubscriptionOperation } from '../operations/DeleteSubscriptionOperation';
import { DeleteTagOperation } from '../operations/DeleteTagOperation';
import { Operation } from '../operations/Operation';
import { RefreshUserOperation } from '../operations/RefreshUserOperation';
import { SetAliasOperation } from '../operations/SetAliasOperation';
import { SetPropertyOperation } from '../operations/SetPropertyOperation';
import { SetTagOperation } from '../operations/SetTagOperation';
import { TrackSessionEndOperation } from '../operations/TrackSessionEndOperation';
import { TrackSessionStartOperation } from '../operations/TrackSessionStartOperation';
import { TransferSubscriptionOperation } from '../operations/TransferSubscriptionOperation';
import { UpdateSubscriptionOperation } from '../operations/UpdateSubscriptionOperation';
import { ModelStore } from './ModelStore';
export class OperationModelStore extends ModelStore<Operation> {
constructor(prefs: IPreferencesService) {
super('operations', prefs);
}
loadOperations(): void {
this.load();
}
create(jsonObject: { name?: string } | null): Operation | null {
if (jsonObject === null) {
Log.error('null jsonObject sent to OperationModelStore.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.SET_ALIAS:
operation = new SetAliasOperation();
break;
case OPERATION_NAME.DELETE_ALIAS:
operation = new DeleteAliasOperation();
break;
case OPERATION_NAME.CREATE_SUBSCRIPTION:
operation = new CreateSubscriptionOperation();
break;
case OPERATION_NAME.UPDATE_SUBSCRIPTION:
operation = new UpdateSubscriptionOperation();
break;
case OPERATION_NAME.DELETE_SUBSCRIPTION:
operation = new DeleteSubscriptionOperation();
break;
case OPERATION_NAME.TRANSFER_SUBSCRIPTION:
operation = new TransferSubscriptionOperation();
break;
case OPERATION_NAME.REFRESH_USER:
operation = new RefreshUserOperation();
break;
case OPERATION_NAME.SET_TAG:
operation = new SetTagOperation();
break;
case OPERATION_NAME.DELETE_TAG:
operation = new DeleteTagOperation();
break;
case OPERATION_NAME.SET_PROPERTY:
operation = new SetPropertyOperation();
break;
case OPERATION_NAME.TRACK_SESSION_START:
operation = new TrackSessionStartOperation();
break;
case OPERATION_NAME.TRACK_SESSION_END:
operation = new TrackSessionEndOperation();
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;
}): boolean {
const operationName = object.name;
if (!operationName) {
Log.error("jsonObject must have 'name' attribute");
return false;
}
const excluded = new Set<string>([
// TODO: Add these back in once the executors are implemented
// LoginUserOperationExecutor.LOGIN_USER,
// LoginUserFromSubscriptionOperationExecutor.LOGIN_USER_FROM_SUBSCRIPTION_USER,
]);
// 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;
}
}