diff --git a/web/sdk/client/views/profile/profile-update-body.test.ts b/web/sdk/client/views/profile/profile-update-body.test.ts new file mode 100644 index 000000000..297d00413 --- /dev/null +++ b/web/sdk/client/views/profile/profile-update-body.test.ts @@ -0,0 +1,35 @@ +import { profileUpdateBody } from './profile-update-body'; + +describe('profileUpdateBody', () => { + const storedUser = { + id: 'user-1', + name: 'jane_doe', + title: 'Jane Doe', + email: 'jane@raystack.org', + avatar: 'data:image/png;base64,old', + metadata: { onboarded: true, team: 'core' } + }; + + it('leaves out metadata when only the avatar changes', () => { + const formValues = { ...storedUser, avatar: 'data:image/png;base64,new' }; + + const body = profileUpdateBody(formValues, storedUser.name); + + expect(body).toEqual({ + title: 'Jane Doe', + email: 'jane@raystack.org', + avatar: 'data:image/png;base64,new', + name: 'jane_doe' + }); + expect(body).not.toHaveProperty('metadata'); + }); + + it('leaves out metadata when the title changes', () => { + const formValues = { ...storedUser, title: 'Jane Smith' }; + + const body = profileUpdateBody(formValues, storedUser.name); + + expect(body.title).toBe('Jane Smith'); + expect(body).not.toHaveProperty('metadata'); + }); +}); diff --git a/web/sdk/client/views/profile/profile-update-body.ts b/web/sdk/client/views/profile/profile-update-body.ts new file mode 100644 index 000000000..974d72e81 --- /dev/null +++ b/web/sdk/client/views/profile/profile-update-body.ts @@ -0,0 +1,17 @@ +export interface ProfileFormValues { + title: string; + email: string; + avatar?: string; +} + +export function profileUpdateBody( + values: ProfileFormValues, + currentName: string | undefined +) { + return { + title: values.title, + email: values.email, + avatar: values.avatar, + name: currentName + }; +} diff --git a/web/sdk/client/views/profile/profile-view.tsx b/web/sdk/client/views/profile/profile-view.tsx index 2aa69b835..4a2f30126 100644 --- a/web/sdk/client/views/profile/profile-view.tsx +++ b/web/sdk/client/views/profile/profile-view.tsx @@ -27,6 +27,7 @@ import { DeleteUserDialog } from '../../components/delete-user-dialog'; import styles from './profile-view.module.css'; import { handleConnectError } from '~/utils/error'; +import { profileUpdateBody } from './profile-update-body'; const profileSchema = yup .object({ @@ -101,7 +102,7 @@ export function ProfileView({ async function onSubmit(data: FormData) { try { if (!user?.id) return; - await updateCurrentUser({ body: data }); + await updateCurrentUser({ body: profileUpdateBody(data, user.name) }); toastManager.add({ title: 'Updated user', type: 'success' }); } catch (err) { handleConnectError(err, {