1
1
const cds = require ( "@sap/cds" ) ;
2
2
const notifier = require ( "./lib/notifications" ) ;
3
3
const {
4
- messages,
5
4
validateNotificationTypes,
6
- readFileContent,
5
+ readFile,
6
+ doesKeyExist
7
7
} = require ( "./lib/utils" ) ;
8
8
9
9
cds . once ( "served" , ( ) => {
@@ -14,23 +14,33 @@ cds.once("served", () => {
14
14
/**
15
15
* TODO: Decide the properties to be added in the alerts section for notificationtype files.
16
16
*/
17
- if ( cds . requires ?. notifications ?. types ) {
18
- let notificationTypes = readFileContent (
19
- cds . requires . notifications . types
20
- ) ;
21
- if ( validateNotificationTypes ( notificationTypes ) ) {
17
+ const profiles = cds . env . profiles ?? [ ] ;
18
+ const production = profiles . includes ( 'production' ) ;
19
+ if ( cds . env . requires ?. notifications ?. types ) {
20
+ // read notification types
21
+ const notificationTypes = readFile ( cds . env . requires . notifications . types ) ;
22
+
23
+ // validate notification types
24
+ validateNotificationTypes ( notificationTypes ) ;
25
+
26
+ // create notification types
27
+ if ( production ) {
22
28
notificationTypes . forEach ( ( oNotificationType ) => {
23
29
notifier . postNotificationType ( oNotificationType ) ;
24
30
} ) ;
25
31
} else {
26
- /**
27
- * TODO: Move this message inside the validation function
28
- * ? Should we throw error message or warning for the specific invalid NotificationType?
29
- * ? e.g., If we have 5 notificationTypes and 1 out of the 5 is invalid type, we should
30
- * ? go ahead and create the valid ones and display INFO/Warning for the invalid type
31
- */
32
- console . log ( messages . INVALID_NOTIFICATION_TYPES ) ;
32
+ const types = { } ;
33
+ notificationTypes . forEach ( ( oNotificationType ) => {
34
+ if ( ! doesKeyExist ( types , oNotificationType . NotificationTypeKey ) ) {
35
+ types [ oNotificationType . NotificationTypeKey ] = { } ;
36
+ }
37
+
38
+ types [ oNotificationType . NotificationTypeKey ] [ oNotificationType . NotificationTypeVersion ] = oNotificationType ;
39
+ } ) ;
40
+
41
+ cds . notifications = { local : { types } } ;
33
42
}
43
+ } else if ( ! production ) {
44
+ cds . notifications = { local : { types : { } } } ;
34
45
}
35
46
} ) ;
36
-
0 commit comments