Skip to content

Commit d810851

Browse files
committed
Address comments
1 parent 51882a7 commit d810851

File tree

3 files changed

+46
-56
lines changed

3 files changed

+46
-56
lines changed

lib/notifications.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { buildCsrfHeaders } = require("@sap-cloud-sdk/core");
2-
const { getNotificationDestination, getNotificationTypesKeyWithPrefix, executeRequest, createDefaultNotificationObject, POSSIBLE_PRIORITIES } = require("./utils");
2+
const { getNotificationDestination, executeRequest, createNotification } = require("./utils");
33

44
const NOTIFICATIONS_DESTINATION_NAME = cds.env.requires.notifications?.destination ?? "SAP_Notifications";
55
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
@@ -10,30 +10,8 @@ async function postNotification() {
1010
url: NOTIFICATIONS_API_ENDPOINT,
1111
});
1212

13-
if (arguments.length == 1 && typeof arguments[0] === "object") {
14-
let notification = arguments[0];
15-
notification["NotificationTypeKey"] = getNotificationTypesKeyWithPrefix(notification["NotificationTypeKey"]);
16-
17-
return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notifServiceDest, csrfHeaders)).data.d;
18-
} else {
19-
let recipients = arguments[0];
20-
let priority = arguments[1];
21-
let title = arguments[2];
22-
let description = arguments[3] ? arguments[3] : "";
23-
24-
if (!POSSIBLE_PRIORITIES.includes(priority)) {
25-
throw new Error(`Invalid priority ${priority}. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH`);
26-
}
27-
28-
let notification = createDefaultNotificationObject(
29-
recipients,
30-
priority,
31-
title,
32-
description
33-
);
34-
35-
return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notifServiceDest, csrfHeaders)).data.d;
36-
}
13+
const notification = createNotification(arguments);
14+
return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notifServiceDest, csrfHeaders)).data.d;
3715
}
3816

3917
module.exports = {

lib/utils.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ const fs = require('fs');
22
const { basename } = require('path')
33
const cds = require("@sap/cds");
44
const { executeHttpRequest, getDestination } = require("@sap-cloud-sdk/core");
5-
const POSSIBLE_PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];
5+
const PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];
66

77
const messages = {
88
INVALID_NOTIFICATION_TYPES: "Notification Types must contain the following keys: 'NotificationTypeKey' and 'NotificationTypeVersion'.",
99
DESTINATION_NOT_FOUND: "Failed to get destination: ",
10+
MANDATORY_PARAMETER_NOT_PASSED: "Recipients, priority and title are mandatory parameters",
1011
};
1112

1213
function validateNotificationTypes(notificationTypes) {
@@ -17,6 +18,16 @@ function validateNotificationTypes(notificationTypes) {
1718
});
1819
}
1920

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+
2031
function doesKeyExist(obj, key) {
2132
return typeof(key) === 'string' && typeof(obj) === 'object' && key in obj;
2233
}
@@ -42,12 +53,12 @@ function getNotificationTypesKeyWithPrefix(notificationTypeKey) {
4253
return `${prefix}/${notificationTypeKey}`;
4354
}
4455

45-
async function executeRequest(httpmethod, targetUrl, payload, notifServiceDest, csrfHeaders) {
56+
async function executeRequest(httpMethod, targetUrl, payload, notifServiceDest, csrfHeaders) {
4657
let response = {};
4758
try {
4859
response = await executeHttpRequest(notifServiceDest, {
4960
url: targetUrl,
50-
method: httpmethod,
61+
method: httpMethod,
5162
data: payload,
5263
headers: csrfHeaders,
5364
});
@@ -58,13 +69,13 @@ async function executeRequest(httpmethod, targetUrl, payload, notifServiceDest,
5869
return response;
5970
}
6071

61-
function createDefaultNotificationObject(
72+
function createDefaultNotification(
6273
recipients,
6374
priority,
6475
title,
6576
description
6677
) {
67-
let properties = [
78+
const properties = [
6879
{
6980
Key: "title",
7081
Language: "en",
@@ -90,15 +101,38 @@ function createDefaultNotificationObject(
90101
};
91102
}
92103

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+
93128
module.exports = {
94129
messages,
95-
POSSIBLE_PRIORITIES,
96130
validateNotificationTypes,
97131
readFile,
98132
doesKeyExist,
99133
getNotificationDestination,
100134
getPrefix,
101135
getNotificationTypesKeyWithPrefix,
102136
executeRequest,
103-
createDefaultNotificationObject
137+
createNotification
104138
};

srv/notifyToConsole.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const NotificationService = require('./service');
2-
const { getNotificationTypesKeyWithPrefix, createDefaultNotificationObject, POSSIBLE_PRIORITIES } = require("./../lib/utils");
2+
const { createNotification } = require("./../lib/utils");
33

44
module.exports = class NotifyToConsole extends NotificationService {
55
async init() {
@@ -8,29 +8,7 @@ module.exports = class NotifyToConsole extends NotificationService {
88
}
99

1010
notify() {
11-
let notification;
12-
13-
if (arguments.length == 1 && typeof arguments[0] === "object") {
14-
notification = arguments[0];
15-
notification["NotificationTypeKey"] = getNotificationTypesKeyWithPrefix(notification["NotificationTypeKey"]);
16-
17-
} else {
18-
let recipients = arguments[0];
19-
let priority = arguments[1];
20-
let title = arguments[2];
21-
let description = arguments[3] ? arguments[3] : "";
22-
23-
if (!POSSIBLE_PRIORITIES.includes(priority)) {
24-
throw new Error(`Invalid priority ${priority}. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH`);
25-
}
26-
27-
notification = createDefaultNotificationObject(
28-
recipients,
29-
priority,
30-
title,
31-
description
32-
);
33-
}
11+
const notification = createNotification(arguments);
3412

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

0 commit comments

Comments
 (0)