From b33d003ba581385ab92236f5be349b91edb4ffbc Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 5 Jan 2023 15:21:09 +0600 Subject: [PATCH] feat: error message updated --- packages/hoppscotch-backend/src/errors.ts | 6 +- .../user-settings.service.spec.ts | 91 ++++++++----------- .../user-settings/user-settings.service.ts | 13 ++- 3 files changed, 49 insertions(+), 61 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 8e4e2d447..2842d2b2f 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -172,13 +172,13 @@ export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const; * User setting not found for a user * (UserSettingsService) */ -export const USER_SETTINGS_EXIST = 'user_settings/exist' as const; +export const USER_SETTINGS_ALREADY_EXISTS = 'user_settings/settings_already_present' as const; /** - * User setting invalid (null) properties + * User setting invalid (null) settings * (UserSettingsService) */ -export const USER_SETTINGS_INVALID_PROPERTIES = 'user_settings/invalid_properties' as const; +export const USER_SETTINGS_NULL_SETTINGS = 'user_settings/null_settings' as const; /* diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts index 3cd44a9ed..8c4a1b641 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts @@ -5,7 +5,7 @@ import { UserSettingsService } from './user-settings.service'; import { JSON_INVALID, USER_NOT_FOUND, - USER_SETTINGS_INVALID_PROPERTIES, + USER_SETTINGS_NULL_SETTINGS, USER_SETTINGS_NOT_FOUND, } from 'src/errors'; import { UserSettings } from './user-settings.model'; @@ -22,15 +22,15 @@ const userSettingsService = new UserSettingsService( ); const user: User = { - uid: 'user-uid', + uid: 'aabb22ccdd', displayName: 'user-display-name', email: 'user-email', photoURL: 'user-photo-url', }; -const userSettings: UserSettings = { +const settings: UserSettings = { id: '1', userUid: user.uid, - properties: JSON.stringify({ key: 'k', value: 'v' }), + userSettings: JSON.stringify({ key: 'k', value: 'v' }), updatedOn: new Date('2022-12-19T12:43:18.635Z'), }; @@ -42,90 +42,75 @@ beforeEach(() => { describe('UserSettingsService', () => { describe('createUserSettings', () => { test('should create a user setting with valid user and properties', async () => { - mockPrisma.userSettings.create.mockResolvedValue(userSettings); + mockPrisma.userSettings.create.mockResolvedValue({ + ...settings, + settings: JSON.parse(settings.userSettings), + }); const result = await userSettingsService.createUserSettings( user, - JSON.stringify(userSettings.properties), + settings.userSettings, ); - expect(result).toEqualRight({ - ...userSettings, - properties: JSON.stringify(userSettings.properties), - }); - }); - test('should reject for invalid user', async () => { - const result = await userSettingsService.createUserSettings( - null as any, - JSON.stringify(userSettings.properties), - ); - - expect(result).toEqualLeft(USER_NOT_FOUND); + expect(result).toEqualRight(settings); }); test('should reject for invalid properties', async () => { const result = await userSettingsService.createUserSettings( user, - 'invalid-properties', + 'invalid-settings', ); + expect(result).toEqualLeft(JSON_INVALID); }); - test('should reject for null properties', async () => { + test('should reject for null settings', async () => { const result = await userSettingsService.createUserSettings( user, null as any, ); - expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES); + + expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); }); }); describe('updateUserSettings', () => { - test('should update a user setting for valid user and properties', async () => { - mockPrisma.userSettings.update.mockResolvedValue(userSettings); - - const result = await userSettingsService.updateUserSettings( - user, - JSON.stringify(userSettings.properties), - ); - - expect(result).toEqualRight({ - ...userSettings, - properties: JSON.stringify(userSettings.properties), + test('should update a user setting for valid user and settings', async () => { + mockPrisma.userSettings.update.mockResolvedValue({ + ...settings, + settings: JSON.parse(settings.userSettings), }); - }); - test('should reject for invalid user', async () => { - const result = await userSettingsService.updateUserSettings( - null as any, - JSON.stringify(userSettings.properties), - ); - expect(result).toEqualLeft(USER_SETTINGS_NOT_FOUND); - }); - test('should reject for invalid stringified JSON properties', async () => { + const result = await userSettingsService.updateUserSettings( user, - 'invalid-properties', + settings.userSettings, ); + + expect(result).toEqualRight(settings); + }); + test('should reject for invalid stringified JSON settings', async () => { + const result = await userSettingsService.updateUserSettings( + user, + 'invalid-settings', + ); + expect(result).toEqualLeft(JSON_INVALID); }); - test('should reject for null properties', async () => { + test('should reject for null settings', async () => { const result = await userSettingsService.updateUserSettings( user, null as any, ); - expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES); + expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); }); test('should publish message over pubsub on successful update', async () => { - mockPrisma.userSettings.update.mockResolvedValue(userSettings); + mockPrisma.userSettings.update.mockResolvedValue({ + ...settings, + settings: JSON.parse(settings.userSettings), + }); - await userSettingsService.updateUserSettings( - user, - JSON.stringify(userSettings.properties), - ); + await userSettingsService.updateUserSettings(user, settings.userSettings); expect(mockPubSub.publish).toBeCalledWith( `user_settings/${user.uid}/updated`, - { - ...userSettings, - properties: JSON.stringify(userSettings.properties), - }, + settings, ); }); }); diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts index 86ae9dabc..b040b4b38 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -6,8 +6,8 @@ import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; import { UserSettings } from './user-settings.model'; import { - USER_SETTINGS_EXIST, - USER_SETTINGS_INVALID_PROPERTIES, + USER_SETTINGS_ALREADY_EXISTS, + USER_SETTINGS_NULL_SETTINGS, USER_SETTINGS_NOT_FOUND, } from 'src/errors'; @@ -33,6 +33,7 @@ export class UserSettingsService { ...userSettings, userSettings: JSON.stringify(userSettings.settings), }; + delete (settings as any).settings; return E.right(settings); } catch (e) { @@ -47,7 +48,7 @@ export class UserSettingsService { * @returns an Either of `UserSettings` or error */ async createUserSettings(user: User, settingsString: string) { - if (!settingsString) return E.left(USER_SETTINGS_INVALID_PROPERTIES); + if (!settingsString) return E.left(USER_SETTINGS_NULL_SETTINGS); const settingsObject = stringToJson(settingsString); if (E.isLeft(settingsObject)) return E.left(settingsObject.left); @@ -64,10 +65,11 @@ export class UserSettingsService { ...userSettings, userSettings: JSON.stringify(userSettings.settings), }; + delete (settings as any).settings; return E.right(settings); } catch (e) { - return E.left(USER_SETTINGS_EXIST); + return E.left(USER_SETTINGS_ALREADY_EXISTS); } } @@ -78,7 +80,7 @@ export class UserSettingsService { * @returns */ async updateUserSettings(user: User, settingsString: string) { - if (!settingsString) return E.left(USER_SETTINGS_INVALID_PROPERTIES); + if (!settingsString) return E.left(USER_SETTINGS_NULL_SETTINGS); const settingsObject = stringToJson(settingsString); if (E.isLeft(settingsObject)) return E.left(settingsObject.left); @@ -95,6 +97,7 @@ export class UserSettingsService { ...updatedUserSettings, userSettings: JSON.stringify(updatedUserSettings.settings), }; + delete (settings as any).settings; // Publish subscription for environment creation await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings);