Skip to content

Commit 885cf69

Browse files
committed
PR changes
1 parent 5019198 commit 885cf69

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

packages/backend/src/notifications.test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const keysA = {
2121

2222
describe('Notifications', () => {
2323
describe('get NotificationChannels', () => {
24-
it('Returns 500 as global notification is turned off', async () => {
24+
it('Returns empty array as global notification is turned off', async () => {
2525
const app = express();
2626
app.use(bodyParser.json());
2727
app.use(notifications());
@@ -47,6 +47,8 @@ describe('Notifications', () => {
4747
return {};
4848
},
4949
getIdEnsName: async (ensName: string) => ensName,
50+
getGlobalNotification: async (ensName: string) =>
51+
Promise.resolve({ isEnabled: false }),
5052
getUsersNotificationChannels: async (ensName: string) =>
5153
Promise.resolve([]),
5254
};
@@ -62,7 +64,10 @@ describe('Notifications', () => {
6264
})
6365
.send();
6466

65-
expect(status).toBe(500);
67+
expect(status).toBe(200);
68+
expect(body).toEqual({
69+
notificationChannels: [],
70+
});
6671
});
6772

6873
it('Returns 200 with empty notification channels as global notification is turned on', async () => {
@@ -196,7 +201,7 @@ describe('Notifications', () => {
196201
expect(status).toBe(400);
197202
});
198203

199-
it('Returns 500 on setup email notifications as globalNotifications is turned off', async () => {
204+
it('Returns 400 on setup email notifications as globalNotifications is turned off', async () => {
200205
const app = express();
201206
app.use(bodyParser.json());
202207
app.use(notifications());
@@ -235,7 +240,7 @@ describe('Notifications', () => {
235240
notificationChannelType: NotificationChannelType.EMAIL,
236241
});
237242

238-
expect(status).toBe(500);
243+
expect(status).toBe(400);
239244
});
240245

241246
it('User can setup email notifications', async () => {

packages/backend/src/notifications.ts

+25-21
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,21 @@ export default () => {
8787

8888
// Throw error if global notification is turned off
8989
if (!globalNotification.isEnabled) {
90-
throw new Error('Global notifications is off');
91-
}
92-
93-
// Adding a user's notification channel to the database
94-
await req.app.locals.db.addUsersNotificationChannel(account, {
95-
type: notificationChannelType,
96-
config: {
97-
recipientValue: recipientValue,
98-
},
99-
});
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+
});
100101

101-
// Sending a success response
102-
res.sendStatus(200);
102+
// Sending a success response
103+
res.sendStatus(200);
104+
}
103105
} catch (e) {
104106
// Passing the error to the next middleware
105107
next(e);
@@ -115,17 +117,19 @@ export default () => {
115117
const globalNotification =
116118
await req.app.locals.db.getGlobalNotification(account);
117119

118-
// Throw error if global notification is turned off
120+
// if global notification is turned off
119121
if (!globalNotification.isEnabled) {
120-
throw new Error('Global notifications is off');
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+
);
129+
130+
// Sending the fetched notification channels as a JSON response
131+
res.status(200).json({ notificationChannels });
121132
}
122-
123-
// Getting notification channels for a user from the database
124-
const notificationChannels =
125-
await req.app.locals.db.getUsersNotificationChannels(account);
126-
127-
// Sending the fetched notification channels as a JSON response
128-
res.json({ notificationChannels });
129133
} catch (e) {
130134
// Passing the error to the next middleware
131135
next(e);
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import { NotificationChannelType } from '@dm3-org/dm3-lib-delivery';
22

33
/* eslint-disable max-len */
4+
const EMAIL_REGEX =
5+
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
6+
47
// checks regex pattern for recipient value
58
export const checkRegexPattern = (
69
notificationChannelType: string,
710
recipientValue: string,
8-
): boolean => {
11+
) => {
912
switch (notificationChannelType) {
1013
case NotificationChannelType.EMAIL:
1114
// checks regex pattern of email ID
1215
const patternCheck = String(recipientValue)
1316
.toLowerCase()
14-
.match(
15-
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
16-
);
17-
return patternCheck ? true : false;
17+
.match(EMAIL_REGEX);
18+
return patternCheck;
1819
default:
19-
return false;
20+
return null;
2021
}
2122
};

0 commit comments

Comments
 (0)