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 17, 2024
1 parent cae70e1 commit 1f73e80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
35 changes: 23 additions & 12 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 @@ -260,7 +261,7 @@ export class CoreModuleDirector {
const pushToken = await MainHelper.getCurrentPushToken();
if (pushToken) {
return this.getSubscriptionOfTypeWithToken(
ModelName.Subscriptions,
SubscriptionChannel.Push,
pushToken,
);
}
Expand All @@ -278,7 +279,7 @@ export class CoreModuleDirector {
const { lastKnownPushToken } = await Database.getAppState();
if (lastKnownPushToken) {
return this.getSubscriptionOfTypeWithToken(
ModelName.Subscriptions,
SubscriptionChannel.Push,
lastKnownPushToken,
);
}
Expand Down Expand Up @@ -332,7 +333,7 @@ export class CoreModuleDirector {
}

public getSubscriptionOfTypeWithToken(
type: SubscriptionType | ModelName.Subscriptions,
type: SubscriptionChannel | undefined,
token: string,
): OSModel<SupportedSubscription> | undefined {
logMethodCall('CoreModuleDirector.getSubscriptionOfTypeWithToken', {
Expand All @@ -343,21 +344,16 @@ export class CoreModuleDirector {
let subscriptions: Record<string, OSModel<SupportedSubscription>>;

switch (type) {
case SubscriptionType.Email:
case SubscriptionChannel.Email:
subscriptions = this.getEmailSubscriptionModels();
break;
case SubscriptionType.SMS:
case SubscriptionChannel.SMS:
subscriptions = this.getSmsSubscriptionModels();
break;
case ModelName.Subscriptions:
case SubscriptionChannel.Push:
subscriptions = this.getAllPushSubscriptionModels();
break;
default:
if (this.isPushSubscriptionType(type)) {
subscriptions = this.getAllPushSubscriptionModels();
break;
}

return undefined;
}

Expand Down Expand Up @@ -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 1f73e80

Please sign in to comment.