Skip to content

Commit

Permalink
Merge pull request #48 from Giveth/hotfix_46_fix_notification_setting…
Browse files Browse the repository at this point in the history
…s_default

Hotfix 46 fix notification settings default
  • Loading branch information
mohammadranjbarz authored Feb 26, 2023
2 parents abee13e + d02581d commit 85aa594
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 41 deletions.
11 changes: 9 additions & 2 deletions migrations/1660716115917-seedNotificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: '',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notification-venter",
"version": "1.0.0",
"version": "2.0.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/entities/notificationSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 4 additions & 3 deletions src/repositories/notificationSettingRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 () => {
Expand Down
28 changes: 19 additions & 9 deletions src/repositories/notificationSettingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotificationSetting> = {
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);
};
Expand Down Expand Up @@ -150,7 +161,6 @@ export const updateUserNotificationSetting = async (params: {
allowDappPushNotification: notificationSetting.allowDappPushNotification,
});
}

return notificationSetting.save();
};

Expand Down
9 changes: 9 additions & 0 deletions src/repositories/notificationTypeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 13 additions & 24 deletions src/routes/v1/notificationSettingsRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 () => {
Expand All @@ -122,23 +123,22 @@ 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}` } },
);

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);
Expand Down Expand Up @@ -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(
Expand All @@ -194,8 +198,8 @@ function updateMultipleNotificationsTestCases() {
settings: notificationSettings.map(setting => {
return {
id: setting!.id,
allowEmailNotification: false,
allowDappPushNotification: false,
allowEmailNotification: !setting.allowEmailNotification,
allowDappPushNotification: !setting.allowDappPushNotification,
};
}),
},
Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 85aa594

Please sign in to comment.