Skip to content

Commit 6db8964

Browse files
committed
create notfication types from the file and publish from plugin
1 parent 81fe99b commit 6db8964

File tree

4 files changed

+184
-6
lines changed

4 files changed

+184
-6
lines changed

cds-plugin.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const cds = require("@sap/cds");
2+
const fs = require("fs").promises;
3+
const notifier = require("./srv/notify");
4+
5+
global.alert = {
6+
notify: notifier.postNotification,
7+
};
8+
9+
const oErrorMessages = {
10+
INVALID_NOTIFICATION_TYPES:
11+
"Failed to create Notification Types as they are not valid.",
12+
};
13+
14+
let fAreNotificationTypesValid = async (aNotificationtypes) => {
15+
/**
16+
* TODO: write a logic to check all the required fields.
17+
*/
18+
return true;
19+
};
20+
21+
cds.once("served", async () => {
22+
/**
23+
* For local testing initialise VCAP_SERVICES variables for the application
24+
* process.env.VCAP_SERVICES = Strigified VCAP_SERVICES variable
25+
*/
26+
/**
27+
* TODO: Decide the properties to be added in the alerts section for notificationtype files.
28+
*/
29+
if (cds.requires.alerts && cds.requires.alerts.notificationTypes) {
30+
let notificationTypes = JSON.parse(
31+
await fs.readFile(cds.requires.alerts.notificationTypes)
32+
);
33+
if (fAreNotificationTypesValid) {
34+
notificationTypes.forEach((oNotificationType) => {
35+
notifier.postNotificationType(oNotificationType);
36+
});
37+
} else {
38+
console.log(oErrorMessages.INVALID_NOTIFICATION_TYPES);
39+
}
40+
}
41+
});
42+
43+
module.exports = { alert: alert };

index.cds

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace sap.alert;
2+
3+
service AlertNotificationService {
4+
/**
5+
* TODO : connect action to notify api.
6+
*/
7+
action notify();
8+
}

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
"license": "SEE LICENSE IN LICENSE",
99
"main": "cds-plugin.js",
1010
"files": [
11-
"lib",
12-
"srv"
11+
"lib",
12+
"srv"
1313
],
1414
"scripts": {
15-
"lint": "npx eslint .",
16-
"test": "npx jest --silent"
15+
"lint": "npx eslint .",
16+
"test": "npx jest --silent"
17+
},
18+
"dependencies": {
19+
"@sap-cloud-sdk/core": "^1.54.2"
1720
}
18-
}
19-
21+
}

srv/notify.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
};

0 commit comments

Comments
 (0)