Skip to content

Commit cd22abf

Browse files
authored
Merge pull request #15 from cap-js/fix_signature
Change Notify
2 parents 870d48a + a7daf1f commit cd22abf

File tree

3 files changed

+113
-27
lines changed

3 files changed

+113
-27
lines changed

lib/notifications.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
22
const { getNotificationDestination, executeRequest, buildNotification } = require("./utils");
33
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
44

5-
async function postNotification() {
5+
async function postNotification(notificationData) {
66
const notificationDestination = await getNotificationDestination();
77
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
88
url: NOTIFICATIONS_API_ENDPOINT,
99
});
1010

11-
const notification = buildNotification(arguments);
11+
const notification = buildNotification(notificationData);
1212

1313
if (notification) {
1414
const response = await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notificationDestination, csrfHeaders);
@@ -19,3 +19,4 @@ async function postNotification() {
1919
module.exports = {
2020
postNotification
2121
};
22+

lib/utils.js

Lines changed: 109 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ const messages = {
99
TYPES_FILE_NOT_EXISTS: "Notification Types file path is incorrect.",
1010
INVALID_NOTIFICATION_TYPES: "Notification Types must contain the following key: 'NotificationTypeKey'.",
1111
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 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"
1321
};
1422

1523
function validateNotificationTypes(notificationTypes) {
@@ -23,17 +31,66 @@ function validateNotificationTypes(notificationTypes) {
2331
return true;
2432
}
2533

26-
function validateNotifyParameters(recipients, priority, title) {
27-
if (!recipients || !priority || !title) {
28-
console.log(messages.MANDATORY_PARAMETER_NOT_PASSED);
34+
function validateDefaultNotifyParameters(recipients, priority, title, description) {
35+
if (!recipients || !title) {
36+
console.log(messages.MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION);
2937
return false;
3038
}
3139

32-
if (!PRIORITIES.includes(priority)) {
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);
47+
return false;
48+
}
49+
50+
if (priority && !PRIORITIES.includes(priority.toUpperCase())) {
51+
console.log(`Invalid priority ${priority}. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH`);
52+
return false;
53+
}
54+
55+
if (description && 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 && !PRIORITIES.includes(priority.toUpperCase())) {
3375
console.log(`Invalid priority ${priority}. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH`);
3476
return false;
3577
}
3678

79+
if (properties && !Array.isArray(properties)) {
80+
console.log(messages.PROPERTIES_IS_NOT_OBJECT);
81+
return false;
82+
}
83+
84+
if (navigation && typeof navigation !== "object") {
85+
console.log(messages.NAVIGATION_IS_NOT_OBJECT);
86+
return false;
87+
}
88+
89+
if (payload && typeof payload !== "object") {
90+
console.log(messages.PAYLOAD_IS_NOT_OBJECT);
91+
return false;
92+
}
93+
3794
return true;
3895
}
3996

@@ -87,9 +144,9 @@ async function executeRequest(httpMethod, targetUrl, payload, notificationDestin
87144

88145
function buildDefaultNotification(
89146
recipients,
90-
priority,
147+
priority = "NEUTRAL",
91148
title,
92-
description
149+
description = ""
93150
) {
94151
const properties = [
95152
{
@@ -117,30 +174,58 @@ function buildDefaultNotification(
117174
};
118175
}
119176

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"] : "NEUTRAL",
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+
}
130205

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"])) {
132217
return;
133218
}
134219

135220
notification = buildDefaultNotification(
136-
recipients,
137-
priority,
138-
title,
139-
description
140-
);
221+
notificationData["recipients"],
222+
notificationData["priority"],
223+
notificationData["title"],
224+
notificationData["description"]
225+
)
141226
}
142227

143-
return notification;
228+
return JSON.parse(JSON.stringify(notification));
144229
}
145230

146231
module.exports = {

srv/notifyToConsole.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = class NotifyToConsole extends NotificationService {
1010

1111
notify() {
1212

13-
const notification = buildNotification(arguments);
13+
const notification = buildNotification(arguments[0]);
1414

1515
if (notification) {
1616
console.log(`SAP Alert Notification service notification: ${JSON.stringify(notification, null, 2)}`);

0 commit comments

Comments
 (0)