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

Feature/add multiupdate settings #4

Merged
merged 2 commits into from
Sep 28, 2022
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/1660716115917-seedNotificationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ import {
import { MICRO_SERVICES, THIRD_PARTY_EMAIL_SERVICES } from '../src/utils/utils';

export const GivethNotificationTypes = {
EMAIL_NOTIFICATIONS: {
Copy link
Contributor Author

@CarlosQ96 CarlosQ96 Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This are two global notifications settings

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',
Expand Down
51 changes: 51 additions & 0 deletions src/controllers/v1/notificationSettingsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,62 @@ 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('/')
@Security('JWT')
public async updateNotificationSettings(
@Body()
body: {
settings: SettingParams[];
},
@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()
body: {
Expand Down
83 changes: 82 additions & 1 deletion src/routes/v1/notificationSettingsRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -70,3 +78,76 @@ function updateNotificationsTestCases() {
);
});
}

function updateMultipleNotificationsTestCases() {
it('should update notification settings', async () => {
const userAddress = await createNewUserAddressIfNotExists(walletAddress2);
const notificationSettings = await NotificationSetting.createQueryBuilder(
'notificationSetting',
)
.where('notificationSetting.userAddressId = :id', { id: userAddress.id })
.take(2)
.getMany();

const jwtToken = jwt.sign({ publicAddress: walletAddress2 }, 'xxxx');
const result = await Axios.put(
`${apiBaseUrl}/v1/notification_settings`,
{
settings: notificationSettings.map(setting => {
return {
id: setting!.id,
allowEmailNotification: 'false',
allowDappPushNotification: 'false',
};
}),
},
{ headers: { Authorization: `Bearer ${jwtToken}` } },
);

const updatedNotifications: any[] = Object.values(result.data);
assert.isOk(result);
// didnt update this value so it remains 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(
updatedNotification1.allowNotifications ===
notificationSettings[0]?.allowNotifications,
);
assert.isTrue(updatedNotification1.allowEmailNotification === false);
assert.isTrue(
updatedNotification1.allowEmailNotification !==
notificationSettings[0]?.allowEmailNotification,
);
assert.isTrue(updatedNotification1.allowDappPushNotification === false);
assert.isTrue(
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,
);
});
}
21 changes: 21 additions & 0 deletions src/routes/v1/notificationSettingsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,24 @@ notificationSettingsRouter.put(
}
},
);

notificationSettingsRouter.put(
'/notification_settings',
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);
}
},
);