diff --git a/CHANGELOG.md b/CHANGELOG.md index fd0947d..e4eb95e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- only update user fields in update mutation if they changed + ### Security ## [1.0.0] - 2022-02-28 diff --git a/src/service/user/user.update.service.ts b/src/service/user/user.update.service.ts index 01d44b0..f2d2ecc 100644 --- a/src/service/user/user.update.service.ts +++ b/src/service/user/user.update.service.ts @@ -16,15 +16,15 @@ export class UserUpdateService { } async update(user: UserEntity, input: UserUpdateInput): Promise { - if (input.firstName !== undefined) { + if (this.shouldUpdate(input, user, 'firstName')) { user.firstName = input.firstName } - if (input.lastName !== undefined) { + if (this.shouldUpdate(input, user, 'lastName')) { user.lastName = input.lastName } - if (input.email !== undefined) { + if (this.shouldUpdate(input, user, 'email')) { user.email = input.email user.emailVerified = false // TODO request email verification @@ -34,15 +34,15 @@ export class UserUpdateService { } } - if (input.username !== undefined) { + if (this.shouldUpdate(input, user, 'username')) { user.username = input.username } - if (input.roles !== undefined) { + if (this.shouldUpdate(input, user, 'roles')) { user.roles = input.roles as rolesType } - if (input.language !== undefined) { + if (this.shouldUpdate(input, user, 'language')) { user.language = input.language } @@ -54,4 +54,16 @@ export class UserUpdateService { return user } + + private shouldUpdate( + input: UserUpdateInput, + user: UserEntity, + property: keyof UserUpdateInput + ): boolean { + if (input[property] === undefined) { + return false + } + + return input[property] == user[property] + } }