Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/core/modelRepo/OperationModelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IPreferencesService } from 'src/types/preferences';
import { OPERATION_NAME } from '../executors/constants';
import { CreateSubscriptionOperation } from '../operations/CreateSubscriptionOperation';
import { DeleteAliasOperation } from '../operations/DeleteAliasOperation';
import { DeleteSubscriptionOperation } from '../operations/DeleteSubscriptionOperation';
import { DeleteTagOperation } from '../operations/DeleteTagOperation';
import { Operation } from '../operations/Operation';
import { RefreshUserOperation } from '../operations/RefreshUserOperation';
Expand All @@ -11,7 +12,10 @@ import { SetPropertyOperation } from '../operations/SetPropertyOperation';
import { SetTagOperation } from '../operations/SetTagOperation';
import { TrackSessionEndOperation } from '../operations/TrackSessionEndOperation';
import { TrackSessionStartOperation } from '../operations/TrackSessionStartOperation';
import { TransferSubscriptionOperation } from '../operations/TransferSubscriptionOperation';
import { UpdateSubscriptionOperation } from '../operations/UpdateSubscriptionOperation';
import { ModelStore } from './ModelStore';

export class OperationModelStore extends ModelStore<Operation> {
constructor(prefs: IPreferencesService) {
super('operations', prefs);
Expand Down Expand Up @@ -45,6 +49,15 @@ export class OperationModelStore extends ModelStore<Operation> {
case OPERATION_NAME.CREATE_SUBSCRIPTION:
operation = new CreateSubscriptionOperation();
break;
case OPERATION_NAME.UPDATE_SUBSCRIPTION:
operation = new UpdateSubscriptionOperation();
break;
case OPERATION_NAME.DELETE_SUBSCRIPTION:
operation = new DeleteSubscriptionOperation();
break;
case OPERATION_NAME.TRANSFER_SUBSCRIPTION:
operation = new TransferSubscriptionOperation();
break;
case OPERATION_NAME.REFRESH_USER:
operation = new RefreshUserOperation();
break;
Expand Down
148 changes: 148 additions & 0 deletions src/core/operations/BaseSubscriptionOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { IDManager } from 'src/shared/managers/IDManager';
import { SubscriptionStateKind } from 'src/shared/models/SubscriptionStateKind';
import { SubscriptionType } from '../models/SubscriptionModels';
import { Operation } from './Operation';
import { Subscription } from './types';

/**
* Base class for subscription-related operations
*/
export abstract class BaseSubscriptionOperation extends Operation {
constructor(
operationName: string,
appId?: string,
onesignalId?: string,
subscription?: Subscription,
) {
super(operationName, appId, onesignalId);

if (subscription) {
this.subscriptionId = subscription.subscriptionId;
this.type = subscription.type;
this.enabled = subscription.enabled;
this.notification_types = subscription.notification_types;
this.sdk = subscription.sdk;
this.device_model = subscription.device_model;
this.device_os = subscription.device_os;
this.web_auth = subscription.web_auth;
this.web_p256 = subscription.web_p256;
}
}

/**
* The subscription ID for the operation. This ID *may* be locally generated
* and can be checked via IDManager.isLocalId to ensure correct processing.
*/
get subscriptionId(): string {
return this.getProperty<string>('subscriptionId');
}
protected set subscriptionId(value: string) {
this.setProperty<string>('subscriptionId', value);
}

/**
* The type of subscription.
*/
get type(): SubscriptionType {
return this.getProperty<SubscriptionType>('type');
}
protected set type(value: SubscriptionType) {
this.setProperty<SubscriptionType>('type', value);
}

/**
* Whether this subscription is currently enabled.
*/
get enabled(): boolean {
return this.getProperty<boolean>('enabled');
}
protected set enabled(value: boolean) {
this.setProperty<boolean>('enabled', value);
}

/**
* The notification types this subscription is subscribed to.
*/
get notification_types(): SubscriptionStateKind {
return this.getProperty<SubscriptionStateKind>('notification_types');
}
protected set notification_types(value: SubscriptionStateKind) {
this.setProperty<SubscriptionStateKind>('notification_types', value);
}

/**
* The SDK identifier
*/
get sdk(): string | undefined {
return this.getProperty<string | undefined>('sdk');
}
protected set sdk(value: string | undefined) {
this.setProperty<string | undefined>('sdk', value);
}

/**
* The device model
*/
get device_model(): string | undefined {
return this.getProperty<string | undefined>('device_model');
}
protected set device_model(value: string | undefined) {
this.setProperty<string | undefined>('device_model', value);
}

/**
* The device OS version
*/
get device_os(): number | undefined {
return this.getProperty<number | undefined>('device_os');
}
protected set device_os(value: number | undefined) {
this.setProperty<number | undefined>('device_os', value);
}

/**
* Web authentication value
*/
get web_auth(): string | undefined {
return this.getProperty<string | undefined>('web_auth');
}
protected set web_auth(value: string | undefined) {
this.setProperty<string | undefined>('web_auth', value);
}

/**
* Web P256 value
*/
get web_p256(): string | undefined {
return this.getProperty<string | undefined>('web_p256');
}
protected set web_p256(value: string | undefined) {
this.setProperty<string | undefined>('web_p256', value);
}

override get createComparisonKey(): string {
return `${this.appId}.User.${this.onesignalId}`;
}

override get modifyComparisonKey(): string {
return `${this.appId}.User.${this.onesignalId}.Subscription.${this.subscriptionId}`;
}

override get canStartExecute(): boolean {
return (
!IDManager.isLocalId(this.onesignalId) &&
!IDManager.isLocalId(this.subscriptionId)
);
}

override get applyToRecordId(): string {
return this.subscriptionId;
}

override translateIds(map: Record<string, string>): void {
super.translateIds(map);
if (map[this.subscriptionId]) {
this.subscriptionId = map[this.subscriptionId];
}
}
}
105 changes: 12 additions & 93 deletions src/core/operations/CreateSubscriptionOperation.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,33 @@
import { SubscriptionStateKind } from 'src/shared/models/SubscriptionStateKind';
import { IDManager } from 'src/shared/managers/IDManager';
import { OPERATION_NAME } from '../executors/constants';
import { SubscriptionType } from '../models/SubscriptionModels';
import { Operation } from './Operation';
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
import { Subscription } from './types';

/**
* An Operation to create a new subscription in the OneSignal backend. The subscription will
* be associated to the user with the appId and onesignalId provided.
*/
export class CreateSubscriptionOperation extends Operation {
export class CreateSubscriptionOperation extends BaseSubscriptionOperation {
constructor(subscription?: Subscription) {
super(
OPERATION_NAME.CREATE_SUBSCRIPTION,
subscription?.appId,
subscription?.onesignalId,
subscription,
);
if (subscription) {
this.subscriptionId = subscription.subscriptionId;
this.type = subscription.type;
this.enabled = subscription.enabled;
this.notification_types = subscription.notification_types;
this.sdk = subscription.sdk;
this.device_model = subscription.device_model;
this.device_os = subscription.device_os;
this.web_auth = subscription.web_auth;
this.web_p256 = subscription.web_p256;
}
}

/**
* The local ID of the subscription being created. The subscription model with this ID will have its
* ID updated with the backend-generated ID post-create.
*/
get subscriptionId(): string {
return this.getProperty<string>('subscriptionId');
}
private set subscriptionId(value: string) {
this.setProperty<string>('subscriptionId', value);
}

/**
* The type of subscription.
*/
get type(): SubscriptionType {
return this.getProperty<SubscriptionType>('type');
}
private set type(value: SubscriptionType) {
this.setProperty<SubscriptionType>('type', value);
}

/**
* Whether this subscription is currently enabled.
*/
get enabled(): boolean {
return this.getProperty<boolean>('enabled');
}
private set enabled(value: boolean) {
this.setProperty<boolean>('enabled', value);
override get canStartExecute(): boolean {
return !IDManager.isLocalId(this.onesignalId);
}

/**
* The notification types this subscription is subscribed to.
*/
get notification_types(): SubscriptionStateKind {
return this.getProperty<SubscriptionStateKind>('notification_types');
}
private set notification_types(value: SubscriptionStateKind) {
this.setProperty<SubscriptionStateKind>('notification_types', value);
override get applyToRecordId(): string {
return this.onesignalId;
}

get sdk(): string | undefined {
return this.getProperty<string | undefined>('sdk');
}
private set sdk(value: string | undefined) {
this.setProperty<string | undefined>('sdk', value);
}

get device_model(): string | undefined {
return this.getProperty<string | undefined>('device_model');
}
private set device_model(value: string | undefined) {
this.setProperty<string | undefined>('device_model', value);
}

get device_os(): number | undefined {
return this.getProperty<number | undefined>('device_os');
}
private set device_os(value: number | undefined) {
this.setProperty<number | undefined>('device_os', value);
}

get web_auth(): string | undefined {
return this.getProperty<string | undefined>('web_auth');
}
private set web_auth(value: string | undefined) {
this.setProperty<string | undefined>('web_auth', value);
}

get web_p256(): string | undefined {
return this.getProperty<string | undefined>('web_p256');
}
private set web_p256(value: string | undefined) {
this.setProperty<string | undefined>('web_p256', value);
}

override get createComparisonKey(): string {
return `${this.appId}.User.${this.onesignalId}`;
}

override get modifyComparisonKey(): string {
return `${this.appId}.User.${this.onesignalId}.Subscription.${this.subscriptionId}`;
override translateIds(map: Record<string, string>): void {
if (map[this.onesignalId]) {
this.onesignalId = map[this.onesignalId];
}
}
}
19 changes: 19 additions & 0 deletions src/core/operations/DeleteSubscriptionOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { OPERATION_NAME } from '../executors/constants';
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
import { GroupComparisonType, GroupComparisonValue } from './Operation';

/**
* An Operation to delete a subscription from the OneSignal backend.
*/
export class DeleteSubscriptionOperation extends BaseSubscriptionOperation {
constructor(appId?: string, onesignalId?: string, subscriptionId?: string) {
super(OPERATION_NAME.DELETE_SUBSCRIPTION, appId, onesignalId);
if (subscriptionId) {
this.subscriptionId = subscriptionId;
}
}

override get groupComparisonType(): GroupComparisonValue {
return GroupComparisonType.NONE;
}
}
23 changes: 23 additions & 0 deletions src/core/operations/TransferSubscriptionOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OPERATION_NAME } from '../executors/constants';
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
import { GroupComparisonType, GroupComparisonValue } from './Operation';

/**
* An Operation to transfer a subscription to a new owner on the OneSignal backend.
*/
export class TransferSubscriptionOperation extends BaseSubscriptionOperation {
constructor(appId?: string, onesignalId?: string, subscriptionId?: string) {
super(OPERATION_NAME.TRANSFER_SUBSCRIPTION, appId, onesignalId);
if (subscriptionId) {
this.subscriptionId = subscriptionId;
}
}

override get groupComparisonType(): GroupComparisonValue {
return GroupComparisonType.NONE;
}

override get modifyComparisonKey(): string {
return `${this.appId}.Subscription.${this.subscriptionId}.Transfer`;
}
}
17 changes: 17 additions & 0 deletions src/core/operations/UpdateSubscriptionOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { OPERATION_NAME } from '../executors/constants';
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
import { Subscription } from './types';

/**
* An Operation to update an existing subscription in the OneSignal backend.
*/
export class UpdateSubscriptionOperation extends BaseSubscriptionOperation {
constructor(subscription?: Subscription) {
super(
OPERATION_NAME.UPDATE_SUBSCRIPTION,
subscription?.appId,
subscription?.onesignalId,
subscription,
);
}
}