Skip to content

Commit fa24dd5

Browse files
authored
Merge pull request #1 from cap-js/MVP
create notification types from the file and publish from plugin
2 parents 81fe99b + 24a1b3d commit fa24dd5

24 files changed

+7671
-17
lines changed

.github/workflows/node.js.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 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
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ "main", "MVP" ]
9+
pull_request:
10+
branches: [ "main", "MVP" ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [18.x]
20+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v3
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
cache: 'npm'
29+
- run: npm i
30+
- run: npm run test-with-coverage

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"printWidth": 200,
5+
"trailingComma" : "none"
6+
}

README.md

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,163 @@
1-
# alert-notification
1+
2+
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.
3+
4+
### Table of Contents
5+
6+
- [Setup](#setup)
7+
- [Usage](#usage)
8+
- [Update Notification Configuration](#update-notification-configuration)
9+
- [Notification Destination](#notification-destination)
10+
- [Notification Types Path](#notification-types-path)
11+
- [Notification Type Prefix](#notification-type-prefix)
12+
- [Add Notification Types](#add-notification-types)
13+
- [Update handlers to publish notification](#update-handlers-to-publish-notification)
14+
- [Simple Notificaiton with title](#simple-notificaiton-with-title)
15+
- [Simple Notificaiton with title & description](#simple-notificaiton-with-title)
16+
- [Custom Notifications with notification types](#simple-notificaiton-with-title)
17+
- [Sample Application with notifications](#sample-application-with-notifications)
18+
- [In Local Environment](#in-local-environment)
19+
- [In Production Environment](#in-production-environment)
20+
- [Contributing](#contributing)
21+
- [Code of Conduct](#code-of-conduct)
22+
- [Licensing](#licensing)
23+
24+
## Setup
25+
26+
To enable notifications, simply add this self-configuring plugin package to your project:
27+
28+
```sh
29+
cds add notifications
30+
```
31+
32+
<img width="1300" alt="cds add notifications" style="border-radius:0.5rem" src="_assets/cdsAddNotifications.gif">
33+
34+
## Usage
35+
36+
In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base, to publish notifications.
37+
38+
### Update Notification Configuration
39+
40+
`cds add notifications` will add default configurations for notifications in the `package.json` file.
41+
42+
<img width="1300" alt="Default Notification config" style="border-radius:0.5rem" src="_assets/packageJsonConfig.gif">
43+
44+
#### **Notification Destination**
45+
46+
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.
47+
48+
#### **Notification Types Path**
49+
50+
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.
51+
52+
#### **Notification Type Prefix**
53+
54+
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.
55+
56+
### Add Notification Types
57+
58+
If you want to send custom notifications in your application, you can add the notification types in the `notificationtype.json` file.
59+
60+
Sample: If you want to send the notification when the new incident is reported, you can modify the `notificationtypes.json` as below:
61+
62+
```jsonc
63+
[
64+
{
65+
"NotificationTypeKey": "IncidentReported",
66+
"NotificationTypeVersion": "1",
67+
"Templates": [
68+
{
69+
"Language": "en",
70+
"TemplatePublic": "Incident Reported",
71+
"TemplateSensitive": "Incident '{{name}}' Reported",
72+
"TemplateGrouped": "New Incidents",
73+
"TemplateLanguage": "mustache",
74+
"Subtitle": "Incident '{{name}}' reported by '{{customer}}'."
75+
}
76+
]
77+
}
78+
]
79+
```
80+
81+
### Update handlers to publish notification
82+
83+
In the handler files, connect to the notifications plugin by:
84+
85+
```js
86+
const alert = await cds.connect.to('notifications');
87+
```
88+
89+
#### **Simple Notificaiton with title**
90+
You can use the following signature to send the simple notification with title
91+
```js
92+
alert.notify({
93+
recipients: recipients,
94+
priority: priority,
95+
title: title
96+
});
97+
```
98+
#### **Simple Notificaiton with title & description**
99+
You can use the following signature to send the simple notification with title and description
100+
```js
101+
alert.notify({
102+
recipients: recipients,
103+
priority: priority,
104+
title: title,
105+
description: description
106+
});
107+
```
108+
#### **Custom Notifications with notification types**
109+
You can use the following signature to send the custom notification with pre-defined notification types.
110+
```js
111+
alert.notify({
112+
NotificationTypeKey: 'IncidentCreated',
113+
NotificationTypeVersion: '1',
114+
Priority: 'NEUTRAL',
115+
Properties: [
116+
{
117+
Key: 'name',
118+
IsSensitive: false,
119+
Language: 'en',
120+
Value: 'Engine overheating',
121+
Type: 'String'
122+
},
123+
{
124+
Key: 'customer',
125+
IsSensitive: false,
126+
Language: 'en',
127+
Value: 'John',
128+
Type: 'String'
129+
}
130+
],
131+
Recipients: recipients
132+
});
133+
```
134+
135+
### Sample Application with notifications
136+
137+
#### **In Local Environment**
138+
In local environment, when you publish notification, it is mocked to publish the nofication to the console.
139+
140+
<img width="1300" alt="Notify to console" style="border-radius:0.5rem;padding:1rem;background:rgb(24 24 24)" src="_assets/notifyToConsole.png">
141+
142+
#### **In Production Environment**
143+
144+
Once application is deployed and integrated with SAP Build Work Zone, you can see the notification under fiori notifications icon!
145+
146+
<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif">
147+
148+
## Contributing
149+
150+
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).
151+
152+
153+
### Code of Conduct
154+
155+
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.
156+
157+
158+
## Licensing
159+
160+
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).
161+
162+
2163

_assets/cdsAddNotifications.gif

117 KB
Loading
2.21 MB
Loading

_assets/notifyToConsole.png

109 KB
Loading

_assets/packageJsonConfig.gif

224 KB
Loading

cds-plugin.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const cds = require("@sap/cds");
2+
const build = require('@sap/cds-dk/lib/build')
3+
const { validateNotificationTypes, readFile } = require("./lib/utils");
4+
const { createNotificationTypesMap } = require("./lib/notificationTypes");
5+
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");
6+
7+
// register build plugin
8+
build.register('notifications', { impl: '@cap-js/notifications/lib/build', description: 'Notifications build plugin', taskDefaults: { src: cds.env.folders.srv } });
9+
10+
cds.once("served", async () => {
11+
setGlobalLogLevel("error");
12+
const profiles = cds.env.profiles ?? [];
13+
const production = profiles.includes("production");
14+
15+
// read notification types
16+
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
17+
18+
if (validateNotificationTypes(notificationTypes)) {
19+
if (!production) {
20+
const notificationTypesMap = createNotificationTypesMap(notificationTypes, true);
21+
cds.notifications = { local: { types: notificationTypesMap } };
22+
}
23+
}
24+
});

jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// FIXME: should not be necessary
2+
process.env.CDS_ENV = 'better-sqlite'
3+
4+
const config = {
5+
testTimeout: 42222,
6+
testMatch: ['**/*.test.js']
7+
}
8+
9+
module.exports = config

lib/build.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const cds = require('@sap/cds')
2+
const { BuildPlugin } = require('@sap/cds-dk/lib/build')
3+
4+
const { copy, exists, path } = cds.utils
5+
6+
module.exports = class NotificationsBuildPlugin extends BuildPlugin {
7+
8+
static hasTask() {
9+
const notificationTypesFile = cds.env.requires?.notifications?.types;
10+
return notificationTypesFile === undefined ? false : exists(notificationTypesFile);
11+
}
12+
13+
async build() {
14+
if(exists(cds.env.requires.notifications.types)) {
15+
const fileName = path.basename(cds.env.requires.notifications.types);
16+
await copy(cds.env.requires.notifications.types).to(path.join(this.task.dest, fileName));
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)