diff --git a/src/user/user.service.spec.ts b/src/user/user.service.spec.ts index 08552d90..803c065a 100644 --- a/src/user/user.service.spec.ts +++ b/src/user/user.service.spec.ts @@ -296,6 +296,19 @@ describe('UserService', () => { ); }); + it('when supplied a firebase user dto with an email that already exists, it should return an error', async () => { + const repoSaveSpy = jest.spyOn(repo, 'save'); + const authServiceUpdateEmailSpy = jest + .spyOn(mockAuthService, 'updateFirebaseUserEmail') + .mockImplementationOnce(async () => { + throw new Error('Email already exists'); + }); + + await expect(service.updateUser(updateUserDto, mockUserEntity.id)).rejects.toThrow( + 'Email already exists', + ); + }); + it('should not fail update on crisp api call errors', async () => { const mocked = jest.mocked(updateCrispProfile); mocked.mockRejectedValue(new Error('Crisp API call failed')); @@ -305,7 +318,6 @@ describe('UserService', () => { expect(mocked).toHaveBeenCalled(); expect(user.name).toBe(updateUserDto.name); expect(user.email).toBe(updateUserDto.email); - mocked.mockReset(); }); diff --git a/src/user/user.service.ts b/src/user/user.service.ts index a47a1b4b..2ea7fa40 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -10,7 +10,7 @@ import { SubscriptionUserService } from 'src/subscription-user/subscription-user import { TherapySessionService } from 'src/therapy-session/therapy-session.service'; import { SIGNUP_TYPE } from 'src/utils/constants'; import { FIREBASE_ERRORS } from 'src/utils/errors'; -import { FIREBASE_EVENTS } from 'src/utils/logs'; +import { FIREBASE_EVENTS, USER_SERVICE_EVENTS } from 'src/utils/logs'; import { createServiceUserProfiles, updateServiceUserProfilesUser, @@ -218,6 +218,11 @@ export class UserService { ...updateUserDto, }; const updatedUser = await this.userRepository.save(newUserData); + this.logger.log({ + event: USER_SERVICE_EVENTS.USER_UPDATED, + userId: user.id, + fields: Object.keys(updateUserDto), + }); const isCrispBaseUpdateRequired = user.signUpLanguage !== updateUserDto.signUpLanguage && user.name !== updateUserDto.name; diff --git a/src/utils/logs.ts b/src/utils/logs.ts index 1a3418dc..4b0ee61e 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -2,3 +2,7 @@ export enum FIREBASE_EVENTS { UPDATE_FIREBASE_USER_EMAIL = 'UPDATE_FIREBASE_USER_EMAIL', UPDATE_FIREBASE_EMAIL_ALREADY_UPDATED = 'UPDATE_FIREBASE_EMAIL_ALREADY_UPDATED', } + +export enum USER_SERVICE_EVENTS { + USER_UPDATED = 'USER_UPDATED', +}