Skip to content

Commit 143a821

Browse files
author
Anmol Binani
committed
reference change
1 parent 4c011a1 commit 143a821

File tree

2 files changed

+5
-126
lines changed

2 files changed

+5
-126
lines changed

cds-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const cds = require("@sap/cds");
2-
const notifier = require("./srv/service");
2+
const notifier = require("./lib/notifications");
33
const {
44
messages,
55
validateNotificationTypes,

srv/service.js

Lines changed: 4 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,4 @@
1-
const cloudSDK = require("@sap-cloud-sdk/core");
2-
const { getDestination, executeHttpRequest, buildCsrfHeaders } = cloudSDK;
3-
4-
const NOTIFICATIONS_DESTINATION_NAME = "SAP_Notifications";
5-
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
6-
const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc";
7-
8-
async function _getDestination(destinationName) {
9-
const notifServiceDest = await getDestination(destinationName);
10-
if (!notifServiceDest) {
11-
throw new Error(`failed to get destination: ${destinationName}`);
12-
}
13-
return notifServiceDest;
14-
}
15-
16-
async function getNotificationTypes(
17-
destinationName = NOTIFICATIONS_DESTINATION_NAME
18-
) {
19-
const notifServiceDest = await _getDestination(destinationName);
20-
const response = await executeHttpRequest(notifServiceDest, {
21-
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`,
22-
method: "get",
23-
});
24-
return response.data.d.results;
25-
}
26-
27-
async function postNotificationType(
28-
notificationType,
29-
destinationName = NOTIFICATIONS_DESTINATION_NAME
30-
) {
31-
const notifServiceDest = await _getDestination(destinationName);
32-
const csrfHeaders = await buildCsrfHeaders(notifServiceDest, {
33-
url: NOTIFICATION_TYPES_API_ENDPOINT,
34-
});
35-
const aExistingNotificationTypes = await this.getNotificationTypes();
36-
const bIsDuplicateNotificationType = aExistingNotificationTypes.find(
37-
(nType) =>
38-
nType.NotificationTypeKey === notificationType.NotificationTypeKey &&
39-
nType.NotificationTypeVersion === notificationType.NotificationTypeVersion
40-
);
41-
42-
if (!bIsDuplicateNotificationType) {
43-
console.log(
44-
`Notification Type of key ${notificationType.NotificationTypeKey} and version ${notificationType.NotificationTypeVersion} was not found. Creating it...`
45-
);
46-
const response = await executeHttpRequest(notifServiceDest, {
47-
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`,
48-
method: "post",
49-
data: notificationType,
50-
headers: csrfHeaders,
51-
});
52-
return response.data.d;
53-
} else {
54-
return true;
55-
}
56-
}
57-
58-
async function createNotificationObject(
59-
recipients,
60-
notificationTypeKey,
61-
notificationTypeVersion,
62-
notificationData,
63-
language = "en"
64-
) {
65-
let aProperties = [];
66-
if (typeof notificationData === "object") {
67-
for (sProperty of Object.keys(notificationData)) {
68-
//TODO: recheck if can be sent from application. Check for localization
69-
aProperties.push({
70-
Key: sProperty,
71-
Language: language,
72-
Value: notificationData[sProperty],
73-
Type: "String",
74-
IsSensitive: false,
75-
});
76-
}
77-
}
78-
return {
79-
NotificationTypeKey: notificationTypeKey,
80-
NotificationTypeVersion: notificationTypeVersion,
81-
Priority: "High",
82-
Properties: aProperties,
83-
Recipients: recipients.map((recipient) => ({ RecipientId: recipient })),
84-
};
85-
}
86-
87-
async function postNotification(
88-
recipients,
89-
notificationTypeKey,
90-
notificationTypeVersion,
91-
notificationData,
92-
language = "en",
93-
destinationName = NOTIFICATIONS_DESTINATION_NAME
94-
) {
95-
const notification = await createNotificationObject(
96-
recipients,
97-
notificationTypeKey,
98-
notificationTypeVersion,
99-
notificationData,
100-
language
101-
);
102-
const notifServiceDest = await _getDestination(destinationName);
103-
const csrfHeaders = await buildCsrfHeaders(notifServiceDest, {
104-
url: NOTIFICATIONS_API_ENDPOINT,
105-
});
106-
107-
let response = {};
108-
try {
109-
response = await executeHttpRequest(notifServiceDest, {
110-
url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`,
111-
method: "post",
112-
data: notification,
113-
headers: csrfHeaders,
114-
});
115-
} catch (e) {
116-
console.log(e);
117-
}
118-
return response.data.d;
119-
}
120-
121-
module.exports = {
122-
getNotificationTypes,
123-
postNotificationType,
124-
postNotification,
125-
};
1+
/**
2+
* TODO: Add the entry point for the Plugin Service here.
3+
* We will branch out for development and production profile here
4+
*/

0 commit comments

Comments
 (0)