diff --git a/migrations/1660716115917-seedNotificationType.ts b/migrations/1660716115917-seedNotificationType.ts index 8145d0b..fcc6978 100644 --- a/migrations/1660716115917-seedNotificationType.ts +++ b/migrations/1660716115917-seedNotificationType.ts @@ -759,6 +759,8 @@ export const GivethNotificationTypes = { title: 'GIVbacks', description: 'Notify me when my GIV from GIVbacks is ready to claim.', showOnSettingPage: true, + categoryGroup: NOTIFICATION_CATEGORY_GROUPS.GIVBACKS, + isGroupParent: true, emailDefaultValue: false, isEmailEditable: false, microService: MICRO_SERVICES.givEconomyNotificationMicroService, @@ -1184,6 +1186,8 @@ export const GivethNotificationTypes = { showOnSettingPage: true, emailDefaultValue: false, isEmailEditable: false, + isGroupParent: true, + categoryGroup: NOTIFICATION_CATEGORY_GROUPS.YOUR_PROJECT_UPDATE, microService: MICRO_SERVICES.givethio, category: NOTIFICATION_CATEGORY.PROJECT_RELATED, icon: '', @@ -1236,6 +1240,8 @@ export const GivethNotificationTypes = { showOnSettingPage: true, emailDefaultValue: false, isEmailEditable: false, + categoryGroup: NOTIFICATION_CATEGORY_GROUPS.PROJECT_LIKES, + isGroupParent: true, microService: MICRO_SERVICES.givethio, category: NOTIFICATION_CATEGORY.PROJECT_RELATED, icon: 'IconHeartFilled', @@ -1267,12 +1273,13 @@ export const GivethNotificationTypes = { showOnSettingPage: true, emailDefaultValue: false, isEmailEditable: false, + categoryGroup: + NOTIFICATION_CATEGORY_GROUPS.SUPPORTED_BY_YOU_PROJECT_HAS_NEW_UPDATE_GROUP, + isGroupParent: true, microService: MICRO_SERVICES.givethio, category: NOTIFICATION_CATEGORY.SUPPORTED_PROJECTS, icon: '', schemaValidator: SCHEMA_VALIDATORS_NAMES.PROJECT_UPDATED_ADDED_WHO_SUPPORTS, - categoryGroup: - NOTIFICATION_CATEGORY_GROUPS.SUPPORTED_BY_YOU_PROJECT_HAS_NEW_UPDATE_GROUP, emailNotifierService: THIRD_PARTY_EMAIL_SERVICES.SEGMENT, emailNotificationId: 'Project update added - Users who supported', pushNotifierService: null, diff --git a/package-lock.json b/package-lock.json index a54325e..3f91a1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "notification-venter", - "version": "1.0.0", + "version": "2.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "notification-venter", - "version": "1.0.0", + "version": "2.0.1", "license": "ISC", "dependencies": { "@adminjs/express": "4.1.0", diff --git a/package.json b/package.json index 667a858..297135f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "notification-venter", - "version": "1.0.0", + "version": "2.0.1", "description": "", "main": "index.js", "scripts": { diff --git a/src/entities/notificationSetting.ts b/src/entities/notificationSetting.ts index 6d67a9d..d471209 100644 --- a/src/entities/notificationSetting.ts +++ b/src/entities/notificationSetting.ts @@ -20,6 +20,8 @@ export const NOTIFICATION_CATEGORY_GROUPS = { 'supportedByYouProjectsStatusChange', SUPPORTED_BY_YOU_PROJECT_HAS_NEW_UPDATE_GROUP: 'supportedByYouProjectsHasNewUpdate', + YOUR_PROJECT_UPDATE: 'yourProjectUpdate', + PROJECT_LIKES: 'projectLikes', PROJECT_STATUS: 'projectStatus', DONATIONS_MADE: 'donationsMade', DONATIONS_RECEIVED: 'donationsReceived', diff --git a/src/repositories/notificationSettingRepository.test.ts b/src/repositories/notificationSettingRepository.test.ts index 97aa7eb..c8ad092 100644 --- a/src/repositories/notificationSettingRepository.test.ts +++ b/src/repositories/notificationSettingRepository.test.ts @@ -152,8 +152,9 @@ function updateUserNotificationSettingTestCases() { .where('setting."userAddressId" = :userAddressId', { userAddressId: userAddress.id, }) - .andWhere('notificationType.isGroupParent = false') + .andWhere('notificationType.isGroupParent = true') .andWhere('notificationType.isEmailEditable = false') + .andWhere('setting.allowEmailNotification = false') .andWhere('notificationType.isWebEditable = true') .getOne(); @@ -162,11 +163,11 @@ function updateUserNotificationSettingTestCases() { const updatedSetting = await updateUserNotificationSetting({ notificationSettingId: userNotificationSetting!.id, userAddressId: userAddress.id, - allowEmailNotification: false, + allowEmailNotification: true, allowDappPushNotification: false, }); assert.isOk(updatedSetting); - assert.isTrue(updatedSetting?.allowEmailNotification); + assert.isFalse(updatedSetting?.allowEmailNotification); assert.isFalse(updatedSetting?.allowDappPushNotification); }); it('update user notification settings, cant change when isWebEditable is false', async () => { diff --git a/src/repositories/notificationSettingRepository.ts b/src/repositories/notificationSettingRepository.ts index f333e49..20ce266 100644 --- a/src/repositories/notificationSettingRepository.ts +++ b/src/repositories/notificationSettingRepository.ts @@ -5,26 +5,37 @@ import { errorMessages } from '../utils/errorMessages'; import { createQueryBuilder } from 'typeorm'; import { logger } from '../utils/logger'; import { StandardError } from '../types/StandardError'; +import { findNotificationTypeParent } from './notificationTypeRepository'; export const createNotificationSettingsForNewUser = async ( user: UserAddress, ) => { const notificationTypes = await NotificationType.find(); // rest of values are set by default - const typeSettings = notificationTypes.map(notificationType => { + const notificationTypeSettings = []; + for (const notificationType of notificationTypes) { + const notificationParent = notificationType?.isGroupParent + ? notificationType + : await findNotificationTypeParent( + notificationType.categoryGroup as string, + ); const payload: Partial = { notificationType: notificationType, userAddress: user, - // allowEmailNotification: - // notificationType.emailDefaultValue !== undefined - // ? notificationType.emailDefaultValue - // : true, + allowEmailNotification: + notificationParent?.emailDefaultValue !== undefined + ? notificationParent?.emailDefaultValue + : true, + allowDappPushNotification: + notificationParent?.webDefaultValue !== undefined + ? notificationParent?.webDefaultValue + : true, }; - return payload; - }); + notificationTypeSettings.push(payload); + } - const userSettings = NotificationSetting.create(typeSettings); + const userSettings = NotificationSetting.create(notificationTypeSettings); return await NotificationSetting.save(userSettings); }; @@ -150,7 +161,6 @@ export const updateUserNotificationSetting = async (params: { allowDappPushNotification: notificationSetting.allowDappPushNotification, }); } - return notificationSetting.save(); }; diff --git a/src/repositories/notificationTypeRepository.ts b/src/repositories/notificationTypeRepository.ts index 7dc0531..b13f7ea 100644 --- a/src/repositories/notificationTypeRepository.ts +++ b/src/repositories/notificationTypeRepository.ts @@ -6,6 +6,15 @@ export const getNotificationTypeByEventName = async (eventName: string) => { .getOne(); }; +export const findNotificationTypeParent = async (categoryGroup: string) => { + return NotificationType.createQueryBuilder() + .where('"categoryGroup" = :categoryGroup', { + categoryGroup, + }) + .andWhere('"isGroupParent" = true') + .getOne(); +}; + export const getNotificationTypeByEventNameAndMicroservice = async (params: { eventName: string; microService: string; diff --git a/src/routes/v1/notificationSettingsRouter.test.ts b/src/routes/v1/notificationSettingsRouter.test.ts index b57d799..7b7a55a 100644 --- a/src/routes/v1/notificationSettingsRouter.test.ts +++ b/src/routes/v1/notificationSettingsRouter.test.ts @@ -73,6 +73,7 @@ function updateNotificationsTestCases() { .where('notificationSetting.userAddressId = :id', { id: userAddress.id }) .andWhere('notificationType.isEmailEditable = true') .andWhere('notificationType.isWebEditable = true') + .andWhere('notificationType.isGroupParent = true') .getOne(); const jwtToken = jwt.sign({ publicAddress: walletAddress }, 'xxxx'); const result = await Axios.put( @@ -101,14 +102,14 @@ function updateNotificationsTestCases() { .where('notificationSetting.userAddressId = :id', { id: userAddress.id }) .andWhere( 'notificationType.isGroupParent = false AND notificationType.categoryGroup = :categoryGroup', - { categoryGroup: NOTIFICATION_CATEGORY_GROUPS.GIVPOWER_ALLOCATIONS }, + { categoryGroup: notificationSetting?.notificationType?.categoryGroup }, ) .getMany(); updatedChildSettings.forEach(setting => { assert.isTrue(setting.allowNotifications); - assert.isTrue(setting.allowEmailNotification); - assert.isTrue(setting.allowDappPushNotification); + assert.isFalse(setting.allowEmailNotification); + assert.isFalse(setting.allowDappPushNotification); }); }); it('should update notification setting, when isEmailEditable is false should not change email setting', async () => { @@ -122,14 +123,14 @@ function updateNotificationsTestCases() { ) .where('notificationSetting.userAddressId = :id', { id: userAddress.id }) .andWhere('notificationType.isEmailEditable = false') - .andWhere('notificationSetting.allowDappPushNotification = true') + .andWhere('notificationSetting.allowEmailNotification = false') .getOne(); const jwtToken = jwt.sign({ publicAddress: walletAddress }, 'xxxx'); const result = await Axios.put( `${apiBaseUrl}/v1/notification_settings/${notificationSetting?.id}`, { id: notificationSetting!.id, - allowEmailNotification: false, + allowEmailNotification: true, allowDappPushNotification: false, }, { headers: { Authorization: `Bearer ${jwtToken}` } }, @@ -137,8 +138,7 @@ function updateNotificationsTestCases() { const updatedNotification = result.data; assert.isOk(result); - - assert.isTrue(updatedNotification.allowEmailNotification); + assert.isFalse(updatedNotification.allowEmailNotification); }); it('should update notification setting, when isWebEditable is false should not change dapp push notification setting', async () => { const userAddress = await createNewUserAddressIfNotExists(walletAddress); @@ -182,10 +182,14 @@ function updateMultipleNotificationsTestCases() { 'notificationType', ) .where('notificationSetting.userAddressId = :id', { id: userAddress.id }) + .andWhere('notificationType.isGroupParent = true') .andWhere('notificationType.isEmailEditable = true') .andWhere('notificationType.isWebEditable = true') + // .andWhere('notificationSetting.allowEmailNotification = true') + // .andWhere('notificationSetting.allowDappPushNotification = true') .take(2) .getMany(); + assert.equal(notificationSettings.length, 2); const jwtToken = jwt.sign({ publicAddress: walletAddress2 }, 'xxxx'); const result = await Axios.put( @@ -194,8 +198,8 @@ function updateMultipleNotificationsTestCases() { settings: notificationSettings.map(setting => { return { id: setting!.id, - allowEmailNotification: false, - allowDappPushNotification: false, + allowEmailNotification: !setting.allowEmailNotification, + allowDappPushNotification: !setting.allowDappPushNotification, }; }), }, @@ -204,9 +208,6 @@ function updateMultipleNotificationsTestCases() { const updatedNotifications: any[] = Object.values(result.data); assert.isOk(result); - // didnt update this value so it remains true - assert.isTrue(updatedNotifications[0].allowNotifications); - assert.isTrue(updatedNotifications[1].allowNotifications); const updatedNotification1 = updatedNotifications.find((setting: any) => { return setting.id === notificationSettings[0].id; @@ -217,32 +218,20 @@ function updateMultipleNotificationsTestCases() { }); // notification 1 - assert.isTrue( - updatedNotification1.allowNotifications === - notificationSettings[0]?.allowNotifications, - ); - assert.isFalse(updatedNotification1.allowEmailNotification); assert.isTrue( updatedNotification1.allowEmailNotification !== notificationSettings[0]?.allowEmailNotification, ); - assert.isFalse(updatedNotification1.allowDappPushNotification); assert.isTrue( updatedNotification1.allowDappPushNotification !== notificationSettings[0]?.allowDappPushNotification, ); // notification 2 - assert.isTrue( - updatedNotification2.allowNotifications === - notificationSettings[1]?.allowNotifications, - ); - assert.isFalse(updatedNotification2.allowEmailNotification); assert.isTrue( updatedNotification2.allowEmailNotification !== notificationSettings[1]?.allowEmailNotification, ); - assert.isFalse(updatedNotification2.allowDappPushNotification); assert.isTrue( updatedNotification2.allowDappPushNotification !== notificationSettings[1]?.allowDappPushNotification,