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

create notification types from the file and publish from plugin #1

Merged
merged 80 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
6db8964
create notfication types from the file and publish from plugin
RamIndia Aug 24, 2023
4c011a1
folder restructure and minor changes
Aug 29, 2023
143a821
reference change
Aug 29, 2023
e066543
correcting the configuration props in plugin
RamIndia Aug 29, 2023
f11399c
jest dependency added for dev
Sep 4, 2023
b5d8ccf
local interface
jeevitha011 Sep 4, 2023
7e1b1b7
Merge pull request #2 from cap-js/jest_configuration
RamIndia Sep 8, 2023
61e2749
removed global.alert
jeevitha011 Sep 8, 2023
10c0d33
name changes
jeevitha011 Sep 8, 2023
1b1ca5c
Update package.json
jeevitha011 Sep 8, 2023
314516c
name changes
jeevitha011 Sep 8, 2023
4a6c859
Merge pull request #3 from cap-js/create-class
AnmolBinani Sep 8, 2023
8b2ca5c
Local testing (#4)
sidakphull Sep 20, 2023
79991f1
refactor notification types creation
simeonPetkov96 Sep 25, 2023
6fa55e4
Refactor Post Notification
ipaunov Sep 20, 2023
9bdbe42
Fix conflicts
ipaunov Sep 25, 2023
51882a7
Address comments and change notify signature
ipaunov Sep 26, 2023
d810851
Address comments
ipaunov Sep 27, 2023
a651a47
Adress comments
ipaunov Sep 27, 2023
06ec403
Fix comments
ipaunov Sep 27, 2023
b7ce7c1
Merge pull request #7 from cap-js/refactor_notification
ipaunov Sep 27, 2023
c3038fb
add default type and fix notification types creation (#8)
sidakphull Sep 29, 2023
1c72aec
Fix: Adding validations for properties
RamIndia Sep 29, 2023
74674cc
Changing the default types to empty array
RamIndia Sep 29, 2023
98c846a
Merge pull request #10 from cap-js/missing-validations
RamIndia Sep 29, 2023
303c784
Add test directory
ipaunov Sep 29, 2023
efae405
Merge pull request #11 from cap-js/post_notification_tests
ipaunov Sep 29, 2023
870d48a
update cloud-sdk (#13)
sidakphull Oct 6, 2023
9667f25
Change Notify
ipaunov Oct 10, 2023
a7daf1f
Fix comments
ipaunov Oct 11, 2023
cd22abf
Merge pull request #15 from cap-js/fix_signature
ipaunov Oct 12, 2023
b1fb6c3
Deploy Notification Types within a CF Task (#14)
simeonPetkov96 Oct 13, 2023
b53c33d
Bring back package lock
simeonPetkov96 Oct 13, 2023
fd12c89
use outbox emit to send notifications
sidakphull Oct 13, 2023
707ade8
Post Notification Unit Tests
ipaunov Oct 13, 2023
feff6a7
package.json update
Oct 16, 2023
867e9d4
Merge pull request #19 from cap-js/package_json_update
AnmolBinani Oct 16, 2023
e349e57
Merge branch 'MVP' into unit-tests-post-notification
ipaunov Oct 16, 2023
e680d93
Merge branch 'MVP' into package_lock
AnmolBinani Oct 17, 2023
719af4a
Merge branch 'MVP' into outbox-integration
sidakphull Oct 17, 2023
5a3dc77
typo
sidakphull Oct 17, 2023
52a1dc2
Merge pull request #16 from cap-js/package_lock
RamIndia Oct 18, 2023
fe5481d
Merge branch 'MVP' into outbox-integration
RamIndia Oct 18, 2023
67e3c9a
Merge pull request #17 from cap-js/unit-tests-post-notification
ipaunov Oct 18, 2023
b47ce17
Merge branch 'MVP' into outbox-integration
AnmolBinani Oct 18, 2023
0c1a0e5
copy types file during build
sidakphull Oct 18, 2023
6623b61
Tests for Notification Types
simeonPetkov96 Oct 18, 2023
8548907
add log
sidakphull Oct 19, 2023
bc449d4
Fix comments
simeonPetkov96 Oct 19, 2023
3cea352
Merge branch 'MVP' into unit_tests_for_ntypes
simeonPetkov96 Oct 19, 2023
d9742da
Create project build voter (#22)
simeonPetkov96 Oct 19, 2023
fda591b
Merge branch 'MVP' into unit_tests_for_ntypes
simeonPetkov96 Oct 19, 2023
97ea4a8
cds logger added, minor cosmetic and indentation changes
Oct 19, 2023
2230b69
test fix
Oct 19, 2023
34ca813
Merge branch 'MVP' into add_logger
AnmolBinani Oct 19, 2023
d854399
Delete index.cds
AnmolBinani Oct 19, 2023
c166544
Adding table of content for Notification Plugin
RamIndia Oct 19, 2023
885c0d7
abstract notify function added
Oct 20, 2023
2398737
Merge pull request #24 from cap-js/add_logger
AnmolBinani Oct 20, 2023
73bf764
merged MVP branch
Oct 20, 2023
6787fa8
cosmetic changes
Oct 20, 2023
867a1bb
Merge pull request #18 from cap-js/outbox-integration
AnmolBinani Oct 20, 2023
f554f94
Adding setup and usage instructions
RamIndia Oct 20, 2023
6d93b78
adding missing link
RamIndia Oct 20, 2023
ef40128
Adding demo gif
RamIndia Oct 20, 2023
f0c5bfd
Adding new demo gif
RamIndia Oct 20, 2023
9169a90
Merge branch 'MVP' into updateReadme
AnmolBinani Oct 20, 2023
7eada21
Merge pull request #25 from cap-js/updateReadme
AnmolBinani Oct 20, 2023
09cb228
fix notification test cases
sidakphull Oct 23, 2023
b54c4f3
Merge branch 'MVP' into update-tests
sidakphull Oct 23, 2023
92a9b53
updating cds-dk version
RamIndia Oct 23, 2023
c2dd479
minor changes
Oct 23, 2023
83f4035
Resolving conflicts
RamIndia Oct 23, 2023
7874669
Merge pull request #26 from cap-js/update-tests
RamIndia Oct 23, 2023
ce35fb5
removing `cds` mocking from test
Oct 23, 2023
5f763a7
Merge branch 'MVP' into unit_tests_for_ntypes
AnmolBinani Oct 23, 2023
bbad0f9
Merge pull request #20 from cap-js/unit_tests_for_ntypes
RamIndia Oct 23, 2023
54ff157
minor changes
Oct 23, 2023
beaf837
minor change
Oct 23, 2023
24a1b3d
Merge pull request #21 from cap-js/build-plugin
AnmolBinani Oct 23, 2023
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
43 changes: 43 additions & 0 deletions cds-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const cds = require("@sap/cds");
const fs = require("fs").promises;
const notifier = require("./srv/notify");

global.alert = {
notify: notifier.postNotification,
};

const oErrorMessages = {
INVALID_NOTIFICATION_TYPES:
"Failed to create Notification Types as they are not valid.",
};
AnmolBinani marked this conversation as resolved.
Show resolved Hide resolved

let fAreNotificationTypesValid = async (aNotificationtypes) => {
/**
* TODO: write a logic to check all the required fields.
*/
return true;
};
AnmolBinani marked this conversation as resolved.
Show resolved Hide resolved

cds.once("served", async () => {
/**
* For local testing initialise VCAP_SERVICES variables for the application
* process.env.VCAP_SERVICES = Strigified VCAP_SERVICES variable
*/
/**
* TODO: Decide the properties to be added in the alerts section for notificationtype files.
*/
if (cds.requires.alerts && cds.requires.alerts.notificationTypes) {
let notificationTypes = JSON.parse(
await fs.readFile(cds.requires.alerts.notificationTypes)
);
if (fAreNotificationTypesValid) {
notificationTypes.forEach((oNotificationType) => {
notifier.postNotificationType(oNotificationType);
});
} else {
console.log(oErrorMessages.INVALID_NOTIFICATION_TYPES);
}
}
});

module.exports = { alert: alert };
8 changes: 8 additions & 0 deletions index.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace sap.alert;

service AlertNotificationService {
AnmolBinani marked this conversation as resolved.
Show resolved Hide resolved
/**
* TODO : connect action to notify api.
*/
action notify();
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
"license": "SEE LICENSE IN LICENSE",
"main": "cds-plugin.js",
"files": [
"lib",
"srv"
"lib",
"srv"
],
"scripts": {
"lint": "npx eslint .",
"test": "npx jest --silent"
"lint": "npx eslint .",
"test": "npx jest --silent"
},
"dependencies": {
"@sap-cloud-sdk/core": "^1.54.2"
}
}

}
125 changes: 125 additions & 0 deletions srv/notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const cloudSDK = require("@sap-cloud-sdk/core");
AnmolBinani marked this conversation as resolved.
Show resolved Hide resolved
const { getDestination, executeHttpRequest, buildCsrfHeaders } = cloudSDK;

const NOTIFICATIONS_DESTINATION_NAME = "SAP_Notifications";
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc";

async function _getDestination(destinationName) {
const notifServiceDest = await getDestination(destinationName);
if (!notifServiceDest) {
throw new Error(`failed to get destination: ${destinationName}`);
}
return notifServiceDest;
}

async function getNotificationTypes(
destinationName = NOTIFICATIONS_DESTINATION_NAME
) {
const notifServiceDest = await _getDestination(destinationName);
const response = await executeHttpRequest(notifServiceDest, {
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`,
method: "get",
});
return response.data.d.results;
}

async function postNotificationType(
notificationType,
destinationName = NOTIFICATIONS_DESTINATION_NAME
) {
const notifServiceDest = await _getDestination(destinationName);
const csrfHeaders = await buildCsrfHeaders(notifServiceDest, {
url: NOTIFICATION_TYPES_API_ENDPOINT,
});
const aExistingNotificationTypes = await this.getNotificationTypes();
const bIsDuplicateNotificationType = aExistingNotificationTypes.find(
(nType) =>
nType.NotificationTypeKey === notificationType.NotificationTypeKey &&
nType.NotificationTypeVersion === notificationType.NotificationTypeVersion
);

if (!bIsDuplicateNotificationType) {
console.log(
`Notification Type of key ${notificationType.NotificationTypeKey} and version ${notificationType.NotificationTypeVersion} was not found. Creating it...`
);
const response = await executeHttpRequest(notifServiceDest, {
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`,
method: "post",
data: notificationType,
headers: csrfHeaders,
});
return response.data.d;
} else {
return true;
}
}

async function createNotificationObject(
recipients,
notificationTypeKey,
notificationTypeVersion,
notificationData,
language = "en"
) {
let aProperties = [];
if (typeof notificationData === "object") {
for (sProperty of Object.keys(notificationData)) {
//TODO: recheck if can be sent from application. Check for localization
aProperties.push({
Key: sProperty,
Language: language,
Value: notificationData[sProperty],
Type: "String",
IsSensitive: false,
});
}
}
return {
NotificationTypeKey: notificationTypeKey,
NotificationTypeVersion: notificationTypeVersion,
Priority: "High",
Properties: aProperties,
Recipients: recipients.map((recipient) => ({ RecipientId: recipient })),
};
}

async function postNotification(
recipients,
notificationTypeKey,
notificationTypeVersion,
notificationData,
language = "en",
destinationName = NOTIFICATIONS_DESTINATION_NAME
) {
const notification = await createNotificationObject(
recipients,
notificationTypeKey,
notificationTypeVersion,
notificationData,
language
);
const notifServiceDest = await _getDestination(destinationName);
const csrfHeaders = await buildCsrfHeaders(notifServiceDest, {
url: NOTIFICATIONS_API_ENDPOINT,
});

let response = {};
try {
response = await executeHttpRequest(notifServiceDest, {
url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`,
method: "post",
data: notification,
headers: csrfHeaders,
});
} catch (e) {
console.log(e);
}
return response.data.d;
}

module.exports = {
getNotificationTypes,
postNotificationType,
postNotification,
};