Skip to content

Commit

Permalink
Add new subscription channel type
Browse files Browse the repository at this point in the history
Create new type to make it clear what subscription channel is being filtered for in getSubscriptionOfTypeWithToken
  • Loading branch information
shepherd-l committed Sep 27, 2024
1 parent 5eab7f9 commit 4d38448
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/core/CoreModuleDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OSModel } from './modelRepo/OSModel';
import { SupportedIdentity } from './models/IdentityModel';
import { ModelStoresMap } from './models/ModelStoresMap';
import {
SubscriptionChannel,
SubscriptionModel,
SubscriptionType,
SupportedSubscription,
Expand Down Expand Up @@ -122,7 +123,7 @@ export class CoreModuleDirector {
*/
const existingSubscription = !!subscription.token
? this.getSubscriptionOfTypeWithToken(
subscription.type,
this.toSubscriptionChannel(subscription.type),
subscription.token,
)
: undefined;
Expand Down Expand Up @@ -259,9 +260,9 @@ export class CoreModuleDirector {
logMethodCall('CoreModuleDirector.getPushSubscriptionModelByCurrentToken');
const pushToken = await MainHelper.getCurrentPushToken();
if (pushToken) {
const pushSubscriptions = this.getAllPushSubscriptionModels();
return Object.values(pushSubscriptions).find(
(subscription) => subscription.data.token === pushToken,
return this.getSubscriptionOfTypeWithToken(
SubscriptionChannel.Push,
pushToken,
);
}
return undefined;
Expand All @@ -277,9 +278,9 @@ export class CoreModuleDirector {
);
const { lastKnownPushToken } = await Database.getAppState();
if (lastKnownPushToken) {
const pushSubscriptions = this.getAllPushSubscriptionModels();
return Object.values(pushSubscriptions).find(
(subscription) => subscription.data.token === lastKnownPushToken,
return this.getSubscriptionOfTypeWithToken(
SubscriptionChannel.Push,
lastKnownPushToken,
);
}
return undefined;
Expand Down Expand Up @@ -332,38 +333,33 @@ export class CoreModuleDirector {
}

public getSubscriptionOfTypeWithToken(
type: SubscriptionType,
type: SubscriptionChannel | undefined,
token: string,
): OSModel<SupportedSubscription> | undefined {
logMethodCall('CoreModuleDirector.getSubscriptionOfTypeWithToken', {
type,
token,
});

let subscriptions: Record<string, OSModel<SupportedSubscription>>;

switch (type) {
case SubscriptionType.Email: {
const emailSubscriptions = this.getEmailSubscriptionModels();
return Object.values(emailSubscriptions).find(
(subscription) => subscription.data.token === token,
);
}
case SubscriptionType.SMS: {
const smsSubscriptions = this.getSmsSubscriptionModels();
return Object.values(smsSubscriptions).find(
(subscription) => subscription.data.token === token,
);
}
case SubscriptionType.ChromePush:
case SubscriptionType.SafariPush:
case SubscriptionType.SafariLegacyPush:
case SubscriptionType.FirefoxPush: {
const pushSubscriptions = this.getAllPushSubscriptionModels();
return Object.values(pushSubscriptions).find(
(subscription) => subscription.data.token === token,
);
}
case SubscriptionChannel.Email:
subscriptions = this.getEmailSubscriptionModels();
break;
case SubscriptionChannel.SMS:
subscriptions = this.getSmsSubscriptionModels();
break;
case SubscriptionChannel.Push:
subscriptions = this.getAllPushSubscriptionModels();
break;
default:
return undefined;
}

return Object.values(subscriptions).find(
(subscription) => subscription.data.token === token,
);
}

/* P R I V A T E */
Expand All @@ -386,4 +382,19 @@ export class CoreModuleDirector {
return false;
}
}

public toSubscriptionChannel(type: SubscriptionType) {
switch (type) {
case SubscriptionType.Email:
return SubscriptionChannel.Email;
case SubscriptionType.SMS:
return SubscriptionChannel.SMS;
default:
if (this.isPushSubscriptionType(type)) {
return SubscriptionChannel.Push;
}

return undefined;
}
}
}
6 changes: 6 additions & 0 deletions src/core/models/SubscriptionModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export enum SubscriptionType {
// There are other OneSignal types, but only including ones used here.
}

export enum SubscriptionChannel {
Email = 'Email',
SMS = 'SMS',
Push = 'Push',
}

export interface FutureSubscriptionModel {
type: SubscriptionType;
token?: string; // maps to legacy player.identifier
Expand Down

0 comments on commit 4d38448

Please sign in to comment.