|
1 | 1 | const { buildCsrfHeaders } = require("@sap-cloud-sdk/core");
|
2 |
| -const { getNotificationDestination, getNotificationTypesKeyWithPrefix, executeRequest } = require("./utils"); |
| 2 | +const { getNotificationDestination, getNotificationTypesKeyWithPrefix, executeRequest, createDefaultNotificationObject, POSSIBLE_PRIORITIES } = require("./utils"); |
3 | 3 |
|
4 | 4 | const NOTIFICATIONS_DESTINATION_NAME = cds.env.requires.notifications?.destination ?? "SAP_Notifications";
|
5 | 5 | 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 |
| -} |
65 | 6 |
|
66 | 7 | async function postNotification() {
|
67 | 8 | const notifServiceDest = await getNotificationDestination(NOTIFICATIONS_DESTINATION_NAME);
|
68 | 9 | const csrfHeaders = await buildCsrfHeaders(notifServiceDest, {
|
69 |
| - url: NOTIFICATIONS_API_ENDPOINT, |
| 10 | + url: NOTIFICATIONS_API_ENDPOINT, |
70 | 11 | });
|
71 | 12 |
|
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"]); |
81 | 16 |
|
82 | 17 | 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`); |
116 | 26 | }
|
117 | 27 |
|
| 28 | + let notification = createDefaultNotificationObject( |
| 29 | + recipients, |
| 30 | + priority, |
| 31 | + title, |
| 32 | + description |
| 33 | + ); |
| 34 | + |
118 | 35 | return (await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notifServiceDest, csrfHeaders)).data.d;
|
119 | 36 | }
|
120 |
| - |
121 |
| - return new Error("Invalid invocation of postNotification"); |
122 | 37 | }
|
123 | 38 |
|
124 | 39 | module.exports = {
|
125 |
| - postNotification, |
| 40 | + postNotification |
126 | 41 | };
|
0 commit comments