1
1
const fs = require ( 'fs' ) ;
2
2
const { basename } = require ( 'path' )
3
3
const cds = require ( "@sap/cds" ) ;
4
- const { getDestination } = require ( "@sap-cloud-sdk/core" ) ;
4
+ const { executeHttpRequest, getDestination } = require ( "@sap-cloud-sdk/core" ) ;
5
+ const PRIORITIES = [ "LOW" , "NEUTRAL" , "MEDIUM" , "HIGH" ] ;
5
6
6
7
const messages = {
7
8
INVALID_NOTIFICATION_TYPES : "Notification Types must contain the following keys: 'NotificationTypeKey' and 'NotificationTypeVersion'." ,
8
9
DESTINATION_NOT_FOUND : "Failed to get destination: " ,
10
+ MANDATORY_PARAMETER_NOT_PASSED : "Recipients, priority and title are mandatory parameters" ,
9
11
} ;
10
12
11
13
function validateNotificationTypes ( notificationTypes ) {
@@ -16,6 +18,20 @@ function validateNotificationTypes(notificationTypes) {
16
18
} ) ;
17
19
}
18
20
21
+ function validateNotifyParameters ( recipients , priority , title ) {
22
+ if ( ! recipients || ! priority || ! title ) {
23
+ console . log ( messages . MANDATORY_PARAMETER_NOT_PASSED ) ;
24
+ return false ;
25
+ }
26
+
27
+ if ( ! PRIORITIES . includes ( priority ) ) {
28
+ console . log ( `Invalid priority ${ priority } . Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH` ) ;
29
+ return false ;
30
+ }
31
+
32
+ return true ;
33
+ }
34
+
19
35
function doesKeyExist ( obj , key ) {
20
36
return typeof ( key ) === 'string' && typeof ( obj ) === 'object' && key in obj ;
21
37
}
@@ -41,12 +57,87 @@ function getNotificationTypesKeyWithPrefix(notificationTypeKey) {
41
57
return `${ prefix } /${ notificationTypeKey } ` ;
42
58
}
43
59
60
+ async function executeRequest ( httpMethod , targetUrl , payload , notifServiceDest , csrfHeaders ) {
61
+ let response = { } ;
62
+ try {
63
+ response = await executeHttpRequest ( notifServiceDest , {
64
+ url : targetUrl ,
65
+ method : httpMethod ,
66
+ data : payload ,
67
+ headers : csrfHeaders ,
68
+ } ) ;
69
+ } catch ( e ) {
70
+ console . log ( e ) ;
71
+ }
72
+ return response ;
73
+ }
74
+
75
+ function buildDefaultNotification (
76
+ recipients ,
77
+ priority ,
78
+ title ,
79
+ description
80
+ ) {
81
+ const properties = [
82
+ {
83
+ Key : "title" ,
84
+ Language : "en" ,
85
+ Value : title ,
86
+ Type : "String" ,
87
+ IsSensitive : false ,
88
+ } ,
89
+ {
90
+ Key : "description" ,
91
+ Language : "en" ,
92
+ Value : description ,
93
+ Type : "String" ,
94
+ IsSensitive : false ,
95
+ } ,
96
+ ] ;
97
+
98
+ return {
99
+ NotificationTypeKey : "Default" ,
100
+ NotificationTypeVersion : "1" ,
101
+ Priority : priority ,
102
+ Properties : properties ,
103
+ Recipients : recipients . map ( ( recipient ) => ( { RecipientId : recipient } ) )
104
+ } ;
105
+ }
106
+
107
+ function buildNotification ( passedArguments ) {
108
+ let notification ;
109
+ if ( passedArguments . length == 1 && typeof passedArguments [ 0 ] === "object" && ! Array . isArray ( passedArguments [ 0 ] ) ) {
110
+ notification = passedArguments [ 0 ] ;
111
+ notification [ "NotificationTypeKey" ] = getNotificationTypesKeyWithPrefix ( notification [ "NotificationTypeKey" ] ) ;
112
+ } else {
113
+ const recipients = passedArguments [ 0 ] ;
114
+ const priority = passedArguments [ 1 ] ;
115
+ const title = passedArguments [ 2 ] ;
116
+ const description = passedArguments [ 3 ] ? passedArguments [ 3 ] : "" ;
117
+
118
+ if ( ! validateNotifyParameters ( recipients , priority , title ) ) {
119
+ return ;
120
+ }
121
+
122
+ notification = buildDefaultNotification (
123
+ recipients ,
124
+ priority ,
125
+ title ,
126
+ description
127
+ ) ;
128
+ }
129
+
130
+ return notification ;
131
+ }
132
+
44
133
module . exports = {
45
134
messages,
46
135
validateNotificationTypes,
47
136
readFile,
48
137
doesKeyExist,
49
138
getNotificationDestination,
50
139
getPrefix,
51
- getNotificationTypesKeyWithPrefix
140
+ getNotificationTypesKeyWithPrefix,
141
+ executeRequest,
142
+ buildNotification
52
143
} ;
0 commit comments