@neoimpulse/cap-js-notifications 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/cds-plugin.js +19 -20
- package/lib/build.js +14 -14
- package/lib/content-deployment.js +9 -9
- package/lib/notificationTypes.js +248 -229
- package/lib/utils.js +211 -206
- package/package.json +1 -1
- package/srv/notification-types.json +14 -14
- package/srv/notifyToConsole.js +33 -35
- package/srv/notifyToRest.js +38 -38
package/srv/notifyToRest.js
CHANGED
|
@@ -2,49 +2,49 @@ const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
|
|
|
2
2
|
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
|
|
3
3
|
const { getNotificationDestination, readFile, buildNotification } = require("../lib/utils");
|
|
4
4
|
const cds = require("@sap/cds");
|
|
5
|
-
const LOG = cds.log(
|
|
5
|
+
const LOG = cds.log("notifications");
|
|
6
6
|
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
|
|
7
7
|
|
|
8
|
+
module.exports = exports = class NotifyToRest extends cds.Service {
|
|
9
|
+
async init() {
|
|
10
|
+
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
|
|
11
|
+
const eventsToListen = notificationTypes.map((notificationType) => notificationType.NotificationTypeKey);
|
|
12
|
+
LOG._info && LOG.info(`Listening to events: ${eventsToListen.join(", ")}`);
|
|
13
|
+
this.on(eventsToListen, (req) => this.postNotification(req));
|
|
14
|
+
return super.init();
|
|
15
|
+
}
|
|
8
16
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.on(eventsToListen, req => this.postNotification(req))
|
|
15
|
-
return super.init()
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async postNotification(req) {
|
|
19
|
-
const type = req.event
|
|
20
|
-
let message = req.data
|
|
21
|
-
message.type = type
|
|
22
|
-
message = buildNotification(message);
|
|
17
|
+
async postNotification(req) {
|
|
18
|
+
const type = req.event;
|
|
19
|
+
let message = req.data;
|
|
20
|
+
message.type = type;
|
|
21
|
+
message = buildNotification(message);
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
const notificationDestination = await getNotificationDestination();
|
|
24
|
+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
|
|
25
|
+
url: NOTIFICATIONS_API_ENDPOINT
|
|
26
|
+
});
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
28
|
+
try {
|
|
29
|
+
LOG._info &&
|
|
30
|
+
LOG.info(
|
|
31
|
+
`Sending notification of key: ${message.NotificationTypeKey} and version: ${message.NotificationTypeVersion}`
|
|
32
|
+
);
|
|
33
|
+
await executeHttpRequest(notificationDestination, {
|
|
34
|
+
url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`,
|
|
35
|
+
method: "post",
|
|
36
|
+
data: message,
|
|
37
|
+
headers: csrfHeaders
|
|
38
|
+
});
|
|
39
|
+
} catch (err) {
|
|
40
|
+
const message = err.response.data?.error?.message?.value ?? err.response.message;
|
|
41
|
+
const error = new cds.error(message);
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
if (/^4\d\d$/.test(err.response?.status) && err.response?.status != 429) {
|
|
44
|
+
error.unrecoverable = true;
|
|
45
|
+
}
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
-
}
|
|
50
|
+
};
|