Skip to content

Commit 9667f25

Browse files
committed
Change Notify
1 parent 870d48a commit 9667f25

File tree

2 files changed

+110
-23
lines changed

2 files changed

+110
-23
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: 107 additions & 21 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, 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"
1321
};
1422

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

26-
function validateNotifyParameters(recipients, priority, title) {
34+
function validateDefaultNotifyParameters(recipients, priority, title, description) {
2735
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);
2947
return false;
3048
}
3149

@@ -34,6 +52,45 @@ function validateNotifyParameters(recipients, priority, title) {
3452
return false;
3553
}
3654

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+
3794
return true;
3895
}
3996

@@ -89,7 +146,7 @@ function buildDefaultNotification(
89146
recipients,
90147
priority,
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"] : "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+
}
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 = {
@@ -154,3 +239,4 @@ module.exports = {
154239
executeRequest,
155240
buildNotification
156241
};
242+

0 commit comments

Comments
 (0)