Skip to content

Commit 27d7330

Browse files
authored
Merge pull request #977 from OneSignal/user-model/post-backend-fixes
User model/post backend fixes
2 parents 2832233 + 819bcb8 commit 27d7330

File tree

7 files changed

+39
-17
lines changed

7 files changed

+39
-17
lines changed

src/core/executors/ExecutorBase.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export default abstract class ExecutorBase {
4949

5050
public enqueueDelta(delta: CoreDelta<SupportedModel>): void {
5151
logMethodCall("ExecutorBase.enqueueDelta", { delta });
52-
// deep copy (snapshot)
52+
/**
53+
* deep copy (snapshot)
54+
* if we add alias and then login to a user, we want to ensure that the external id of the
55+
* login call doesn't get included in the add alias call so this helps keep the changes separate
56+
*/
5357
const deltaCopy = JSON.parse(JSON.stringify(delta));
5458
this._deltaQueue.push(deltaCopy);
5559
}

src/core/operationRepo/Operation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Operation<Model> {
1717
constructor(readonly changeType: CoreChangeType, readonly modelName: ModelName, deltas?: CoreDelta<Model>[]) {
1818
this.operationId = Math.random().toString(36).substring(2);
1919
this.payload = deltas ? this.getPayload(deltas) : undefined;
20-
this.model = deltas ? deltas[0].model : undefined;
20+
this.model = deltas ? deltas[deltas.length-1].model : undefined;
2121
this.timestamp = Date.now();
2222
this.jwtTokenAvailable = new Promise<void>(async resolve => {
2323
this.jwtToken = await Database.getJWTToken();

src/core/requestService/RequestService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class RequestService {
152152
subscription: Partial<SubscriptionModel>)
153153
: Promise<OneSignalApiBaseResponse> {
154154
const { appId } = requestMetadata;
155-
return OneSignalApiBase.patch(`apps/${appId}/subscriptions/${subscriptionId}`, subscription);
155+
return OneSignalApiBase.patch(`apps/${appId}/subscriptions/${subscriptionId}`, { subscription });
156156
}
157157

158158
/**
@@ -192,7 +192,7 @@ export class RequestService {
192192
const { appId } = requestMetadata;
193193
return OneSignalApiBase.patch(
194194
`apps/${appId}/users/by/subscriptions/${subscriptionId}/identity`,
195-
identity,
195+
{ identity },
196196
requestMetadata.jwtHeader
197197
);
198198
}
@@ -214,7 +214,7 @@ export class RequestService {
214214
retainPreviousOwner: boolean): Promise<OneSignalApiBaseResponse> {
215215
const { appId } = requestMetadata;
216216
return OneSignalApiBase.patch(`apps/${appId}/subscriptions/${subscriptionId}/owner`, {
217-
identity,
217+
identity: { ...identity },
218218
retain_previous_owner: retainPreviousOwner
219219
}, requestMetadata.jwtHeader);
220220
}

src/core/requestService/SubscriptionRequests.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,19 @@ export default class SubscriptionRequests {
4141
static async updateSubscription<Model>(operation: Operation<Model>): Promise<ExecutorResult<SupportedSubscription>> {
4242
logMethodCall("SubscriptionRequests.updateSubscription", operation);
4343

44-
const { subscription, subscriptionId } = processSubscriptionOperation(operation);
44+
const { payload, subscriptionId } = processSubscriptionOperation(operation);
4545

4646
if (!subscriptionId) {
4747
throw new OneSignalError("updateSubscription: subscriptionId is not defined");
4848
}
49+
50+
if (!payload) {
51+
throw new OneSignalError("updateSubscription: payload is not defined");
52+
}
53+
4954
const appId = await MainHelper.getAppId();
5055

51-
const response = await RequestService.updateSubscription({ appId }, subscriptionId, subscription);
56+
const response = await RequestService.updateSubscription({ appId }, subscriptionId, payload);
5257
return SubscriptionRequests._processSubscriptionResponse(response);
5358
}
5459

src/core/requestService/helpers.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function processSubscriptionOperation<Model>(operation: Operation<Model>)
1111
subscription: SupportedSubscription;
1212
aliasPair: AliasPair;
1313
subscriptionId?: string;
14+
payload?: Partial<SupportedSubscription>
1415
} {
1516
const subscriptionOSModel = operation.model;
1617
const subscription = subscriptionOSModel?.data;
@@ -25,20 +26,21 @@ export function processSubscriptionOperation<Model>(operation: Operation<Model>)
2526
throw new OneSignalError(`processSubscriptionModel: bad subscription object: ${JSON.stringify(subscription)}`);
2627
}
2728

28-
let subscriptionId;
29-
if (isCompleteSubscriptionObject(subscription)) {
30-
subscriptionId = subscription?.id;
31-
}
32-
3329
// fixes typescript errors
3430
if (!subscriptionOSModel.onesignalId) {
3531
throw new OneSignalError(`processSubscriptionModel: missing onesignalId: ${JSON.stringify(subscriptionOSModel)}`);
3632
}
3733

34+
let subscriptionId;
35+
if (isCompleteSubscriptionObject(subscription)) {
36+
subscriptionId = subscription?.id;
37+
}
38+
3839
return {
3940
subscription,
4041
aliasPair: new AliasPair(AliasPair.ONESIGNAL_ID, subscriptionOSModel.onesignalId),
4142
subscriptionId,
43+
payload: operation.payload as Partial<SupportedSubscription>
4244
};
4345
}
4446

@@ -54,14 +56,17 @@ export function processIdentityOperation<Model>(operation: Operation<Model>): {
5456
}
5557

5658
const { onesignal_id: onesignalId } = identity;
59+
// delete onesignal_id from identity object, backend expects it to be in the URI only
60+
const identityCopy = JSON.parse(JSON.stringify(identity));
61+
delete identityCopy['onesignal_id'];
5762

5863
// fixes typescript errors
5964
if (!onesignalId) {
6065
throw new OneSignalError(`processIdentityModel: missing onesignalId: ${JSON.stringify(identity)}`);
6166
}
6267

6368
return {
64-
identity,
69+
identity: identityCopy,
6570
aliasPair: new AliasPair(AliasPair.ONESIGNAL_ID, onesignalId)
6671
};
6772
}

src/page/managers/LoginManager.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ export default class LoginManager {
4646
static async identifyUser(userData: UserData, pushSubscriptionId?: string): Promise<Partial<UserData>> {
4747
logMethodCall("LoginManager.identifyUser", { userData, pushSubscriptionId });
4848

49+
const { onesignal_id: onesignalId } = userData.identity;
50+
51+
// only accepts one alias, so remove other aliases only leaving external_id
52+
this.prepareIdentityForUpsert(userData);
4953
let { identity } = userData;
5054

51-
if (!identity || !identity.onesignal_id) {
55+
if (!identity || !onesignalId) {
5256
throw new OneSignalError("identifyUser failed: no identity found");
5357
}
5458

5559
const appId = await MainHelper.getAppId();
56-
const aliasPair = new AliasPair(AliasPair.ONESIGNAL_ID, identity.onesignal_id);
60+
const aliasPair = new AliasPair(AliasPair.ONESIGNAL_ID, onesignalId);
5761
const identifyUserResponse = await RequestService.addAlias({ appId }, aliasPair, identity);
5862

5963
const identifyResponseStatus = identifyUserResponse?.status;
@@ -63,8 +67,6 @@ export default class LoginManager {
6367
Log.info(`identifyUser failed: externalId already exists. Attempting to transfer push subscription...`);
6468

6569
const retainPreviousOwner = false;
66-
// only accepts one alias, so remove other aliases only leaving external_id
67-
this.prepareIdentityForUpsert(userData);
6870
identity = userData.identity;
6971
const transferResponse = await RequestService.transferSubscription(
7072
{ appId },

src/shared/managers/SubscriptionManager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ export class SubscriptionManager {
180180
);
181181
}
182182
pushModel.set("notification_types", notificationTypes);
183+
184+
if (notificationTypes === 1) {
185+
pushModel.set('enabled', true);
186+
} else if (notificationTypes === -2) {
187+
pushModel.set('enabled', false);
188+
}
183189
}
184190

185191
/**

0 commit comments

Comments
 (0)