Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isEmailEditable and isWebEditable to notificationType schema #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions migrations/1660540253547-createNotificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ export class createNotificationType1660540253547 implements MigrationInterface {
isNullable: false,
default: false,
},
{
name: 'webDefaultValue',
type: 'boolean',
isNullable: false,
default: true,
},
{
name: 'emailDefaultValue',
type: 'boolean',
isNullable: false,
default: false,
},
{
name: 'isWebEditable',
type: 'boolean',
isNullable: false,
default: true,
},
{
name: 'isEmailEditable',
type: 'boolean',
isNullable: false,
default: false,
},
{
name: 'categoryGroup',
type: 'text',
Expand Down
22 changes: 10 additions & 12 deletions migrations/1660716115917-seedNotificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const GivethNotificationTypes = {
title: 'Project published',
description: 'When Project has been published',
showOnSettingPage: true,
isEmailEditable: true,
category: NOTIFICATION_CATEGORY.PROJECT_RELATED,
icon: 'IconPublish',
microService: MICRO_SERVICES.givethio,
Expand Down Expand Up @@ -1756,22 +1757,21 @@ export const GivethNotificationTypes = {
PROJECT_STATUS_GROUP: {
name: 'Project status',
title: 'Project status',
description:
'Your project status',
description: 'Your project status',
showOnSettingPage: true,
isEmailEditable: true,
isWebEditable: false,
microService: MICRO_SERVICES.givethio,
schemaValidator: null,
emailNotifierService: null,
emailNotificationId: null,
pushNotifierService: null,
isGroupParent: true,
content:
'Your project status',
content: 'Your project status',
htmlTemplate: [
{
type: 'p',
content:
'Your project status',
content: 'Your project status',
},
],
category: NOTIFICATION_CATEGORY.PROJECT_RELATED,
Expand All @@ -1784,6 +1784,7 @@ export const GivethNotificationTypes = {
description:
'When someone donates to your project, when you donate to a project, donation success and failure.',
showOnSettingPage: true,
isEmailEditable: true,
microService: MICRO_SERVICES.givethio,
schemaValidator: null,
emailNotifierService: null,
Expand Down Expand Up @@ -1853,22 +1854,19 @@ export const GivethNotificationTypes = {
LIKED_BY_YOU_PROJECT_GROUP: {
name: 'Project status - Users Who Liked',
title: 'Project status',
description:
'Your liked project status',
description: 'Your liked project status',
showOnSettingPage: true,
microService: MICRO_SERVICES.givethio,
schemaValidator: null,
emailNotifierService: null,
emailNotificationId: null,
pushNotifierService: null,
isGroupParent: true,
content:
'Your liked project status',
content: 'Your liked project status',
htmlTemplate: [
{
type: 'p',
content:
'Your liked project status',
content: 'Your liked project status',
},
],
category: NOTIFICATION_CATEGORY.PROJECT_RELATED,
Expand Down
51 changes: 27 additions & 24 deletions src/controllers/v1/notificationSettingsController.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Route, Tags, Body, Security, Inject, Get, Query, Put } from 'tsoa';
import { logger } from '../../utils/logger';
import {
findNotificationSettingById,
getUserNotificationSettings,
updateUserNotificationSetting,
} from '../../repositories/notificationSettingRepository';
import { UserAddress } from '../../entities/userAddress';
import { errorMessages } from '../../utils/errorMessages';
import {
sendNotificationValidator,
updateNotificationSettings,
updateOneNotificationSetting,
validateWithJoiSchema,
} from '../../validators/schemaValidators';

interface SettingParams {
id: number;
allowNotifications?: string;
allowEmailNotification?: string;
allowDappPushNotification?: string;
allowEmailNotification: boolean;
allowDappPushNotification: boolean;
}

@Route('/v1/notification_settings')
Expand All @@ -32,19 +39,16 @@ export class NotificationSettingsController {
const { settings } = body;

try {
validateWithJoiSchema(body, updateNotificationSettings);

const updatedNotifications = await Promise.all(
settings.map(async setting => {
const {
id,
allowNotifications,
allowEmailNotification,
allowDappPushNotification,
} = setting;
const { id, allowEmailNotification, allowDappPushNotification } =
setting;

const updatedNotification = await updateUserNotificationSetting({
notificationSettingId: id,
userAddressId: user.id,
allowNotifications,
allowEmailNotification,
allowDappPushNotification,
});
Expand All @@ -66,32 +70,31 @@ export class NotificationSettingsController {
@Body()
body: {
id: number;
allowNotifications?: string;
allowEmailNotification?: string;
allowDappPushNotification?: string;
allowEmailNotification: boolean;
allowDappPushNotification: boolean;
},
@Inject()
params: {
user: UserAddress;
},
) {
const { user } = params;
const {
id,
allowNotifications,
allowEmailNotification,
allowDappPushNotification,
} = body;
const { id, allowEmailNotification, allowDappPushNotification } = body;
try {
const updatedNotification = await updateUserNotificationSetting({
validateWithJoiSchema(body, updateOneNotificationSetting);

const notificationSetting = await findNotificationSettingById(id);

if (!notificationSetting) {
throw new Error(errorMessages.NOTIFICATION_SETTING_NOT_FOUND);
}
const newSettingData = {
notificationSettingId: id,
userAddressId: user.id,
allowNotifications,
allowEmailNotification,
allowDappPushNotification,
});

return updatedNotification;
};
return await updateUserNotificationSetting(newSettingData);
} catch (e) {
logger.error('updateNotificationSetting() error', e);
throw e;
Expand Down
13 changes: 13 additions & 0 deletions src/entities/notificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ export class NotificationType extends BaseEntity {
@Column('boolean', { default: false })
showOnSettingPage?: boolean;

// https://github.com/Giveth/impact-graph/issues/746
@Column('boolean', { nullable: true, default: false })
isEmailEditable: boolean;

@Column('boolean', { nullable: true, default: true })
isWebEditable: boolean;

@Column('boolean', { nullable: true, default: false })
emailDefaultValue: boolean;

@Column('boolean', { nullable: true, default: false })
webDefaultValue: boolean;

@Index()
@Column('text', { nullable: true })
categoryGroup?: string;
Expand Down
65 changes: 54 additions & 11 deletions src/repositories/notificationSettingRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,69 @@ function updateUserNotificationSettingTestCases() {
userAddressId: userAddress.id,
})
.andWhere('notificationType.isGroupParent = false')
.andWhere('notificationType.isEmailEditable = true')
.andWhere('notificationType.isWebEditable = true')
.getOne();

assert.isTrue(userNotificationSetting?.userAddressId === userAddress.id);

const updatedSetting = await updateUserNotificationSetting({
notificationSettingId: userNotificationSetting!.id,
userAddressId: userAddress.id,
allowNotifications: 'false',
allowEmailNotification: 'false',
allowDappPushNotification: 'false',
allowEmailNotification: false,
allowDappPushNotification: false,
});
assert.isOk(updatedSetting);
assert.isFalse(updatedSetting?.allowEmailNotification);
assert.isFalse(updatedSetting?.allowDappPushNotification);
});
it('update user notification settings, cant change when isEmailEditable is false', async () => {
const userAddress = await createNewUserAddressIfNotExists(walletAddress);

const userNotificationSetting =
await NotificationSetting.createQueryBuilder('setting')
.leftJoinAndSelect('setting.notificationType', 'notificationType')
.where('setting."userAddressId" = :userAddressId', {
userAddressId: userAddress.id,
})
.andWhere('notificationType.isGroupParent = false')
.andWhere('notificationType.isEmailEditable = false')
.andWhere('notificationType.isWebEditable = true')
.getOne();

assert.isTrue(userNotificationSetting?.userAddressId === userAddress.id);

const updatedSetting = await updateUserNotificationSetting({
notificationSettingId: userNotificationSetting!.id,
userAddressId: userAddress.id,
allowEmailNotification: false,
allowDappPushNotification: false,
});
assert.isOk(updatedSetting);
assert.isTrue(
userNotificationSetting!.allowNotifications !==
updatedSetting!.allowNotifications,
);
assert.equal(userNotificationSetting!.allowNotifications, true);
assert.equal(updatedSetting.allowNotifications, false);
assert.equal(updatedSetting.allowEmailNotification, false);
assert.equal(updatedSetting.allowDappPushNotification, false);
assert.isTrue(updatedSetting?.allowEmailNotification);
assert.isFalse(updatedSetting?.allowDappPushNotification);
});
it('update user notification settings, cant change when isWebEditable is false', async () => {
const userAddress = await createNewUserAddressIfNotExists(walletAddress);

const userNotificationSetting =
await NotificationSetting.createQueryBuilder('setting')
.leftJoinAndSelect('setting.notificationType', 'notificationType')
.where('setting."userAddressId" = :userAddressId', {
userAddressId: userAddress.id,
})
.andWhere('notificationType.isWebEditable = false')
.getOne();

assert.isTrue(userNotificationSetting?.userAddressId === userAddress.id);

const updatedSetting = await updateUserNotificationSetting({
notificationSettingId: userNotificationSetting!.id,
userAddressId: userAddress.id,
allowEmailNotification: false,
allowDappPushNotification: false,
});
assert.isOk(updatedSetting);
assert.isTrue(updatedSetting?.allowDappPushNotification);
});
}
Loading