-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathUpdateUserOperationExecutor.ts
More file actions
196 lines (173 loc) · 6.16 KB
/
UpdateUserOperationExecutor.ts
File metadata and controls
196 lines (173 loc) · 6.16 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
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
import {
getResponseStatusType,
ResponseStatusType,
} from 'src/shared/helpers/network';
import Log from 'src/shared/libraries/Log';
import { IdentityConstants, OPERATION_NAME } from '../constants';
import { type IPropertiesModelKeys } from '../models/PropertiesModel';
import { type IdentityModelStore } from '../modelStores/IdentityModelStore';
import { PropertiesModelStore } from '../modelStores/PropertiesModelStore';
import { type NewRecordsState } from '../operationRepo/NewRecordsState';
import { Operation } from '../operations/Operation';
import { SetPropertyOperation } from '../operations/SetPropertyOperation';
import { updateUserByAlias } from '../requests/api';
import { ModelChangeTags } from '../types/models';
import type { ExecutionResponse } from '../types/operation';
import { ExecutionResult, type IOperationExecutor } from '../types/operation';
import { type IRebuildUserService } from '../types/user';
type PropertiesObject = {
ip?: string;
tags?: Record<string, string>;
language?: string;
timezone_id?: string;
country?: string;
};
// Implements logic similar to Android's SDK's UpdateUserOperationExecutor
// Reference: https://github.com/OneSignal/OneSignal-Android-SDK/blob/5.1.31/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/UpdateUserOperationExecutor.kt
export class UpdateUserOperationExecutor implements IOperationExecutor {
private _identityModelStore: IdentityModelStore;
private _propertiesModelStore: PropertiesModelStore;
private _buildUserService: IRebuildUserService;
private _newRecordState: NewRecordsState;
constructor(
_identityModelStore: IdentityModelStore,
_propertiesModelStore: PropertiesModelStore,
_buildUserService: IRebuildUserService,
_newRecordState: NewRecordsState,
) {
this._identityModelStore = _identityModelStore;
this._propertiesModelStore = _propertiesModelStore;
this._buildUserService = _buildUserService;
this._newRecordState = _newRecordState;
}
get _operations(): string[] {
return [OPERATION_NAME._SetProperty];
}
private _processOperations(operations: Operation[]) {
let appId: string | null = null;
let onesignalId: string | null = null;
let propertiesObject: PropertiesObject = {};
const refreshDeviceMetadata = false;
for (const operation of operations) {
if (operation instanceof SetPropertyOperation) {
if (!appId) {
appId = operation._appId;
onesignalId = operation._onesignalId;
}
propertiesObject = createPropertiesFromOperation(
operation,
propertiesObject,
);
} else {
throw new Error(`Unrecognized operation: ${operation}`);
}
}
return {
appId,
onesignalId,
propertiesObject,
refreshDeviceMetadata,
};
}
async _execute(operations: Operation[]): Promise<ExecutionResponse> {
Log._debug(`UpdateUserOpExec(${operations})`);
const { appId, onesignalId, propertiesObject, refreshDeviceMetadata } =
this._processOperations(operations);
if (!appId || !onesignalId) return { _result: ExecutionResult._Success };
const response = await updateUserByAlias(
{ appId },
{
label: IdentityConstants._OneSignalID,
id: onesignalId,
},
{
properties: propertiesObject,
refresh_device_metadata: refreshDeviceMetadata,
},
);
const { ok, retryAfterSeconds, status } = response;
const isTagProperty = (
op: SetPropertyOperation,
): op is SetPropertyOperation<'tags'> => op._property === 'tags';
if (ok) {
if (this._identityModelStore._model._onesignalId === onesignalId) {
for (const operation of operations) {
if (operation instanceof SetPropertyOperation) {
// removing empty string tags from operation.value to save space in IndexedDB and local memory.
let value = operation.value;
if (isTagProperty(operation)) {
value = { ...operation.value };
for (const key in value) if (value[key] === '') delete value[key];
}
this._propertiesModelStore._model._setProperty(
operation._property as IPropertiesModelKeys,
value,
ModelChangeTags._Hydrate,
);
}
}
}
return { _result: ExecutionResult._Success };
}
const responseType = getResponseStatusType(status);
switch (responseType) {
case ResponseStatusType._Retryable:
return {
_result: ExecutionResult._FailRetry,
_retryAfterSeconds: retryAfterSeconds,
};
case ResponseStatusType._Unauthorized:
return {
_result: ExecutionResult._FailUnauthorized,
_retryAfterSeconds: retryAfterSeconds,
};
case ResponseStatusType._Missing: {
if (
status === 404 &&
this._newRecordState._isInMissingRetryWindow(onesignalId)
) {
return {
_result: ExecutionResult._FailRetry,
_retryAfterSeconds: retryAfterSeconds,
};
}
const rebuildOps =
await this._buildUserService._getRebuildOperationsIfCurrentUser(
appId,
onesignalId,
);
if (!rebuildOps) return { _result: ExecutionResult._FailNoretry };
return {
_result: ExecutionResult._FailRetry,
_retryAfterSeconds: retryAfterSeconds,
_operations: rebuildOps,
};
}
default:
return { _result: ExecutionResult._FailNoretry };
}
}
}
function createPropertiesFromOperation(
operation: Operation,
properties: PropertiesObject,
): PropertiesObject {
if (operation instanceof SetPropertyOperation) {
const propertyKey = operation._property;
const allowedKeys: IPropertiesModelKeys[] = [
'ip',
'tags',
'language',
'timezone_id',
'country',
];
if (allowedKeys.includes(propertyKey as IPropertiesModelKeys)) {
return {
...properties,
[propertyKey]: operation.value,
};
}
return { ...properties };
}
throw new Error(`Unsupported operation type: ${operation._name}`);
}