diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index d5da9ddbe..31cd009b0 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -161,6 +161,19 @@ export const TEAM_ENVIRONMMENT_NOT_FOUND = export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = 'team_environment/not_team_member' as const; +/** + * User setting not found for a user + * (UserSettingsService) + */ +export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const; + +/** + * User setting invalid (null) properties + * (UserSettingsService) + */ +export const USER_SETTINGS_INVALID_PROPERTIES = 'user_settings/invalid_properties' as const; + + /* |------------------------------------| @@ -215,21 +228,3 @@ export const BUG_TEAM_ENV_GUARD_NO_REQUIRE_ROLES = */ export const BUG_TEAM_ENV_GUARD_NO_ENV_ID = 'bug/team_env/guard_no_env_id' as const; - -/** - * User settings update failed - * (UserSettingsService) - */ -export const USER_SETTINGS_UPDATE_FAILED = 'user_settings/update_failed' as const; - -/** - * User settings not found - * (UserSettingsService) - */ -export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const; - -/** - * User settings invalid properties - * (UserSettingsService) - */ -export const USER_SETTINGS_INVALID_PROPERTIES = 'user_settings/invalid_properties' as const; diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts index f49824689..090803f8b 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts @@ -3,22 +3,22 @@ import { Field, ID, ObjectType } from '@nestjs/graphql'; @ObjectType() export class UserSettings { @Field(() => ID, { - description: 'ID of the User Settings', + description: 'ID of the User Setting', }) id: string; @Field(() => ID, { - description: 'ID of the user this settings belongs to', + description: 'ID of the user this setting belongs to', }) userUid: string; @Field({ - description: 'All properties present in the settings', + description: 'Stringified JSON settings object', }) properties: string; // JSON string of the properties object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client @Field({ - description: 'Last updated date-time of the settings', + description: 'Last updated on', }) updatedOn: Date; } diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts index d0ce3f92f..d3b5de9d5 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -19,14 +19,14 @@ export class UserSettingsResolver { /* Mutations */ @Mutation(() => UserSettings, { - description: 'Creates a new user settings for given user', + description: 'Creates a new user setting for a given user', }) @UseGuards(GqlAuthGuard) async createUserSettings( @GqlUser() user: User, @Args({ name: 'properties', - description: 'JSON string of properties object', + description: 'Stringified JSON settings object', }) properties: string, ) { @@ -40,14 +40,14 @@ export class UserSettingsResolver { } @Mutation(() => UserSettings, { - description: 'Update user settings for given user', + description: 'Update user setting for a given user', }) @UseGuards(GqlAuthGuard) async updateUserSettings( @GqlUser() user: User, @Args({ name: 'properties', - description: 'JSON string of properties object', + description: 'Stringified JSON settings object', }) properties: string, ) { @@ -61,7 +61,7 @@ export class UserSettingsResolver { /* Subscriptions */ @Subscription(() => UserSettings, { - description: 'Listen for user setting updating', + description: 'Listen for user setting updates', resolve: (value) => value, }) @UseGuards(GqlAuthGuard) 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 67dde886e..3cd44a9ed 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 @@ -6,8 +6,10 @@ import { JSON_INVALID, USER_NOT_FOUND, USER_SETTINGS_INVALID_PROPERTIES, - USER_SETTINGS_UPDATE_FAILED, + USER_SETTINGS_NOT_FOUND, } from 'src/errors'; +import { UserSettings } from './user-settings.model'; +import { User } from 'src/user/user.model'; const mockPrisma = mockDeep(); const mockPubSub = mockDeep(); @@ -19,16 +21,16 @@ const userSettingsService = new UserSettingsService( mockPubSub as any, ); -const user = { +const user: User = { uid: 'user-uid', displayName: 'user-display-name', email: 'user-email', photoURL: 'user-photo-url', }; -const userSettings = { +const userSettings: UserSettings = { id: '1', userUid: user.uid, - properties: { key: 'k', value: 'v' }, + properties: JSON.stringify({ key: 'k', value: 'v' }), updatedOn: new Date('2022-12-19T12:43:18.635Z'), }; @@ -39,7 +41,7 @@ beforeEach(() => { describe('UserSettingsService', () => { describe('createUserSettings', () => { - test('should create a user settings with valid user and properties', async () => { + test('should create a user setting with valid user and properties', async () => { mockPrisma.userSettings.create.mockResolvedValue(userSettings); const result = await userSettingsService.createUserSettings( @@ -76,7 +78,7 @@ describe('UserSettingsService', () => { }); }); describe('updateUserSettings', () => { - test('should update a user settings for valid user and properties', async () => { + test('should update a user setting for valid user and properties', async () => { mockPrisma.userSettings.update.mockResolvedValue(userSettings); const result = await userSettingsService.updateUserSettings( @@ -94,9 +96,9 @@ describe('UserSettingsService', () => { null as any, JSON.stringify(userSettings.properties), ); - expect(result).toEqualLeft(USER_SETTINGS_UPDATE_FAILED); + expect(result).toEqualLeft(USER_SETTINGS_NOT_FOUND); }); - test('should reject for invalid properties', async () => { + test('should reject for invalid stringified JSON properties', async () => { const result = await userSettingsService.updateUserSettings( user, 'invalid-properties', @@ -110,7 +112,7 @@ describe('UserSettingsService', () => { ); expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES); }); - test('should publish message on pubnub after update successfully', async () => { + test('should publish message over pubsub on successful update', async () => { mockPrisma.userSettings.update.mockResolvedValue(userSettings); await userSettingsService.updateUserSettings( 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 441452b2e..a91f51653 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -9,7 +9,6 @@ import { USER_NOT_FOUND, USER_SETTINGS_INVALID_PROPERTIES, USER_SETTINGS_NOT_FOUND, - USER_SETTINGS_UPDATE_FAILED, } from 'src/errors'; @Injectable() @@ -19,83 +18,90 @@ export class UserSettingsService { private readonly pubsub: PubSubService, ) {} + /** + * Fetch user setting for a given user + * @param user User object + * @returns an Either of `UserSettings` or error + */ async fetchUserSettings(user: User) { try { - const dbUserSettings = await this.prisma.userSettings.findUnique({ + const userSettings = await this.prisma.userSettings.findUniqueOrThrow({ where: { userUid: user.uid }, - rejectOnNotFound: true, }); - const userSettings: UserSettings = { - id: dbUserSettings.id, - userUid: dbUserSettings.userUid, - properties: JSON.stringify(dbUserSettings.properties), - updatedOn: dbUserSettings.updatedOn, + const settings: UserSettings = { + ...userSettings, + properties: JSON.stringify(userSettings.properties), }; - return E.right(userSettings); + return E.right(settings); } catch (e) { return E.left(USER_SETTINGS_NOT_FOUND); } } + /** + * Create user setting for a given user + * @param user User object + * @param properties User setting properties + * @returns an Either of `UserSettings` or error + */ async createUserSettings(user: User, properties: string) { if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES); - const jsonProperties = stringToJson(properties); - if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); + const settingsObject = stringToJson(properties); + if (E.isLeft(settingsObject)) return E.left(settingsObject.left); try { - const dbUserSettings = await this.prisma.userSettings.create({ + const userSettings = await this.prisma.userSettings.create({ data: { - properties: jsonProperties.right, + properties: settingsObject.right, userUid: user.uid, }, }); - const userSettings: UserSettings = { - id: dbUserSettings.id, - userUid: dbUserSettings.userUid, - properties, - updatedOn: dbUserSettings.updatedOn, + const settings: UserSettings = { + ...userSettings, + properties: JSON.stringify(userSettings.properties), }; - return E.right(userSettings); + return E.right(settings); } catch (e) { return E.left(USER_NOT_FOUND); } } + /** + * Update user setting for a given user + * @param user User object + * @param properties + * @returns + */ async updateUserSettings(user: User, properties: string) { if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES); - const jsonProperties = stringToJson(properties); - if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); + const settingsObject = stringToJson(properties); + if (E.isLeft(settingsObject)) return E.left(settingsObject.left); try { - const dbUpdatedUserSettings = await this.prisma.userSettings.update({ + const updatedUserSettings = await this.prisma.userSettings.update({ where: { userUid: user.uid }, data: { - properties: jsonProperties.right, + properties: settingsObject.right, }, }); - const updatedUserSettings: UserSettings = { - id: dbUpdatedUserSettings.id, - userUid: dbUpdatedUserSettings.userUid, - properties, - updatedOn: dbUpdatedUserSettings.updatedOn, + const settings: UserSettings = { + ...updatedUserSettings, + properties: JSON.stringify(updatedUserSettings.properties), }; // Publish subscription for environment creation - await this.pubsub.publish( - `user_settings/${user.uid}/updated`, - updatedUserSettings, - ); + await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings); - return E.right(updatedUserSettings); + return E.right(settings); } catch (e) { - return E.left(USER_SETTINGS_UPDATE_FAILED); + return E.left(USER_SETTINGS_NOT_FOUND); } } } diff --git a/packages/hoppscotch-backend/src/user-settings/user.resolver.ts b/packages/hoppscotch-backend/src/user-settings/user.resolver.ts index 7967355bf..b5f63de6e 100644 --- a/packages/hoppscotch-backend/src/user-settings/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user.resolver.ts @@ -12,7 +12,7 @@ export class UserSettingsUserResolver { @ResolveField(() => UserSettings, { description: 'Returns user settings', }) - async settings(@Parent() user: User): Promise { + async settings(@Parent() user: User) { const userSettings = await this.userSettingsService.fetchUserSettings(user); if (E.isLeft(userSettings)) throwErr(userSettings.left);