From f3296f2ef0b3ab979920bcf3964bb51b1ecbcc19 Mon Sep 17 00:00:00 2001 From: Carlos Quintero Date: Mon, 19 Sep 2022 22:57:38 -0500 Subject: [PATCH 1/2] add multiple notifications settings put request --- .../1660716115917-seedNotificationType.ts | 24 +++++++++ .../v1/notificationSettingsController.ts | 53 +++++++++++++++++- .../v1/notificationSettingsRouter.test.ts | 54 ++++++++++++++++++- src/routes/v1/notificationSettingsRouter.ts | 21 ++++++++ 4 files changed, 150 insertions(+), 2 deletions(-) diff --git a/migrations/1660716115917-seedNotificationType.ts b/migrations/1660716115917-seedNotificationType.ts index 587d78f..e6743aa 100644 --- a/migrations/1660716115917-seedNotificationType.ts +++ b/migrations/1660716115917-seedNotificationType.ts @@ -6,6 +6,30 @@ import { import { MICRO_SERVICES, THIRD_PARTY_EMAIL_SERVICES } from '../src/utils/utils'; export const GivethNotificationTypes = { + EMAIL_NOTIFICATIONS: { + name: 'Email notifications', + description: 'Turn on/off all email notifications', + microService: MICRO_SERVICES.givethio, + category: 'general', + schemaValidator: null, + emailNotifierService: null, + emailNotificationId: null, // doesn't sent + pushNotifierService: null, + title: 'Email notifications', + content: 'Turn on/off all email notifications', // Missing copy + }, + DAPP_NOTIFICATIONS: { + name: 'Dapp notifications', + description: 'Turn on/off all Dapp notifications', + microService: MICRO_SERVICES.givethio, + category: 'general', + schemaValidator: null, + emailNotifierService: null, + emailNotificationId: null, // doesn't sent + pushNotifierService: null, + title: 'Dapp notifications', + content: 'Turn on/off all Dapp notifications', // Missing copy + }, // SEGMENT INCOMPLETE_PROFILE: { name: 'Incomplete profile', diff --git a/src/controllers/v1/notificationSettingsController.ts b/src/controllers/v1/notificationSettingsController.ts index e1e0b08..e6e4475 100644 --- a/src/controllers/v1/notificationSettingsController.ts +++ b/src/controllers/v1/notificationSettingsController.ts @@ -9,7 +9,58 @@ import { UserAddress } from '../../entities/userAddress'; @Route('/v1/notification_settings') @Tags('NotificationSettings') export class NotificationSettingsController { - @Put('/') + @Put('/multiple') + @Security('JWT') + public async updateNotificationSettings( + @Body() + body: { + settings: [ + { + id: number; + allowNotifications?: string; + allowEmailNotification?: string; + allowDappPushNotification?: string; + }, + ]; + }, + @Inject() + params: { + user: UserAddress; + }, + ) { + const { user } = params; + const { settings } = body; + + try { + const updatedNotifications = await Promise.all( + settings.map(async setting => { + const { + id, + allowNotifications, + allowEmailNotification, + allowDappPushNotification, + } = setting; + + const updatedNotification = await updateUserNotificationSetting({ + notificationSettingId: id, + userAddressId: user.id, + allowNotifications, + allowEmailNotification, + allowDappPushNotification, + }); + + return updatedNotification; + }), + ); + + return updatedNotifications; + } catch (e) { + logger.error('updateNotificationSetting() error', e); + throw e; + } + } + + @Put('/:id') @Security('JWT') public async updateNotificationSetting( @Body() diff --git a/src/routes/v1/notificationSettingsRouter.test.ts b/src/routes/v1/notificationSettingsRouter.test.ts index 14cc204..0044e52 100644 --- a/src/routes/v1/notificationSettingsRouter.test.ts +++ b/src/routes/v1/notificationSettingsRouter.test.ts @@ -14,9 +14,17 @@ describe( '/notification_settings GET test cases', getNotificationSettingsTestCases, ); -describe('/notification_settings PUT test cases', updateNotificationsTestCases); +describe( + '/notification_settings/:id PUT test cases', + updateNotificationsTestCases, +); +describe( + '/notification_settings PUT test cases', + updateMultipleNotificationsTestCases, +); const walletAddress = generateRandomEthereumAddress().toLowerCase(); +const walletAddress2 = generateRandomEthereumAddress().toLowerCase(); function getNotificationSettingsTestCases() { it('should return success response', async () => { @@ -70,3 +78,47 @@ function updateNotificationsTestCases() { ); }); } + +function updateMultipleNotificationsTestCases() { + it('should update notification settings', async () => { + const userAddress = await createNewUserAddressIfNotExists(walletAddress2); + const notificationSetting = await NotificationSetting.createQueryBuilder( + 'notificationSetting', + ) + .where('notificationSetting.userAddressId = :id', { id: userAddress.id }) + .getOne(); + const jwtToken = jwt.sign({ publicAddress: walletAddress }, 'xxxx'); + const result = await Axios.put( + `${apiBaseUrl}/v1/notification_settings/multiple`, + { + settings: [ + { + id: notificationSetting!.id, + allowEmailNotification: 'false', + allowDappPushNotification: 'false', + }, + ], + }, + { headers: { Authorization: `Bearer ${jwtToken}` } }, + ); + + const updatedNotification = result.data; + assert.isOk(result); + // didnt update this value so it remains true + assert.isTrue(updatedNotification[0].allowNotifications === true); + assert.isTrue( + updatedNotification[0].allowNotifications === + notificationSetting?.allowNotifications, + ); + assert.isTrue(updatedNotification[0].allowEmailNotification === false); + assert.isTrue( + updatedNotification[0].allowEmailNotification !== + notificationSetting?.allowEmailNotification, + ); + assert.isTrue(updatedNotification[0].allowDappPushNotification === false); + assert.isTrue( + updatedNotification[0].allowDappPushNotification !== + notificationSetting?.allowDappPushNotification, + ); + }); +} diff --git a/src/routes/v1/notificationSettingsRouter.ts b/src/routes/v1/notificationSettingsRouter.ts index b7137fa..e88c5ac 100644 --- a/src/routes/v1/notificationSettingsRouter.ts +++ b/src/routes/v1/notificationSettingsRouter.ts @@ -50,3 +50,24 @@ notificationSettingsRouter.put( } }, ); + +notificationSettingsRouter.put( + '/notification_settings/multiple', + validateAuthMicroserviceJwt, + async (req: Request, res: Response, next) => { + const { user } = res.locals; + + try { + const result = + await notificationSettingsController.updateNotificationSettings( + req.body, + { + user, + }, + ); + return sendStandardResponse({ res, result }); + } catch (e) { + next(e); + } + }, +); From 85813b7b4e944b0805dad644519d776efdd422cc Mon Sep 17 00:00:00 2001 From: Carlos Quintero Date: Thu, 22 Sep 2022 00:11:55 -0500 Subject: [PATCH 2/2] fix multiple update of settings tests --- .../v1/notificationSettingsController.ts | 18 ++--- .../v1/notificationSettingsRouter.test.ts | 67 +++++++++++++------ src/routes/v1/notificationSettingsRouter.ts | 2 +- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/controllers/v1/notificationSettingsController.ts b/src/controllers/v1/notificationSettingsController.ts index e6e4475..7dd480a 100644 --- a/src/controllers/v1/notificationSettingsController.ts +++ b/src/controllers/v1/notificationSettingsController.ts @@ -6,22 +6,22 @@ import { } from '../../repositories/notificationSettingRepository'; import { UserAddress } from '../../entities/userAddress'; +interface SettingParams { + id: number; + allowNotifications?: string; + allowEmailNotification?: string; + allowDappPushNotification?: string; +} + @Route('/v1/notification_settings') @Tags('NotificationSettings') export class NotificationSettingsController { - @Put('/multiple') + @Put('/') @Security('JWT') public async updateNotificationSettings( @Body() body: { - settings: [ - { - id: number; - allowNotifications?: string; - allowEmailNotification?: string; - allowDappPushNotification?: string; - }, - ]; + settings: SettingParams[]; }, @Inject() params: { diff --git a/src/routes/v1/notificationSettingsRouter.test.ts b/src/routes/v1/notificationSettingsRouter.test.ts index 0044e52..0c6ed0c 100644 --- a/src/routes/v1/notificationSettingsRouter.test.ts +++ b/src/routes/v1/notificationSettingsRouter.test.ts @@ -82,43 +82,72 @@ function updateNotificationsTestCases() { function updateMultipleNotificationsTestCases() { it('should update notification settings', async () => { const userAddress = await createNewUserAddressIfNotExists(walletAddress2); - const notificationSetting = await NotificationSetting.createQueryBuilder( + const notificationSettings = await NotificationSetting.createQueryBuilder( 'notificationSetting', ) .where('notificationSetting.userAddressId = :id', { id: userAddress.id }) - .getOne(); - const jwtToken = jwt.sign({ publicAddress: walletAddress }, 'xxxx'); + .take(2) + .getMany(); + + const jwtToken = jwt.sign({ publicAddress: walletAddress2 }, 'xxxx'); const result = await Axios.put( - `${apiBaseUrl}/v1/notification_settings/multiple`, + `${apiBaseUrl}/v1/notification_settings`, { - settings: [ - { - id: notificationSetting!.id, + settings: notificationSettings.map(setting => { + return { + id: setting!.id, allowEmailNotification: 'false', allowDappPushNotification: 'false', - }, - ], + }; + }), }, { headers: { Authorization: `Bearer ${jwtToken}` } }, ); - const updatedNotification = result.data; + const updatedNotifications: any[] = Object.values(result.data); assert.isOk(result); // didnt update this value so it remains true - assert.isTrue(updatedNotification[0].allowNotifications === true); + assert.isTrue(updatedNotifications[0].allowNotifications === true); + assert.isTrue(updatedNotifications[1].allowNotifications === true); + + const updatedNotification1 = updatedNotifications.find((setting: any) => { + return setting.id === notificationSettings[0].id; + }); + + const updatedNotification2 = updatedNotifications.find((setting: any) => { + return setting.id === notificationSettings[1].id; + }); + + // notification 1 assert.isTrue( - updatedNotification[0].allowNotifications === - notificationSetting?.allowNotifications, + updatedNotification1.allowNotifications === + notificationSettings[0]?.allowNotifications, ); - assert.isTrue(updatedNotification[0].allowEmailNotification === false); + assert.isTrue(updatedNotification1.allowEmailNotification === false); assert.isTrue( - updatedNotification[0].allowEmailNotification !== - notificationSetting?.allowEmailNotification, + updatedNotification1.allowEmailNotification !== + notificationSettings[0]?.allowEmailNotification, ); - assert.isTrue(updatedNotification[0].allowDappPushNotification === false); + assert.isTrue(updatedNotification1.allowDappPushNotification === false); assert.isTrue( - updatedNotification[0].allowDappPushNotification !== - notificationSetting?.allowDappPushNotification, + updatedNotification1.allowDappPushNotification !== + notificationSettings[0]?.allowDappPushNotification, + ); + + // notification 2 + assert.isTrue( + updatedNotification2.allowNotifications === + notificationSettings[1]?.allowNotifications, + ); + assert.isTrue(updatedNotification2.allowEmailNotification === false); + assert.isTrue( + updatedNotification2.allowEmailNotification !== + notificationSettings[1]?.allowEmailNotification, + ); + assert.isTrue(updatedNotification2.allowDappPushNotification === false); + assert.isTrue( + updatedNotification2.allowDappPushNotification !== + notificationSettings[1]?.allowDappPushNotification, ); }); } diff --git a/src/routes/v1/notificationSettingsRouter.ts b/src/routes/v1/notificationSettingsRouter.ts index e88c5ac..0f662c0 100644 --- a/src/routes/v1/notificationSettingsRouter.ts +++ b/src/routes/v1/notificationSettingsRouter.ts @@ -52,7 +52,7 @@ notificationSettingsRouter.put( ); notificationSettingsRouter.put( - '/notification_settings/multiple', + '/notification_settings', validateAuthMicroserviceJwt, async (req: Request, res: Response, next) => { const { user } = res.locals;