Skip to content

Commit 2253ba4

Browse files
committed
add update subscription operation
1 parent bbf4ef0 commit 2253ba4

5 files changed

Lines changed: 138 additions & 89 deletions

File tree

src/core/modelRepo/OperationModelStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SetPropertyOperation } from '../operations/SetPropertyOperation';
1212
import { SetTagOperation } from '../operations/SetTagOperation';
1313
import { TrackSessionEndOperation } from '../operations/TrackSessionEndOperation';
1414
import { TrackSessionStartOperation } from '../operations/TrackSessionStartOperation';
15+
import { UpdateSubscriptionOperation } from '../operations/UpdateSubscriptionOperation';
1516
import { ModelStore } from './ModelStore';
1617

1718
export class OperationModelStore extends ModelStore<Operation> {
@@ -47,6 +48,9 @@ export class OperationModelStore extends ModelStore<Operation> {
4748
case OPERATION_NAME.CREATE_SUBSCRIPTION:
4849
operation = new CreateSubscriptionOperation();
4950
break;
51+
case OPERATION_NAME.UPDATE_SUBSCRIPTION:
52+
operation = new UpdateSubscriptionOperation();
53+
break;
5054
case OPERATION_NAME.DELETE_SUBSCRIPTION:
5155
operation = new DeleteSubscriptionOperation();
5256
break;

src/core/operations/BaseSubscriptionOperation.ts

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import { IDManager } from 'src/shared/managers/IDManager';
2+
import { SubscriptionStateKind } from 'src/shared/models/SubscriptionStateKind';
3+
import { SubscriptionType } from '../models/SubscriptionModels';
14
import { Operation } from './Operation';
5+
import { Subscription } from './types';
26

37
/**
48
* Base class for subscription-related operations
@@ -8,11 +12,20 @@ export abstract class BaseSubscriptionOperation extends Operation {
812
operationName: string,
913
appId?: string,
1014
onesignalId?: string,
11-
subscriptionId?: string,
15+
subscription?: Subscription,
1216
) {
1317
super(operationName, appId, onesignalId);
14-
if (subscriptionId) {
15-
this.subscriptionId = subscriptionId;
18+
19+
if (subscription) {
20+
this.subscriptionId = subscription.subscriptionId;
21+
this.type = subscription.type;
22+
this.enabled = subscription.enabled;
23+
this.notification_types = subscription.notification_types;
24+
this.sdk = subscription.sdk;
25+
this.device_model = subscription.device_model;
26+
this.device_os = subscription.device_os;
27+
this.web_auth = subscription.web_auth;
28+
this.web_p256 = subscription.web_p256;
1629
}
1730
}
1831

@@ -27,6 +40,86 @@ export abstract class BaseSubscriptionOperation extends Operation {
2740
this.setProperty<string>('subscriptionId', value);
2841
}
2942

43+
/**
44+
* The type of subscription.
45+
*/
46+
get type(): SubscriptionType {
47+
return this.getProperty<SubscriptionType>('type');
48+
}
49+
protected set type(value: SubscriptionType) {
50+
this.setProperty<SubscriptionType>('type', value);
51+
}
52+
53+
/**
54+
* Whether this subscription is currently enabled.
55+
*/
56+
get enabled(): boolean {
57+
return this.getProperty<boolean>('enabled');
58+
}
59+
protected set enabled(value: boolean) {
60+
this.setProperty<boolean>('enabled', value);
61+
}
62+
63+
/**
64+
* The notification types this subscription is subscribed to.
65+
*/
66+
get notification_types(): SubscriptionStateKind {
67+
return this.getProperty<SubscriptionStateKind>('notification_types');
68+
}
69+
protected set notification_types(value: SubscriptionStateKind) {
70+
this.setProperty<SubscriptionStateKind>('notification_types', value);
71+
}
72+
73+
/**
74+
* The SDK identifier
75+
*/
76+
get sdk(): string | undefined {
77+
return this.getProperty<string | undefined>('sdk');
78+
}
79+
protected set sdk(value: string | undefined) {
80+
this.setProperty<string | undefined>('sdk', value);
81+
}
82+
83+
/**
84+
* The device model
85+
*/
86+
get device_model(): string | undefined {
87+
return this.getProperty<string | undefined>('device_model');
88+
}
89+
protected set device_model(value: string | undefined) {
90+
this.setProperty<string | undefined>('device_model', value);
91+
}
92+
93+
/**
94+
* The device OS version
95+
*/
96+
get device_os(): number | undefined {
97+
return this.getProperty<number | undefined>('device_os');
98+
}
99+
protected set device_os(value: number | undefined) {
100+
this.setProperty<number | undefined>('device_os', value);
101+
}
102+
103+
/**
104+
* Web authentication value
105+
*/
106+
get web_auth(): string | undefined {
107+
return this.getProperty<string | undefined>('web_auth');
108+
}
109+
protected set web_auth(value: string | undefined) {
110+
this.setProperty<string | undefined>('web_auth', value);
111+
}
112+
113+
/**
114+
* Web P256 value
115+
*/
116+
get web_p256(): string | undefined {
117+
return this.getProperty<string | undefined>('web_p256');
118+
}
119+
protected set web_p256(value: string | undefined) {
120+
this.setProperty<string | undefined>('web_p256', value);
121+
}
122+
30123
override get createComparisonKey(): string {
31124
return `${this.appId}.User.${this.onesignalId}`;
32125
}
@@ -35,6 +128,13 @@ export abstract class BaseSubscriptionOperation extends Operation {
35128
return `${this.appId}.User.${this.onesignalId}.Subscription.${this.subscriptionId}`;
36129
}
37130

131+
override get canStartExecute(): boolean {
132+
return (
133+
!IDManager.isLocalId(this.onesignalId) &&
134+
!IDManager.isLocalId(this.subscriptionId)
135+
);
136+
}
137+
38138
override get applyToRecordId(): string {
39139
return this.subscriptionId;
40140
}
Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { SubscriptionStateKind } from 'src/shared/models/SubscriptionStateKind';
1+
import { IDManager } from 'src/shared/managers/IDManager';
22
import { OPERATION_NAME } from '../executors/constants';
3-
import { SubscriptionType } from '../models/SubscriptionModels';
43
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
54
import { Subscription } from './types';
65

@@ -14,82 +13,21 @@ export class CreateSubscriptionOperation extends BaseSubscriptionOperation {
1413
OPERATION_NAME.CREATE_SUBSCRIPTION,
1514
subscription?.appId,
1615
subscription?.onesignalId,
17-
subscription?.subscriptionId,
16+
subscription,
1817
);
19-
if (subscription) {
20-
this.type = subscription.type;
21-
this.enabled = subscription.enabled;
22-
this.notification_types = subscription.notification_types;
23-
this.sdk = subscription.sdk;
24-
this.device_model = subscription.device_model;
25-
this.device_os = subscription.device_os;
26-
this.web_auth = subscription.web_auth;
27-
this.web_p256 = subscription.web_p256;
28-
}
29-
}
30-
31-
/**
32-
* The type of subscription.
33-
*/
34-
get type(): SubscriptionType {
35-
return this.getProperty<SubscriptionType>('type');
36-
}
37-
private set type(value: SubscriptionType) {
38-
this.setProperty<SubscriptionType>('type', value);
39-
}
40-
41-
/**
42-
* Whether this subscription is currently enabled.
43-
*/
44-
get enabled(): boolean {
45-
return this.getProperty<boolean>('enabled');
46-
}
47-
private set enabled(value: boolean) {
48-
this.setProperty<boolean>('enabled', value);
49-
}
50-
51-
/**
52-
* The notification types this subscription is subscribed to.
53-
*/
54-
get notification_types(): SubscriptionStateKind {
55-
return this.getProperty<SubscriptionStateKind>('notification_types');
56-
}
57-
private set notification_types(value: SubscriptionStateKind) {
58-
this.setProperty<SubscriptionStateKind>('notification_types', value);
5918
}
6019

61-
get sdk(): string | undefined {
62-
return this.getProperty<string | undefined>('sdk');
63-
}
64-
private set sdk(value: string | undefined) {
65-
this.setProperty<string | undefined>('sdk', value);
66-
}
67-
68-
get device_model(): string | undefined {
69-
return this.getProperty<string | undefined>('device_model');
70-
}
71-
private set device_model(value: string | undefined) {
72-
this.setProperty<string | undefined>('device_model', value);
73-
}
74-
75-
get device_os(): number | undefined {
76-
return this.getProperty<number | undefined>('device_os');
77-
}
78-
private set device_os(value: number | undefined) {
79-
this.setProperty<number | undefined>('device_os', value);
20+
override get canStartExecute(): boolean {
21+
return !IDManager.isLocalId(this.onesignalId);
8022
}
8123

82-
get web_auth(): string | undefined {
83-
return this.getProperty<string | undefined>('web_auth');
84-
}
85-
private set web_auth(value: string | undefined) {
86-
this.setProperty<string | undefined>('web_auth', value);
24+
override get applyToRecordId(): string {
25+
return this.onesignalId;
8726
}
8827

89-
get web_p256(): string | undefined {
90-
return this.getProperty<string | undefined>('web_p256');
91-
}
92-
private set web_p256(value: string | undefined) {
93-
this.setProperty<string | undefined>('web_p256', value);
28+
override translateIds(map: Record<string, string>): void {
29+
if (map[this.onesignalId]) {
30+
this.onesignalId = map[this.onesignalId];
31+
}
9432
}
9533
}
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { IDManager } from 'src/shared/managers/IDManager';
21
import { OPERATION_NAME } from '../executors/constants';
32
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
43
import { GroupComparisonType, GroupComparisonValue } from './Operation';
@@ -8,22 +7,13 @@ import { GroupComparisonType, GroupComparisonValue } from './Operation';
87
*/
98
export class DeleteSubscriptionOperation extends BaseSubscriptionOperation {
109
constructor(appId?: string, onesignalId?: string, subscriptionId?: string) {
11-
super(
12-
OPERATION_NAME.DELETE_SUBSCRIPTION,
13-
appId,
14-
onesignalId,
15-
subscriptionId,
16-
);
10+
super(OPERATION_NAME.DELETE_SUBSCRIPTION, appId, onesignalId);
11+
if (subscriptionId) {
12+
this.subscriptionId = subscriptionId;
13+
}
1714
}
1815

1916
override get groupComparisonType(): GroupComparisonValue {
2017
return GroupComparisonType.NONE;
2118
}
22-
23-
override get canStartExecute(): boolean {
24-
return (
25-
!IDManager.isLocalId(this.onesignalId) &&
26-
!IDManager.isLocalId(this.subscriptionId)
27-
);
28-
}
2919
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { OPERATION_NAME } from '../executors/constants';
2+
import { BaseSubscriptionOperation } from './BaseSubscriptionOperation';
3+
import { Subscription } from './types';
4+
5+
/**
6+
* An Operation to update an existing subscription in the OneSignal backend.
7+
*/
8+
export class UpdateSubscriptionOperation extends BaseSubscriptionOperation {
9+
constructor(subscription?: Subscription) {
10+
super(
11+
OPERATION_NAME.UPDATE_SUBSCRIPTION,
12+
subscription?.appId,
13+
subscription?.onesignalId,
14+
subscription,
15+
);
16+
}
17+
}

0 commit comments

Comments
 (0)