Skip to content

Commit

Permalink
Move helper method into helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherd-l committed Sep 27, 2024
1 parent 1c2b297 commit d4bbe89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
35 changes: 3 additions & 32 deletions src/core/CoreModuleDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import User from '../onesignal/User';
import OneSignal from '../onesignal/OneSignal';
import Database from '../shared/services/Database';
import EventHelper from '../shared/helpers/EventHelper';
import SubscriptionHelper from '../../src/shared/helpers/SubscriptionHelper';

/* Contains OneSignal User-Model-specific logic*/

Expand Down Expand Up @@ -123,7 +124,7 @@ export class CoreModuleDirector {
*/
const existingSubscription = !!subscription.token
? this.getSubscriptionOfTypeWithToken(
this.toSubscriptionChannel(subscription.type),
SubscriptionHelper.toSubscriptionChannel(subscription.type),
subscription.token,
)
: undefined;
Expand Down Expand Up @@ -247,7 +248,7 @@ export class CoreModuleDirector {

const pushSubscriptions = Object.fromEntries(
Object.entries(subscriptionModels).filter(([, model]) =>
this.isPushSubscriptionType(model.data?.type),
SubscriptionHelper.isPushSubscriptionType(model.data?.type),
),
);

Expand Down Expand Up @@ -367,34 +368,4 @@ export class CoreModuleDirector {
private getModelStores(): ModelStoresMap<SupportedModel> {
return this.core.modelRepo?.modelStores as ModelStoresMap<SupportedModel>;
}

/**
* Helper that checks if a given SubscriptionType is a push subscription.
*/
public isPushSubscriptionType(type: SubscriptionType): boolean {
switch (type) {
case SubscriptionType.ChromePush:
case SubscriptionType.SafariPush:
case SubscriptionType.SafariLegacyPush:
case SubscriptionType.FirefoxPush:
return true;
default:
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;
}
}
}
35 changes: 35 additions & 0 deletions src/shared/helpers/SubscriptionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Log from '../libraries/Log';
import { ContextSWInterface } from '../models/ContextSW';
import SdkEnvironment from '../managers/SdkEnvironment';
import { PermissionUtils } from '../utils/PermissionUtils';
import {
SubscriptionChannel,
SubscriptionType,
} from '../../../src/core/models/SubscriptionModels';

export default class SubscriptionHelper {
public static async registerForPush(): Promise<Subscription | null> {
Expand Down Expand Up @@ -43,4 +47,35 @@ export default class SubscriptionHelper {

return subscription;
}

/**
* Helper that checks if a given SubscriptionType is a push subscription.
*/
public static isPushSubscriptionType(type: SubscriptionType): boolean {
switch (type) {
case SubscriptionType.ChromePush:
case SubscriptionType.SafariPush:
case SubscriptionType.SafariLegacyPush:
case SubscriptionType.FirefoxPush:
return true;
default:
return false;
}
}

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

0 comments on commit d4bbe89

Please sign in to comment.