Skip to content

Commit 4045d06

Browse files
authored
Merge pull request #32 from cap-js/fine-tuning
Fine-tuning implementation
2 parents ad48792 + ca4c8a7 commit 4045d06

16 files changed

+330
-385
lines changed

README.md

Lines changed: 117 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -7,169 +7,162 @@ The `@cap-js/notifications` package is a [CDS plugin](https://cap.cloud.sap/docs
77
### Table of Contents
88

99
- [Setup](#setup)
10-
- [Usage](#usage)
11-
- [Update Notification Configuration](#update-notification-configuration)
12-
- [Notification Types Path](#notification-types-path)
13-
- [Notification Type Prefix](#notification-type-prefix)
14-
- [Add Notification Types](#add-notification-types)
15-
- [Add code to send notifications](#add-code-to-send-notifications)
16-
- [Simple Notification with title](#simple-notification-with-title)
17-
- [Simple Notification with title and description](#simple-notification-with-title-and-description)
18-
- [Custom Notifications](#custom-notifications)
19-
- [With standard parameters](#with-standard-parameters)
20-
- [Passing the whole notification object](#passing-the-whole-notification-object)
21-
- [Sample Application with notifications](#sample-application-with-notifications)
22-
- [In Local Environment](#in-local-environment)
23-
- [In Production Environment](#in-production-environment)
24-
- [Notification Destination](#notification-destination)
25-
- [Integrate with SAP Build Work Zone](#integrate-with-sap-build-work-zone)
10+
- [Send Notifications](#send-notifications)
11+
- [Use Notification Types](#use-notification-types)
12+
- [API Reference](#api-reference)
13+
- [Test-drive Locally](#test-drive-locally)
14+
- [Run in Production](#run-in-production)
15+
- [Advanced Usage](#advanced-usage)
2616
- [Contributing](#contributing)
27-
- [Code of Conduct](#code-of-conduct)
17+
- [Code of Conduct](#code-of-conduct)
2818
- [Licensing](#licensing)
2919

3020
## Setup
3121

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

3424
```sh
35-
cds add notifications
25+
npm add @cap-js/notifications
3626
```
3727

38-
<img width="1300" alt="cds add notifications" style="border-radius:0.5rem" src="_assets/cdsAddNotifications.gif">
39-
40-
## Usage
41-
4228
In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base, to publish notifications.
4329

44-
### Update Notification Configuration
30+
## Send Notifications
4531

46-
`cds add notifications` will add default configurations for notifications in the `package.json` file.
32+
With that you can use the NotificationService as any other CAP Service like so in you event handlers:
4733

48-
<img width="1300" alt="Default Notification config" style="border-radius:0.5rem" src="_assets/packageJsonConfig.gif">
34+
```js
35+
const alert = await cds.connect.to('notifications');
36+
```
4937

50-
#### Notification Types Path
38+
You can use the following signature to send the simple notification with title and description
5139

52-
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.
40+
```js
41+
alert.notify({
42+
recipients: [ ...supporters() ],
43+
priority: "HIGH",
44+
title: "New high priority incident is assigned to you!",
45+
description: "Incident titled 'Engine overheating' created by 'customer X' with priority high is assigned to you!"
46+
});
47+
```
5348

54-
#### Notification Type Prefix
49+
* **priority** - Priority of the notification, this argument is optional, it defaults to NEUTRAL
50+
* **description** - Subtitle for the notification, this argument is optional
5551

56-
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.
52+
## Use Notification Types
5753

58-
### Add Notification Types
54+
### 1. Add notification types
5955

60-
If you want to send custom notifications in your application, you can add the notification types in the `notificationtype.json` file.
56+
If you want to send custom notifications in your application, you can add the notification types in the `srv/notification-types.json` file.
6157

62-
Sample: If you want to send the notification when the new incident is reported, you can modify the `notificationtypes.json` as below:
58+
Sample: If you want to send the notification when the incident is resolved, you can modify the `srv/notification-types.json` as below:
6359

64-
```jsonc
65-
[
66-
{
67-
"NotificationTypeKey": "IncidentReported",
68-
"NotificationTypeVersion": "1",
69-
"Templates": [
70-
{
71-
"Language": "en",
72-
"TemplatePublic": "Incident Reported",
73-
"TemplateSensitive": "Incident '{{name}}' Reported",
74-
"TemplateGrouped": "New Incidents",
75-
"TemplateLanguage": "mustache",
76-
"Subtitle": "Incident '{{name}}' reported by '{{customer}}'."
77-
}
78-
]
79-
}
80-
]
60+
```json
61+
[
62+
{
63+
"NotificationTypeKey": "IncidentResolved",
64+
"NotificationTypeVersion": "1",
65+
"Templates": [
66+
{
67+
"Language": "en",
68+
"TemplatePublic": "Incident Resolved",
69+
"TemplateSensitive": "Incident '{{title}}' Resolved",
70+
"TemplateGrouped": "Incident Status Update",
71+
"TemplateLanguage": "mustache",
72+
"Subtitle": "Incident from '{{customer}}' resolved by {{user}}."
73+
}
74+
]
75+
}
76+
]
8177
```
8278

83-
### Add code to send notifications
79+
### 2. Use pre-defined types in your code like that:
8480

85-
In the handler files, connect to the notifications plugin by:
86-
8781
```js
88-
const alert = await cds.connect.to('notifications');
82+
await alert.notify ('IncidentResolved', {
83+
recipients: [ customer.id ],
84+
data: {
85+
customer: customer.info,
86+
title: incident.title,
87+
user: cds.context.user.id,
88+
}
89+
})
8990
```
9091

91-
#### Simple Notification with title
92-
You can use the following signature to send the simple notification with title
93-
```js
94-
alert.notify({
95-
96-
priority: "HIGH",
97-
title: "New incident is reported!"
98-
});
99-
```
100-
#### Simple Notification with title and description
101-
You can use the following signature to send the simple notification with title and description
102-
```js
103-
alert.notify({
104-
recipients: ["[email protected]"],
105-
priority: "HIGH",
106-
title: "New high priority incident is assigned to you!",
107-
description: "Incident titled 'Engine overheating' created by 'customer X' with priority high is assigned to you!"
108-
});
109-
```
92+
## API Reference
93+
94+
* **recipients** - List of the recipients, this argument is mandatory
95+
* **type** - Notification type key, this argument is mandatory
96+
* **priority** - Priority of the notification, this argument is optional, it defaults to NEUTRAL
97+
* **data** - A key-value pair that is used to fill a placeholder of the notification type template, this argument is optional
98+
99+
## Test-drive Locally
100+
In local environment, when you publish notification, it is mocked to publish the nofication to the console.
101+
102+
<img width="700" alt="Notify to console" style="border-radius:0.5rem" src="_assets/notifyToConsole.png">
103+
104+
## Run in Production
105+
106+
#### Notification Destination
107+
108+
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_Notifications` is added, you can modify the destination name that you are configuring.
109+
110+
#### Integrate with SAP Build Work Zone
111+
112+
Once application is deployed and [integrated with SAP Build Work Zone](https://github.com/cap-js/calesi/tree/main/samples/notifications), you can see the notification under fiori notifications icon!
113+
114+
<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif">
115+
116+
117+
118+
## Advanced Usage
119+
120+
### Custom Notification Types Path
121+
122+
Notifications plugin configures `srv/notification-types.json` as default notification types file. If you are using different file, you can update the file path in `cds.env.requires.notifications.types`
123+
124+
### Custom Notification Type Prefix
125+
126+
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 `cds.env.requires.notifications.prefix` if required.
127+
128+
### Low-level Notifications API
110129

111-
#### Custom Notifications
112130
You can use these two signature to send the custom notification with pre-defined notification types.
113131

114-
##### With standard parameters
132+
#### With pre-defined parameters
133+
134+
By using this approach you can send notifications with the predefined parameters - recipients, data, priority, type and other parameters listed in the [API documentation](https://help.sap.com/docs/build-work-zone-standard-edition/sap-build-work-zone-standard-edition/developing-cloud-foundry-applications-with-notifications)
115135

116-
By using this approach you can post a notification by providing different parts of the notification object grouped in related units
117136
```js
118137
alert.notify({
119-
120-
type: "IncidentCreated"
138+
recipients: [...supporters()],
139+
type: "IncidentResolved",
121140
priority: 'NEUTRAL',
122-
properties: [
141+
data: {
142+
customer: customer.info,
143+
title: incident.title,
144+
user: cds.context.user.id,
145+
},
146+
OriginId: "Example Origin Id",
147+
NotificationTypeVersion: "1",
148+
ProviderId: "/SAMPLEPROVIDER",
149+
ActorId: "BACKENDACTORID",
150+
ActorDisplayText: "ActorName",
151+
ActorImageURL: "https://some-url",
152+
NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z",
153+
TargetParameters: [
123154
{
124-
Key: 'name',
125-
IsSensitive: false,
126-
Language: 'en',
127-
Value: 'Engine overheating',
128-
Type: 'String'
129-
},
130-
{
131-
Key: 'customer',
132-
IsSensitive: false,
133-
Language: 'en',
134-
Value: 'John',
135-
Type: 'String'
155+
"Key": "string",
156+
"Value": "string"
136157
}
137-
],
138-
navigation: {
139-
NavigationTargetAction: "displayInbox",
140-
NavigationTargetObject: "WorkflowTask",
141-
}
142-
payload: {
143-
Id: "01234567-89ab-cdef-0123-456789abcdef",
144-
OriginId: "Example Origin Id",
145-
NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef",
146-
NotificationTypeVersion: "1",
147-
ProviderId: "/SAMPLEPROVIDER",
148-
ActorId: "BACKENDACTORID",
149-
ActorDisplayText: "ActorName",
150-
ActorImageURL: "https://some-url",
151-
NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z",
152-
TargetParameters: [
153-
{
154-
"Key": "string",
155-
"Value": "string"
156-
}
157158
]
158-
}
159-
});
159+
});
160160
```
161161

162-
Possible parameters:
163-
* **recipients** - List of the recipients, this argument is mandatory
164-
* **type** - Notification type key, this argument is mandatory
165-
* **priority** - Priority of the notification, this argument is optional, it defaults to NEUTRAL
166-
* **properties** - A key-value pair that is used to fill a placeholder of the notification type template, this argument is optional
167-
* **navigation** - All navigation related parameters, this argument is optional
168-
* **payload** - The rest parameters that can be passed, this argument is optional
162+
#### Passing the whole notification object
169163

170-
##### Passing the whole notification object
164+
By using this approach you need to pass the whole notification object as described in the [API documentation](https://help.sap.com/docs/build-work-zone-standard-edition/sap-build-work-zone-standard-edition/developing-cloud-foundry-applications-with-notifications)
171165

172-
By using this approach you need to pass the whole notification object as described in the API documentation
173166
```js
174167
alert.notify({
175168
NotificationTypeKey: 'IncidentCreated',
@@ -187,46 +180,22 @@ alert.notify({
187180
Key: 'customer',
188181
IsSensitive: false,
189182
Language: 'en',
190-
Value: 'John',
183+
Value: 'Dave',
191184
Type: 'String'
192185
}
193186
],
194-
Recipients: ["admin1@test.com","admin2@test.com"]
187+
Recipients: [{ RecipientId: "supportuser1@mycompany.com" },{ RecipientId: "supportuser2@mycompany.com" }]
195188
});
196189
```
197190

198-
### Sample Application with notifications
199-
200-
#### In Local Environment
201-
In local environment, when you publish notification, it is mocked to publish the nofication to the console.
202-
203-
<img width="1300" alt="Notify to console" style="border-radius:0.5rem;padding:1rem;background:rgb(24 24 24)" src="_assets/notifyToConsole.png">
204-
205-
#### In Production Environment
206-
207-
##### Notification Destination
208-
209-
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.
210-
211-
##### Integrate with SAP Build Work Zone
212-
213-
Once application is deployed and [integrated with SAP Build Work Zone](https://github.com/cap-js/calesi/tree/main/samples/notifications), you can see the notification under fiori notifications icon!
214-
215-
<img width="1300" alt="Sample Application Demo" style="border-radius:0.5rem;" src="_assets/incidentsNotificationDemo.gif">
216-
217191
## Contributing
218192

219193
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).
220194

221-
222-
### Code of Conduct
195+
## Code of Conduct
223196

224197
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.
225198

226-
227199
## Licensing
228200

229201
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).
230-
231-
232-

_assets/cdsAddNotifications.gif

-117 KB
Binary file not shown.
-8.31 MB
Loading

_assets/notifyToConsole.png

-11.6 KB
Loading

lib/notificationTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
22
const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
3-
const { getNotificationDestination, doesKeyExist, getPrefix, getNotificationTypesKeyWithPrefix } = require("./utils");
3+
const { getNotificationDestination, getPrefix, getNotificationTypesKeyWithPrefix } = require("./utils");
44
const _ = require("lodash");
55
const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc";
66
const cds = require("@sap/cds");
@@ -49,7 +49,7 @@ function createNotificationTypesMap(notificationTypesJSON, isLocal = false) {
4949
// update the notification type key with prefix
5050
notificationType.NotificationTypeKey = notificationTypeKeyWithPrefix;
5151

52-
if (!doesKeyExist(types, notificationTypeKeyWithPrefix)) {
52+
if (!(notificationTypeKeyWithPrefix in types)) {
5353
types[notificationTypeKeyWithPrefix] = {};
5454
}
5555

lib/notifications.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)