Skip to content

Commit

Permalink
addEmail and addSms now save onesignal_id
Browse files Browse the repository at this point in the history
When a pushSubscription is created it creates and then hydrates the
user. We are mkaing this consistent for email and sms. Introduced a new
createAndHydrateUser function to encapsulate this logic.
  • Loading branch information
jkasten2 committed Nov 14, 2023
1 parent 0cdc867 commit 36b8b7b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/onesignal/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default class User {
});
}

public addEmail(email: string): void {
public async addEmail(email: string): Promise<void> {
logMethodCall('addEmail', { email });

if (typeof email !== 'string') {
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class User {
newSubscription,
false,
);
UserDirector.createUserOnServer();
await UserDirector.createAndHydrateUser();
}

UserDirector.updateModelWithCurrentUserOneSignalId(newSubscription).catch(
Expand All @@ -167,7 +167,7 @@ export default class User {
);
}

public addSms(sms: string): void {
public async addSms(sms: string): Promise<void> {
logMethodCall('addSms', { sms });

if (typeof sms !== 'string') {
Expand Down Expand Up @@ -206,7 +206,7 @@ export default class User {
newSubscription,
false,
);
UserDirector.createUserOnServer();
await UserDirector.createAndHydrateUser();
}

UserDirector.updateModelWithCurrentUserOneSignalId(newSubscription).catch(
Expand Down
7 changes: 7 additions & 0 deletions src/onesignal/UserDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export default class UserDirector {
}
}

static async createAndHydrateUser(): Promise<void> {
const userData = await UserDirector.createUserOnServer();
if (userData) {
OneSignal.coreDirector.hydrateUser(userData);
}
}

static async getAllUserData(): Promise<UserData> {
logMethodCall('LoginManager.getAllUserData');

Expand Down
5 changes: 1 addition & 4 deletions src/shared/managers/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ export class SubscriptionManager {

if (!pushModel) {
OneSignal.coreDirector.generatePushSubscriptionModel(rawPushSubscription);
const userData = await UserDirector.createUserOnServer();
if (userData) {
OneSignal.coreDirector.hydrateUser(userData);
}
await UserDirector.createAndHydrateUser();
return;
} else {
// resubscribing. update existing push subscription model
Expand Down

0 comments on commit 36b8b7b

Please sign in to comment.