@@ -9,7 +9,15 @@ const messages = {
9
9
TYPES_FILE_NOT_EXISTS : "Notification Types file path is incorrect." ,
10
10
INVALID_NOTIFICATION_TYPES : "Notification Types must contain the following key: 'NotificationTypeKey'." ,
11
11
DESTINATION_NOT_FOUND : "Failed to get destination: " ,
12
- MANDATORY_PARAMETER_NOT_PASSED : "Recipients, priority and title are mandatory parameters" ,
12
+ MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION : "Recipients, priority and title are mandatory parameters" ,
13
+ MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION : "Recipients, type are mandatory parameters" ,
14
+ RECIPIENTS_IS_NOT_ARRAY : "Recipients is not an array or it is empty" ,
15
+ TITLE_IS_NOT_STRING : "Title is not a string" ,
16
+ DESCRIPTION_IS_NOT_STRING : "Description is not a string" ,
17
+ PROPERTIES_IS_NOT_OBJECT : "Properties is not an object" ,
18
+ NAVIGATION_IS_NOT_OBJECT : "Navigation is not an object" ,
19
+ PAYLOAD_IS_NOT_OBJECT : "Payload is not an object" ,
20
+ EMPTY_OBJECT_FOR_NOTIFY : "Empty object is passed a single parameter to notify function"
13
21
} ;
14
22
15
23
function validateNotificationTypes ( notificationTypes ) {
@@ -23,9 +31,19 @@ function validateNotificationTypes(notificationTypes) {
23
31
return true ;
24
32
}
25
33
26
- function validateNotifyParameters ( recipients , priority , title ) {
34
+ function validateDefaultNotifyParameters ( recipients , priority , title , description ) {
27
35
if ( ! recipients || ! priority || ! title ) {
28
- console . log ( messages . MANDATORY_PARAMETER_NOT_PASSED ) ;
36
+ console . log ( messages . MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION ) ;
37
+ return false ;
38
+ }
39
+
40
+ if ( ! Array . isArray ( recipients ) || recipients . length == 0 ) {
41
+ console . log ( messages . RECIPIENTS_IS_NOT_ARRAY ) ;
42
+ return false ;
43
+ }
44
+
45
+ if ( typeof title !== "string" ) {
46
+ console . log ( messages . TITLE_IS_NOT_STRING ) ;
29
47
return false ;
30
48
}
31
49
@@ -34,6 +52,45 @@ function validateNotifyParameters(recipients, priority, title) {
34
52
return false ;
35
53
}
36
54
55
+ if ( description !== undefined && typeof description !== "string" ) {
56
+ console . log ( messages . DESCRIPTION_IS_NOT_STRING ) ;
57
+ return false ;
58
+ }
59
+
60
+ return true ;
61
+ }
62
+
63
+ function validateCustomNotifyParameters ( type , recipients , properties , navigation , priority , payload ) {
64
+ if ( ! recipients || ! type ) {
65
+ console . log ( messages . MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION ) ;
66
+ return false ;
67
+ }
68
+
69
+ if ( ! Array . isArray ( recipients ) || recipients . length == 0 ) {
70
+ console . log ( messages . RECIPIENTS_IS_NOT_ARRAY ) ;
71
+ return false ;
72
+ }
73
+
74
+ if ( priority === undefined || ! PRIORITIES . includes ( priority ) ) {
75
+ console . log ( `Invalid priority ${ priority } . Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH` ) ;
76
+ return false ;
77
+ }
78
+
79
+ if ( properties !== undefined && ! Array . isArray ( properties ) ) {
80
+ console . log ( messages . PROPERTIES_IS_NOT_OBJECT ) ;
81
+ return false ;
82
+ }
83
+
84
+ if ( navigation !== undefined && typeof navigation !== "object" ) {
85
+ console . log ( messages . NAVIGATION_IS_NOT_OBJECT ) ;
86
+ return false ;
87
+ }
88
+
89
+ if ( payload !== undefined && typeof payload !== "object" ) {
90
+ console . log ( messages . PAYLOAD_IS_NOT_OBJECT ) ;
91
+ return false ;
92
+ }
93
+
37
94
return true ;
38
95
}
39
96
@@ -89,7 +146,7 @@ function buildDefaultNotification(
89
146
recipients ,
90
147
priority ,
91
148
title ,
92
- description
149
+ description = ""
93
150
) {
94
151
const properties = [
95
152
{
@@ -117,30 +174,58 @@ function buildDefaultNotification(
117
174
} ;
118
175
}
119
176
120
- function buildNotification ( passedArguments ) {
121
- let notification ;
122
- if ( passedArguments . length == 1 && typeof passedArguments [ 0 ] === "object" && ! Array . isArray ( passedArguments [ 0 ] ) ) {
123
- notification = passedArguments [ 0 ] ;
124
- notification [ "NotificationTypeKey" ] = getNotificationTypesKeyWithPrefix ( notification [ "NotificationTypeKey" ] ) ;
125
- } else {
126
- const recipients = passedArguments [ 0 ] ;
127
- const priority = passedArguments [ 1 ] ;
128
- const title = passedArguments [ 2 ] ;
129
- const description = passedArguments [ 3 ] ? passedArguments [ 3 ] : "" ;
177
+ function buildCustomNotification ( notificationData ) {
178
+ return {
179
+ Id : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "Id" ] : undefined ,
180
+ OriginId : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "OriginId" ] : undefined ,
181
+ NotificationTypeId : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "NotificationTypeId" ] : undefined ,
182
+ NotificationTypeKey : getNotificationTypesKeyWithPrefix ( notificationData [ "type" ] ) ,
183
+ NotificationTypeVersion : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "NotificationTypeVersion" ] : "1" ,
184
+ NavigationTargetAction : notificationData [ "navigation" ] ? notificationData [ "navigation" ] [ "NavigationTargetAction" ] : undefined ,
185
+ NavigationTargetObject : notificationData [ "navigation" ] ? notificationData [ "navigation" ] [ "NavigationTargetObject" ] : undefined ,
186
+ Priority : notificationData [ "priority" ] ? notificationData [ "priority" ] : "MEDIUM" ,
187
+ ProviderId : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "ProviderId" ] : undefined ,
188
+ ActorId : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "ActorId" ] : undefined ,
189
+ ActorDisplayText : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "ActorDisplayText" ] : undefined ,
190
+ ActorImageURL : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "ActorImageURL" ] : undefined ,
191
+ NotificationTypeTimestamp : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "NotificationTypeTimestamp" ] : undefined ,
192
+ Recipients : notificationData [ "recipients" ] . map ( ( recipient ) => ( { RecipientId : recipient } ) ) ,
193
+ Properties : notificationData [ "properties" ] ? notificationData [ "properties" ] : undefined ,
194
+ TargetParameters : notificationData [ "payload" ] ? notificationData [ "payload" ] [ "TargetParameters" ] : undefined
195
+ } ;
196
+ }
197
+
198
+ function buildNotification ( notificationData ) {
199
+ let notification
200
+
201
+ if ( Object . keys ( notificationData ) . length === 0 ) {
202
+ console . log ( messages . EMPTY_OBJECT_FOR_NOTIFY ) ;
203
+ return ;
204
+ }
130
205
131
- if ( ! validateNotifyParameters ( recipients , priority , title ) ) {
206
+ if ( notificationData [ "type" ] ) {
207
+ if ( ! validateCustomNotifyParameters ( notificationData [ "type" ] , notificationData [ "recipients" ] , notificationData [ "properties" ] , notificationData [ "navigation" ] , notificationData [ "priority" ] , notificationData [ "payload" ] ) ) {
208
+ return ;
209
+ }
210
+
211
+ notification = buildCustomNotification ( notificationData )
212
+ } else if ( notificationData [ "NotificationTypeKey" ] ) {
213
+ notificationData [ "NotificationTypeKey" ] = getNotificationTypesKeyWithPrefix ( notificationData [ "NotificationTypeKey" ] )
214
+ notification = notificationData ;
215
+ } else {
216
+ if ( ! validateDefaultNotifyParameters ( notificationData [ "recipients" ] , notificationData [ "priority" ] , notificationData [ "title" ] , notificationData [ "description" ] ) ) {
132
217
return ;
133
218
}
134
219
135
220
notification = buildDefaultNotification (
136
- recipients ,
137
- priority ,
138
- title ,
139
- description
140
- ) ;
221
+ notificationData [ " recipients" ] ,
222
+ notificationData [ " priority" ] ,
223
+ notificationData [ " title" ] ,
224
+ notificationData [ " description" ]
225
+ )
141
226
}
142
227
143
- return notification ;
228
+ return JSON . parse ( JSON . stringify ( notification ) ) ;
144
229
}
145
230
146
231
module . exports = {
@@ -154,3 +239,4 @@ module.exports = {
154
239
executeRequest,
155
240
buildNotification
156
241
} ;
242
+
0 commit comments