Skip to content

Commit 4c011a1

Browse files
author
Anmol Binani
committed
folder restructure and minor changes
1 parent 6db8964 commit 4c011a1

File tree

6 files changed

+175
-25
lines changed

6 files changed

+175
-25
lines changed

cds-plugin.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
const cds = require("@sap/cds");
2-
const fs = require("fs").promises;
3-
const notifier = require("./srv/notify");
2+
const notifier = require("./srv/service");
3+
const {
4+
messages,
5+
validateNotificationTypes,
6+
readFileContent,
7+
} = require("./lib/utils");
48

59
global.alert = {
610
notify: notifier.postNotification,
711
};
812

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 () => {
13+
cds.once("served", () => {
2214
/**
2315
* For local testing initialise VCAP_SERVICES variables for the application
2416
* process.env.VCAP_SERVICES = Strigified VCAP_SERVICES variable
2517
*/
2618
/**
2719
* TODO: Decide the properties to be added in the alerts section for notificationtype files.
2820
*/
29-
if (cds.requires.alerts && cds.requires.alerts.notificationTypes) {
30-
let notificationTypes = JSON.parse(
31-
await fs.readFile(cds.requires.alerts.notificationTypes)
21+
if (cds.requires?.notifications?.notificationTypes) {
22+
let notificationTypes = readFileContent(
23+
cds.requires.alerts.notificationTypes
3224
);
33-
if (fAreNotificationTypesValid) {
25+
if (validateNotificationTypes(notificationTypes)) {
3426
notificationTypes.forEach((oNotificationType) => {
3527
notifier.postNotificationType(oNotificationType);
3628
});
3729
} else {
38-
console.log(oErrorMessages.INVALID_NOTIFICATION_TYPES);
30+
/**
31+
* TODO: Move this message inside the validation function
32+
* ? Should we throw error message or warning for the specific invalid NotificationType?
33+
* ? e.g., If we have 5 notificationTypes and 1 out of the 5 is invalid type, we should
34+
* ? go ahead and create the valid ones and display INFO/Warning for the invalid type
35+
*/
36+
console.log(messages.INVALID_NOTIFICATION_TYPES);
3937
}
4038
}
4139
});

index.cds

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace sap.alert;
1+
namespace sap.notifications;
22

33
service AlertNotificationService {
44
/**
55
* TODO : connect action to notify api.
66
*/
77
action notify();
8-
}
8+
}

lib/notifications.js

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

lib/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require('fs');
2+
3+
const messages = {
4+
INVALID_NOTIFICATION_TYPES:
5+
"Failed to create Notification Types as they are not valid.",
6+
DESTINATION_NOT_FOUND: "Failed to get destination: ",
7+
};
8+
9+
function validateNotificationTypes(notificationTypes = {}) {
10+
/**
11+
* TODO: write a logic to check all the required fields.
12+
*/
13+
return true;
14+
}
15+
16+
function readFileContent(filePath) {
17+
return JSON.parse(
18+
fs.readFileSync(cds.requires.alerts.notificationTypes)
19+
);
20+
}
21+
22+
module.exports = {
23+
messages,
24+
validateNotificationTypes,
25+
readFileContent
26+
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
"lib",
1212
"srv"
1313
],
14+
"dependencies": {
15+
"@sap-cloud-sdk/core": "^1.54.2"
16+
},
1417
"scripts": {
1518
"lint": "npx eslint .",
1619
"test": "npx jest --silent"
17-
},
18-
"dependencies": {
19-
"@sap-cloud-sdk/core": "^1.54.2"
2020
}
2121
}
File renamed without changes.

0 commit comments

Comments
 (0)