Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional tests for notifyToRest and utils #28

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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