-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationConfig.ts
More file actions
132 lines (116 loc) · 4.46 KB
/
Copy pathPushNotificationConfig.ts
File metadata and controls
132 lines (116 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { SubEvent } from "./EventSummary";
export const PushNotificationRuleKeys = {
TRANSIT_INWARD: "transit.inward",
TRANSIT_OUTWARD: "transit.outward",
PEEK_INWARD: "peek.inward",
PEEK_OUTWARD: "peek.outward",
BREACH_INWARD: "breach.inward",
BREACH_OUTWARD: "breach.outward",
CONTRABAND: "contraband"
} as const;
export type PushNotificationRuleKey = typeof PushNotificationRuleKeys[keyof typeof PushNotificationRuleKeys];
export const PushNotificationRuleValues = {
OFF: "off",
ON: "on",
CRITICAL: "critical"
} as const;
export type PushNotificationRuleValue = typeof PushNotificationRuleValues[keyof typeof PushNotificationRuleValues];
export interface PushNotificationQuietHours {
enabled: boolean;
timeRange: string;
timezone: string;
criticalBypasses: boolean;
}
export type PushNotificationRules = Record<PushNotificationRuleKey, PushNotificationRuleValue>;
export const DEFAULT_PUSH_NOTIFICATION_LOCALE = "en";
export interface PushNotificationConfigJson {
schemaVersion: 1;
locale: string;
pushEnabled: boolean;
rules: PushNotificationRules;
quietHours: PushNotificationQuietHours;
[key: string]: any;
}
export const DEFAULT_PUSH_NOTIFICATION_RULES: PushNotificationRules = {
[PushNotificationRuleKeys.TRANSIT_INWARD]: PushNotificationRuleValues.ON,
[PushNotificationRuleKeys.TRANSIT_OUTWARD]: PushNotificationRuleValues.ON,
[PushNotificationRuleKeys.PEEK_INWARD]: PushNotificationRuleValues.ON,
[PushNotificationRuleKeys.PEEK_OUTWARD]: PushNotificationRuleValues.ON,
[PushNotificationRuleKeys.BREACH_INWARD]: PushNotificationRuleValues.ON,
[PushNotificationRuleKeys.BREACH_OUTWARD]: PushNotificationRuleValues.ON,
[PushNotificationRuleKeys.CONTRABAND]: PushNotificationRuleValues.CRITICAL
};
export const DEFAULT_PUSH_NOTIFICATION_QUIET_HOURS: PushNotificationQuietHours = {
enabled: false,
timeRange: "22:00-07:00",
timezone: "Europe/London",
criticalBypasses: true
};
export const DEFAULT_PUSH_NOTIFICATION_CONFIG: PushNotificationConfigJson = {
schemaVersion: 1,
locale: DEFAULT_PUSH_NOTIFICATION_LOCALE,
pushEnabled: true,
rules: DEFAULT_PUSH_NOTIFICATION_RULES,
quietHours: DEFAULT_PUSH_NOTIFICATION_QUIET_HOURS
};
export class PushNotificationConfig implements PushNotificationConfigJson {
appToken?: string;
schemaVersion: 1;
locale: string;
pushEnabled: boolean;
rules: PushNotificationRules;
quietHours: PushNotificationQuietHours;
updatedAt?: Date | null;
[key: string]: any;
constructor(initObj: Partial<PushNotificationConfig> & Record<string, any>) {
this.appToken = initObj.appToken;
this.schemaVersion = 1;
this.locale = initObj.locale ?? DEFAULT_PUSH_NOTIFICATION_CONFIG.locale;
this.pushEnabled = initObj.pushEnabled ?? DEFAULT_PUSH_NOTIFICATION_CONFIG.pushEnabled;
this.rules = {
...DEFAULT_PUSH_NOTIFICATION_RULES,
...(initObj.rules ?? {})
};
this.quietHours = {
...DEFAULT_PUSH_NOTIFICATION_QUIET_HOURS,
...(initObj.quietHours ?? {})
};
this.updatedAt = initObj.updatedAt ? new Date(initObj.updatedAt) : null;
for (const key in initObj) {
if (initObj.hasOwnProperty(key) && !this.hasOwnProperty(key)) {
this[key] = initObj[key];
}
}
}
toConfigJson(): PushNotificationConfigJson {
const { appToken, updatedAt, ...config } = this;
return {
...config,
schemaVersion: 1,
locale: this.locale,
pushEnabled: this.pushEnabled,
rules: this.rules,
quietHours: this.quietHours
};
}
}
export function getPushNotificationRuleKey(
subevent: Pick<SubEvent, "action" | "direction">
): PushNotificationRuleKey | null {
switch (subevent.action) {
case "TRANSIT":
return subevent.direction === "INWARD"
? PushNotificationRuleKeys.TRANSIT_INWARD
: PushNotificationRuleKeys.TRANSIT_OUTWARD;
case "PEEK":
return subevent.direction === "INWARD"
? PushNotificationRuleKeys.PEEK_INWARD
: PushNotificationRuleKeys.PEEK_OUTWARD;
case "BREACH":
return subevent.direction === "INWARD"
? PushNotificationRuleKeys.BREACH_INWARD
: PushNotificationRuleKeys.BREACH_OUTWARD;
default:
return null;
}
}