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