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 all commits
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
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "main", "MVP" ]
pull_request:
branches: [ "main", "MVP" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm i
- run: npm run test-with-coverage
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 200,
"trailingComma" : "none"
}
163 changes: 162 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,163 @@
# alert-notification

The `@cap-js/notifications` package is a [CDS plugin](https://cap.cloud.sap/docs/node.js/cds-plugins#cds-plugin-packages) providing out-of-the box support for publishing business notifications.

### Table of Contents

- [Setup](#setup)
- [Usage](#usage)
- [Update Notification Configuration](#update-notification-configuration)
- [Notification Destination](#notification-destination)
- [Notification Types Path](#notification-types-path)
- [Notification Type Prefix](#notification-type-prefix)
- [Add Notification Types](#add-notification-types)
- [Update handlers to publish notification](#update-handlers-to-publish-notification)
- [Simple Notificaiton with title](#simple-notificaiton-with-title)
- [Simple Notificaiton with title & description](#simple-notificaiton-with-title)
- [Custom Notifications with notification types](#simple-notificaiton-with-title)
- [Sample Application with notifications](#sample-application-with-notifications)
- [In Local Environment](#in-local-environment)
- [In Production Environment](#in-production-environment)
- [Contributing](#contributing)
- [Code of Conduct](#code-of-conduct)
- [Licensing](#licensing)

## Setup

To enable notifications, simply add this self-configuring plugin package to your project:

```sh
cds add notifications
```

<img width="1300" alt="cds add notifications" style="border-radius:0.5rem" src="_assets/cdsAddNotifications.gif">

## Usage

In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base, to publish notifications.

### Update Notification Configuration

`cds add notifications` will add default configurations for notifications in the `package.json` file.

<img width="1300" alt="Default Notification config" style="border-radius:0.5rem" src="_assets/packageJsonConfig.gif">

#### **Notification Destination**

As a pre-requisite to publish the notification, you need to have a [destination](https://help.sap.com/docs/build-work-zone-standard-edition/sap-build-work-zone-standard-edition/enabling-notifications-for-custom-apps-on-sap-btp-cloud-foundry#configure-the-destination-to-the-notifications-service) configured to publish the notification. In the `package.json` by default destination name `SAP_Notification` is added, you can modify the destination name that you are configuring.

#### **Notification Types Path**

When you run `cds add notifications`, it will add `notificationstype.json` file with template for a notification type in the project root folder. You can add the notification types in the `notificationtype.json` file for sending the custom notification types.

#### **Notification Type Prefix**

To make notification types unique to the application, prefix is added to the type key. By default, `application name` is added as the prefix. You can update the `prefix` if required.

### Add Notification Types

If you want to send custom notifications in your application, you can add the notification types in the `notificationtype.json` file.

Sample: If you want to send the notification when the new incident is reported, you can modify the `notificationtypes.json` as below:

```jsonc
[
{
"NotificationTypeKey": "IncidentReported",
"NotificationTypeVersion": "1",
"Templates": [
{
"Language": "en",
"TemplatePublic": "Incident Reported",
"TemplateSensitive": "Incident '{{name}}' Reported",
"TemplateGrouped": "New Incidents",
"TemplateLanguage": "mustache",
"Subtitle": "Incident '{{name}}' reported by '{{customer}}'."
}
]
}
]
```

### Update handlers to publish notification

In the handler files, connect to the notifications plugin by:

```js
const alert = await cds.connect.to('notifications');
```

#### **Simple Notificaiton with title**
You can use the following signature to send the simple notification with title
```js
alert.notify({
recipients: recipients,
priority: priority,
title: title
});
```
#### **Simple Notificaiton with title & description**
You can use the following signature to send the simple notification with title and description
```js
alert.notify({
recipients: recipients,
priority: priority,
title: title,
description: description
});
```
#### **Custom Notifications with notification types**
You can use the following signature to send the custom notification with pre-defined notification types.
```js
alert.notify({
NotificationTypeKey: 'IncidentCreated',
NotificationTypeVersion: '1',
Priority: 'NEUTRAL',
Properties: [
{
Key: 'name',
IsSensitive: false,
Language: 'en',
Value: 'Engine overheating',
Type: 'String'
},
{
Key: 'customer',
IsSensitive: false,
Language: 'en',
Value: 'John',
Type: 'String'
}
],
Recipients: recipients
});
```

### Sample Application with notifications

#### **In Local Environment**
In local environment, when you publish notification, it is mocked to publish the nofication to the console.

<img width="1300" alt="Notify to console" style="border-radius:0.5rem;padding:1rem;background:rgb(24 24 24)" src="_assets/notifyToConsole.png">

#### **In Production Environment**

Once application is deployed and integrated with SAP Build Work Zone, you can see the notification under fiori notifications icon!

<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif">

## Contributing

This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/change-tracking/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).


### Code of Conduct

We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](CODE_OF_CONDUCT.md) at all times.


## Licensing

Copyright 2023 SAP SE or an SAP affiliate company and contributors. Please see our [LICENSE](LICENSE) for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available [via the REUSE tool](https://api.reuse.software/info/github.com/cap-js/change-tracking).



Binary file added _assets/cdsAddNotifications.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/incidentsNotificationDemo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/notifyToConsole.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _assets/packageJsonConfig.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions cds-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const cds = require("@sap/cds");
const build = require('@sap/cds-dk/lib/build')
const { validateNotificationTypes, readFile } = require("./lib/utils");
const { createNotificationTypesMap } = require("./lib/notificationTypes");
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");

// register build plugin
build.register('notifications', { impl: '@cap-js/notifications/lib/build', description: 'Notifications build plugin', taskDefaults: { src: cds.env.folders.srv } });

cds.once("served", async () => {
setGlobalLogLevel("error");
const profiles = cds.env.profiles ?? [];
const production = profiles.includes("production");

// read notification types
const notificationTypes = readFile(cds.env.requires?.notifications?.types);

if (validateNotificationTypes(notificationTypes)) {
if (!production) {
const notificationTypesMap = createNotificationTypesMap(notificationTypes, true);
cds.notifications = { local: { types: notificationTypesMap } };
}
}
});
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// FIXME: should not be necessary
process.env.CDS_ENV = 'better-sqlite'
AnmolBinani marked this conversation as resolved.
Show resolved Hide resolved

const config = {
testTimeout: 42222,
testMatch: ['**/*.test.js']
}

module.exports = config
19 changes: 19 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const cds = require('@sap/cds')
const { BuildPlugin } = require('@sap/cds-dk/lib/build')

const { copy, exists, path } = cds.utils

module.exports = class NotificationsBuildPlugin extends BuildPlugin {

static hasTask() {
const notificationTypesFile = cds.env.requires?.notifications?.types;
return notificationTypesFile === undefined ? false : exists(notificationTypesFile);
}

async build() {
if(exists(cds.env.requires.notifications.types)) {
const fileName = path.basename(cds.env.requires.notifications.types);
await copy(cds.env.requires.notifications.types).to(path.join(this.task.dest, fileName));
}
}
}
24 changes: 24 additions & 0 deletions lib/content-deployment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const cds = require("@sap/cds");
const { validateNotificationTypes, readFile } = require("./utils");
const { processNotificationTypes } = require("./notificationTypes");
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");
const { basename } = require('path');

async function deployNotificationTypes() {
setGlobalLogLevel("error");

// read notification types
const filePath = cds.env.requires?.notifications?.types ?? '';
const fileName = basename(filePath);
const notificationTypes = readFile(fileName);

if (validateNotificationTypes(notificationTypes)) {
await processNotificationTypes(notificationTypes);
}
}

deployNotificationTypes();

module.exports = {
deployNotificationTypes
}
Loading
Loading