Skip to content

Commit 8b2ca5c

Browse files
sidakphullAnmol Binani
andauthored
Local testing (#4)
* add log2rest kind and implement log2console * rename files * update profiles * rename alert to notify * track notifications types for local * throw error if notification type is invalid --------- Co-authored-by: Anmol Binani <[email protected]>
1 parent 4a6c859 commit 8b2ca5c

File tree

7 files changed

+135
-70
lines changed

7 files changed

+135
-70
lines changed

cds-plugin.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const cds = require("@sap/cds");
22
const notifier = require("./lib/notifications");
33
const {
4-
messages,
54
validateNotificationTypes,
6-
readFileContent,
5+
readFile,
6+
doesKeyExist
77
} = require("./lib/utils");
88

99
cds.once("served", () => {
@@ -14,23 +14,33 @@ cds.once("served", () => {
1414
/**
1515
* TODO: Decide the properties to be added in the alerts section for notificationtype files.
1616
*/
17-
if (cds.requires?.notifications?.types) {
18-
let notificationTypes = readFileContent(
19-
cds.requires.notifications.types
20-
);
21-
if (validateNotificationTypes(notificationTypes)) {
17+
const profiles = cds.env.profiles ?? [];
18+
const production = profiles.includes('production');
19+
if (cds.env.requires?.notifications?.types) {
20+
// read notification types
21+
const notificationTypes = readFile(cds.env.requires.notifications.types);
22+
23+
// validate notification types
24+
validateNotificationTypes(notificationTypes);
25+
26+
// create notification types
27+
if (production) {
2228
notificationTypes.forEach((oNotificationType) => {
2329
notifier.postNotificationType(oNotificationType);
2430
});
2531
} else {
26-
/**
27-
* TODO: Move this message inside the validation function
28-
* ? Should we throw error message or warning for the specific invalid NotificationType?
29-
* ? e.g., If we have 5 notificationTypes and 1 out of the 5 is invalid type, we should
30-
* ? go ahead and create the valid ones and display INFO/Warning for the invalid type
31-
*/
32-
console.log(messages.INVALID_NOTIFICATION_TYPES);
32+
const types = {};
33+
notificationTypes.forEach((oNotificationType) => {
34+
if (!doesKeyExist(types, oNotificationType.NotificationTypeKey)) {
35+
types[oNotificationType.NotificationTypeKey] = {};
36+
}
37+
38+
types[oNotificationType.NotificationTypeKey][oNotificationType.NotificationTypeVersion] = oNotificationType;
39+
});
40+
41+
cds.notifications = { local: { types }};
3342
}
43+
} else if (!production) {
44+
cds.notifications = { local: { types: {} }};
3445
}
3546
});
36-

lib/notifications.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async function postNotificationType(
5656
}
5757
}
5858

59-
async function createNotificationObject(
59+
function createNotificationObject(
6060
recipients,
6161
notificationTypeKey,
6262
notificationTypeVersion,
@@ -93,7 +93,7 @@ async function postNotification(
9393
language = "en",
9494
destinationName = NOTIFICATIONS_DESTINATION_NAME
9595
) {
96-
const notification = await createNotificationObject(
96+
const notification = createNotificationObject(
9797
recipients,
9898
notificationTypeKey,
9999
notificationTypeVersion,
@@ -122,5 +122,6 @@ async function postNotification(
122122
module.exports = {
123123
getNotificationTypes,
124124
postNotificationType,
125+
createNotificationObject,
125126
postNotification,
126127
};

lib/utils.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@ const messages = {
66
DESTINATION_NOT_FOUND: "Failed to get destination: ",
77
};
88

9+
// we should at least verify that NotificationTypeKey and NotificationTypeVersion is present
910
function validateNotificationTypes(notificationTypes = {}) {
1011
/**
1112
* TODO: write a logic to check all the required fields.
1213
*/
14+
15+
// throw error in case types are invalid
16+
// throw new Error(messages.INVALID_NOTIFICATION_TYPES);
17+
1318
return true;
1419
}
1520

16-
function readFileContent(filePath) {
17-
return JSON.parse(
18-
fs.readFileSync(cds.requires.notifications.types)
19-
);
21+
function doesKeyExist(obj, key) {
22+
return typeof(key) === 'string' && typeof(obj) === 'object' && key in obj;
23+
}
24+
25+
function readFile(filePath) {
26+
return JSON.parse(fs.readFileSync(filePath));
2027
}
2128

2229
module.exports = {
2330
messages,
2431
validateNotificationTypes,
25-
readFileContent
32+
readFile,
33+
doesKeyExist
2634
};

package.json

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
{
2-
"name": "@cap-js/alert-notification",
3-
"version": "0.0.1",
4-
"description": "CDS plugin providing integration to the SAP BTP Alert Notification Service.",
5-
"repository": "cap-js/alert-notification",
6-
"author": "SAP SE (https://www.sap.com)",
7-
"homepage": "https://cap.cloud.sap/",
8-
"license": "SEE LICENSE IN LICENSE",
9-
"main": "cds-plugin.js",
10-
"files": [
11-
"lib",
12-
"srv"
13-
],
14-
"dependencies": {
15-
"@sap-cloud-sdk/core": "^1.54.2"
16-
},
17-
"devDependencies": {
18-
"jest": "^29.6.4"
19-
},
20-
"scripts": {
21-
"lint": "npx eslint .",
22-
"test": "npx jest --silent"
23-
},
24-
"cds": {
25-
"requires": {
26-
"kinds": {
27-
"notifications": {
28-
"[development]": {
29-
"kind": "notify-to-console"
30-
}
31-
},
32-
"notify-to-console": {
33-
"impl": "@cap-js/alert-notification/srv/notifyToConsole",
34-
"outbox": false
35-
}
2+
"name": "@cap-js/alert-notification",
3+
"version": "0.0.1",
4+
"description": "CDS plugin providing integration to the SAP BTP Alert Notification Service.",
5+
"repository": "cap-js/alert-notification",
6+
"author": "SAP SE (https://www.sap.com)",
7+
"homepage": "https://cap.cloud.sap/",
8+
"license": "SEE LICENSE IN LICENSE",
9+
"main": "cds-plugin.js",
10+
"files": [
11+
"lib",
12+
"srv"
13+
],
14+
"dependencies": {
15+
"@sap-cloud-sdk/core": "^1.54.2"
16+
},
17+
"devDependencies": {
18+
"jest": "^29.6.4"
19+
},
20+
"scripts": {
21+
"lint": "npx eslint .",
22+
"test": "npx jest --silent"
23+
},
24+
"cds": {
25+
"requires": {
26+
"kinds": {
27+
"notifications": {
28+
"[development]": {
29+
"kind": "notify-to-console"
30+
},
31+
"[production]": {
32+
"kind": "notify-to-rest"
3633
}
34+
},
35+
"notify-to-console": {
36+
"impl": "@cap-js/alert-notification/srv/notifyToConsole",
37+
"outbox": false
38+
},
39+
"notify-to-rest": {
40+
"impl": "@cap-js/alert-notification/srv/notifyToRest",
41+
"outbox": true
3742
}
3843
}
39-
}
44+
}
45+
}
46+
}

srv/notifyToConsole.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1-
const AlertNotificationService = require('./service')
1+
const NotificationService = require('./service');
22

3-
module.exports = class NotifyToConsole extends AlertNotificationService {
3+
const cds = require("@sap/cds");
4+
const notifier = require("../lib/notifications");
5+
const { doesKeyExist } = require('../lib/utils');
6+
7+
module.exports = class NotifyToConsole extends NotificationService {
48
async init() {
9+
// call NotificationService's init
10+
await super.init();
11+
}
12+
13+
notify (
14+
recipients,
15+
notificationTypeKey,
16+
notificationTypeVersion,
17+
notificationData,
18+
language = "en"
19+
) {
20+
21+
const types = cds.notifications.local.types;
22+
if (!doesKeyExist(types, notificationTypeKey)) {
23+
throw new Error(`Invalid Notification Type Key: ${notificationTypeKey}`);
24+
}
25+
26+
if (!doesKeyExist(types[notificationTypeKey], notificationTypeVersion)) {
27+
throw new Error(`Invalid Notification Type Version for Key ${notificationTypeKey}: ${notificationTypeVersion}`);
28+
}
29+
30+
const notification = notifier.createNotificationObject(
31+
recipients,
32+
notificationTypeKey,
33+
notificationTypeVersion,
34+
notificationData,
35+
language
36+
);
537

6-
// call AlertNotificationService's init
7-
await super.init()
38+
console.log(`[ans] - ${notificationTypeKey} - ${notificationTypeVersion}:`, notification);
839
}
940
}

srv/notifyToRest.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const NotificationService = require('./service');
2+
3+
const notifier = require("../lib/notifications");
4+
5+
module.exports = class NotifyToRest extends NotificationService {
6+
async init() {
7+
8+
this.notify = notifier.postNotification;
9+
10+
// call NotificationService's init
11+
await super.init();
12+
}
13+
}

srv/service.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
* We will branch out for development and production profile here
44
*/
55

6-
const cds = require('@sap/cds')
7-
const notifier = require("../lib/notifications");
8-
96
// REVISIT: cds.OutboxService or technique to avoid extending OutboxService
10-
const OutboxService = require('@sap/cds/libx/_runtime/messaging/Outbox')
7+
const OutboxService = require('@sap/cds/libx/_runtime/messaging/Outbox');
118

12-
module.exports = class AlertNotificationService extends OutboxService {
9+
module.exports = class NotificationService extends OutboxService {
1310
async init() {
14-
15-
// call OutboxService's init
16-
await super.init()
1711

18-
this.notify = notifier.postNotification
12+
// call OutboxService's init
13+
await super.init();
1914
}
2015
}

0 commit comments

Comments
 (0)