Skip to content

Commit 90f31c4

Browse files
Add additional tests for notifyToRest and utils (#28)
* Add additional tests for notifyToRest * Additional tests
1 parent ffad3b5 commit 90f31c4

File tree

5 files changed

+654
-615
lines changed

5 files changed

+654
-615
lines changed

lib/utils.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const { existsSync, readFileSync } = require('fs');
22
const { basename } = require('path');
33
const cds = require("@sap/cds");
44
const LOG = cds.log('notifications');
5-
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
65
const { getDestination } = require("@sap-cloud-sdk/connectivity");
76
const PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];
87

@@ -23,12 +22,12 @@ const messages = {
2322
};
2423

2524
function validateNotificationTypes(notificationTypes) {
26-
notificationTypes.forEach((notificationType) => {
25+
for(notificationType of notificationTypes){
2726
if (!("NotificationTypeKey" in notificationType)) {
2827
LOG._warn && LOG.warn(messages.INVALID_NOTIFICATION_TYPES);
2928
return false;
3029
}
31-
});
30+
}
3231

3332
return true;
3433
}
@@ -128,22 +127,6 @@ function getNotificationTypesKeyWithPrefix(notificationTypeKey) {
128127
return `${prefix}/${notificationTypeKey}`;
129128
}
130129

131-
async function executeRequest(httpMethod, targetUrl, payload, notificationDestination, csrfHeaders) {
132-
let response = {};
133-
try {
134-
response = await executeHttpRequest(notificationDestination, {
135-
url: targetUrl,
136-
method: httpMethod,
137-
data: payload,
138-
headers: csrfHeaders,
139-
});
140-
} catch (e) {
141-
console.log(e);
142-
response = e.response.data;
143-
}
144-
return response;
145-
}
146-
147130
function buildDefaultNotification(
148131
recipients,
149132
priority = "NEUTRAL",
@@ -255,6 +238,5 @@ module.exports = {
255238
getNotificationDestination,
256239
getPrefix,
257240
getNotificationTypesKeyWithPrefix,
258-
executeRequest,
259241
buildNotification
260242
};

srv/notifyToRest.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
const NotificationService = require('./service');
1+
const NotificationService = require("./service");
22
const { buildNotification } = require("../lib/utils");
3-
const { postNotification } = require('../lib/notifications');
3+
const { postNotification } = require("../lib/notifications");
44

55
module.exports = class NotifyToRest extends NotificationService {
66
async init() {
7-
87
// call NotificationService's init
98
await super.init();
109

11-
this.on('postNotificationEvent', async function(req) {
12-
const { data } = req;
13-
14-
try {
15-
await postNotification(data);
16-
} catch (err) {
17-
throw err;
18-
}
19-
});
10+
this.on("postNotificationEvent", async (req) => await postNotification(req.data));
2011
}
2112

2213
async notify(notificationData) {
2314
const notification = buildNotification(notificationData);
2415

2516
if (notification) {
26-
await this.emit({ event: 'postNotificationEvent', data: notification });
17+
await this.emit({ event: "postNotificationEvent", data: notification });
2718
}
2819
}
29-
}
20+
};

0 commit comments

Comments
 (0)