only update user fields if they changed

This commit is contained in:
Michael Schramm 2022-02-28 22:48:19 +01:00
parent 1e3f47c11c
commit 5b717a2963
2 changed files with 20 additions and 6 deletions

View File

@ -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

View File

@ -16,15 +16,15 @@ export class UserUpdateService {
}
async update(user: UserEntity, input: UserUpdateInput): Promise<UserEntity> {
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]
}
}