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

fix(profile-settings): notifications settings dropping after profile update #396

Merged
merged 2 commits into from
Feb 21, 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
43 changes: 43 additions & 0 deletions migrations/20220218172429-user-notifications-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* This migration fixes dropped user notifications settings which can happen after profile update
* @see https://github.com/codex-team/hawk.api.nodejs/issues/395
*/
module.exports = {
/**
* Add dropped 'isEnabled' and 'whatToReceive' fields
*/
async up(db) {
await db
.getCollection('users')
.updateMany(
{
'notifications.channels.email.isEnabled': { $exists: false }
},
{
$set: {
'notifications.channels.email.isEnabled': true,
'notifications.whatToReceive': { IssueAssigning: true, WeeklyDigest: true, SystemMessages: true }
}
}
);
},

/**
* Rollback adding of dropped 'isEnabled' and 'whatToReceive' fields
*/
async down(db) {
await db
.getCollection('users')
.updateMany(
{
'notifications.channels.email.isEnabled': { $exists: false}
},
{
$unset: {
'notifications.channels.email.isEnabled': '',
'notifications.whatToReceive': ''
}
}
);
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.0.9",
"version": "1.0.10",
"main": "index.ts",
"license": "UNLICENSED",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us

/**
* Update user profile data
* @param userId - user ID
* @param user – user object
*/
public async updateProfile(user: Partial<UserDBScheme>): Promise<void> {
Expand All @@ -239,6 +238,7 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
email: true,
image: true,
notifications: true,
'notifications.channels.email.endpoint': true
})) {
throw new Error('User object has invalid properties');
}
Expand All @@ -249,6 +249,7 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
user
);
} catch (e) {
console.error(e);
throw new Error('Can\'t update profile');
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/resolvers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,7 @@ export default {
const options: {[key: string]: string | object} = {
name,
email,
notifications: {
channels: {
email: { endpoint: email },
},
},
'notifications.channels.email.endpoint': email,
};

if (image) {
Expand All @@ -205,7 +201,7 @@ export default {

await currentUser!.updateProfile(options);
} catch (err) {
throw new ApolloError('Something went wrong');
throw new ApolloError(err);
}

return true;
Expand Down