From 4affb2bc5bb8e7995c4ed96fe66a410a3ace9be7 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 17:38:46 +0600 Subject: [PATCH 01/25] feat: added user-settings schema and user-settings module --- .../hoppscotch-backend/prisma/schema.prisma | 9 +++++ packages/hoppscotch-backend/src/app.module.ts | 2 + packages/hoppscotch-backend/src/errors.ts | 6 +++ .../src/user-settings/user-settings.model.ts | 24 ++++++++++++ .../src/user-settings/user-settings.module.ts | 11 ++++++ .../user-settings/user-settings.resolver.ts | 39 +++++++++++++++++++ .../user-settings/user-settings.service.ts | 36 +++++++++++++++++ packages/hoppscotch-backend/src/utils.ts | 15 +++++++ 8 files changed, 142 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user-settings/user-settings.model.ts create mode 100644 packages/hoppscotch-backend/src/user-settings/user-settings.module.ts create mode 100644 packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts create mode 100644 packages/hoppscotch-backend/src/user-settings/user-settings.service.ts diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 947d158a8..650affa80 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -83,6 +83,15 @@ model User { displayName String? email String? photoURL String? + settings UserSettings? +} + +model UserSettings { + id String @id @default(uuid()) + userUid String @unique + user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) + properties Json + updatedOn DateTime @updatedAt } enum TeamMemberRole { diff --git a/packages/hoppscotch-backend/src/app.module.ts b/packages/hoppscotch-backend/src/app.module.ts index 7614320b9..92c105f00 100644 --- a/packages/hoppscotch-backend/src/app.module.ts +++ b/packages/hoppscotch-backend/src/app.module.ts @@ -3,6 +3,7 @@ import { GraphQLModule } from '@nestjs/graphql'; import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; import { UserModule } from './user/user.module'; import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin'; +import { UserSettingsModule } from './user-settings/user-settings.module'; @Module({ imports: [ @@ -44,6 +45,7 @@ import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin'; driver: ApolloDriver, }), UserModule, + UserSettingsModule ], providers: [GQLComplexityPlugin], }) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 794a06db5..2e8641e50 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -8,6 +8,12 @@ export const EMAIL_FAILED = 'email/failed' as const; */ export const AUTH_FAIL = 'auth/fail'; +/** + * Invalid JSON + * (Utils) + */ +export const JSON_INVALID = 'json_invalid'; + /** * Tried to delete an user data document from fb firestore but failed. * (FirebaseService) diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts new file mode 100644 index 000000000..f49824689 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts @@ -0,0 +1,24 @@ +import { Field, ID, ObjectType } from '@nestjs/graphql'; + +@ObjectType() +export class UserSettings { + @Field(() => ID, { + description: 'ID of the User Settings', + }) + id: string; + + @Field(() => ID, { + description: 'ID of the user this settings belongs to', + }) + userUid: string; + + @Field({ + description: 'All properties present in the settings', + }) + 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', + }) + updatedOn: Date; +} diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts new file mode 100644 index 000000000..f85f36150 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { PrismaModule } from 'src/prisma/prisma.module'; +import { PubSubModule } from 'src/pubsub/pubsub.module'; +import { UserSettingsResolver } from './user-settings.resolver'; +import { UserSettingsService } from './user-settings.service'; + +@Module({ + imports: [PrismaModule, PubSubModule], + providers: [UserSettingsResolver, UserSettingsService], +}) +export class UserSettingsModule {} diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts new file mode 100644 index 000000000..a686a9b69 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -0,0 +1,39 @@ +import { UseGuards } from '@nestjs/common'; +import { Args, Mutation, Resolver } from '@nestjs/graphql'; +import { GqlUser } from 'src/decorators/gql-user.decorator'; +import { GqlAuthGuard } from 'src/guards/gql-auth.guard'; +import { User } from 'src/user/user.model'; +import * as E from 'fp-ts/Either'; +import { throwErr } from 'src/utils'; +import { UserSettings } from './user-settings.model'; +import { UserSettingsService } from './user-settings.service'; + +@Resolver() +export class UserSettingsResolver { + constructor(private readonly userSettingsService: UserSettingsService) {} + + /* Mutations */ + + @Mutation(() => UserSettings, { + description: 'Creates a new user settings for given user', + }) + @UseGuards(GqlAuthGuard) + async createUserSettings( + @GqlUser() user: User, + @Args({ + name: 'properties', + description: 'JSON string of properties object', + }) + properties: string, + ) { + const userSettings = await this.userSettingsService.createUserSettings( + user, + properties, + ); + + if (E.isLeft(userSettings)) throwErr(userSettings.left); + return userSettings.right; + } + + /* Subscriptions */ +} diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts new file mode 100644 index 000000000..f69eb20e8 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -0,0 +1,36 @@ +import { Injectable } from '@nestjs/common'; +import { PrismaService } from 'src/prisma/prisma.service'; +import { PubSubService } from 'src/pubsub/pubsub.service'; +import { User } from 'src/user/user.model'; +import * as E from 'fp-ts/Either'; +import { stringToJson } from 'src/utils'; +import { UserSettings } from './user-settings.model'; + +@Injectable() +export class UserSettingsService { + constructor( + private readonly prisma: PrismaService, + private readonly pubsub: PubSubService, + ) {} + + async createUserSettings(user: User, properties: string) { + const jsonProperties = stringToJson(properties); + if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); + + const dbUserSettings = await this.prisma.userSettings.create({ + data: { + properties: jsonProperties.right, + userUid: user.uid, + }, + }); + + const userSettings: UserSettings = { + id: dbUserSettings.id, + userUid: dbUserSettings.userUid, + properties, + updatedOn: dbUserSettings.updatedOn, + }; + + return E.right(userSettings); + } +} diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts index a4756f5e6..35d2c8139 100644 --- a/packages/hoppscotch-backend/src/utils.ts +++ b/packages/hoppscotch-backend/src/utils.ts @@ -4,8 +4,10 @@ import { pipe } from 'fp-ts/lib/function'; import * as O from 'fp-ts/Option'; import * as TE from 'fp-ts/TaskEither'; import * as T from 'fp-ts/Task'; +import * as E from 'fp-ts/Either'; import { User } from './user/user.model'; import * as A from 'fp-ts/Array'; +import { JSON_INVALID } from './errors'; /** * A workaround to throw an exception in an expression. @@ -28,6 +30,19 @@ export const trace = (val: T) => { return val; }; +/** + * String to JSON parser + * @param {str} str The string to parse + * @returns {E.Right | E.Left<"json_invalid">} An Either of the parsed JSON + */ +export function stringToJson(str: string): E.Right | E.Left { + try { + return E.right(JSON.parse(str)); + } catch (err) { + return E.left(JSON_INVALID); + } +} + /** * Similar to `trace` but allows for labels and also an * optional transform function. From 53dc40e8c7828675373e0cd595c1baba7efcf42a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 18:12:49 +0600 Subject: [PATCH 02/25] feat: added mutation for update user settings --- packages/hoppscotch-backend/src/errors.ts | 6 ++++ .../user-settings/user-settings.resolver.ts | 19 +++++++++++ .../user-settings/user-settings.service.ts | 32 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 2e8641e50..0caed05e3 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -215,3 +215,9 @@ 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; \ No newline at end of file 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 a686a9b69..65e13489b 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -35,5 +35,24 @@ export class UserSettingsResolver { return userSettings.right; } + @Mutation(() => UserSettings, { + description: 'Update user settings for given user', + }) + @UseGuards(GqlAuthGuard) + async updateUserSettings( + @GqlUser() user: User, + @Args({ + name: 'properties', + description: 'JSON string of properties object', + }) + properties: string, + ) { + const updatedUserSettings = + await this.userSettingsService.updateUserSettings(user, properties); + + if (E.isLeft(updatedUserSettings)) throwErr(updatedUserSettings.left); + return updatedUserSettings.right; + } + /* Subscriptions */ } 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 f69eb20e8..e9dc0b0f7 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -5,6 +5,7 @@ import { User } from 'src/user/user.model'; import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; import { UserSettings } from './user-settings.model'; +import { USER_SETTINGS_UPDATE_FAILED } from 'src/errors'; @Injectable() export class UserSettingsService { @@ -33,4 +34,35 @@ export class UserSettingsService { return E.right(userSettings); } + + async updateUserSettings(user: User, properties: string) { + const jsonProperties = stringToJson(properties); + if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); + + try { + const dbUpdatedUserSettings = await this.prisma.userSettings.update({ + where: { userUid: user.uid }, + data: { + properties: jsonProperties.right, + }, + }); + + const updatedUserSettings: UserSettings = { + id: dbUpdatedUserSettings.id, + userUid: dbUpdatedUserSettings.userUid, + properties, + updatedOn: dbUpdatedUserSettings.updatedOn, + }; + + // Publish subscription for environment creation + await this.pubsub.publish( + `user_settings/${user.uid}/updated`, + updatedUserSettings, + ); + + return E.right(updatedUserSettings); + } catch (e) { + return E.left(USER_SETTINGS_UPDATE_FAILED); + } + } } From 24434cc61a3f667b011c657e890e3a258acdfb0f Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 18:18:34 +0600 Subject: [PATCH 03/25] feat: added subscriber for update user settings --- .../src/user-settings/user-settings.resolver.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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 65e13489b..d0ce3f92f 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -1,5 +1,5 @@ import { UseGuards } from '@nestjs/common'; -import { Args, Mutation, Resolver } from '@nestjs/graphql'; +import { Args, Mutation, Resolver, Subscription } from '@nestjs/graphql'; import { GqlUser } from 'src/decorators/gql-user.decorator'; import { GqlAuthGuard } from 'src/guards/gql-auth.guard'; import { User } from 'src/user/user.model'; @@ -7,10 +7,14 @@ import * as E from 'fp-ts/Either'; import { throwErr } from 'src/utils'; import { UserSettings } from './user-settings.model'; import { UserSettingsService } from './user-settings.service'; +import { PubSubService } from 'src/pubsub/pubsub.service'; @Resolver() export class UserSettingsResolver { - constructor(private readonly userSettingsService: UserSettingsService) {} + constructor( + private readonly userSettingsService: UserSettingsService, + private readonly pubsub: PubSubService, + ) {} /* Mutations */ @@ -55,4 +59,13 @@ export class UserSettingsResolver { } /* Subscriptions */ + + @Subscription(() => UserSettings, { + description: 'Listen for user setting updating', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userSettingsUpdated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_settings/${user.uid}/updated`); + } } From 83437ae4ba7ee33a3a0ec20a45ebc8538716e9ee Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 18:42:13 +0600 Subject: [PATCH 04/25] feat: added fetchUserSettings for --- packages/hoppscotch-backend/src/errors.ts | 8 +++++- .../src/user-settings/user-settings.module.ts | 10 ++++++-- .../user-settings/user-settings.service.ts | 25 ++++++++++++++++++- .../src/user-settings/user.resolver.ts | 21 ++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 packages/hoppscotch-backend/src/user-settings/user.resolver.ts diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 0caed05e3..e8da11b9c 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -220,4 +220,10 @@ export const BUG_TEAM_ENV_GUARD_NO_ENV_ID = * User settings update failed * (UserSettingsService) */ -export const USER_SETTINGS_UPDATE_FAILED = 'user_settings/update_failed' as const; \ No newline at end of file +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; diff --git a/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts index f85f36150..df3547243 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts @@ -1,11 +1,17 @@ import { Module } from '@nestjs/common'; import { PrismaModule } from 'src/prisma/prisma.module'; import { PubSubModule } from 'src/pubsub/pubsub.module'; +import { UserModule } from 'src/user/user.module'; import { UserSettingsResolver } from './user-settings.resolver'; import { UserSettingsService } from './user-settings.service'; +import { UserSettingsUserResolver } from './user.resolver'; @Module({ - imports: [PrismaModule, PubSubModule], - providers: [UserSettingsResolver, UserSettingsService], + imports: [PrismaModule, PubSubModule, UserModule], + providers: [ + UserSettingsResolver, + UserSettingsService, + UserSettingsUserResolver, + ], }) export class UserSettingsModule {} 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 e9dc0b0f7..ddb870777 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -5,7 +5,10 @@ import { User } from 'src/user/user.model'; import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; import { UserSettings } from './user-settings.model'; -import { USER_SETTINGS_UPDATE_FAILED } from 'src/errors'; +import { + USER_SETTINGS_NOT_FOUND, + USER_SETTINGS_UPDATE_FAILED, +} from 'src/errors'; @Injectable() export class UserSettingsService { @@ -14,6 +17,26 @@ export class UserSettingsService { private readonly pubsub: PubSubService, ) {} + async fetchUserSettings(user: User) { + try { + const dbUserSettings = await this.prisma.userSettings.findUnique({ + where: { userUid: user.uid }, + rejectOnNotFound: true, + }); + + const userSettings: UserSettings = { + id: dbUserSettings.id, + userUid: dbUserSettings.userUid, + properties: JSON.stringify(dbUserSettings.properties), + updatedOn: dbUserSettings.updatedOn, + }; + + return E.right(userSettings); + } catch (e) { + return E.left(USER_SETTINGS_NOT_FOUND); + } + } + async createUserSettings(user: User, properties: string) { const jsonProperties = stringToJson(properties); if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); diff --git a/packages/hoppscotch-backend/src/user-settings/user.resolver.ts b/packages/hoppscotch-backend/src/user-settings/user.resolver.ts new file mode 100644 index 000000000..7967355bf --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user.resolver.ts @@ -0,0 +1,21 @@ +import { Parent, ResolveField, Resolver } from '@nestjs/graphql'; +import { User } from 'src/user/user.model'; +import { UserSettings } from './user-settings.model'; +import { UserSettingsService } from './user-settings.service'; +import * as E from 'fp-ts/Either'; +import { throwErr } from 'src/utils'; + +@Resolver(() => User) +export class UserSettingsUserResolver { + constructor(private readonly userSettingsService: UserSettingsService) {} + + @ResolveField(() => UserSettings, { + description: 'Returns user settings', + }) + async settings(@Parent() user: User): Promise { + const userSettings = await this.userSettingsService.fetchUserSettings(user); + + if (E.isLeft(userSettings)) throwErr(userSettings.left); + return userSettings.right; + } +} From 5c032e84be7cbac62d3e7c90db2d0d8cfa8618cb Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 23:56:50 +0600 Subject: [PATCH 05/25] fix: null value checked on user_settings.properties --- packages/hoppscotch-backend/src/errors.ts | 6 ++++++ .../src/user-settings/user-settings.service.ts | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index e8da11b9c..d5da9ddbe 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -227,3 +227,9 @@ export const USER_SETTINGS_UPDATE_FAILED = 'user_settings/update_failed' as cons * (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.service.ts b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts index ddb870777..700b49857 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -6,6 +6,7 @@ import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; import { UserSettings } from './user-settings.model'; import { + USER_SETTINGS_INVALID_PROPERTIES, USER_SETTINGS_NOT_FOUND, USER_SETTINGS_UPDATE_FAILED, } from 'src/errors'; @@ -38,6 +39,8 @@ export class UserSettingsService { } 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); @@ -59,6 +62,8 @@ export class UserSettingsService { } 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); From b66656ad845d181c168ad367de50640b37cff633 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 23:58:23 +0600 Subject: [PATCH 06/25] fix: prisma service import --- packages/hoppscotch-backend/src/prisma/prisma.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/prisma/prisma.service.ts b/packages/hoppscotch-backend/src/prisma/prisma.service.ts index 5b962c430..8febf1b5b 100644 --- a/packages/hoppscotch-backend/src/prisma/prisma.service.ts +++ b/packages/hoppscotch-backend/src/prisma/prisma.service.ts @@ -1,5 +1,5 @@ import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'; -import { PrismaClient } from '@prisma/client/scripts/default-index'; +import { PrismaClient } from '@prisma/client'; @Injectable() export class PrismaService From b4290c24b3551f2388b287adad1ea08da098332e Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 20 Dec 2022 14:51:58 +0600 Subject: [PATCH 07/25] fix: invalid user handled on createUserSettings --- .../user-settings/user-settings.service.ts | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) 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 700b49857..441452b2e 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -6,6 +6,7 @@ import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; import { UserSettings } from './user-settings.model'; import { + USER_NOT_FOUND, USER_SETTINGS_INVALID_PROPERTIES, USER_SETTINGS_NOT_FOUND, USER_SETTINGS_UPDATE_FAILED, @@ -44,21 +45,25 @@ export class UserSettingsService { const jsonProperties = stringToJson(properties); if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); - const dbUserSettings = await this.prisma.userSettings.create({ - data: { - properties: jsonProperties.right, - userUid: user.uid, - }, - }); + try { + const dbUserSettings = await this.prisma.userSettings.create({ + data: { + properties: jsonProperties.right, + userUid: user.uid, + }, + }); - const userSettings: UserSettings = { - id: dbUserSettings.id, - userUid: dbUserSettings.userUid, - properties, - updatedOn: dbUserSettings.updatedOn, - }; + const userSettings: UserSettings = { + id: dbUserSettings.id, + userUid: dbUserSettings.userUid, + properties, + updatedOn: dbUserSettings.updatedOn, + }; - return E.right(userSettings); + return E.right(userSettings); + } catch (e) { + return E.left(USER_NOT_FOUND); + } } async updateUserSettings(user: User, properties: string) { From 7a036883e88d62225151d1f6866fcf8b8800e70a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 20 Dec 2022 15:00:13 +0600 Subject: [PATCH 08/25] test: added user-settings test cases for service file --- packages/hoppscotch-backend/package.json | 16 ++- .../user-settings.service.spec.ts | 131 ++++++++++++++++++ 2 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts diff --git a/packages/hoppscotch-backend/package.json b/packages/hoppscotch-backend/package.json index 6e3a31005..10be4a53e 100644 --- a/packages/hoppscotch-backend/package.json +++ b/packages/hoppscotch-backend/package.json @@ -18,7 +18,8 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" + "test:e2e": "jest --config ./test/jest-e2e.json", + "postinstall": "prisma generate" }, "dependencies": { "@nestjs/apollo": "^10.1.6", @@ -29,11 +30,13 @@ "@prisma/client": "^4.7.1", "apollo-server-express": "^3.11.1", "apollo-server-plugin-base": "^3.7.1", + "express": "^4.17.1", "fp-ts": "^2.13.1", "graphql": "^15.5.0", "graphql-query-complexity": "^0.12.0", "graphql-redis-subscriptions": "^2.5.0", "graphql-subscriptions": "^2.0.0", + "io-ts": "^2.2.16", "ioredis": "^5.2.4", "prisma": "^4.7.1", "reflect-metadata": "^0.1.13", @@ -44,8 +47,9 @@ "@nestjs/cli": "^9.1.5", "@nestjs/schematics": "^9.0.3", "@nestjs/testing": "^9.2.1", + "@relmify/jest-fp-ts": "^2.0.2", "@types/express": "^4.17.14", - "@types/jest": "29.2.3", + "@types/jest": "^27.5.2", "@types/node": "^18.11.10", "@types/supertest": "^2.0.12", "@typescript-eslint/eslint-plugin": "^5.45.0", @@ -53,7 +57,8 @@ "eslint": "^8.29.0", "eslint-config-prettier": "^8.5.0", "eslint-plugin-prettier": "^4.2.1", - "jest": "29.3.1", + "jest": "^29.3.1", + "jest-mock-extended": "^3.0.1", "prettier": "^2.8.0", "source-map-support": "^0.5.21", "supertest": "^6.3.2", @@ -69,6 +74,9 @@ "json", "ts" ], + "moduleNameMapper": { + "^src/(.*)$": "/$1" + }, "rootDir": "src", "testRegex": ".*\\.spec\\.ts$", "transform": { @@ -80,4 +88,4 @@ "coverageDirectory": "../coverage", "testEnvironment": "node" } -} +} \ No newline at end of file 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 new file mode 100644 index 000000000..25b25a7b8 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts @@ -0,0 +1,131 @@ +import { mockDeep, mockReset } from 'jest-mock-extended'; +import { PrismaService } from 'src/prisma/prisma.service'; +import { PubSubService } from 'src/pubsub/pubsub.service'; +import { UserSettingsService } from './user-settings.service'; +import '@relmify/jest-fp-ts'; +import { + JSON_INVALID, + USER_NOT_FOUND, + USER_SETTINGS_INVALID_PROPERTIES, + USER_SETTINGS_UPDATE_FAILED, +} from 'src/errors'; + +const mockPrisma = mockDeep(); +const mockPubSub = mockDeep(); + +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +const userSettingsService = new UserSettingsService( + mockPrisma, + mockPubSub as any, +); + +const user = { + uid: 'user-uid', + displayName: 'user-display-name', + email: 'user-email', + photoURL: 'user-photo-url', +}; +const userSettings = { + id: '1', + userUid: user.uid, + properties: { key: 'k', value: 'v' }, + updatedOn: new Date('2022-12-19T12:43:18.635Z'), +}; + +beforeEach(() => { + mockReset(mockPrisma); + mockPubSub.publish.mockClear(); +}); + +describe('UserSettingsService', () => { + describe('createUserSettings', () => { + test('should create a user settings with valid user and properties', async () => { + mockPrisma.userSettings.create.mockResolvedValue(userSettings); + + const result = await userSettingsService.createUserSettings( + user, + JSON.stringify(userSettings.properties), + ); + + 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); + }); + test('should reject for invalid properties', async () => { + const result = await userSettingsService.createUserSettings( + user, + 'invalid-properties', + ); + expect(result).toEqualLeft(JSON_INVALID); + }); + test('should reject for null properties', async () => { + const result = await userSettingsService.createUserSettings( + user, + null as any, + ); + expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES); + }); + }); + describe('updateUserSettings', () => { + test('should update a user settings 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 reject for invalid user', async () => { + const result = await userSettingsService.updateUserSettings( + null as any, + JSON.stringify(userSettings.properties), + ); + expect(result).toEqualLeft(USER_SETTINGS_UPDATE_FAILED); + }); + test('should reject for invalid properties', async () => { + const result = await userSettingsService.updateUserSettings( + user, + 'invalid-properties', + ); + expect(result).toEqualLeft(JSON_INVALID); + }); + test('should reject for null properties', async () => { + const result = await userSettingsService.updateUserSettings( + user, + null as any, + ); + expect(result).toEqualLeft(USER_SETTINGS_INVALID_PROPERTIES); + }); + test('should publish message on pubnub after update successfully', async () => { + mockPrisma.userSettings.update.mockResolvedValue(userSettings); + + await userSettingsService.updateUserSettings( + user, + JSON.stringify(userSettings.properties), + ); + + expect(mockPubSub.publish).toBeCalledWith( + `user_settings/${user.uid}/updated`, + { + ...userSettings, + properties: JSON.stringify(userSettings.properties), + }, + ); + }); + }); +}); From 2a8fd245041829e46a38f4282a75bec9e34267d0 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 20 Dec 2022 16:36:45 +0600 Subject: [PATCH 09/25] chore: removed redundent import statement --- .../src/user-settings/user-settings.service.spec.ts | 1 - 1 file changed, 1 deletion(-) 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 25b25a7b8..67dde886e 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 @@ -2,7 +2,6 @@ import { mockDeep, mockReset } from 'jest-mock-extended'; import { PrismaService } from 'src/prisma/prisma.service'; import { PubSubService } from 'src/pubsub/pubsub.service'; import { UserSettingsService } from './user-settings.service'; -import '@relmify/jest-fp-ts'; import { JSON_INVALID, USER_NOT_FOUND, From 9b5734f2ff78cbdf63de4c106ee83acf08a4b35b Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Fri, 23 Dec 2022 12:28:22 +0600 Subject: [PATCH 10/25] refactor: user-settings module --- packages/hoppscotch-backend/src/errors.ts | 31 ++++---- .../src/user-settings/user-settings.model.ts | 8 +- .../user-settings/user-settings.resolver.ts | 10 +-- .../user-settings.service.spec.ts | 20 ++--- .../user-settings/user-settings.service.ts | 74 ++++++++++--------- .../src/user-settings/user.resolver.ts | 2 +- 6 files changed, 74 insertions(+), 71 deletions(-) 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); From b31e54b3e5ed57e6eb5ac0b78df7aa79e88be38c Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Fri, 23 Dec 2022 13:13:21 +0600 Subject: [PATCH 11/25] feat: user-settings schema update and relative service file modified --- .../hoppscotch-backend/prisma/schema.prisma | 20 +++++++------- packages/hoppscotch-backend/src/errors.ts | 7 +++++ .../src/user-settings/user-settings.model.ts | 2 +- .../user-settings/user-settings.resolver.ts | 20 +++++++------- .../user-settings/user-settings.service.ts | 26 +++++++++---------- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 650affa80..b7774c5dc 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -79,19 +79,19 @@ model TeamEnvironment { } model User { - uid String @id @default(cuid()) - displayName String? - email String? - photoURL String? - settings UserSettings? + uid String @id @default(cuid()) + displayName String? + email String? + photoURL String? + settings UserSettings? } model UserSettings { - id String @id @default(uuid()) - userUid String @unique - user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) - properties Json - updatedOn DateTime @updatedAt + id String @id @default(cuid()) + userUid String @unique + user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) + settings Json + updatedOn DateTime @updatedAt @db.Timestamptz(3) } enum TeamMemberRole { diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 31cd009b0..8e4e2d447 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -167,6 +167,13 @@ export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = */ 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; + /** * User setting invalid (null) properties * (UserSettingsService) 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 090803f8b..9fabd971f 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts @@ -15,7 +15,7 @@ export class UserSettings { @Field({ 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 + userSettings: string; // JSON string of the userSettings object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client @Field({ description: 'Last updated on', 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 d3b5de9d5..5794bc667 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -25,18 +25,16 @@ export class UserSettingsResolver { async createUserSettings( @GqlUser() user: User, @Args({ - name: 'properties', + name: 'userSettings', description: 'Stringified JSON settings object', }) - properties: string, + userSettings: string, ) { - const userSettings = await this.userSettingsService.createUserSettings( - user, - properties, - ); + const createdUserSettings = + await this.userSettingsService.createUserSettings(user, userSettings); - if (E.isLeft(userSettings)) throwErr(userSettings.left); - return userSettings.right; + if (E.isLeft(createdUserSettings)) throwErr(createdUserSettings.left); + return createdUserSettings.right; } @Mutation(() => UserSettings, { @@ -46,13 +44,13 @@ export class UserSettingsResolver { async updateUserSettings( @GqlUser() user: User, @Args({ - name: 'properties', + name: 'userSettings', description: 'Stringified JSON settings object', }) - properties: string, + userSettings: string, ) { const updatedUserSettings = - await this.userSettingsService.updateUserSettings(user, properties); + await this.userSettingsService.updateUserSettings(user, userSettings); if (E.isLeft(updatedUserSettings)) throwErr(updatedUserSettings.left); return updatedUserSettings.right; 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 a91f51653..86ae9dabc 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -6,7 +6,7 @@ import * as E from 'fp-ts/Either'; import { stringToJson } from 'src/utils'; import { UserSettings } from './user-settings.model'; import { - USER_NOT_FOUND, + USER_SETTINGS_EXIST, USER_SETTINGS_INVALID_PROPERTIES, USER_SETTINGS_NOT_FOUND, } from 'src/errors'; @@ -31,7 +31,7 @@ export class UserSettingsService { const settings: UserSettings = { ...userSettings, - properties: JSON.stringify(userSettings.properties), + userSettings: JSON.stringify(userSettings.settings), }; return E.right(settings); @@ -46,28 +46,28 @@ export class UserSettingsService { * @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); + async createUserSettings(user: User, settingsString: string) { + if (!settingsString) return E.left(USER_SETTINGS_INVALID_PROPERTIES); - const settingsObject = stringToJson(properties); + const settingsObject = stringToJson(settingsString); if (E.isLeft(settingsObject)) return E.left(settingsObject.left); try { const userSettings = await this.prisma.userSettings.create({ data: { - properties: settingsObject.right, + settings: settingsObject.right, userUid: user.uid, }, }); const settings: UserSettings = { ...userSettings, - properties: JSON.stringify(userSettings.properties), + userSettings: JSON.stringify(userSettings.settings), }; return E.right(settings); } catch (e) { - return E.left(USER_NOT_FOUND); + return E.left(USER_SETTINGS_EXIST); } } @@ -77,23 +77,23 @@ export class UserSettingsService { * @param properties * @returns */ - async updateUserSettings(user: User, properties: string) { - if (!properties) return E.left(USER_SETTINGS_INVALID_PROPERTIES); + async updateUserSettings(user: User, settingsString: string) { + if (!settingsString) return E.left(USER_SETTINGS_INVALID_PROPERTIES); - const settingsObject = stringToJson(properties); + const settingsObject = stringToJson(settingsString); if (E.isLeft(settingsObject)) return E.left(settingsObject.left); try { const updatedUserSettings = await this.prisma.userSettings.update({ where: { userUid: user.uid }, data: { - properties: settingsObject.right, + settings: settingsObject.right, }, }); const settings: UserSettings = { ...updatedUserSettings, - properties: JSON.stringify(updatedUserSettings.properties), + userSettings: JSON.stringify(updatedUserSettings.settings), }; // Publish subscription for environment creation From d863aa7aa6e6bd9665eca14875abe74271bb2433 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Fri, 23 Dec 2022 13:14:57 +0600 Subject: [PATCH 12/25] chore: postinstall update in package.json --- packages/hoppscotch-backend/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/package.json b/packages/hoppscotch-backend/package.json index 23ae94f5e..9c442dbfe 100644 --- a/packages/hoppscotch-backend/package.json +++ b/packages/hoppscotch-backend/package.json @@ -18,8 +18,7 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json", - "postinstall": "prisma generate" + "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@nestjs/apollo": "^10.1.6", From a372cf01783830a2401a37d81ee3b9e71a409fff Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Fri, 23 Dec 2022 21:50:39 +0600 Subject: [PATCH 13/25] chore: addd seed for user-settings --- packages/hoppscotch-backend/package.json | 3 +- packages/hoppscotch-backend/prisma/seed.ts | 53 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/hoppscotch-backend/prisma/seed.ts diff --git a/packages/hoppscotch-backend/package.json b/packages/hoppscotch-backend/package.json index 9c442dbfe..f7771df2c 100644 --- a/packages/hoppscotch-backend/package.json +++ b/packages/hoppscotch-backend/package.json @@ -18,7 +18,8 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" + "test:e2e": "jest --config ./test/jest-e2e.json", + "db-seed": "tsc prisma/seed.ts && cat prisma/seed.js | node --input-type=\"commonjs\" && rm prisma/seed.js" }, "dependencies": { "@nestjs/apollo": "^10.1.6", diff --git a/packages/hoppscotch-backend/prisma/seed.ts b/packages/hoppscotch-backend/prisma/seed.ts new file mode 100644 index 000000000..54a7d8da3 --- /dev/null +++ b/packages/hoppscotch-backend/prisma/seed.ts @@ -0,0 +1,53 @@ +import { PrismaClient, User, UserSettings } from '@prisma/client'; +const prisma = new PrismaClient(); + +const createUsers = async () => { + console.log(`users creating`); + let users: User[] = [ + { + uid: 'aabb22ccdd', + displayName: 'exampleUser', + photoURL: 'http://example.com/avatar', + email: 'me@example.com', + }, + ]; + await prisma.user.createMany({ + data: users, + skipDuplicates: true, + }); + console.log(`users created`); +}; + +const createUserSettings = async () => { + console.log(`user setting creating`); + let userSettings: any[] = [ + { + userUid: 'aabb22ccdd', + settings: { key: 'background', value: 'system' }, + }, + ]; + await prisma.userSettings.createMany({ + data: userSettings, + skipDuplicates: true, + }); + console.log(`user setting created`); +}; + +async function main() { + console.log(`Start seeding ...`); + + await createUsers(); + await createUserSettings(); + + console.log(`Seeding finished.`); +} + +main() + .then(async () => { + await prisma.$disconnect(); + }) + .catch(async (e) => { + console.error(e); + await prisma.$disconnect(); + process.exit(1); + }); From 55f79507fe3fe8e21b1bbce9b821355cfaa417b3 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 5 Jan 2023 13:22:19 +0600 Subject: [PATCH 14/25] chore: updated seed file --- packages/hoppscotch-backend/prisma/seed.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/hoppscotch-backend/prisma/seed.ts b/packages/hoppscotch-backend/prisma/seed.ts index 54a7d8da3..734b7c2ab 100644 --- a/packages/hoppscotch-backend/prisma/seed.ts +++ b/packages/hoppscotch-backend/prisma/seed.ts @@ -2,8 +2,8 @@ import { PrismaClient, User, UserSettings } from '@prisma/client'; const prisma = new PrismaClient(); const createUsers = async () => { - console.log(`users creating`); - let users: User[] = [ + console.log(`Creating new users`); + const users: User[] = [ { uid: 'aabb22ccdd', displayName: 'exampleUser', @@ -19,8 +19,8 @@ const createUsers = async () => { }; const createUserSettings = async () => { - console.log(`user setting creating`); - let userSettings: any[] = [ + console.log(`Creating user settings property`); + const userSettings: any[] = [ { userUid: 'aabb22ccdd', settings: { key: 'background', value: 'system' }, From b33d003ba581385ab92236f5be349b91edb4ffbc Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 5 Jan 2023 15:21:09 +0600 Subject: [PATCH 15/25] 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); From e2d8ea0a7099ea2f966750af1f0fc51d3e7c4d17 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 5 Jan 2023 15:38:18 +0600 Subject: [PATCH 16/25] refactor: error message updated --- packages/hoppscotch-backend/src/errors.ts | 2 +- .../src/user-settings/user-settings.service.spec.ts | 2 +- .../src/user-settings/user-settings.service.ts | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 2842d2b2f..da6426178 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -165,7 +165,7 @@ export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = * User setting not found for a user * (UserSettingsService) */ -export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const; +export const USER_SETTINGS_DATA_NOT_FOUND = 'user_settings/data_not_found' 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 8c4a1b641..d3a7a6935 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,7 +6,7 @@ import { JSON_INVALID, USER_NOT_FOUND, USER_SETTINGS_NULL_SETTINGS, - USER_SETTINGS_NOT_FOUND, + USER_SETTINGS_DATA_NOT_FOUND, } from 'src/errors'; import { UserSettings } from './user-settings.model'; import { User } from 'src/user/user.model'; 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 b040b4b38..23626c6a9 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -8,7 +8,7 @@ import { UserSettings } from './user-settings.model'; import { USER_SETTINGS_ALREADY_EXISTS, USER_SETTINGS_NULL_SETTINGS, - USER_SETTINGS_NOT_FOUND, + USER_SETTINGS_DATA_NOT_FOUND, } from 'src/errors'; @Injectable() @@ -37,7 +37,7 @@ export class UserSettingsService { return E.right(settings); } catch (e) { - return E.left(USER_SETTINGS_NOT_FOUND); + return E.left(USER_SETTINGS_DATA_NOT_FOUND); } } @@ -104,7 +104,7 @@ export class UserSettingsService { return E.right(settings); } catch (e) { - return E.left(USER_SETTINGS_NOT_FOUND); + return E.left(USER_SETTINGS_DATA_NOT_FOUND); } } } From 08ac9680d798c47cc39f235c4c7ff07dcdc4beaf Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 19 Jan 2023 17:11:32 +0600 Subject: [PATCH 17/25] fix: keeping timestamp without timezone --- packages/hoppscotch-backend/prisma/schema.prisma | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index b7774c5dc..2d10e28cd 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -91,7 +91,7 @@ model UserSettings { userUid String @unique user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) settings Json - updatedOn DateTime @updatedAt @db.Timestamptz(3) + updatedOn DateTime @updatedAt @db.Timestamp(3) } enum TeamMemberRole { From dcadbac4d5c2cc2465f2867529e09d173385f9fd Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 16:33:48 +0600 Subject: [PATCH 18/25] docs: update mutation description --- .../src/user-settings/user-settings.resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5794bc667..e4e63157c 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -19,7 +19,7 @@ export class UserSettingsResolver { /* Mutations */ @Mutation(() => UserSettings, { - description: 'Creates a new user setting for a given user', + description: 'Creates a new user setting', }) @UseGuards(GqlAuthGuard) async createUserSettings( From bfac3f8ad0a7eae4f18dbe002ebcfc4ac8426f6a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 20:25:48 +0600 Subject: [PATCH 19/25] feat: db column update from settings to properties --- .../hoppscotch-backend/prisma/schema.prisma | 10 ++--- .../src/user-settings/user-settings.model.ts | 2 +- .../user-settings/user-settings.resolver.ts | 12 +++--- .../user-settings/user-settings.service.ts | 40 +++++++++---------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index ab3346a23..6165b3d6a 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -88,11 +88,11 @@ model User { } model UserSettings { - id String @id @default(cuid()) - userUid String @unique - user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) - settings Json - updatedOn DateTime @updatedAt @db.Timestamp(3) + id String @id @default(cuid()) + userUid String @unique + user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) + properties Json + updatedOn DateTime @updatedAt @db.Timestamp(3) } model UserEnvironment { 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 9fabd971f..81d1af84a 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.model.ts @@ -15,7 +15,7 @@ export class UserSettings { @Field({ description: 'Stringified JSON settings object', }) - userSettings: string; // JSON string of the userSettings object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client + properties: string; // JSON string of the userSettings object (format:[{ key: "background", value: "system" }, ...] ) which will be received from the client @Field({ description: 'Last updated on', 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 e4e63157c..adabbc772 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -25,13 +25,13 @@ export class UserSettingsResolver { async createUserSettings( @GqlUser() user: User, @Args({ - name: 'userSettings', + name: 'properties', description: 'Stringified JSON settings object', }) - userSettings: string, + properties: string, ) { const createdUserSettings = - await this.userSettingsService.createUserSettings(user, userSettings); + await this.userSettingsService.createUserSettings(user, properties); if (E.isLeft(createdUserSettings)) throwErr(createdUserSettings.left); return createdUserSettings.right; @@ -44,13 +44,13 @@ export class UserSettingsResolver { async updateUserSettings( @GqlUser() user: User, @Args({ - name: 'userSettings', + name: 'properties', description: 'Stringified JSON settings object', }) - userSettings: string, + properties: string, ) { const updatedUserSettings = - await this.userSettingsService.updateUserSettings(user, userSettings); + await this.userSettingsService.updateUserSettings(user, properties); if (E.isLeft(updatedUserSettings)) throwErr(updatedUserSettings.left); return updatedUserSettings.right; 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 23626c6a9..a7a47b64e 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -21,7 +21,7 @@ export class UserSettingsService { /** * Fetch user setting for a given user * @param user User object - * @returns an Either of `UserSettings` or error + * @returns Promise of an Either of `UserSettings` or error */ async fetchUserSettings(user: User) { try { @@ -31,9 +31,8 @@ export class UserSettingsService { const settings: UserSettings = { ...userSettings, - userSettings: JSON.stringify(userSettings.settings), + properties: JSON.stringify(userSettings.properties), }; - delete (settings as any).settings; return E.right(settings); } catch (e) { @@ -44,28 +43,30 @@ export class UserSettingsService { /** * Create user setting for a given user * @param user User object - * @param properties User setting properties + * @param properties stringified user settings properties * @returns an Either of `UserSettings` or error */ - async createUserSettings(user: User, settingsString: string) { - if (!settingsString) return E.left(USER_SETTINGS_NULL_SETTINGS); + async createUserSettings(user: User, properties: string) { + if (!properties) return E.left(USER_SETTINGS_NULL_SETTINGS); - const settingsObject = stringToJson(settingsString); - if (E.isLeft(settingsObject)) return E.left(settingsObject.left); + const jsonProperties = stringToJson(properties); + if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); try { const userSettings = await this.prisma.userSettings.create({ data: { - settings: settingsObject.right, + properties: jsonProperties.right, userUid: user.uid, }, }); const settings: UserSettings = { ...userSettings, - userSettings: JSON.stringify(userSettings.settings), + properties: JSON.stringify(userSettings.properties), }; - delete (settings as any).settings; + + // Publish subscription for environment creation + await this.pubsub.publish(`user_settings/${user.uid}/created`, settings); return E.right(settings); } catch (e) { @@ -76,28 +77,27 @@ export class UserSettingsService { /** * Update user setting for a given user * @param user User object - * @param properties - * @returns + * @param properties stringified user settings + * @returns Promise of an Either of `UserSettings` or error */ - async updateUserSettings(user: User, settingsString: string) { - if (!settingsString) return E.left(USER_SETTINGS_NULL_SETTINGS); + async updateUserSettings(user: User, properties: string) { + if (!properties) return E.left(USER_SETTINGS_NULL_SETTINGS); - const settingsObject = stringToJson(settingsString); - if (E.isLeft(settingsObject)) return E.left(settingsObject.left); + const jsonProperties = stringToJson(properties); + if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); try { const updatedUserSettings = await this.prisma.userSettings.update({ where: { userUid: user.uid }, data: { - settings: settingsObject.right, + properties: jsonProperties.right, }, }); const settings: UserSettings = { ...updatedUserSettings, - userSettings: JSON.stringify(updatedUserSettings.settings), + properties: JSON.stringify(updatedUserSettings.properties), }; - delete (settings as any).settings; // Publish subscription for environment creation await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings); From 3cd9639f34930deffb06a915686e35101486161f Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 21:32:00 +0600 Subject: [PATCH 20/25] test: feedback updated on test script --- packages/hoppscotch-backend/src/errors.ts | 2 +- .../src/pubsub/topicsDefs.ts | 4 +-- .../user-settings.service.spec.ts | 33 ++++++++----------- .../user-settings/user-settings.service.ts | 10 +++--- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 706d660d5..bdde00c20 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -165,7 +165,7 @@ export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = * User setting not found for a user * (UserSettingsService) */ -export const USER_SETTINGS_DATA_NOT_FOUND = 'user_settings/data_not_found' as const; +export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const; /** * User setting not found for a user diff --git a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts index d16c26b4c..d55033582 100644 --- a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts +++ b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts @@ -7,8 +7,6 @@ export type TopicDef = { [ topic: `user_environment/${string}/${'created' | 'updated' | 'deleted'}` ]: UserEnvironment; - [ - topic: `user_settings/${string}/${'created' | 'updated' | 'deleted'}` - ]: UserSettings; + [topic: `user_settings/${string}/${'created' | 'updated'}`]: UserSettings; [topic: `user_environment/${string}/deleted_many`]: number; }; 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 d3a7a6935..8f4f76138 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 @@ -2,12 +2,7 @@ import { mockDeep, mockReset } from 'jest-mock-extended'; import { PrismaService } from 'src/prisma/prisma.service'; import { PubSubService } from 'src/pubsub/pubsub.service'; import { UserSettingsService } from './user-settings.service'; -import { - JSON_INVALID, - USER_NOT_FOUND, - USER_SETTINGS_NULL_SETTINGS, - USER_SETTINGS_DATA_NOT_FOUND, -} from 'src/errors'; +import { JSON_INVALID, USER_SETTINGS_NULL_SETTINGS } from 'src/errors'; import { UserSettings } from './user-settings.model'; import { User } from 'src/user/user.model'; @@ -30,7 +25,7 @@ const user: User = { const settings: UserSettings = { id: '1', userUid: user.uid, - userSettings: JSON.stringify({ key: 'k', value: 'v' }), + properties: JSON.stringify({ key: 'k', value: 'v' }), updatedOn: new Date('2022-12-19T12:43:18.635Z'), }; @@ -41,20 +36,20 @@ beforeEach(() => { describe('UserSettingsService', () => { describe('createUserSettings', () => { - test('should create a user setting with valid user and properties', async () => { + test('Should resolve right and create an user setting with valid user and properties', async () => { mockPrisma.userSettings.create.mockResolvedValue({ ...settings, - settings: JSON.parse(settings.userSettings), + properties: JSON.parse(settings.properties), }); const result = await userSettingsService.createUserSettings( user, - settings.userSettings, + settings.properties, ); expect(result).toEqualRight(settings); }); - test('should reject for invalid properties', async () => { + test('Should reject user settings creation for invalid properties', async () => { const result = await userSettingsService.createUserSettings( user, 'invalid-settings', @@ -62,7 +57,7 @@ describe('UserSettingsService', () => { expect(result).toEqualLeft(JSON_INVALID); }); - test('should reject for null settings', async () => { + test('Should reject user settings creation for null properties', async () => { const result = await userSettingsService.createUserSettings( user, null as any, @@ -72,20 +67,20 @@ describe('UserSettingsService', () => { }); }); describe('updateUserSettings', () => { - test('should update a user setting for valid user and settings', async () => { + test('Should update a user setting for valid user and settings', async () => { mockPrisma.userSettings.update.mockResolvedValue({ ...settings, - settings: JSON.parse(settings.userSettings), + properties: JSON.parse(settings.properties), }); const result = await userSettingsService.updateUserSettings( user, - settings.userSettings, + settings.properties, ); expect(result).toEqualRight(settings); }); - test('should reject for invalid stringified JSON settings', async () => { + test('Should reject user settings updation for invalid stringified JSON settings', async () => { const result = await userSettingsService.updateUserSettings( user, 'invalid-settings', @@ -93,7 +88,7 @@ describe('UserSettingsService', () => { expect(result).toEqualLeft(JSON_INVALID); }); - test('should reject for null settings', async () => { + test('Should reject user settings updation for null properties', async () => { const result = await userSettingsService.updateUserSettings( user, null as any, @@ -103,10 +98,10 @@ describe('UserSettingsService', () => { test('should publish message over pubsub on successful update', async () => { mockPrisma.userSettings.update.mockResolvedValue({ ...settings, - settings: JSON.parse(settings.userSettings), + properties: JSON.parse(settings.properties), }); - await userSettingsService.updateUserSettings(user, settings.userSettings); + await userSettingsService.updateUserSettings(user, settings.properties); expect(mockPubSub.publish).toBeCalledWith( `user_settings/${user.uid}/updated`, 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 a7a47b64e..f069c9f81 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -8,7 +8,7 @@ import { UserSettings } from './user-settings.model'; import { USER_SETTINGS_ALREADY_EXISTS, USER_SETTINGS_NULL_SETTINGS, - USER_SETTINGS_DATA_NOT_FOUND, + USER_SETTINGS_NOT_FOUND, } from 'src/errors'; @Injectable() @@ -36,7 +36,7 @@ export class UserSettingsService { return E.right(settings); } catch (e) { - return E.left(USER_SETTINGS_DATA_NOT_FOUND); + return E.left(USER_SETTINGS_NOT_FOUND); } } @@ -65,7 +65,7 @@ export class UserSettingsService { properties: JSON.stringify(userSettings.properties), }; - // Publish subscription for environment creation + // Publish subscription for user settings creation await this.pubsub.publish(`user_settings/${user.uid}/created`, settings); return E.right(settings); @@ -99,12 +99,12 @@ export class UserSettingsService { properties: JSON.stringify(updatedUserSettings.properties), }; - // Publish subscription for environment creation + // Publish subscription for user settings update await this.pubsub.publish(`user_settings/${user.uid}/updated`, settings); return E.right(settings); } catch (e) { - return E.left(USER_SETTINGS_DATA_NOT_FOUND); + return E.left(USER_SETTINGS_NOT_FOUND); } } } From 27b9f57d7aaf54e850615aac7ca798251aaf32bb Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 21:51:46 +0600 Subject: [PATCH 21/25] test: pubsub test case added on user settings create --- .../user-settings/user-settings.service.spec.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 8f4f76138..b891ac08a 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 @@ -65,6 +65,19 @@ describe('UserSettingsService', () => { expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); }); + test('Should publish message over pubsub on successful user settings create', async () => { + mockPrisma.userSettings.create.mockResolvedValue({ + ...settings, + properties: JSON.parse(settings.properties), + }); + + await userSettingsService.createUserSettings(user, settings.properties); + + expect(mockPubSub.publish).toBeCalledWith( + `user_settings/${user.uid}/created`, + settings, + ); + }); }); describe('updateUserSettings', () => { test('Should update a user setting for valid user and settings', async () => { @@ -95,7 +108,7 @@ describe('UserSettingsService', () => { ); expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); }); - test('should publish message over pubsub on successful update', async () => { + test('Should publish message over pubsub on successful user settings update', async () => { mockPrisma.userSettings.update.mockResolvedValue({ ...settings, properties: JSON.parse(settings.properties), From e40d77420cae6fff17eedaba3d711d700f040c83 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 21:54:19 +0600 Subject: [PATCH 22/25] chore: comment text updated --- .../src/user-settings/user-settings.service.spec.ts | 4 ++-- .../src/user-settings/user-settings.service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 b891ac08a..c8bc9705e 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 @@ -65,7 +65,7 @@ describe('UserSettingsService', () => { expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); }); - test('Should publish message over pubsub on successful user settings create', async () => { + test('Should publish pubsub message on successful user settings create', async () => { mockPrisma.userSettings.create.mockResolvedValue({ ...settings, properties: JSON.parse(settings.properties), @@ -108,7 +108,7 @@ describe('UserSettingsService', () => { ); expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); }); - test('Should publish message over pubsub on successful user settings update', async () => { + test('Should publish pubsub message on successful user settings update', async () => { mockPrisma.userSettings.update.mockResolvedValue({ ...settings, properties: JSON.parse(settings.properties), 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 f069c9f81..40b630297 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -19,7 +19,7 @@ export class UserSettingsService { ) {} /** - * Fetch user setting for a given user + * Fetch user settings for a given user * @param user User object * @returns Promise of an Either of `UserSettings` or error */ From bc82e9c7fab839a8e9070acefdc55cd488cf0ca5 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 07:26:47 +0600 Subject: [PATCH 23/25] feat: user settings create subscription added and fixed typos --- packages/hoppscotch-backend/src/errors.ts | 4 ++-- .../src/user-settings/user-settings.resolver.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index bdde00c20..7abd61d80 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -168,10 +168,10 @@ export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = export const USER_SETTINGS_NOT_FOUND = 'user_settings/not_found' as const; /** - * User setting not found for a user + * User setting already exists for a user * (UserSettingsService) */ -export const USER_SETTINGS_ALREADY_EXISTS = 'user_settings/settings_already_present' as const; +export const USER_SETTINGS_ALREADY_EXISTS = 'user_settings/settings_already_exists' as const; /** * User setting invalid (null) settings 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 adabbc772..fd484b035 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -58,6 +58,15 @@ export class UserSettingsResolver { /* Subscriptions */ + @Subscription(() => UserSettings, { + description: 'Listen for user setting creates', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userSettingsCreated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_settings/${user.uid}/created`); + } + @Subscription(() => UserSettings, { description: 'Listen for user setting updates', resolve: (value) => value, From e8e176ed4040f6ae6e243e427bd769bb640232dc Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 12:49:22 +0600 Subject: [PATCH 24/25] fix: removed seed scripts --- .../migration.sql | 15 ------ packages/hoppscotch-backend/prisma/seed.ts | 53 ------------------- .../user-settings/user-settings.resolver.ts | 2 +- 3 files changed, 1 insertion(+), 69 deletions(-) delete mode 100644 packages/hoppscotch-backend/prisma/migrations/20230123090914_craete_user_settings/migration.sql delete mode 100644 packages/hoppscotch-backend/prisma/seed.ts diff --git a/packages/hoppscotch-backend/prisma/migrations/20230123090914_craete_user_settings/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230123090914_craete_user_settings/migration.sql deleted file mode 100644 index f0ec24276..000000000 --- a/packages/hoppscotch-backend/prisma/migrations/20230123090914_craete_user_settings/migration.sql +++ /dev/null @@ -1,15 +0,0 @@ --- CreateTable -CREATE TABLE "UserSettings" ( - "id" TEXT NOT NULL, - "userUid" TEXT NOT NULL, - "settings" JSONB NOT NULL, - "updatedOn" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "UserSettings_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "UserSettings_userUid_key" ON "UserSettings"("userUid"); - --- AddForeignKey -ALTER TABLE "UserSettings" ADD CONSTRAINT "UserSettings_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/hoppscotch-backend/prisma/seed.ts b/packages/hoppscotch-backend/prisma/seed.ts deleted file mode 100644 index 734b7c2ab..000000000 --- a/packages/hoppscotch-backend/prisma/seed.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { PrismaClient, User, UserSettings } from '@prisma/client'; -const prisma = new PrismaClient(); - -const createUsers = async () => { - console.log(`Creating new users`); - const users: User[] = [ - { - uid: 'aabb22ccdd', - displayName: 'exampleUser', - photoURL: 'http://example.com/avatar', - email: 'me@example.com', - }, - ]; - await prisma.user.createMany({ - data: users, - skipDuplicates: true, - }); - console.log(`users created`); -}; - -const createUserSettings = async () => { - console.log(`Creating user settings property`); - const userSettings: any[] = [ - { - userUid: 'aabb22ccdd', - settings: { key: 'background', value: 'system' }, - }, - ]; - await prisma.userSettings.createMany({ - data: userSettings, - skipDuplicates: true, - }); - console.log(`user setting created`); -}; - -async function main() { - console.log(`Start seeding ...`); - - await createUsers(); - await createUserSettings(); - - console.log(`Seeding finished.`); -} - -main() - .then(async () => { - await prisma.$disconnect(); - }) - .catch(async (e) => { - console.error(e); - await prisma.$disconnect(); - process.exit(1); - }); 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 fd484b035..74e103c70 100644 --- a/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -59,7 +59,7 @@ export class UserSettingsResolver { /* Subscriptions */ @Subscription(() => UserSettings, { - description: 'Listen for user setting creates', + description: 'Listen for user setting creation', resolve: (value) => value, }) @UseGuards(GqlAuthGuard) From b50b97a4d16e89f70b790ef4071109e4bc777bb2 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 12:50:31 +0600 Subject: [PATCH 25/25] chore: removed seed cmd form package.json --- packages/hoppscotch-backend/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/package.json b/packages/hoppscotch-backend/package.json index f7771df2c..9c442dbfe 100644 --- a/packages/hoppscotch-backend/package.json +++ b/packages/hoppscotch-backend/package.json @@ -18,8 +18,7 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json", - "db-seed": "tsc prisma/seed.ts && cat prisma/seed.js | node --input-type=\"commonjs\" && rm prisma/seed.js" + "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@nestjs/apollo": "^10.1.6",