1
1
// Importing the necessary modules and functions
2
2
import cors from 'cors' ;
3
- import { NotificationChannelType } from '@dm3-org/dm3-lib-delivery' ;
4
3
import { normalizeEnsName } from '@dm3-org/dm3-lib-profile' ;
5
4
import express from 'express' ;
6
5
import { auth } from './utils' ;
6
+ import { validateNotificationChannel } from './validation/notification/notificationChannelValidation' ;
7
7
8
8
// Exporting a function that returns an Express router
9
9
export default ( ) => {
@@ -15,20 +15,24 @@ export default () => {
15
15
// Adding a route parameter middleware named 'ensName'
16
16
router . param ( 'ensName' , auth ) ;
17
17
18
- // Defining a route to handle POST requests for adding an email notification channel
19
- router . post ( '/email /:ensName' , async ( req , res , next ) => {
18
+ // Defining a route to enable/disable global notifications
19
+ router . post ( '/global /:ensName' , async ( req , res , next ) => {
20
20
try {
21
21
const account = normalizeEnsName ( req . params . ensName ) ;
22
22
23
- // Extracting recipientAddress from the request body
24
- const { recipientAddress } = req . body ;
23
+ // Extracting isEnabled from the request body
24
+ const { isEnabled } = req . body ;
25
25
26
- // Adding a user's notification channel to the database
27
- await req . app . locals . db . addUsersNotificationChannel ( account , {
28
- type : NotificationChannelType . EMAIL ,
29
- config : {
30
- recipientAddress,
31
- } ,
26
+ // return if value is not a boolean
27
+ if ( typeof isEnabled !== 'boolean' ) {
28
+ return res . sendStatus ( 400 ) . json ( {
29
+ error : 'Invalid value' ,
30
+ } ) ;
31
+ }
32
+
33
+ // set global notification to the database
34
+ await req . app . locals . db . setGlobalNotification ( account , {
35
+ isEnabled,
32
36
} ) ;
33
37
34
38
// Sending a success response
@@ -39,17 +43,93 @@ export default () => {
39
43
}
40
44
} ) ;
41
45
46
+ // Defining a route to handle GET requests for fetching global notification
47
+ router . get ( '/global/:ensName' , async ( req , res , next ) => {
48
+ try {
49
+ const account = normalizeEnsName ( req . params . ensName ) ;
50
+
51
+ // fetching global notification setting for a user from the database
52
+ const globalNotification =
53
+ await req . app . locals . db . getGlobalNotification ( account ) ;
54
+
55
+ // Sending the fetched global notification setting as a JSON response
56
+ res . json ( globalNotification ) ;
57
+ } catch ( e ) {
58
+ // Passing the error to the next middleware
59
+ next ( e ) ;
60
+ }
61
+ } ) ;
62
+
63
+ // Defining a route to handle POST requests for adding an notification channel
64
+ router . post ( '/:ensName' , async ( req , res , next ) => {
65
+ try {
66
+ const account = normalizeEnsName ( req . params . ensName ) ;
67
+
68
+ // Extracting recipientValue & notificationChannelType from the request body
69
+ const { recipientValue, notificationChannelType } = req . body ;
70
+
71
+ // Validate req.body data
72
+ const { isValid, errorMessage } = validateNotificationChannel (
73
+ notificationChannelType ,
74
+ recipientValue ,
75
+ ) ;
76
+
77
+ // Return if invalid data found
78
+ if ( ! isValid ) {
79
+ res . sendStatus ( 400 ) . json ( {
80
+ error : errorMessage ,
81
+ } ) ;
82
+ }
83
+
84
+ // Fetch global notification data of user from database
85
+ const globalNotification =
86
+ await req . app . locals . db . getGlobalNotification ( account ) ;
87
+
88
+ // Throw error if global notification is turned off
89
+ if ( ! globalNotification . isEnabled ) {
90
+ res . sendStatus ( 400 ) . json ( {
91
+ error : 'Global notifications is off' ,
92
+ } ) ;
93
+ } else {
94
+ // Adding a user's notification channel to the database
95
+ await req . app . locals . db . addUsersNotificationChannel ( account , {
96
+ type : notificationChannelType ,
97
+ config : {
98
+ recipientValue : recipientValue ,
99
+ } ,
100
+ } ) ;
101
+
102
+ // Sending a success response
103
+ res . sendStatus ( 200 ) ;
104
+ }
105
+ } catch ( e ) {
106
+ // Passing the error to the next middleware
107
+ next ( e ) ;
108
+ }
109
+ } ) ;
110
+
42
111
// Defining a route to handle GET requests for fetching notification channels
43
112
router . get ( '/:ensName' , async ( req , res , next ) => {
44
113
try {
45
114
const account = normalizeEnsName ( req . params . ensName ) ;
46
115
47
- // Getting notification channels for a user from the database
48
- const notificationChannels =
49
- await req . app . locals . db . getUsersNotificationChannels ( account ) ;
116
+ // Fetch global notification data of user from database
117
+ const globalNotification =
118
+ await req . app . locals . db . getGlobalNotification ( account ) ;
119
+
120
+ // if global notification is turned off
121
+ if ( ! globalNotification . isEnabled ) {
122
+ res . status ( 200 ) . json ( { notificationChannels : [ ] } ) ;
123
+ } else {
124
+ // Getting notification channels for a user from the database
125
+ const notificationChannels =
126
+ await req . app . locals . db . getUsersNotificationChannels (
127
+ account ,
128
+ ) ;
50
129
51
- // Sending the fetched notification channels as a JSON response
52
- res . json ( notificationChannels ) ;
130
+ // Sending the fetched notification channels as a JSON response
131
+ res . status ( 200 ) . json ( { notificationChannels } ) ;
132
+ }
53
133
} catch ( e ) {
54
134
// Passing the error to the next middleware
55
135
next ( e ) ;
0 commit comments