From 4d38448be4275ab0df828ffa868d772ce572cc0b Mon Sep 17 00:00:00 2001 From: Shepherd Date: Fri, 27 Sep 2024 12:16:58 -0400 Subject: [PATCH] Add new subscription channel type Create new type to make it clear what subscription channel is being filtered for in getSubscriptionOfTypeWithToken --- src/core/CoreModuleDirector.ts | 69 ++++++++++++++++----------- src/core/models/SubscriptionModels.ts | 6 +++ 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/core/CoreModuleDirector.ts b/src/core/CoreModuleDirector.ts index f94520361..7ef1d0cd6 100644 --- a/src/core/CoreModuleDirector.ts +++ b/src/core/CoreModuleDirector.ts @@ -5,6 +5,7 @@ import { OSModel } from './modelRepo/OSModel'; import { SupportedIdentity } from './models/IdentityModel'; import { ModelStoresMap } from './models/ModelStoresMap'; import { + SubscriptionChannel, SubscriptionModel, SubscriptionType, SupportedSubscription, @@ -122,7 +123,7 @@ export class CoreModuleDirector { */ const existingSubscription = !!subscription.token ? this.getSubscriptionOfTypeWithToken( - subscription.type, + this.toSubscriptionChannel(subscription.type), subscription.token, ) : undefined; @@ -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; @@ -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; @@ -332,38 +333,33 @@ export class CoreModuleDirector { } public getSubscriptionOfTypeWithToken( - type: SubscriptionType, + type: SubscriptionChannel | undefined, token: string, ): OSModel | undefined { logMethodCall('CoreModuleDirector.getSubscriptionOfTypeWithToken', { type, token, }); + + let subscriptions: Record>; + 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 */ @@ -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; + } + } } diff --git a/src/core/models/SubscriptionModels.ts b/src/core/models/SubscriptionModels.ts index be9c0e3c2..595d02c1a 100644 --- a/src/core/models/SubscriptionModels.ts +++ b/src/core/models/SubscriptionModels.ts @@ -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