Skip to content

Commit

Permalink
Add additional tests for notifyToRest and utils (#28)
Browse files Browse the repository at this point in the history
* Add additional tests for notifyToRest

* Additional tests
  • Loading branch information
simeonPetkov96 authored Oct 24, 2023
1 parent ffad3b5 commit 90f31c4
Show file tree
Hide file tree
Showing 5 changed files with 654 additions and 615 deletions.
22 changes: 2 additions & 20 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { existsSync, readFileSync } = require('fs');
const { basename } = require('path');
const cds = require("@sap/cds");
const LOG = cds.log('notifications');
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
const { getDestination } = require("@sap-cloud-sdk/connectivity");
const PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];

Expand All @@ -23,12 +22,12 @@ const messages = {
};

function validateNotificationTypes(notificationTypes) {
notificationTypes.forEach((notificationType) => {
for(notificationType of notificationTypes){
if (!("NotificationTypeKey" in notificationType)) {
LOG._warn && LOG.warn(messages.INVALID_NOTIFICATION_TYPES);
return false;
}
});
}

return true;
}
Expand Down Expand Up @@ -128,22 +127,6 @@ function getNotificationTypesKeyWithPrefix(notificationTypeKey) {
return `${prefix}/${notificationTypeKey}`;
}

async function executeRequest(httpMethod, targetUrl, payload, notificationDestination, csrfHeaders) {
let response = {};
try {
response = await executeHttpRequest(notificationDestination, {
url: targetUrl,
method: httpMethod,
data: payload,
headers: csrfHeaders,
});
} catch (e) {
console.log(e);
response = e.response.data;
}
return response;
}

function buildDefaultNotification(
recipients,
priority = "NEUTRAL",
Expand Down Expand Up @@ -255,6 +238,5 @@ module.exports = {
getNotificationDestination,
getPrefix,
getNotificationTypesKeyWithPrefix,
executeRequest,
buildNotification
};
19 changes: 5 additions & 14 deletions srv/notifyToRest.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
const NotificationService = require('./service');
const NotificationService = require("./service");
const { buildNotification } = require("../lib/utils");
const { postNotification } = require('../lib/notifications');
const { postNotification } = require("../lib/notifications");

module.exports = class NotifyToRest extends NotificationService {
async init() {

// call NotificationService's init
await super.init();

this.on('postNotificationEvent', async function(req) {
const { data } = req;

try {
await postNotification(data);
} catch (err) {
throw err;
}
});
this.on("postNotificationEvent", async (req) => await postNotification(req.data));
}

async notify(notificationData) {
const notification = buildNotification(notificationData);

if (notification) {
await this.emit({ event: 'postNotificationEvent', data: notification });
await this.emit({ event: "postNotificationEvent", data: notification });
}
}
}
};
Loading

0 comments on commit 90f31c4

Please sign in to comment.