Skip to content

Commit 51882a7

Browse files
committed
Address comments and change notify signature
1 parent 9bdbe42 commit 51882a7

File tree

3 files changed

+83
-110
lines changed

3 files changed

+83
-110
lines changed

lib/notifications.js

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

44
const NOTIFICATIONS_DESTINATION_NAME = cds.env.requires.notifications?.destination ?? "SAP_Notifications";
55
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
6-
const POSSIBLE_PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];
7-
8-
async function createNotificationObject(
9-
recipients,
10-
notificationData
11-
) {
12-
const prefix = "@cap-ans";
13-
// const prefix = process.env.npm_package_name;
14-
return {
15-
Id: notificationData["Id"],
16-
OriginId: notificationData["OriginId"],
17-
NotificationTypeId: notificationData["NotificationTypeId"],
18-
NotificationTypeKey: `${prefix}/${notificationData["NotificationTypeKey"]}`,
19-
NotificationTypeVersion: notificationData["NotificationTypeVersion"],
20-
NavigationTargetAction: notificationData["NavigationTargetAction"],
21-
NavigationTargetObject: notificationData["NavigationTargetObject"],
22-
Priority: notificationData["Priority"] ? notificationData["Priority"] : "NEUTRAL",
23-
ProviderId: notificationData["ProviderId"],
24-
ActorId: notificationData["ActorId"],
25-
ActorDisplayText: notificationData["ActorDisplayText"],
26-
ActorImageURL: notificationData["ActorImageURL"],
27-
NotificationTypeTimestamp: notificationData["NotificationTypeTimestamp"],
28-
Recipients: recipients.map((recipient) => ({ RecipientId: recipient })),
29-
Properties: notificationData["Properties"],
30-
TargetParameters: notificationData["TargetParameters"]
31-
};
32-
}
33-
34-
async function createDefaultNotificationObject(
35-
recipients,
36-
title,
37-
description,
38-
priority
39-
) {
40-
let properties = [
41-
{
42-
Key: "title",
43-
Language: "en",
44-
Value: title,
45-
Type: "String",
46-
IsSensitive: false,
47-
},
48-
{
49-
Key: "description",
50-
Language: "en",
51-
Value: description,
52-
Type: "String",
53-
IsSensitive: false,
54-
},
55-
];
56-
57-
return {
58-
NotificationTypeKey: "Default",
59-
NotificationTypeVersion: "1",
60-
Priority: priority,
61-
Properties: properties,
62-
Recipients: recipients.map((recipient) => ({ RecipientId: recipient }))
63-
};
64-
}
656

667
async function postNotification() {
678
const notifServiceDest = await getNotificationDestination(NOTIFICATIONS_DESTINATION_NAME);
689
const csrfHeaders = await buildCsrfHeaders(notifServiceDest, {
69-
url: NOTIFICATIONS_API_ENDPOINT,
10+
url: NOTIFICATIONS_API_ENDPOINT,
7011
});
7112

72-
if (arguments.length == 1 && typeof arguments[0] === "object") { // using only notification object
73-
74-
return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, arguments[0], notifServiceDest, csrfHeaders)).data.d;
75-
76-
} else if (arguments.length == 2 && (Array.isArray(arguments[0]) && typeof arguments[1] === "object")) { // using array of recipients and partial notification object
77-
let notification = await createNotificationObject(
78-
arguments[0], // recipients
79-
arguments[1] // part of notification object
80-
);
13+
if (arguments.length == 1 && typeof arguments[0] === "object") {
14+
let notification = arguments[0];
15+
notification["NotificationTypeKey"] = getNotificationTypesKeyWithPrefix(notification["NotificationTypeKey"]);
8116

8217
return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notifServiceDest, csrfHeaders)).data.d;
83-
84-
} else if ((arguments.length >= 2) && (Array.isArray(arguments[0]) && typeof arguments[1] === "string")) { // using the default notification type
85-
let notification = {};
86-
87-
if (arguments.length === 2) { // using only recipients and title
88-
notification = await createDefaultNotificationObject(
89-
arguments[0], // recipients
90-
arguments[1], // title
91-
"",
92-
"NEUTRAL"
93-
);
94-
95-
} else if (arguments.length === 3 && POSSIBLE_PRIORITIES.includes(arguments[2])) { // using recipients, title, priority
96-
notification = await createDefaultNotificationObject(
97-
arguments[0], // recipients
98-
arguments[1], // title
99-
"",
100-
arguments[2] // priority
101-
);
102-
} else if (arguments.length === 3 && !POSSIBLE_PRIORITIES.includes(arguments[2])) { // using only recipients, title, description
103-
notification = await createDefaultNotificationObject(
104-
arguments[0], // recipients
105-
arguments[1], // title
106-
arguments[2], // description
107-
"NEUTRAL"
108-
);
109-
} else if (arguments.length === 4 && POSSIBLE_PRIORITIES.includes(arguments[3])) { // using only recipients, title, description, priority
110-
notification = await createDefaultNotificationObject(
111-
arguments[0], // recipients
112-
arguments[1], // title
113-
arguments[2], // description
114-
arguments[3], // priority
115-
);
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`);
11626
}
11727

28+
let notification = createDefaultNotificationObject(
29+
recipients,
30+
priority,
31+
title,
32+
description
33+
);
34+
11835
return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notifServiceDest, csrfHeaders)).data.d;
11936
}
120-
121-
return new Error("Invalid invocation of postNotification");
12237
}
12338

12439
module.exports = {
125-
postNotification,
40+
postNotification
12641
};

lib/utils.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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"];
56

67
const messages = {
78
INVALID_NOTIFICATION_TYPES: "Notification Types must contain the following keys: 'NotificationTypeKey' and 'NotificationTypeVersion'.",
@@ -57,13 +58,47 @@ async function executeRequest(httpmethod, targetUrl, payload, notifServiceDest,
5758
return response;
5859
}
5960

61+
function createDefaultNotificationObject(
62+
recipients,
63+
priority,
64+
title,
65+
description
66+
) {
67+
let properties = [
68+
{
69+
Key: "title",
70+
Language: "en",
71+
Value: title,
72+
Type: "String",
73+
IsSensitive: false,
74+
},
75+
{
76+
Key: "description",
77+
Language: "en",
78+
Value: description,
79+
Type: "String",
80+
IsSensitive: false,
81+
},
82+
];
83+
84+
return {
85+
NotificationTypeKey: "Default",
86+
NotificationTypeVersion: "1",
87+
Priority: priority,
88+
Properties: properties,
89+
Recipients: recipients.map((recipient) => ({ RecipientId: recipient }))
90+
};
91+
}
92+
6093
module.exports = {
6194
messages,
95+
POSSIBLE_PRIORITIES,
6296
validateNotificationTypes,
6397
readFile,
6498
doesKeyExist,
6599
getNotificationDestination,
66100
getPrefix,
67101
getNotificationTypesKeyWithPrefix,
68-
executeRequest
102+
executeRequest,
103+
createDefaultNotificationObject
69104
};

srv/notifyToConsole.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const NotificationService = require('./service');
2-
const cds = require("@sap/cds");
3-
const notifier = require("../lib/notifications");
2+
const { getNotificationTypesKeyWithPrefix, createDefaultNotificationObject, POSSIBLE_PRIORITIES } = require("./../lib/utils");
43

54
module.exports = class NotifyToConsole extends NotificationService {
65
async init() {
@@ -9,6 +8,30 @@ module.exports = class NotifyToConsole extends NotificationService {
98
}
109

1110
notify() {
12-
console.log(JSON.stringify(arguments, null, 2));
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+
}
34+
35+
console.log(`SAP Alert Notification service notification: ${JSON.stringify(notification, null, 2)}`);
1336
}
1437
}

0 commit comments

Comments
 (0)