@neoimpulse/cap-js-notifications 1.0.0
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/LICENSE +201 -0
- package/README.md +0 -0
- package/cds-plugin.js +25 -0
- package/lib/build.js +19 -0
- package/lib/content-deployment.js +22 -0
- package/lib/notificationTypes.js +307 -0
- package/lib/utils.js +247 -0
- package/package.json +65 -0
- package/srv/notification-types.json +16 -0
- package/srv/notifyToConsole.js +41 -0
- package/srv/notifyToRest.js +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
File without changes
|
package/cds-plugin.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const cds = require("@sap/cds/lib");
|
|
2
|
+
|
|
3
|
+
if (cds.cli.command === "build") {
|
|
4
|
+
// register build plugin
|
|
5
|
+
cds.build?.register?.('notifications', require("./lib/build"));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
else cds.once("served", async () => {
|
|
9
|
+
const { validateNotificationTypes, readFile } = require("./lib/utils");
|
|
10
|
+
const { createNotificationTypesMap } = require("./lib/notificationTypes");
|
|
11
|
+
const { deployNotificationTypes } = require("./lib/content-deployment");
|
|
12
|
+
const production = cds.env.profiles?.includes("production");
|
|
13
|
+
|
|
14
|
+
// read notification types
|
|
15
|
+
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
|
|
16
|
+
if (validateNotificationTypes(notificationTypes)) {
|
|
17
|
+
if (!production) {
|
|
18
|
+
const notificationTypesMap = createNotificationTypesMap(notificationTypes, true);
|
|
19
|
+
cds.notifications = { local: { types: notificationTypesMap } };
|
|
20
|
+
}
|
|
21
|
+
deployNotificationTypes();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
require("@sap-cloud-sdk/util").setGlobalLogLevel("error")
|
|
25
|
+
})
|
package/lib/build.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const cds = require('@sap/cds')
|
|
2
|
+
|
|
3
|
+
const { copy, exists, path } = cds.utils
|
|
4
|
+
|
|
5
|
+
module.exports = class NotificationsBuildPlugin extends cds.build.Plugin {
|
|
6
|
+
static taskDefaults = { src: cds.env.folders.srv }
|
|
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,0 +1,22 @@
|
|
|
1
|
+
const cds = require("@sap/cds");
|
|
2
|
+
const { validateNotificationTypes, readFile } = require("./utils");
|
|
3
|
+
const { processNotificationTypes } = require("./notificationTypes");
|
|
4
|
+
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");
|
|
5
|
+
|
|
6
|
+
async function deployNotificationTypes() {
|
|
7
|
+
setGlobalLogLevel("error");
|
|
8
|
+
|
|
9
|
+
// read notification types
|
|
10
|
+
const filePath = cds.env.requires?.notifications?.types ?? '';
|
|
11
|
+
const notificationTypes = readFile(filePath);
|
|
12
|
+
|
|
13
|
+
if (validateNotificationTypes(notificationTypes)) {
|
|
14
|
+
await processNotificationTypes(notificationTypes);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
deployNotificationTypes();
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
deployNotificationTypes
|
|
22
|
+
}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
|
|
2
|
+
const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
|
|
3
|
+
const { getNotificationDestination, getPrefix, getNotificationTypesKeyWithPrefix } = require("./utils");
|
|
4
|
+
const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc";
|
|
5
|
+
const cds = require("@sap/cds");
|
|
6
|
+
const LOG = cds.log('notifications');
|
|
7
|
+
|
|
8
|
+
const defaultTemplate = {
|
|
9
|
+
NotificationTypeKey: "Default",
|
|
10
|
+
NotificationTypeVersion: "1",
|
|
11
|
+
Templates: [
|
|
12
|
+
{
|
|
13
|
+
Language: "en",
|
|
14
|
+
Description: "Other Notifications",
|
|
15
|
+
TemplatePublic: "{{title}}",
|
|
16
|
+
TemplateSensitive: "{{title}}",
|
|
17
|
+
TemplateGrouped: "Other Notifications",
|
|
18
|
+
TemplateLanguage: "mustache",
|
|
19
|
+
Subtitle: "{{description}}"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function fromOdataArrayFormat(objectInArray) {
|
|
25
|
+
if (objectInArray === undefined || objectInArray === null || Array.isArray(objectInArray)) {
|
|
26
|
+
return (objectInArray === undefined || objectInArray === null) ? [] : objectInArray;
|
|
27
|
+
} else {
|
|
28
|
+
return (objectInArray.results === undefined || objectInArray.results === null) ? [] : objectInArray.results;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createNotificationTypesMap(notificationTypesJSON, isLocal = false) {
|
|
33
|
+
const types = {};
|
|
34
|
+
|
|
35
|
+
if (isLocal) {
|
|
36
|
+
types["Default"] = { "1": defaultTemplate };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// add user provided templates
|
|
40
|
+
notificationTypesJSON.forEach((notificationType) => {
|
|
41
|
+
// set default NotificationTypeVersion if required
|
|
42
|
+
if (notificationType.NotificationTypeVersion === undefined) {
|
|
43
|
+
notificationType.NotificationTypeVersion = "1";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const notificationTypeKeyWithPrefix = getNotificationTypesKeyWithPrefix(notificationType.NotificationTypeKey);
|
|
47
|
+
|
|
48
|
+
// update the notification type key with prefix
|
|
49
|
+
notificationType.NotificationTypeKey = notificationTypeKeyWithPrefix;
|
|
50
|
+
|
|
51
|
+
if (!(notificationTypeKeyWithPrefix in types)) {
|
|
52
|
+
types[notificationTypeKeyWithPrefix] = {};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
types[notificationTypeKeyWithPrefix][notificationType.NotificationTypeVersion] = notificationType;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return types;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function getNotificationTypes() {
|
|
62
|
+
const notificationDestination = await getNotificationDestination();
|
|
63
|
+
const response = await executeHttpRequest(notificationDestination, {
|
|
64
|
+
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes?$format=json&$expand=Templates,Actions,DeliveryChannels`,
|
|
65
|
+
method: "get"
|
|
66
|
+
});
|
|
67
|
+
return response.data.d.results;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function createNotificationType(notificationType) {
|
|
71
|
+
const notificationDestination = await getNotificationDestination();
|
|
72
|
+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
|
|
73
|
+
url: NOTIFICATION_TYPES_API_ENDPOINT
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
LOG._warn && LOG.warn(
|
|
77
|
+
`Notification Type of key ${notificationType.NotificationTypeKey} and version ${notificationType.NotificationTypeVersion} was not found. Creating it...`
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const response = await executeHttpRequest(notificationDestination, {
|
|
81
|
+
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes`,
|
|
82
|
+
method: "post",
|
|
83
|
+
data: notificationType,
|
|
84
|
+
headers: csrfHeaders
|
|
85
|
+
});
|
|
86
|
+
return response.data?.d ?? response;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function updateNotificationType(id, notificationType) {
|
|
90
|
+
const notificationDestination = await getNotificationDestination();
|
|
91
|
+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
|
|
92
|
+
url: NOTIFICATION_TYPES_API_ENDPOINT
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
LOG._info && LOG.info(
|
|
96
|
+
`Detected change in notification type of key ${notificationType.NotificationTypeKey} and version ${notificationType.NotificationTypeVersion}. Updating it...`
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const response = await executeHttpRequest(notificationDestination, {
|
|
100
|
+
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes(guid'${id}')`,
|
|
101
|
+
method: "patch",
|
|
102
|
+
data: notificationType,
|
|
103
|
+
headers: csrfHeaders
|
|
104
|
+
});
|
|
105
|
+
return response.status;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function deleteNotificationType(notificationType) {
|
|
109
|
+
const notificationDestination = await getNotificationDestination();
|
|
110
|
+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
|
|
111
|
+
url: NOTIFICATION_TYPES_API_ENDPOINT
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
LOG._info && LOG.info(
|
|
115
|
+
`Notification Type of key ${notificationType.NotificationTypeKey} and version ${notificationType.NotificationTypeVersion} not present in the types file. Deleting it...`
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const response = await executeHttpRequest(notificationDestination, {
|
|
119
|
+
url: `${NOTIFICATION_TYPES_API_ENDPOINT}/NotificationTypes(guid'${notificationType.NotificationTypeId}')`,
|
|
120
|
+
method: "delete",
|
|
121
|
+
headers: csrfHeaders
|
|
122
|
+
});
|
|
123
|
+
return response.status;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function _createChannelsMap(channels) {
|
|
127
|
+
const channelMap = {};
|
|
128
|
+
|
|
129
|
+
channels.forEach((channel) => {
|
|
130
|
+
channelMap[channel.Type] = channel;
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return channelMap;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function areDeliveryChannelsEqual(oldChannels, newChannels) {
|
|
137
|
+
if (oldChannels.length !== newChannels.length) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const oldChannelsMap = _createChannelsMap(oldChannels);
|
|
142
|
+
const newChannelsMap = _createChannelsMap(newChannels);
|
|
143
|
+
|
|
144
|
+
for (let type of Object.keys(oldChannelsMap)) {
|
|
145
|
+
if (!(type in newChannelsMap)) return false;
|
|
146
|
+
|
|
147
|
+
const oldChannel = oldChannelsMap[type];
|
|
148
|
+
const newChannel = newChannelsMap[type];
|
|
149
|
+
|
|
150
|
+
// TODO: Check if language is not there
|
|
151
|
+
const equal =
|
|
152
|
+
oldChannel.Type == newChannel.Type.toUpperCase() &&
|
|
153
|
+
oldChannel.Enabled == newChannel.Enabled &&
|
|
154
|
+
oldChannel.DefaultPreference == newChannel.DefaultPreference &&
|
|
155
|
+
oldChannel.EditablePreference == newChannel.EditablePreference;
|
|
156
|
+
|
|
157
|
+
if (!equal) return false;
|
|
158
|
+
delete newChannelsMap[type];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return Object.keys(newChannelsMap).length == 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isActionEqual(oldAction, newAction) {
|
|
165
|
+
return (
|
|
166
|
+
oldAction.Language == newAction.Language.toUpperCase() &&
|
|
167
|
+
oldAction.ActionId == newAction.ActionId &&
|
|
168
|
+
oldAction.ActionText == newAction.ActionText &&
|
|
169
|
+
oldAction.GroupActionText == newAction.GroupActionText &&
|
|
170
|
+
oldAction.Nature == newAction.Nature
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function areActionsEqual(oldActions, newActions) {
|
|
175
|
+
if (oldActions.length !== newActions.length) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let matchFound = false;
|
|
180
|
+
for (const oldAction of oldActions) {
|
|
181
|
+
for (const newAction of newActions) {
|
|
182
|
+
if (isActionEqual(oldAction, newAction)) {
|
|
183
|
+
matchFound = true;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!matchFound) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function isTemplateEqual(oldTemplate, newTemplate) {
|
|
196
|
+
return (
|
|
197
|
+
oldTemplate.Language == newTemplate.Language.toUpperCase() &&
|
|
198
|
+
oldTemplate.TemplatePublic == newTemplate.TemplatePublic &&
|
|
199
|
+
oldTemplate.TemplateSensitive == newTemplate.TemplateSensitive &&
|
|
200
|
+
oldTemplate.TemplateGrouped == newTemplate.TemplateGrouped &&
|
|
201
|
+
oldTemplate.Description == newTemplate.Description &&
|
|
202
|
+
oldTemplate.TemplateLanguage == newTemplate.TemplateLanguage.toUpperCase() &&
|
|
203
|
+
oldTemplate.Subtitle == newTemplate.Subtitle &&
|
|
204
|
+
oldTemplate.EmailSubject == newTemplate.EmailSubject &&
|
|
205
|
+
oldTemplate.EmailText == newTemplate.EmailText &&
|
|
206
|
+
oldTemplate.EmailHtml == newTemplate.EmailHtml
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function areTemplatesEqual(oldTemplates, newTemplates) {
|
|
211
|
+
if (oldTemplates.length !== newTemplates.length) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let matchFound = false;
|
|
216
|
+
for (const oldTemplate of oldTemplates) {
|
|
217
|
+
for (const newTemplate of newTemplates) {
|
|
218
|
+
if (isTemplateEqual(oldTemplate, newTemplate)) {
|
|
219
|
+
matchFound = true;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!matchFound) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function isNotificationTypeEqual(oldNotificationType, newNotificationType) {
|
|
232
|
+
if (newNotificationType.IsGroupable === undefined) {
|
|
233
|
+
newNotificationType.IsGroupable = true;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return (
|
|
237
|
+
oldNotificationType.IsGroupable == newNotificationType.IsGroupable &&
|
|
238
|
+
areTemplatesEqual(fromOdataArrayFormat(oldNotificationType.Templates), fromOdataArrayFormat(newNotificationType.Templates)) &&
|
|
239
|
+
areActionsEqual(fromOdataArrayFormat(oldNotificationType.Actions), fromOdataArrayFormat(newNotificationType.Actions)) &&
|
|
240
|
+
areDeliveryChannelsEqual(fromOdataArrayFormat(oldNotificationType.DeliveryChannels), fromOdataArrayFormat(newNotificationType.DeliveryChannels))
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function processNotificationTypes(notificationTypesJSON) {
|
|
245
|
+
const notificationTypes = createNotificationTypesMap(notificationTypesJSON);
|
|
246
|
+
const prefix = getPrefix();
|
|
247
|
+
let defaultTemplateExists = false;
|
|
248
|
+
|
|
249
|
+
// get notficationTypes
|
|
250
|
+
const existingTypes = await getNotificationTypes();
|
|
251
|
+
|
|
252
|
+
// iterate through notification types
|
|
253
|
+
for (const existingType of existingTypes) {
|
|
254
|
+
if (existingType.NotificationTypeKey == "Default") {
|
|
255
|
+
defaultTemplateExists = true;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!existingType.NotificationTypeKey.startsWith(`${prefix}/`)) {
|
|
260
|
+
LOG._info && LOG.info(`Skipping Notification Type of other application: ${existingType.NotificationTypeKey}.`);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// if the type isn't present in the JSON file, delete it
|
|
265
|
+
if (
|
|
266
|
+
notificationTypes[existingType.NotificationTypeKey] === undefined ||
|
|
267
|
+
notificationTypes[existingType.NotificationTypeKey][existingType.NotificationTypeVersion] === undefined
|
|
268
|
+
) {
|
|
269
|
+
await deleteNotificationType(existingType);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const newType = JSON.parse(JSON.stringify(notificationTypes[existingType.NotificationTypeKey][existingType.NotificationTypeVersion]));
|
|
274
|
+
|
|
275
|
+
// if the type is there then verify if everything is same or not
|
|
276
|
+
if (!isNotificationTypeEqual(existingType, newType)) {
|
|
277
|
+
await updateNotificationType(existingType.NotificationTypeId, notificationTypes[existingType.NotificationTypeKey][existingType.NotificationTypeVersion]);
|
|
278
|
+
} else {
|
|
279
|
+
LOG._info && LOG.info(
|
|
280
|
+
`Notification Type of key ${existingType.NotificationTypeKey} and version ${existingType.NotificationTypeVersion} unchanged.`
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
delete notificationTypes[existingType.NotificationTypeKey][existingType.NotificationTypeVersion];
|
|
285
|
+
|
|
286
|
+
if (Object.keys(notificationTypes[existingType.NotificationTypeKey]).length == 0) {
|
|
287
|
+
delete notificationTypes[existingType.NotificationTypeKey];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// create default template if required
|
|
292
|
+
if (!defaultTemplateExists) {
|
|
293
|
+
await createNotificationType(defaultTemplate);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// create notification types that aren't there
|
|
297
|
+
for (const notificationTypeKey in notificationTypes) {
|
|
298
|
+
for (const notificationTypeVersion in notificationTypes[notificationTypeKey]) {
|
|
299
|
+
await createNotificationType(notificationTypes[notificationTypeKey][notificationTypeVersion]);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
module.exports = {
|
|
305
|
+
createNotificationTypesMap,
|
|
306
|
+
processNotificationTypes
|
|
307
|
+
}
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
const { existsSync, readFileSync } = require('fs');
|
|
2
|
+
const { basename } = require('path');
|
|
3
|
+
const cds = require("@sap/cds");
|
|
4
|
+
const LOG = cds.log('notifications');
|
|
5
|
+
const { getDestination } = require("@sap-cloud-sdk/connectivity");
|
|
6
|
+
const PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];
|
|
7
|
+
|
|
8
|
+
const messages = {
|
|
9
|
+
TYPES_FILE_NOT_EXISTS: "Notification Types file path is incorrect.",
|
|
10
|
+
INVALID_NOTIFICATION_TYPES: "Notification Types must contain the following key: 'NotificationTypeKey'.",
|
|
11
|
+
DESTINATION_NOT_FOUND: "Failed to get destination: ",
|
|
12
|
+
MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION: "Recipients and title are mandatory parameters.",
|
|
13
|
+
MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION: "Recipients are mandatory parameters.",
|
|
14
|
+
RECIPIENTS_IS_NOT_ARRAY: "Recipients is not an array or it is empty.",
|
|
15
|
+
TITLE_IS_NOT_STRING: "Title is not a string.",
|
|
16
|
+
DESCRIPTION_IS_NOT_STRING: "Description is not a string.",
|
|
17
|
+
PROPERTIES_IS_NOT_OBJECT: "Properties is not an object.",
|
|
18
|
+
NAVIGATION_IS_NOT_OBJECT: "Navigation is not an object.",
|
|
19
|
+
PAYLOAD_IS_NOT_OBJECT: "Payload is not an object.",
|
|
20
|
+
EMPTY_OBJECT_FOR_NOTIFY: "Empty object is passed a single parameter to notify function.",
|
|
21
|
+
NO_OBJECT_FOR_NOTIFY: "An object must be passed to notify function."
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function validateNotificationTypes(notificationTypes) {
|
|
25
|
+
for(let notificationType of notificationTypes){
|
|
26
|
+
if (!("NotificationTypeKey" in notificationType)) {
|
|
27
|
+
LOG._warn && LOG.warn(messages.INVALID_NOTIFICATION_TYPES);
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function validateDefaultNotifyParameters(recipients, priority, title, description) {
|
|
36
|
+
if (!recipients || !title) {
|
|
37
|
+
LOG._warn && LOG.warn(messages.MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!Array.isArray(recipients) || recipients.length == 0) {
|
|
42
|
+
LOG._warn && LOG.warn(messages.RECIPIENTS_IS_NOT_ARRAY);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof title !== "string") {
|
|
47
|
+
LOG._warn && LOG.warn(messages.TITLE_IS_NOT_STRING);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (priority && !PRIORITIES.includes(priority.toUpperCase())) {
|
|
52
|
+
LOG._warn && LOG.warn(`Invalid priority ${priority}. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH`);
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (description && typeof description !== "string") {
|
|
57
|
+
LOG._warn && LOG.warn(messages.DESCRIPTION_IS_NOT_STRING);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function validateCustomNotifyParameters(type, recipients, properties, navigation, priority, payload) {
|
|
65
|
+
if (!recipients) {
|
|
66
|
+
LOG._warn && LOG.warn(messages.MANDATORY_PARAMETER_NOT_PASSED_FOR_CUSTOM_NOTIFICATION);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!Array.isArray(recipients) || recipients.length == 0) {
|
|
71
|
+
LOG._warn && LOG.warn(messages.RECIPIENTS_IS_NOT_ARRAY);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (priority && !PRIORITIES.includes(priority.toUpperCase())) {
|
|
76
|
+
LOG._warn && LOG.warn(`Invalid priority ${priority}. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH`);
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (properties && !Array.isArray(properties)) {
|
|
81
|
+
LOG._warn && LOG.warn(messages.PROPERTIES_IS_NOT_OBJECT);
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (navigation && typeof navigation !== "object") {
|
|
86
|
+
LOG._warn && LOG.warn(messages.NAVIGATION_IS_NOT_OBJECT);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (payload && typeof payload !== "object") {
|
|
91
|
+
LOG._warn && LOG.warn(messages.PAYLOAD_IS_NOT_OBJECT);
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
function readFile(filePath) {
|
|
100
|
+
if (!existsSync(filePath)) {
|
|
101
|
+
LOG._warn && LOG.warn(messages.TYPES_FILE_NOT_EXISTS);
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return JSON.parse(readFileSync(filePath));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function getNotificationDestination() {
|
|
109
|
+
const destinationName = cds.env.requires.notifications?.destination ?? "SAP_Notifications";
|
|
110
|
+
const notificationDestination = await getDestination({ destinationName, useCache: true });
|
|
111
|
+
if (!notificationDestination) {
|
|
112
|
+
// TODO: What to do if destination isn't found??
|
|
113
|
+
throw new Error(messages.DESTINATION_NOT_FOUND + destinationName);
|
|
114
|
+
}
|
|
115
|
+
return notificationDestination;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let prefix // be filled in below...
|
|
119
|
+
function getPrefix() {
|
|
120
|
+
if (!prefix) {
|
|
121
|
+
prefix = cds.env.requires.notifications?.prefix
|
|
122
|
+
if (prefix === "$app-name") try {
|
|
123
|
+
prefix = require(cds.root + '/package.json').name
|
|
124
|
+
} catch { prefix = null }
|
|
125
|
+
if (!prefix) prefix = basename(cds.root)
|
|
126
|
+
}
|
|
127
|
+
return prefix
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function getNotificationTypesKeyWithPrefix(notificationTypeKey) {
|
|
131
|
+
const prefix = getPrefix();
|
|
132
|
+
return `${prefix}/${notificationTypeKey}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function buildDefaultNotification(
|
|
136
|
+
recipients,
|
|
137
|
+
priority = "NEUTRAL",
|
|
138
|
+
title,
|
|
139
|
+
description = ""
|
|
140
|
+
) {
|
|
141
|
+
const properties = [
|
|
142
|
+
{
|
|
143
|
+
Key: "title",
|
|
144
|
+
Language: "en",
|
|
145
|
+
Value: title,
|
|
146
|
+
Type: "String",
|
|
147
|
+
IsSensitive: false,
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
Key: "description",
|
|
151
|
+
Language: "en",
|
|
152
|
+
Value: description,
|
|
153
|
+
Type: "String",
|
|
154
|
+
IsSensitive: false,
|
|
155
|
+
},
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
NotificationTypeKey: "Default",
|
|
160
|
+
NotificationTypeVersion: "1",
|
|
161
|
+
Priority: priority,
|
|
162
|
+
Properties: properties,
|
|
163
|
+
Recipients: recipients.map((recipient) => ({ RecipientId: recipient }))
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function buildCustomNotification(_) {
|
|
168
|
+
let notification = {
|
|
169
|
+
|
|
170
|
+
// Properties with simple API variants
|
|
171
|
+
NotificationTypeKey: getNotificationTypesKeyWithPrefix(_.NotificationTypeKey || _.type),
|
|
172
|
+
Recipients: _.Recipients || _.recipients?.map(id => ({ RecipientId: id })),
|
|
173
|
+
Priority: _.Priority || _.priority || "NEUTRAL",
|
|
174
|
+
Properties: _.Properties || Object.entries(_.data).map(([k,v]) => ({
|
|
175
|
+
Key:k, Value:v, Language: "en", Type: typeof v, // IsSensitive: false
|
|
176
|
+
})),
|
|
177
|
+
|
|
178
|
+
// Low-level API properties
|
|
179
|
+
OriginId: _.OriginId,
|
|
180
|
+
NotificationTypeId: _.NotificationTypeId,
|
|
181
|
+
NotificationTypeVersion: _.NotificationTypeVersion || "1",
|
|
182
|
+
NavigationTargetAction: _.NavigationTargetAction,
|
|
183
|
+
NavigationTargetObject: _.NavigationTargetObject,
|
|
184
|
+
ProviderId: _.ProviderId,
|
|
185
|
+
ActorId: _.ActorId,
|
|
186
|
+
ActorDisplayText: _.ActorDisplayText,
|
|
187
|
+
ActorImageURL: _.ActorImageURL,
|
|
188
|
+
TargetParameters: _.TargetParameters,
|
|
189
|
+
NotificationTypeTimestamp: _.NotificationTypeTimestamp,
|
|
190
|
+
}
|
|
191
|
+
return notification
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function buildNotification(notificationData) {
|
|
195
|
+
let notification;
|
|
196
|
+
|
|
197
|
+
if (Object.keys(notificationData).length === 0) {
|
|
198
|
+
LOG._warn && LOG.warn(messages.EMPTY_OBJECT_FOR_NOTIFY);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (notificationData.type) {
|
|
203
|
+
if (!validateCustomNotifyParameters(
|
|
204
|
+
notificationData.type,
|
|
205
|
+
notificationData.recipients,
|
|
206
|
+
notificationData.properties,
|
|
207
|
+
notificationData.navigation,
|
|
208
|
+
notificationData.priority,
|
|
209
|
+
notificationData.payload)
|
|
210
|
+
) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
notification = buildCustomNotification(notificationData);
|
|
215
|
+
} else if (notificationData.NotificationTypeKey) {
|
|
216
|
+
notificationData.NotificationTypeKey = getNotificationTypesKeyWithPrefix(notificationData.NotificationTypeKey);
|
|
217
|
+
notification = notificationData;
|
|
218
|
+
} else {
|
|
219
|
+
if (!validateDefaultNotifyParameters(
|
|
220
|
+
notificationData.recipients,
|
|
221
|
+
notificationData.priority,
|
|
222
|
+
notificationData.title,
|
|
223
|
+
notificationData.description)
|
|
224
|
+
) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
notification = buildDefaultNotification(
|
|
229
|
+
notificationData.recipients,
|
|
230
|
+
notificationData.priority,
|
|
231
|
+
notificationData.title,
|
|
232
|
+
notificationData.description
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return JSON.parse(JSON.stringify(notification));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
module.exports = {
|
|
240
|
+
messages,
|
|
241
|
+
validateNotificationTypes,
|
|
242
|
+
readFile,
|
|
243
|
+
getNotificationDestination,
|
|
244
|
+
getPrefix,
|
|
245
|
+
getNotificationTypesKeyWithPrefix,
|
|
246
|
+
buildNotification
|
|
247
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neoimpulse/cap-js-notifications",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A CDS plugin to provide an easy action for the notification service",
|
|
5
|
+
"main": "cds-plugin.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"pretty": "npx prettier --write ."
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/neoimpulseGmbH/cap-js-notifications"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"cds",
|
|
15
|
+
"cap",
|
|
16
|
+
"trim",
|
|
17
|
+
"plugin"
|
|
18
|
+
],
|
|
19
|
+
"author": "Neoimpulse GmbH",
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"lib",
|
|
26
|
+
"srv",
|
|
27
|
+
"cds-plugin.js",
|
|
28
|
+
"package.json",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@sap/cds": "^7",
|
|
34
|
+
"express": "^4.17.1"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"prettier": "^3.3.3",
|
|
38
|
+
"semantic-release": "^24.2.1"
|
|
39
|
+
},
|
|
40
|
+
"cds": {
|
|
41
|
+
"requires": {
|
|
42
|
+
"destinations": true,
|
|
43
|
+
"notifications": {
|
|
44
|
+
"[development]": {
|
|
45
|
+
"kind": "notify-to-console"
|
|
46
|
+
},
|
|
47
|
+
"[production]": {
|
|
48
|
+
"destination": "SAP_Notifications",
|
|
49
|
+
"kind": "notify-to-rest"
|
|
50
|
+
},
|
|
51
|
+
"prefix": "$app-name",
|
|
52
|
+
"types": "srv/notification-types.json",
|
|
53
|
+
"outbox": true
|
|
54
|
+
},
|
|
55
|
+
"kinds": {
|
|
56
|
+
"notify-to-console": {
|
|
57
|
+
"impl": "@neoimpulse/cap-js-notifications/srv/notifyToConsole"
|
|
58
|
+
},
|
|
59
|
+
"notify-to-rest": {
|
|
60
|
+
"impl": "@neoimpulse/cap-js-notifications/srv/notifyToRest"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"NotificationTypeKey": "ExampleMessage",
|
|
4
|
+
"NotificationTypeVersion": "1",
|
|
5
|
+
"Templates": [
|
|
6
|
+
{
|
|
7
|
+
"Language": "en",
|
|
8
|
+
"TemplateSensitive": "Example {{Example_ID}} completed.",
|
|
9
|
+
"TemplatePublic": "Rejected Example detected.",
|
|
10
|
+
"TemplateGrouped": "Example for which you are responsible have been rejected.",
|
|
11
|
+
"TemplateLanguage": "Mustache",
|
|
12
|
+
"Subtitle": "The user {{Username}} has completed the unloading process for Example {{Example_ID}}. Please review and proceed with posting."
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const cds = require("@sap/cds");
|
|
2
|
+
const LOG = cds.log('notifications');
|
|
3
|
+
const { readFile } = require("../lib/utils");
|
|
4
|
+
|
|
5
|
+
module.exports = class NotifyToConsole extends cds.Service {
|
|
6
|
+
async init() {
|
|
7
|
+
|
|
8
|
+
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
|
|
9
|
+
const eventsToListen = notificationTypes.map(notificationType => notificationType.NotificationTypeKey);
|
|
10
|
+
LOG._info && LOG.info(`Listening to events: ${eventsToListen.join(", ")}`);
|
|
11
|
+
|
|
12
|
+
this.on(eventsToListen, req => {
|
|
13
|
+
LOG._debug && LOG.debug('Handling notification event:', req.event)
|
|
14
|
+
const notification = req.data; if (!notification) return
|
|
15
|
+
console.log (
|
|
16
|
+
'\n---------------------------------------------------------------\n' +
|
|
17
|
+
'Notification:', req.event,
|
|
18
|
+
notification,
|
|
19
|
+
'\n---------------------------------------------------------------\n',
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
const { NotificationTypeKey, NotificationTypeVersion } = notification
|
|
23
|
+
const types = cds.notifications.local.types // REVISIT: what is this?
|
|
24
|
+
|
|
25
|
+
if (!(NotificationTypeKey in types)) {
|
|
26
|
+
LOG._warn && LOG.warn(
|
|
27
|
+
`Notification Type ${NotificationTypeKey} is not in the notification types file`
|
|
28
|
+
);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!(NotificationTypeVersion in types[NotificationTypeKey])) {
|
|
33
|
+
LOG._warn && LOG.warn(
|
|
34
|
+
`Notification Type Version ${NotificationTypeVersion} for type ${NotificationTypeKey} is not in the notification types file`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return super.init()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
|
|
2
|
+
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
|
|
3
|
+
const { getNotificationDestination, readFile } = require("../lib/utils");
|
|
4
|
+
const cds = require("@sap/cds");
|
|
5
|
+
const LOG = cds.log('notifications');
|
|
6
|
+
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
module.exports = exports = class NotifyToRest extends cds.Service {
|
|
10
|
+
async init() {
|
|
11
|
+
const notificationTypes = readFile(cds.env.requires?.notifications?.types);
|
|
12
|
+
const eventsToListen = notificationTypes.map(notificationType => notificationType.NotificationTypeKey);
|
|
13
|
+
LOG._info && LOG.info(`Listening to events: ${eventsToListen.join(", ")}`);
|
|
14
|
+
this.on(eventsToListen, req => this.postNotification(req.data))
|
|
15
|
+
return super.init()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async postNotification(notificationData) {
|
|
19
|
+
const notificationDestination = await getNotificationDestination();
|
|
20
|
+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
|
|
21
|
+
url: NOTIFICATIONS_API_ENDPOINT,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
LOG._info && LOG.info(
|
|
26
|
+
`Sending notification of key: ${notificationData.NotificationTypeKey} and version: ${notificationData.NotificationTypeVersion}`
|
|
27
|
+
);
|
|
28
|
+
await executeHttpRequest(notificationDestination, {
|
|
29
|
+
url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`,
|
|
30
|
+
method: "post",
|
|
31
|
+
data: notificationData,
|
|
32
|
+
headers: csrfHeaders,
|
|
33
|
+
});
|
|
34
|
+
} catch (err) {
|
|
35
|
+
const message = err.response.data?.error?.message?.value ?? err.response.message;
|
|
36
|
+
const error = new cds.error(message);
|
|
37
|
+
|
|
38
|
+
if (/^4\d\d$/.test(err.response?.status) && err.response?.status != 429) {
|
|
39
|
+
error.unrecoverable = true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|