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/46] 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/46] 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/46] 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/46] 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/46] 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/46] 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/46] 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 d066b9c9136d1c8f4711d71130c7bbc4b92818d2 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:28:15 +0530 Subject: [PATCH 08/46] feat: added prisma schema for user history --- .../hoppscotch-backend/prisma/schema.prisma | 25 ++++++++++++++++--- .../src/prisma/prisma.service.ts | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 947d158a8..2fc841fff 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -79,10 +79,27 @@ model TeamEnvironment { } model User { - uid String @id @default(cuid()) - displayName String? - email String? - photoURL String? + uid String @id @default(cuid()) + displayName String? + email String? + photoURL String? + UserHistory UserHistory[] +} + +model UserHistory { + id String @id @default(cuid()) + userUid String + user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) + type ReqType + request Json + responseMetadata Json + isStarred Boolean + executedOn DateTime @default(now()) +} + +enum ReqType { + REST + GQL } enum TeamMemberRole { 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 e665df21daa2bc6bc3687d9e116f0757338469cd Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:29:31 +0530 Subject: [PATCH 09/46] feat: added resolvers for user model and history --- .../src/user-history/user.resolver.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user-history/user.resolver.ts diff --git a/packages/hoppscotch-backend/src/user-history/user.resolver.ts b/packages/hoppscotch-backend/src/user-history/user.resolver.ts new file mode 100644 index 000000000..12ec75c02 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user.resolver.ts @@ -0,0 +1,27 @@ +import { Parent, ResolveField, Resolver } from '@nestjs/graphql'; +import { User } from '../user/user.model'; +import { UserHistoryService } from './user-history.service'; +import { ReqType, UserHistory } from './user-history.model'; + +@Resolver(() => User) +export class UserHistoryUserResolver { + constructor(private userHistoryService: UserHistoryService) {} + @ResolveField(() => [UserHistory], { + description: 'Returns a users REST history', + }) + async RESTHistory(@Parent() user: User): Promise { + return await this.userHistoryService.fetchUserHistory( + user.uid, + ReqType.REST, + ); + } + @ResolveField(() => [UserHistory], { + description: 'Returns a users GraphQL history', + }) + async GraphQLHistory(@Parent() user: User): Promise { + return await this.userHistoryService.fetchUserHistory( + user.uid, + ReqType.GQL, + ); + } +} From 7a036883e88d62225151d1f6866fcf8b8800e70a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 20 Dec 2022 15:00:13 +0600 Subject: [PATCH 10/46] 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 b677aa1715d0a35897627d785a6b3c72762bc78a Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:30:14 +0530 Subject: [PATCH 11/46] feat: added user history model --- .../src/user-history/user-history.model.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.model.ts diff --git a/packages/hoppscotch-backend/src/user-history/user-history.model.ts b/packages/hoppscotch-backend/src/user-history/user-history.model.ts new file mode 100644 index 000000000..493646466 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.model.ts @@ -0,0 +1,49 @@ +import { Field, ID, ObjectType, registerEnumType } from '@nestjs/graphql'; + +@ObjectType() +export class UserHistory { + @Field(() => ID, { + description: 'ID of the user request in history', + }) + id: string; + + @Field(() => ID, { + description: 'ID of the user this history belongs to', + }) + userUid: string; + + @Field(() => ReqType, { + description: 'Type of the request in the history', + }) + reqType: ReqType; + + @Field({ + description: 'JSON string representing the request data', + }) + request: string; + + @Field({ + description: 'JSON string representing the response meta-data', + }) + responseMetadata: string; + + @Field({ + description: 'If the request in the history starred', + }) + isStarred: boolean; + + @Field({ + description: + 'Timestamp of when the request was executed or history was created', + }) + executedOn: Date; +} + +export enum ReqType { + REST = 'REST', + GQL = 'GQL', +} + +registerEnumType(ReqType, { + name: 'ReqType', +}); From b9ade5d2a37905e93e5025e7b2b6a1d50e11568b Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:31:01 +0530 Subject: [PATCH 12/46] feat: added user-history module to app module --- packages/hoppscotch-backend/src/app.module.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/hoppscotch-backend/src/app.module.ts b/packages/hoppscotch-backend/src/app.module.ts index 7614320b9..2a82e76c8 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 { UserHistoryModule } from './user-history/user-history.module'; @Module({ imports: [ @@ -44,6 +45,7 @@ import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin'; driver: ApolloDriver, }), UserModule, + UserHistoryModule, ], providers: [GQLComplexityPlugin], }) From a28774c2c473c49e1d8831ce0992bd50de9cf51a Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:35:25 +0530 Subject: [PATCH 13/46] feat: added user-history resolvers, service files and module --- .../src/user-history/user-history.module.ts | 14 ++ .../src/user-history/user-history.resolver.ts | 48 ++++ .../src/user-history/user-history.service.ts | 226 ++++++++++++++++++ 3 files changed, 288 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.module.ts create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.resolver.ts create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.service.ts diff --git a/packages/hoppscotch-backend/src/user-history/user-history.module.ts b/packages/hoppscotch-backend/src/user-history/user-history.module.ts new file mode 100644 index 000000000..aaa20bfec --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { PrismaModule } from '../prisma/prisma.module'; +import { PubSubModule } from '../pubsub/pubsub.module'; +import { UserModule } from '../user/user.module'; +import { UserHistoryUserResolver } from './user.resolver'; +import { UserHistoryResolver } from './user-history.resolver'; +import { UserHistoryService } from './user-history.service'; + +@Module({ + imports: [PrismaModule, PubSubModule, UserModule], + providers: [UserHistoryResolver, UserHistoryService, UserHistoryUserResolver], + exports: [UserHistoryService], +}) +export class UserHistoryModule {} diff --git a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts new file mode 100644 index 000000000..a4ee893fb --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts @@ -0,0 +1,48 @@ +import { Args, Mutation, Resolver } from '@nestjs/graphql'; +import { UserHistoryService } from './user-history.service'; +import { PubSubService } from '../pubsub/pubsub.service'; +import { UserHistory } from './user-history.model'; +import { UseGuards } from '@nestjs/common'; +import { GqlAuthGuard } from '../guards/gql-auth.guard'; +import { GqlUser } from '../decorators/gql-user.decorator'; +import { User } from '../user/user.model'; + +@Resolver() +export class UserHistoryResolver { + constructor( + private readonly userHistoryService: UserHistoryService, + private readonly pubsub: PubSubService, + ) {} + + /* Mutations */ + + @Mutation(() => UserHistory, { + description: 'Adds a new REST/GQL request to user history', + }) + @UseGuards(GqlAuthGuard) + async addRequestToHistory( + @GqlUser() user: User, + @Args({ + name: 'reqData', + description: 'JSON string of the request data', + }) + reqData: string, + @Args({ + name: 'resMetadata', + description: 'JSON string of the response metadata', + }) + resMetadata: string, + @Args({ + name: 'reqType', + description: 'string that denotes type of request REST or GQL', + }) + reqType: string, + ): Promise { + return await this.userHistoryService.addRequestToHistory( + user.uid, + reqData, + resMetadata, + reqType, + ); + } +} diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts new file mode 100644 index 000000000..8fcdfbc11 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -0,0 +1,226 @@ +import { Injectable } from '@nestjs/common'; +import { PrismaService } from '../prisma/prisma.service'; +import { PubSubService } from '../pubsub/pubsub.service'; +import { ReqType, UserHistory } from './user-history.model'; +import * as E from 'fp-ts/Either'; + +// Contains constants for the subscription types we send to pubsub service +enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', +} + +@Injectable() +export class UserHistoryService { + constructor( + private readonly prisma: PrismaService, + private readonly pubsub: PubSubService, + ) {} + + /** + * Fetch users REST or GraphQL history based on ReqType argument. + * @param uid Users uid + * @param reqType request Type to fetch i.e. GraphQL or REST + * @returns an array of user history + */ + async fetchUserHistory(uid: string, reqType: ReqType) { + const userHistory = await this.prisma.userHistory.findMany({ + where: { + userUid: uid, + type: reqType, + }, + }); + + const userHistoryColl: UserHistory[] = []; + userHistory.forEach((history) => { + userHistoryColl.push({ + id: history.id, + userUid: history.userUid, + reqType: history.type, + request: JSON.stringify(history.request), + responseMetadata: JSON.stringify(history.responseMetadata), + isStarred: history.isStarred, + }); + }); + + return userHistoryColl; + } + + /** + * Adds a request to users history. + * @param uid Users uid + * @param reqData the request data + * @param resMetadata the response metadata + * @param reqType request Type to fetch i.e. GraphQL or REST + * @returns an array of user history + */ + async addRequestToHistory( + uid: string, + reqData: string, + resMetadata: string, + reqType: string, + ) { + const requestType = this.validateReqType(reqType); + const history = await this.prisma.userHistory.create({ + data: { + userUid: uid, + request: JSON.parse(reqData), + responseMetadata: JSON.parse(resMetadata), + type: requestType, + isStarred: false, + }, + }); + + const userHistory = { + id: history.id, + userUid: history.userUid, + request: JSON.stringify(history.request), + responseMetadata: JSON.stringify(history.responseMetadata), + executedOn: history.executedOn, + isStarred: history.isStarred, + reqType: history.type, + }; + + await this.publishUserHistorySubscription( + userHistory, + SubscriptionType.Created, + ); + + return userHistory; + } + + /** + * Stars or unstars a request in the history + * @param uid Users uid + * @param id id of the request in the history + * @returns an Either of updated `UserHistory` or Error + */ + async starUnstarRequestInHistory(uid: string, id: string) { + const userHistory = await this.prisma.userHistory.findFirst({ + where: { + id: id, + }, + }); + + if (userHistory == null) { + return E.left('history doesnt exist'); + } + try { + const updatedHistory = await this.prisma.userHistory.update({ + where: { + id: id, + }, + data: { + isStarred: !userHistory.isStarred, + }, + }); + + const updatedUserHistory = { + id: updatedHistory.id, + userUid: updatedHistory.userUid, + request: JSON.stringify(updatedHistory.request), + responseMetadata: JSON.stringify(updatedHistory.responseMetadata), + executedOn: updatedHistory.executedOn, + isStarred: updatedHistory.isStarred, + reqType: updatedHistory.type, + }; + + await this.publishUserHistorySubscription( + updatedUserHistory, + SubscriptionType.Updated, + ); + + return E.right(updatedUserHistory); + } catch (e) { + E.left('error updating'); + } + } + + /** + * Removes a REST/GraphQL request from the history + * @param uid Users uid + * @param id id of the request + * @returns an Either of deleted `UserHistory` or Error + */ + async removeRequestFromHistory(uid: string, id: string) { + try { + const delUserHistory = await this.prisma.userHistory.delete({ + where: { + id: id, + }, + }); + + const deletedUserHistory = { + id: delUserHistory.id, + userUid: delUserHistory.userUid, + request: JSON.stringify(delUserHistory.request), + responseMetadata: JSON.stringify(delUserHistory.responseMetadata), + executedOn: delUserHistory.executedOn, + isStarred: delUserHistory.isStarred, + reqType: delUserHistory.type, + }; + + await this.publishUserHistorySubscription( + deletedUserHistory, + SubscriptionType.Deleted, + ); + return E.right(deletedUserHistory); + } catch (e) { + return E.left('error deleting history not found'); + } + } + + /** + * Delete all REST/GraphQl user history based on ReqType + * @param uid Users uid + * @param reqType request type to be deleted i.e. REST or GraphQL + * @returns a count of deleted history + */ + async deleteAllUserHistory(uid: string, reqType: string) { + const requestType = this.validateReqType(reqType); + return await this.prisma.userHistory.deleteMany({ + where: { + userUid: uid, + type: requestType, + }, + }); + } + + // Method that takes a request type argument as string and validates against `ReqType` + validateReqType(reqType: string) { + let requestType: ReqType; + return reqType == ReqType.REST + ? (requestType = ReqType.REST) + : (requestType = ReqType.GQL); + } + + // Method to publish subscriptions based on the subscription type of the history + async publishUserHistorySubscription( + userHistory: UserHistory, + subscriptionType: SubscriptionType, + ) { + switch (subscriptionType) { + case SubscriptionType.Created: + await this.pubsub.publish( + `user_history/${userHistory.id}/created`, + userHistory, + ); + break; + case SubscriptionType.Updated: + await this.pubsub.publish( + `user_history/${userHistory.id}/updated`, + userHistory, + ); + break; + case SubscriptionType.Deleted: + await this.pubsub.publish( + `user_history/${userHistory.id}/deleted`, + userHistory, + ); + break; + default: + break; + } + } +} From 2a8fd245041829e46a38f4282a75bec9e34267d0 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 20 Dec 2022 16:36:45 +0600 Subject: [PATCH 14/46] 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 f7dadda52abab58bb2655f9ffb2ff7c5acf9fa23 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 21:05:58 +0530 Subject: [PATCH 15/46] chore: added service files for user history and unit tests --- .../user-history/user-history.service.spec.ts | 442 ++++++++++++++++++ .../src/user-history/user-history.service.ts | 42 +- 2 files changed, 467 insertions(+), 17 deletions(-) create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts new file mode 100644 index 000000000..e387330c7 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts @@ -0,0 +1,442 @@ +import { UserHistoryService } from './user-history.service'; +import { PrismaService } from '../prisma/prisma.service'; +import { PubSubService } from '../pubsub/pubsub.service'; +import { mockDeep, mockReset } from 'jest-mock-extended'; +import { ReqType, UserHistory } from './user-history.model'; +import { + USER_HISTORY_INVALID_REQ_TYPE, + USER_HISTORY_NOT_FOUND, +} from '../errors'; + +const mockPrisma = mockDeep(); +const mockPubSub = mockDeep(); + +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +const userHistoryService = new UserHistoryService( + mockPrisma, + mockPubSub as any, +); + +enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', +} + +beforeEach(() => { + mockReset(mockPrisma); + mockPubSub.publish.mockClear(); +}); + +describe('UserHistoryService', () => { + describe('fetchUserHistory', () => { + test('Should return a list of users REST history if exists', async () => { + const executedOn = new Date(); + mockPrisma.userHistory.findMany.mockResolvedValueOnce([ + { + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: executedOn, + isStarred: false, + }, + { + userUid: 'abc', + id: '2', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: executedOn, + isStarred: true, + }, + ]); + + const userHistory: UserHistory[] = [ + { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: executedOn, + isStarred: false, + }, + { + userUid: 'abc', + id: '2', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: executedOn, + isStarred: true, + }, + ]; + return expect( + await userHistoryService.fetchUserHistory('abc', ReqType.REST), + ).toEqual(userHistory); + }); + test('Should return a list of users GQL history if exists', async () => { + const executedOn = new Date(); + mockPrisma.userHistory.findMany.mockResolvedValueOnce([ + { + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.GQL, + executedOn: executedOn, + isStarred: false, + }, + { + userUid: 'abc', + id: '2', + request: [{}], + responseMetadata: [{}], + type: ReqType.GQL, + executedOn: executedOn, + isStarred: true, + }, + ]); + + const userHistory: UserHistory[] = [ + { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.GQL, + executedOn: executedOn, + isStarred: false, + }, + { + userUid: 'abc', + id: '2', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.GQL, + executedOn: executedOn, + isStarred: true, + }, + ]; + return expect( + await userHistoryService.fetchUserHistory('abc', ReqType.GQL), + ).toEqual(userHistory); + }); + test('Should return an empty list of users REST history if doesnt exists', async () => { + mockPrisma.userHistory.findMany.mockResolvedValueOnce([]); + + const userHistory: UserHistory[] = []; + return expect( + await userHistoryService.fetchUserHistory('abc', ReqType.REST), + ).toEqual(userHistory); + }); + test('Should return an empty list of users GQL history if doesnt exists', async () => { + mockPrisma.userHistory.findMany.mockResolvedValueOnce([]); + + const userHistory: UserHistory[] = []; + return expect( + await userHistoryService.fetchUserHistory('abc', ReqType.GQL), + ).toEqual(userHistory); + }); + }); + describe('addRequestToHistory', () => { + test('Should resolve right and add a REST request to users history, publish a subscription and return a `UserHistory` object', async () => { + userHistoryService.validateReqType('REST'); + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await userHistoryService.publishUserHistorySubscription( + userHistory, + SubscriptionType.Created, + ); + + return expect( + await userHistoryService.addRequestToHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'REST', + ), + ).toEqualRight(userHistory); + }); + test('Should resolve right and add a GQL request to users history, publish a subscription and return a `UserHistory` object', async () => { + userHistoryService.validateReqType('GQL'); + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.GQL, + executedOn: new Date(), + isStarred: false, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.GQL, + executedOn: new Date(), + isStarred: false, + }; + + await userHistoryService.publishUserHistorySubscription( + userHistory, + SubscriptionType.Created, + ); + + return expect( + await userHistoryService.addRequestToHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'GQL', + ), + ).toEqualRight(userHistory); + }); + test('Should resolve left when invalid ReqType is passed', async () => { + userHistoryService.validateReqType('INVALID'); + return expect( + await userHistoryService.addRequestToHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'INVALID', + ), + ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); + }); + }); + describe('starUnstarRequestInHistory', () => { + test('Should resolve right and star/unstar a request in the history', async () => { + mockPrisma.userHistory.findFirst.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + mockPrisma.userHistory.update.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: true, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: true, + }; + + await userHistoryService.publishUserHistorySubscription( + userHistory, + SubscriptionType.Updated, + ); + + return expect( + await userHistoryService.starUnstarRequestInHistory('abc', '1'), + ).toEqualRight(userHistory); + }); + test('Should resolve left and error out due to invalid request ID', async () => { + mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null); + + return expect( + await userHistoryService.starUnstarRequestInHistory('abc', '1'), + ).toEqualLeft(USER_HISTORY_NOT_FOUND); + }); + test('Should resolve left and error out due to invalid request ID', async () => { + mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null); + + return expect( + await userHistoryService.starUnstarRequestInHistory('abc', '1'), + ).toEqualLeft(USER_HISTORY_NOT_FOUND); + }); + }); + describe('removeRequestFromHistory', () => { + test('Should resolve right and delete request from users history, publish a subscription', async () => { + mockPrisma.userHistory.delete.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await userHistoryService.publishUserHistorySubscription( + userHistory, + SubscriptionType.Deleted, + ); + + return expect( + await userHistoryService.removeRequestFromHistory('abc', '1'), + ).toEqualRight(userHistory); + }); + test('Should resolve left and error out when req id is invalid ', async () => { + mockPrisma.userHistory.delete.mockResolvedValueOnce(null); + + return expect( + await userHistoryService.removeRequestFromHistory('abc', '1'), + ).toEqualLeft(USER_HISTORY_NOT_FOUND); + }); + }); + describe('deleteAllUserHistory', () => { + test('Should resolve right and delete all user REST history for a request type', async () => { + userHistoryService.validateReqType('REST'); + mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ + count: 2, + }); + + return expect( + await userHistoryService.deleteAllUserHistory('abc', 'REST'), + ).toEqualRight(2); + }); + test('Should resolve right and delete all user GQL history for a request type', async () => { + userHistoryService.validateReqType('GQL'); + mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ + count: 2, + }); + + return expect( + await userHistoryService.deleteAllUserHistory('abc', 'GQL'), + ).toEqualRight(2); + }); + test('Should resolve left and error when ReqType is invalid', async () => { + userHistoryService.validateReqType('INVALID'); + + return expect( + await userHistoryService.deleteAllUserHistory('abc', 'INVALID'), + ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); + }); + }); + describe('validateReqType', () => { + test('Should resolve right when a valid REST ReqType is provided', async () => { + return expect(userHistoryService.validateReqType('REST')).toEqualRight( + ReqType.REST, + ); + }); + test('Should resolve right when a valid GQL ReqType is provided', async () => { + return expect(userHistoryService.validateReqType('GQL')).toEqualRight( + ReqType.GQL, + ); + }); + test('Should resolve left and error out when a invalid ReqType is provided', async () => { + return expect(userHistoryService.validateReqType('INVALID')).toEqualLeft( + USER_HISTORY_INVALID_REQ_TYPE, + ); + }); + }); + describe('publishUserHistorySubscription', () => { + test('Should publish a created subscription', async () => { + const result: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await mockPubSub.publish( + `user_history/${result.userUid}/created`, + result, + ); + + return expect( + await userHistoryService.publishUserHistorySubscription( + result, + SubscriptionType.Created, + ), + ).toBeUndefined(); + }); + test('Should publish a updated subscription', async () => { + const result: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await mockPubSub.publish( + `user_history/${result.userUid}/updated`, + result, + ); + + return expect( + await userHistoryService.publishUserHistorySubscription( + result, + SubscriptionType.Updated, + ), + ).toBeUndefined(); + }); + test('Should publish a deleted subscription', async () => { + const result: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await mockPubSub.publish( + `user_history/${result.userUid}/deleted`, + result, + ); + + return expect( + await userHistoryService.publishUserHistorySubscription( + result, + SubscriptionType.Deleted, + ), + ).toBeUndefined(); + }); + }); +}); diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index 8fcdfbc11..6ff279279 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -3,6 +3,10 @@ import { PrismaService } from '../prisma/prisma.service'; import { PubSubService } from '../pubsub/pubsub.service'; import { ReqType, UserHistory } from './user-history.model'; import * as E from 'fp-ts/Either'; +import { + USER_HISTORY_INVALID_REQ_TYPE, + USER_HISTORY_NOT_FOUND, +} from '../errors'; // Contains constants for the subscription types we send to pubsub service enum SubscriptionType { @@ -19,7 +23,7 @@ export class UserHistoryService { ) {} /** - * Fetch users REST or GraphQL history based on ReqType argument. + * Fetch users REST or GraphQL history based on ReqType param. * @param uid Users uid * @param reqType request Type to fetch i.e. GraphQL or REST * @returns an array of user history @@ -41,6 +45,7 @@ export class UserHistoryService { request: JSON.stringify(history.request), responseMetadata: JSON.stringify(history.responseMetadata), isStarred: history.isStarred, + executedOn: history.executedOn, }); }); @@ -53,7 +58,7 @@ export class UserHistoryService { * @param reqData the request data * @param resMetadata the response metadata * @param reqType request Type to fetch i.e. GraphQL or REST - * @returns an array of user history + * @returns a `UserHistory` object */ async addRequestToHistory( uid: string, @@ -62,12 +67,14 @@ export class UserHistoryService { reqType: string, ) { const requestType = this.validateReqType(reqType); + if (E.isLeft(requestType)) return E.left(requestType.left); + const history = await this.prisma.userHistory.create({ data: { userUid: uid, request: JSON.parse(reqData), responseMetadata: JSON.parse(resMetadata), - type: requestType, + type: requestType.right, isStarred: false, }, }); @@ -87,7 +94,7 @@ export class UserHistoryService { SubscriptionType.Created, ); - return userHistory; + return E.right(userHistory); } /** @@ -104,7 +111,7 @@ export class UserHistoryService { }); if (userHistory == null) { - return E.left('history doesnt exist'); + return E.left(USER_HISTORY_NOT_FOUND); } try { const updatedHistory = await this.prisma.userHistory.update({ @@ -130,10 +137,9 @@ export class UserHistoryService { updatedUserHistory, SubscriptionType.Updated, ); - return E.right(updatedUserHistory); } catch (e) { - E.left('error updating'); + E.left(USER_HISTORY_NOT_FOUND); } } @@ -167,7 +173,7 @@ export class UserHistoryService { ); return E.right(deletedUserHistory); } catch (e) { - return E.left('error deleting history not found'); + return E.left(USER_HISTORY_NOT_FOUND); } } @@ -179,20 +185,22 @@ export class UserHistoryService { */ async deleteAllUserHistory(uid: string, reqType: string) { const requestType = this.validateReqType(reqType); - return await this.prisma.userHistory.deleteMany({ + if (E.isLeft(requestType)) return E.left(requestType.left); + + const deletedCount = await this.prisma.userHistory.deleteMany({ where: { userUid: uid, - type: requestType, + type: requestType.right, }, }); + return E.right(deletedCount.count); } // Method that takes a request type argument as string and validates against `ReqType` validateReqType(reqType: string) { - let requestType: ReqType; - return reqType == ReqType.REST - ? (requestType = ReqType.REST) - : (requestType = ReqType.GQL); + if (reqType == ReqType.REST) return E.right(ReqType.REST); + else if (reqType == ReqType.GQL) return E.right(ReqType.GQL); + return E.left(USER_HISTORY_INVALID_REQ_TYPE); } // Method to publish subscriptions based on the subscription type of the history @@ -203,19 +211,19 @@ export class UserHistoryService { switch (subscriptionType) { case SubscriptionType.Created: await this.pubsub.publish( - `user_history/${userHistory.id}/created`, + `user_history/${userHistory.userUid}/created`, userHistory, ); break; case SubscriptionType.Updated: await this.pubsub.publish( - `user_history/${userHistory.id}/updated`, + `user_history/${userHistory.userUid}/updated`, userHistory, ); break; case SubscriptionType.Deleted: await this.pubsub.publish( - `user_history/${userHistory.id}/deleted`, + `user_history/${userHistory.userUid}/deleted`, userHistory, ); break; From 1883be95d5995e417e5659ed19441c16ba9db3c6 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 21:06:49 +0530 Subject: [PATCH 16/46] chore: updated resolvers for user-history --- .../src/user-history/user-history.resolver.ts | 115 +++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts index a4ee893fb..68259c8c6 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts @@ -1,4 +1,4 @@ -import { Args, Mutation, Resolver } from '@nestjs/graphql'; +import { Args, ID, Mutation, Resolver, Subscription } from '@nestjs/graphql'; import { UserHistoryService } from './user-history.service'; import { PubSubService } from '../pubsub/pubsub.service'; import { UserHistory } from './user-history.model'; @@ -6,6 +6,8 @@ import { UseGuards } from '@nestjs/common'; import { GqlAuthGuard } from '../guards/gql-auth.guard'; import { GqlUser } from '../decorators/gql-user.decorator'; import { User } from '../user/user.model'; +import { throwErr } from '../utils'; +import * as E from 'fp-ts/Either'; @Resolver() export class UserHistoryResolver { @@ -38,11 +40,120 @@ export class UserHistoryResolver { }) reqType: string, ): Promise { - return await this.userHistoryService.addRequestToHistory( + const createdHistory = await this.userHistoryService.addRequestToHistory( user.uid, reqData, resMetadata, reqType, ); + if (E.isLeft(createdHistory)) throwErr(createdHistory.left); + return createdHistory.right; + } + + @Mutation(() => UserHistory, { + description: 'Stars/Unstars a REST/GQL request in user history', + }) + @UseGuards(GqlAuthGuard) + async starUnstarRequestInHistory( + @GqlUser() user: User, + @Args({ + name: 'id', + description: 'request id in history', + }) + id: string, + ): Promise { + const updatedHistory = + await this.userHistoryService.starUnstarRequestInHistory(user.uid, id); + if (E.isLeft(updatedHistory)) throwErr(updatedHistory.left); + return updatedHistory.right; + } + + @Mutation(() => UserHistory, { + description: 'Removes a REST/GQL request from user history', + }) + @UseGuards(GqlAuthGuard) + async removeRequestFromHistory( + @GqlUser() user: User, + @Args({ + name: 'id', + description: 'request id in history', + }) + id: string, + ): Promise { + const deletedHistory = + await this.userHistoryService.removeRequestFromHistory(user.uid, id); + if (E.isLeft(deletedHistory)) throwErr(deletedHistory.left); + return deletedHistory.right; + } + + @Mutation(() => Number, { + description: + 'Deletes all REST/GQL history for a user based on Request type', + }) + @UseGuards(GqlAuthGuard) + async deleteAllUserHistory( + @GqlUser() user: User, + @Args({ + name: 'reqType', + description: 'string that denotes type of request REST or GQL', + }) + reqType: string, + ): Promise { + const deletedHistory = await this.userHistoryService.deleteAllUserHistory( + user.uid, + reqType, + ); + if (E.isLeft(deletedHistory)) throwErr(deletedHistory.left); + return deletedHistory.right; + } + + /* Subscriptions */ + + @Subscription(() => UserHistory, { + description: 'Listen for User History Creation', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userHistoryCreated( + @Args({ + name: 'userUid', + description: 'user uid', + type: () => ID, + }) + userUid: string, + ) { + return this.pubsub.asyncIterator(`user_history/${userUid}/created`); + } + + @Subscription(() => UserHistory, { + description: 'Listen for User History update', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userHistoryUpdated( + @Args({ + name: 'userUid', + description: 'user uid', + type: () => ID, + }) + userUid: string, + ) { + return this.pubsub.asyncIterator(`user_history/${userUid}/updated`); + } + + @Subscription(() => UserHistory, { + description: 'Listen for User History deletion', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userHistoryDeleted( + @Args({ + name: 'userUid', + description: 'user uid', + type: () => ID, + }) + userUid: string, + ) { + return this.pubsub.asyncIterator(`user_history/${userUid}/deleted`); } } From 0c9aa2f68186e0262257474a9c7a470210f35dc3 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 21:08:23 +0530 Subject: [PATCH 17/46] chore: added error messages for a user history --- packages/hoppscotch-backend/src/errors.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 794a06db5..e856fdc01 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -155,6 +155,23 @@ export const TEAM_ENVIRONMMENT_NOT_FOUND = export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = 'team_environment/not_team_member' as const; +/* + +/** + * User history not found + * (UserHistoryService) + */ +export const USER_HISTORY_NOT_FOUND = 'user_history/history_not_found' as const; + +/* + +/** + * Invalid Request Type in History + * (UserHistoryService) + */ +export const USER_HISTORY_INVALID_REQ_TYPE = + 'user_history/req_type_invalid' as const; + /* |------------------------------------| From cd4750fcce29354d88c1caaac236b49a02162623 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Wed, 21 Dec 2022 11:45:19 +0530 Subject: [PATCH 18/46] fix: added missing return for star/unstar service method --- .../src/user-history/user-history.service.spec.ts | 7 ------- .../src/user-history/user-history.service.ts | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts index e387330c7..abb265cef 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts @@ -271,13 +271,6 @@ describe('UserHistoryService', () => { test('Should resolve left and error out due to invalid request ID', async () => { mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null); - return expect( - await userHistoryService.starUnstarRequestInHistory('abc', '1'), - ).toEqualLeft(USER_HISTORY_NOT_FOUND); - }); - test('Should resolve left and error out due to invalid request ID', async () => { - mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null); - return expect( await userHistoryService.starUnstarRequestInHistory('abc', '1'), ).toEqualLeft(USER_HISTORY_NOT_FOUND); diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index 6ff279279..dfe658c18 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -139,7 +139,7 @@ export class UserHistoryService { ); return E.right(updatedUserHistory); } catch (e) { - E.left(USER_HISTORY_NOT_FOUND); + return E.left(USER_HISTORY_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 19/46] 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 20/46] 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 21/46] 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 22/46] 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 23/46] 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 24/46] 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 25/46] 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 26/46] 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 523c650c9d4f3a6aa28e8d927e6db083a191fc50 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 13:13:19 +0530 Subject: [PATCH 27/46] chore: updated primsa type for executed on --- 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 2fc841fff..bf9b68236 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -94,7 +94,7 @@ model UserHistory { request Json responseMetadata Json isStarred Boolean - executedOn DateTime @default(now()) + executedOn DateTime @default(now()) @db.Timestamp(3) } enum ReqType { From 863e1ee113e9066001374a9a0c24f305d891176e Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 15:03:03 +0530 Subject: [PATCH 28/46] fix: fixed merge related conflicting data --- packages/hoppscotch-backend/prisma/schema.prisma | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 1ec28dc6e..25f51f433 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -101,11 +101,6 @@ model UserHistory { enum ReqType { REST GQL - uid String @id @default(cuid()) - displayName String? - email String? - photoURL String? - UserEnvironments UserEnvironment[] } model UserEnvironment { From 74e4a77ce663473d0c032673d4b1c5a593373976 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 15:04:05 +0530 Subject: [PATCH 29/46] chore: added topics for user history subscriptions --- packages/hoppscotch-backend/src/pubsub/topicsDefs.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts index 4a1fbcb27..642740577 100644 --- a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts +++ b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts @@ -1,4 +1,5 @@ import { UserEnvironment } from '../user-environment/user-environments.model'; +import { UserHistory } from '../user-history/user-history.model'; // A custom message type that defines the topic and the corresponding payload. // For every module that publishes a subscription add its type def and the possible subscription type. @@ -7,4 +8,8 @@ export type TopicDef = { topic: `user_environment/${string}/${'created' | 'updated' | 'deleted'}` ]: UserEnvironment; [topic: `user_environment/${string}/deleted_many`]: number; + [ + topic: `user_history/${string}/${'created' | 'updated' | 'deleted'}` + ]: UserHistory; + [topic: `user_history/${string}/deleted_many`]: number; }; From d812e6ab966ee84bd907768abc31c88232eeacc4 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 15:06:01 +0530 Subject: [PATCH 30/46] refactor: user history resolver changes --- .../src/user-history/user-history.resolver.ts | 46 +++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts index 68259c8c6..8612df1a3 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts @@ -1,4 +1,4 @@ -import { Args, ID, Mutation, Resolver, Subscription } from '@nestjs/graphql'; +import { Args, Mutation, Resolver, Subscription } from '@nestjs/graphql'; import { UserHistoryService } from './user-history.service'; import { PubSubService } from '../pubsub/pubsub.service'; import { UserHistory } from './user-history.model'; @@ -63,7 +63,7 @@ export class UserHistoryResolver { id: string, ): Promise { const updatedHistory = - await this.userHistoryService.starUnstarRequestInHistory(user.uid, id); + await this.userHistoryService.toggleHistoryStarStatus(user.uid, id); if (E.isLeft(updatedHistory)) throwErr(updatedHistory.left); return updatedHistory.right; } @@ -114,15 +114,8 @@ export class UserHistoryResolver { resolve: (value) => value, }) @UseGuards(GqlAuthGuard) - userHistoryCreated( - @Args({ - name: 'userUid', - description: 'user uid', - type: () => ID, - }) - userUid: string, - ) { - return this.pubsub.asyncIterator(`user_history/${userUid}/created`); + userHistoryCreated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_history/${user.uid}/created`); } @Subscription(() => UserHistory, { @@ -130,15 +123,8 @@ export class UserHistoryResolver { resolve: (value) => value, }) @UseGuards(GqlAuthGuard) - userHistoryUpdated( - @Args({ - name: 'userUid', - description: 'user uid', - type: () => ID, - }) - userUid: string, - ) { - return this.pubsub.asyncIterator(`user_history/${userUid}/updated`); + userHistoryUpdated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_history/${user.uid}/updated`); } @Subscription(() => UserHistory, { @@ -146,14 +132,16 @@ export class UserHistoryResolver { resolve: (value) => value, }) @UseGuards(GqlAuthGuard) - userHistoryDeleted( - @Args({ - name: 'userUid', - description: 'user uid', - type: () => ID, - }) - userUid: string, - ) { - return this.pubsub.asyncIterator(`user_history/${userUid}/deleted`); + userHistoryDeleted(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_history/${user.uid}/deleted`); + } + + @Subscription(() => Number, { + description: 'Listen for User History deleted many', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userHistoryDeletedMany(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_history/${user.uid}/deleted_many`); } } From 25b7ef3d2e8e88a3dee25bc3607d96351d2ad19b Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 15:23:02 +0530 Subject: [PATCH 31/46] refactor: updated pubsub publishing to leverage new topic defs and rewrote map logic --- .../src/user-history/user-history.service.ts | 89 +++++++------------ 1 file changed, 33 insertions(+), 56 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index dfe658c18..0b1543dbf 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -8,13 +8,6 @@ import { USER_HISTORY_NOT_FOUND, } from '../errors'; -// Contains constants for the subscription types we send to pubsub service -enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', -} - @Injectable() export class UserHistoryService { constructor( @@ -36,18 +29,18 @@ export class UserHistoryService { }, }); - const userHistoryColl: UserHistory[] = []; - userHistory.forEach((history) => { - userHistoryColl.push({ - id: history.id, - userUid: history.userUid, - reqType: history.type, - request: JSON.stringify(history.request), - responseMetadata: JSON.stringify(history.responseMetadata), - isStarred: history.isStarred, - executedOn: history.executedOn, - }); - }); + const userHistoryColl: UserHistory[] = userHistory.map( + (history) => + { + id: history.id, + userUid: history.userUid, + reqType: history.type, + request: JSON.stringify(history.request), + responseMetadata: JSON.stringify(history.responseMetadata), + isStarred: history.isStarred, + executedOn: history.executedOn, + }, + ); return userHistoryColl; } @@ -89,9 +82,10 @@ export class UserHistoryService { reqType: history.type, }; - await this.publishUserHistorySubscription( + // Publish created user history subscription + await this.pubsub.publish( + `user_history/${userHistory.userUid}/created`, userHistory, - SubscriptionType.Created, ); return E.right(userHistory); @@ -103,7 +97,7 @@ export class UserHistoryService { * @param id id of the request in the history * @returns an Either of updated `UserHistory` or Error */ - async starUnstarRequestInHistory(uid: string, id: string) { + async toggleHistoryStarStatus(uid: string, id: string) { const userHistory = await this.prisma.userHistory.findFirst({ where: { id: id, @@ -133,9 +127,10 @@ export class UserHistoryService { reqType: updatedHistory.type, }; - await this.publishUserHistorySubscription( + // Publish updated user history subscription + await this.pubsub.publish( + `user_history/${updatedUserHistory.userUid}/updated`, updatedUserHistory, - SubscriptionType.Updated, ); return E.right(updatedUserHistory); } catch (e) { @@ -167,9 +162,10 @@ export class UserHistoryService { reqType: delUserHistory.type, }; - await this.publishUserHistorySubscription( + // Publish deleted user history subscription + await this.pubsub.publish( + `user_history/${deletedUserHistory.userUid}/deleted`, deletedUserHistory, - SubscriptionType.Deleted, ); return E.right(deletedUserHistory); } catch (e) { @@ -193,42 +189,23 @@ export class UserHistoryService { type: requestType.right, }, }); + + // Publish multiple user history deleted subscription + await this.pubsub.publish( + `user_history/${uid}/deleted_many`, + deletedCount.count, + ); return E.right(deletedCount.count); } - // Method that takes a request type argument as string and validates against `ReqType` + /** + * Takes a request type argument as string and validates against `ReqType` + * @param reqType request type to be validated i.e. REST or GraphQL + * @returns an either of `ReqType` or error + */ validateReqType(reqType: string) { if (reqType == ReqType.REST) return E.right(ReqType.REST); else if (reqType == ReqType.GQL) return E.right(ReqType.GQL); return E.left(USER_HISTORY_INVALID_REQ_TYPE); } - - // Method to publish subscriptions based on the subscription type of the history - async publishUserHistorySubscription( - userHistory: UserHistory, - subscriptionType: SubscriptionType, - ) { - switch (subscriptionType) { - case SubscriptionType.Created: - await this.pubsub.publish( - `user_history/${userHistory.userUid}/created`, - userHistory, - ); - break; - case SubscriptionType.Updated: - await this.pubsub.publish( - `user_history/${userHistory.userUid}/updated`, - userHistory, - ); - break; - case SubscriptionType.Deleted: - await this.pubsub.publish( - `user_history/${userHistory.userUid}/deleted`, - userHistory, - ); - break; - default: - break; - } - } } From 626d703d779c13cc932402ef801ce1bae934d5c2 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 15:23:53 +0530 Subject: [PATCH 32/46] refactor: updated test cases to split subscription --- .../user-history/user-history.service.spec.ts | 268 +++++++++++------- 1 file changed, 164 insertions(+), 104 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts index abb265cef..5a69dba7c 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts @@ -18,12 +18,6 @@ const userHistoryService = new UserHistoryService( mockPubSub as any, ); -enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', -} - beforeEach(() => { mockReset(mockPrisma); mockPubSub.publish.mockClear(); @@ -143,7 +137,7 @@ describe('UserHistoryService', () => { }); }); describe('addRequestToHistory', () => { - test('Should resolve right and add a REST request to users history, publish a subscription and return a `UserHistory` object', async () => { + test('Should resolve right and add a REST request to users history and return a `UserHistory` object', async () => { userHistoryService.validateReqType('REST'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', @@ -165,11 +159,6 @@ describe('UserHistoryService', () => { isStarred: false, }; - await userHistoryService.publishUserHistorySubscription( - userHistory, - SubscriptionType.Created, - ); - return expect( await userHistoryService.addRequestToHistory( 'abc', @@ -179,7 +168,7 @@ describe('UserHistoryService', () => { ), ).toEqualRight(userHistory); }); - test('Should resolve right and add a GQL request to users history, publish a subscription and return a `UserHistory` object', async () => { + test('Should resolve right and add a GQL request to users history and return a `UserHistory` object', async () => { userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', @@ -201,11 +190,6 @@ describe('UserHistoryService', () => { isStarred: false, }; - await userHistoryService.publishUserHistorySubscription( - userHistory, - SubscriptionType.Created, - ); - return expect( await userHistoryService.addRequestToHistory( 'abc', @@ -226,8 +210,76 @@ describe('UserHistoryService', () => { ), ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); }); + test('Should add a GQL request to users history and publish a created subscription', async () => { + userHistoryService.validateReqType('GQL'); + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.GQL, + executedOn: new Date(), + isStarred: false, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.GQL, + executedOn: new Date(), + isStarred: false, + }; + + await userHistoryService.addRequestToHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'GQL', + ); + + return expect(await mockPubSub.publish).toHaveBeenCalledWith( + `user_history/${userHistory.userUid}/created`, + userHistory, + ); + }); + test('Should add a REST request to users history and publish a created subscription', async () => { + userHistoryService.validateReqType('REST'); + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await userHistoryService.addRequestToHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'REST', + ); + + return expect(await mockPubSub.publish).toHaveBeenCalledWith( + `user_history/${userHistory.userUid}/created`, + userHistory, + ); + }); }); - describe('starUnstarRequestInHistory', () => { + describe('toggleHistoryStarStatus', () => { test('Should resolve right and star/unstar a request in the history', async () => { mockPrisma.userHistory.findFirst.mockResolvedValueOnce({ userUid: 'abc', @@ -259,25 +311,57 @@ describe('UserHistoryService', () => { isStarred: true, }; - await userHistoryService.publishUserHistorySubscription( - userHistory, - SubscriptionType.Updated, - ); - return expect( - await userHistoryService.starUnstarRequestInHistory('abc', '1'), + await userHistoryService.toggleHistoryStarStatus('abc', '1'), ).toEqualRight(userHistory); }); - test('Should resolve left and error out due to invalid request ID', async () => { + test('Should resolve left and error out due to invalid user history request ID', async () => { mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null); return expect( - await userHistoryService.starUnstarRequestInHistory('abc', '1'), + await userHistoryService.toggleHistoryStarStatus('abc', '1'), ).toEqualLeft(USER_HISTORY_NOT_FOUND); }); + test('Should star/unstar a request in the history and publish a updated subscription', async () => { + mockPrisma.userHistory.findFirst.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + mockPrisma.userHistory.update.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: true, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: true, + }; + + await userHistoryService.toggleHistoryStarStatus('abc', '1'); + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_history/${userHistory.userUid}/updated`, + userHistory, + ); + }); }); describe('removeRequestFromHistory', () => { - test('Should resolve right and delete request from users history, publish a subscription', async () => { + test('Should resolve right and delete request from users history', async () => { mockPrisma.userHistory.delete.mockResolvedValueOnce({ userUid: 'abc', id: '1', @@ -298,11 +382,6 @@ describe('UserHistoryService', () => { isStarred: false, }; - await userHistoryService.publishUserHistorySubscription( - userHistory, - SubscriptionType.Deleted, - ); - return expect( await userHistoryService.removeRequestFromHistory('abc', '1'), ).toEqualRight(userHistory); @@ -314,6 +393,34 @@ describe('UserHistoryService', () => { await userHistoryService.removeRequestFromHistory('abc', '1'), ).toEqualLeft(USER_HISTORY_NOT_FOUND); }); + test('Should delete request from users history and publish deleted subscription', async () => { + mockPrisma.userHistory.delete.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + type: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + const userHistory: UserHistory = { + userUid: 'abc', + id: '1', + request: JSON.stringify([{}]), + responseMetadata: JSON.stringify([{}]), + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }; + + await userHistoryService.removeRequestFromHistory('abc', '1'); + + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_history/${userHistory.userUid}/deleted`, + userHistory, + ); + }); }); describe('deleteAllUserHistory', () => { test('Should resolve right and delete all user REST history for a request type', async () => { @@ -343,6 +450,30 @@ describe('UserHistoryService', () => { await userHistoryService.deleteAllUserHistory('abc', 'INVALID'), ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); }); + test('Should delete all user REST history for a request type and publish deleted many subscription', async () => { + userHistoryService.validateReqType('REST'); + mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ + count: 2, + }); + + await userHistoryService.deleteAllUserHistory('abc', 'REST'); + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_history/abc/deleted_many`, + 2, + ); + }); + test('Should delete all user GQL history for a request type and publish deleted many subscription', async () => { + userHistoryService.validateReqType('GQL'); + mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ + count: 2, + }); + + await userHistoryService.deleteAllUserHistory('abc', 'GQL'); + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_history/abc/deleted_many`, + 2, + ); + }); }); describe('validateReqType', () => { test('Should resolve right when a valid REST ReqType is provided', async () => { @@ -361,75 +492,4 @@ describe('UserHistoryService', () => { ); }); }); - describe('publishUserHistorySubscription', () => { - test('Should publish a created subscription', async () => { - const result: UserHistory = { - userUid: 'abc', - id: '1', - request: JSON.stringify([{}]), - responseMetadata: JSON.stringify([{}]), - reqType: ReqType.REST, - executedOn: new Date(), - isStarred: false, - }; - - await mockPubSub.publish( - `user_history/${result.userUid}/created`, - result, - ); - - return expect( - await userHistoryService.publishUserHistorySubscription( - result, - SubscriptionType.Created, - ), - ).toBeUndefined(); - }); - test('Should publish a updated subscription', async () => { - const result: UserHistory = { - userUid: 'abc', - id: '1', - request: JSON.stringify([{}]), - responseMetadata: JSON.stringify([{}]), - reqType: ReqType.REST, - executedOn: new Date(), - isStarred: false, - }; - - await mockPubSub.publish( - `user_history/${result.userUid}/updated`, - result, - ); - - return expect( - await userHistoryService.publishUserHistorySubscription( - result, - SubscriptionType.Updated, - ), - ).toBeUndefined(); - }); - test('Should publish a deleted subscription', async () => { - const result: UserHistory = { - userUid: 'abc', - id: '1', - request: JSON.stringify([{}]), - responseMetadata: JSON.stringify([{}]), - reqType: ReqType.REST, - executedOn: new Date(), - isStarred: false, - }; - - await mockPubSub.publish( - `user_history/${result.userUid}/deleted`, - result, - ); - - return expect( - await userHistoryService.publishUserHistorySubscription( - result, - SubscriptionType.Deleted, - ), - ).toBeUndefined(); - }); - }); }); From dcadbac4d5c2cc2465f2867529e09d173385f9fd Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 16:33:48 +0600 Subject: [PATCH 33/46] 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 34/46] 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 a5a14f6c76686e5b7eaafad683be8ad34ecef1ff Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 20:21:44 +0530 Subject: [PATCH 35/46] chore: applied review changes for resolvers --- .../src/user-history/user-history.resolver.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts index 8612df1a3..8b64e5af0 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts @@ -36,7 +36,7 @@ export class UserHistoryResolver { resMetadata: string, @Args({ name: 'reqType', - description: 'string that denotes type of request REST or GQL', + description: 'Request type, REST or GQL', }) reqType: string, ): Promise { @@ -54,11 +54,11 @@ export class UserHistoryResolver { description: 'Stars/Unstars a REST/GQL request in user history', }) @UseGuards(GqlAuthGuard) - async starUnstarRequestInHistory( + async toggleHistoryStarStatus( @GqlUser() user: User, @Args({ name: 'id', - description: 'request id in history', + description: 'ID of User History', }) id: string, ): Promise { @@ -76,7 +76,7 @@ export class UserHistoryResolver { @GqlUser() user: User, @Args({ name: 'id', - description: 'request id in history', + description: 'ID of User History', }) id: string, ): Promise { @@ -95,7 +95,7 @@ export class UserHistoryResolver { @GqlUser() user: User, @Args({ name: 'reqType', - description: 'string that denotes type of request REST or GQL', + description: 'Request type, REST or GQL', }) reqType: string, ): Promise { From 3cd9639f34930deffb06a915686e35101486161f Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 23 Jan 2023 21:32:00 +0600 Subject: [PATCH 36/46] 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 37/46] 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 38/46] 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 ebf236b387bd5e61ce2bfc9b1e30007eb917cabc Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 22:51:37 +0530 Subject: [PATCH 39/46] chore: added changes to creating a history in resolvers and service method --- .../src/user-history/user-history.resolver.ts | 4 ++-- .../user-history/user-history.service.spec.ts | 20 +++++++++---------- .../src/user-history/user-history.service.ts | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts index 8b64e5af0..65247c79f 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts @@ -22,7 +22,7 @@ export class UserHistoryResolver { description: 'Adds a new REST/GQL request to user history', }) @UseGuards(GqlAuthGuard) - async addRequestToHistory( + async createUserHistory( @GqlUser() user: User, @Args({ name: 'reqData', @@ -40,7 +40,7 @@ export class UserHistoryResolver { }) reqType: string, ): Promise { - const createdHistory = await this.userHistoryService.addRequestToHistory( + const createdHistory = await this.userHistoryService.createUserHistory( user.uid, reqData, resMetadata, diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts index 5a69dba7c..81eadca14 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts @@ -136,8 +136,8 @@ describe('UserHistoryService', () => { ).toEqual(userHistory); }); }); - describe('addRequestToHistory', () => { - test('Should resolve right and add a REST request to users history and return a `UserHistory` object', async () => { + describe('createUserHistory', () => { + test('Should resolve right and create a REST request to users history and return a `UserHistory` object', async () => { userHistoryService.validateReqType('REST'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', @@ -160,7 +160,7 @@ describe('UserHistoryService', () => { }; return expect( - await userHistoryService.addRequestToHistory( + await userHistoryService.createUserHistory( 'abc', JSON.stringify([{}]), JSON.stringify([{}]), @@ -168,7 +168,7 @@ describe('UserHistoryService', () => { ), ).toEqualRight(userHistory); }); - test('Should resolve right and add a GQL request to users history and return a `UserHistory` object', async () => { + test('Should resolve right and create a GQL request to users history and return a `UserHistory` object', async () => { userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', @@ -191,7 +191,7 @@ describe('UserHistoryService', () => { }; return expect( - await userHistoryService.addRequestToHistory( + await userHistoryService.createUserHistory( 'abc', JSON.stringify([{}]), JSON.stringify([{}]), @@ -202,7 +202,7 @@ describe('UserHistoryService', () => { test('Should resolve left when invalid ReqType is passed', async () => { userHistoryService.validateReqType('INVALID'); return expect( - await userHistoryService.addRequestToHistory( + await userHistoryService.createUserHistory( 'abc', JSON.stringify([{}]), JSON.stringify([{}]), @@ -210,7 +210,7 @@ describe('UserHistoryService', () => { ), ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); }); - test('Should add a GQL request to users history and publish a created subscription', async () => { + test('Should create a GQL request to users history and publish a created subscription', async () => { userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', @@ -232,7 +232,7 @@ describe('UserHistoryService', () => { isStarred: false, }; - await userHistoryService.addRequestToHistory( + await userHistoryService.createUserHistory( 'abc', JSON.stringify([{}]), JSON.stringify([{}]), @@ -244,7 +244,7 @@ describe('UserHistoryService', () => { userHistory, ); }); - test('Should add a REST request to users history and publish a created subscription', async () => { + test('Should create a REST request to users history and publish a created subscription', async () => { userHistoryService.validateReqType('REST'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', @@ -266,7 +266,7 @@ describe('UserHistoryService', () => { isStarred: false, }; - await userHistoryService.addRequestToHistory( + await userHistoryService.createUserHistory( 'abc', JSON.stringify([{}]), JSON.stringify([{}]), diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index 0b1543dbf..83c60698b 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -46,14 +46,14 @@ export class UserHistoryService { } /** - * Adds a request to users history. + * Creates a user history. * @param uid Users uid * @param reqData the request data * @param resMetadata the response metadata * @param reqType request Type to fetch i.e. GraphQL or REST * @returns a `UserHistory` object */ - async addRequestToHistory( + async createUserHistory( uid: string, reqData: string, resMetadata: string, @@ -92,7 +92,7 @@ export class UserHistoryService { } /** - * Stars or unstars a request in the history + * Toggles star status of a user history * @param uid Users uid * @param id id of the request in the history * @returns an Either of updated `UserHistory` or Error From 4023dcf09d0bf5290ac5119d5ebfff034316b7fa Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Tue, 24 Jan 2023 00:40:14 +0530 Subject: [PATCH 40/46] chore: changed the return data to use spread operator --- .../20230123184916_sdfv/migration.sql | 18 +++++++++++++ .../src/user-history/user-history.service.ts | 26 +++++-------------- 2 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql diff --git a/packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql new file mode 100644 index 000000000..6a044925a --- /dev/null +++ b/packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql @@ -0,0 +1,18 @@ +-- CreateEnum +CREATE TYPE "ReqType" AS ENUM ('REST', 'GQL'); + +-- CreateTable +CREATE TABLE "UserHistory" ( + "id" TEXT NOT NULL, + "userUid" TEXT NOT NULL, + "type" "ReqType" NOT NULL, + "request" JSONB NOT NULL, + "responseMetadata" JSONB NOT NULL, + "isStarred" BOOLEAN NOT NULL, + "executedOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "UserHistory_pkey" PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "UserHistory" ADD CONSTRAINT "UserHistory_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index 83c60698b..880c5b500 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -32,13 +32,10 @@ export class UserHistoryService { const userHistoryColl: UserHistory[] = userHistory.map( (history) => { - id: history.id, - userUid: history.userUid, + ...history, reqType: history.type, request: JSON.stringify(history.request), responseMetadata: JSON.stringify(history.responseMetadata), - isStarred: history.isStarred, - executedOn: history.executedOn, }, ); @@ -73,13 +70,10 @@ export class UserHistoryService { }); const userHistory = { - id: history.id, - userUid: history.userUid, + ...history, + reqType: history.type, request: JSON.stringify(history.request), responseMetadata: JSON.stringify(history.responseMetadata), - executedOn: history.executedOn, - isStarred: history.isStarred, - reqType: history.type, }; // Publish created user history subscription @@ -118,13 +112,10 @@ export class UserHistoryService { }); const updatedUserHistory = { - id: updatedHistory.id, - userUid: updatedHistory.userUid, + ...updatedHistory, + reqType: updatedHistory.type, request: JSON.stringify(updatedHistory.request), responseMetadata: JSON.stringify(updatedHistory.responseMetadata), - executedOn: updatedHistory.executedOn, - isStarred: updatedHistory.isStarred, - reqType: updatedHistory.type, }; // Publish updated user history subscription @@ -153,13 +144,10 @@ export class UserHistoryService { }); const deletedUserHistory = { - id: delUserHistory.id, - userUid: delUserHistory.userUid, + ...delUserHistory, + reqType: delUserHistory.type, request: JSON.stringify(delUserHistory.request), responseMetadata: JSON.stringify(delUserHistory.responseMetadata), - executedOn: delUserHistory.executedOn, - isStarred: delUserHistory.isStarred, - reqType: delUserHistory.type, }; // Publish deleted user history subscription From 73ace77305b37b83e8a91c092d18051d07b33473 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Tue, 24 Jan 2023 02:02:17 +0530 Subject: [PATCH 41/46] refactor: changed the test cases to reflect change to UserHistory schema --- .../20230123195828_dfgdbfgb/migration.sql | 10 +++++ .../hoppscotch-backend/prisma/schema.prisma | 2 +- .../user-history/user-history.service.spec.ts | 41 ++++++++----------- .../src/user-history/user-history.service.ts | 10 ++--- 4 files changed, 30 insertions(+), 33 deletions(-) create mode 100644 packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql diff --git a/packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql new file mode 100644 index 000000000..94710bca8 --- /dev/null +++ b/packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `type` on the `UserHistory` table. All the data in the column will be lost. + - Added the required column `reqType` to the `UserHistory` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "UserHistory" DROP COLUMN "type", +ADD COLUMN "reqType" "ReqType" NOT NULL; diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 25f51f433..ae8574a64 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -91,7 +91,7 @@ model UserHistory { id String @id @default(cuid()) userUid String user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) - type ReqType + reqType ReqType request Json responseMetadata Json isStarred Boolean diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts index 81eadca14..40e2343db 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts @@ -7,6 +7,7 @@ import { USER_HISTORY_INVALID_REQ_TYPE, USER_HISTORY_NOT_FOUND, } from '../errors'; +import { ReqType as DBReqType } from '@prisma/client'; const mockPrisma = mockDeep(); const mockPubSub = mockDeep(); @@ -33,7 +34,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: executedOn, isStarred: false, }, @@ -42,7 +43,7 @@ describe('UserHistoryService', () => { id: '2', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: executedOn, isStarred: true, }, @@ -68,6 +69,7 @@ describe('UserHistoryService', () => { isStarred: true, }, ]; + return expect( await userHistoryService.fetchUserHistory('abc', ReqType.REST), ).toEqual(userHistory); @@ -80,7 +82,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.GQL, + reqType: ReqType.GQL, executedOn: executedOn, isStarred: false, }, @@ -89,7 +91,7 @@ describe('UserHistoryService', () => { id: '2', request: [{}], responseMetadata: [{}], - type: ReqType.GQL, + reqType: ReqType.GQL, executedOn: executedOn, isStarred: true, }, @@ -138,13 +140,12 @@ describe('UserHistoryService', () => { }); describe('createUserHistory', () => { test('Should resolve right and create a REST request to users history and return a `UserHistory` object', async () => { - userHistoryService.validateReqType('REST'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: false, }); @@ -169,13 +170,12 @@ describe('UserHistoryService', () => { ).toEqualRight(userHistory); }); test('Should resolve right and create a GQL request to users history and return a `UserHistory` object', async () => { - userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.GQL, + reqType: ReqType.GQL, executedOn: new Date(), isStarred: false, }); @@ -200,7 +200,6 @@ describe('UserHistoryService', () => { ).toEqualRight(userHistory); }); test('Should resolve left when invalid ReqType is passed', async () => { - userHistoryService.validateReqType('INVALID'); return expect( await userHistoryService.createUserHistory( 'abc', @@ -211,13 +210,12 @@ describe('UserHistoryService', () => { ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); }); test('Should create a GQL request to users history and publish a created subscription', async () => { - userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.GQL, + reqType: ReqType.GQL, executedOn: new Date(), isStarred: false, }); @@ -245,13 +243,12 @@ describe('UserHistoryService', () => { ); }); test('Should create a REST request to users history and publish a created subscription', async () => { - userHistoryService.validateReqType('REST'); mockPrisma.userHistory.create.mockResolvedValueOnce({ userUid: 'abc', id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: false, }); @@ -286,7 +283,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: false, }); @@ -296,7 +293,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: true, }); @@ -328,7 +325,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: false, }); @@ -338,7 +335,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: true, }); @@ -367,7 +364,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: false, }); @@ -399,7 +396,7 @@ describe('UserHistoryService', () => { id: '1', request: [{}], responseMetadata: [{}], - type: ReqType.REST, + reqType: ReqType.REST, executedOn: new Date(), isStarred: false, }); @@ -424,7 +421,6 @@ describe('UserHistoryService', () => { }); describe('deleteAllUserHistory', () => { test('Should resolve right and delete all user REST history for a request type', async () => { - userHistoryService.validateReqType('REST'); mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ count: 2, }); @@ -434,7 +430,6 @@ describe('UserHistoryService', () => { ).toEqualRight(2); }); test('Should resolve right and delete all user GQL history for a request type', async () => { - userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ count: 2, }); @@ -444,14 +439,11 @@ describe('UserHistoryService', () => { ).toEqualRight(2); }); test('Should resolve left and error when ReqType is invalid', async () => { - userHistoryService.validateReqType('INVALID'); - return expect( await userHistoryService.deleteAllUserHistory('abc', 'INVALID'), ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); }); test('Should delete all user REST history for a request type and publish deleted many subscription', async () => { - userHistoryService.validateReqType('REST'); mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ count: 2, }); @@ -463,7 +455,6 @@ describe('UserHistoryService', () => { ); }); test('Should delete all user GQL history for a request type and publish deleted many subscription', async () => { - userHistoryService.validateReqType('GQL'); mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({ count: 2, }); diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index 880c5b500..7905d7154 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -25,7 +25,7 @@ export class UserHistoryService { const userHistory = await this.prisma.userHistory.findMany({ where: { userUid: uid, - type: reqType, + reqType: reqType, }, }); @@ -33,7 +33,6 @@ export class UserHistoryService { (history) => { ...history, - reqType: history.type, request: JSON.stringify(history.request), responseMetadata: JSON.stringify(history.responseMetadata), }, @@ -64,14 +63,13 @@ export class UserHistoryService { userUid: uid, request: JSON.parse(reqData), responseMetadata: JSON.parse(resMetadata), - type: requestType.right, + reqType: requestType.right, isStarred: false, }, }); const userHistory = { ...history, - reqType: history.type, request: JSON.stringify(history.request), responseMetadata: JSON.stringify(history.responseMetadata), }; @@ -113,7 +111,6 @@ export class UserHistoryService { const updatedUserHistory = { ...updatedHistory, - reqType: updatedHistory.type, request: JSON.stringify(updatedHistory.request), responseMetadata: JSON.stringify(updatedHistory.responseMetadata), }; @@ -145,7 +142,6 @@ export class UserHistoryService { const deletedUserHistory = { ...delUserHistory, - reqType: delUserHistory.type, request: JSON.stringify(delUserHistory.request), responseMetadata: JSON.stringify(delUserHistory.responseMetadata), }; @@ -174,7 +170,7 @@ export class UserHistoryService { const deletedCount = await this.prisma.userHistory.deleteMany({ where: { userUid: uid, - type: requestType.right, + reqType: requestType.right, }, }); From bc82e9c7fab839a8e9070acefdc55cd488cf0ca5 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 07:26:47 +0600 Subject: [PATCH 42/46] 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 43/46] 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 44/46] 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", From 97eedb568cf350638afd7e913a3f17cd906ffba7 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 24 Jan 2023 12:53:44 +0530 Subject: [PATCH 45/46] refactor: added fetchUserHistoryByID method and added changes for spread --- .../src/user-history/user-history.service.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.ts index 7905d7154..8ee8da421 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -3,6 +3,7 @@ import { PrismaService } from '../prisma/prisma.service'; import { PubSubService } from '../pubsub/pubsub.service'; import { ReqType, UserHistory } from './user-history.model'; import * as E from 'fp-ts/Either'; +import * as O from 'fp-ts/Option'; import { USER_HISTORY_INVALID_REQ_TYPE, USER_HISTORY_NOT_FOUND, @@ -70,6 +71,7 @@ export class UserHistoryService { const userHistory = { ...history, + reqType: history.reqType, request: JSON.stringify(history.request), responseMetadata: JSON.stringify(history.responseMetadata), }; @@ -90,22 +92,18 @@ export class UserHistoryService { * @returns an Either of updated `UserHistory` or Error */ async toggleHistoryStarStatus(uid: string, id: string) { - const userHistory = await this.prisma.userHistory.findFirst({ - where: { - id: id, - }, - }); - - if (userHistory == null) { + const userHistory = await this.fetchUserHistoryByID(id); + if (O.isNone(userHistory)) { return E.left(USER_HISTORY_NOT_FOUND); } + try { const updatedHistory = await this.prisma.userHistory.update({ where: { id: id, }, data: { - isStarred: !userHistory.isStarred, + isStarred: !userHistory.value.isStarred, }, }); @@ -182,6 +180,22 @@ export class UserHistoryService { return E.right(deletedCount.count); } + /** + * Fetch a user history based on history ID. + * @param id User History ID + * @returns an `UserHistory` object + */ + async fetchUserHistoryByID(id: string) { + const userHistory = await this.prisma.userHistory.findFirst({ + where: { + id: id, + }, + }); + if (userHistory == null) return O.none; + + return O.some(userHistory); + } + /** * Takes a request type argument as string and validates against `ReqType` * @param reqType request type to be validated i.e. REST or GraphQL From e78040a376f1b26b26cc2a7cf224754afce66c29 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 24 Jan 2023 13:00:34 +0530 Subject: [PATCH 46/46] refactor: removed migrations --- .../migration.sql | 129 ------------------ .../20230123184916_sdfv/migration.sql | 18 --- .../20230123195828_dfgdbfgb/migration.sql | 10 -- 3 files changed, 157 deletions(-) delete mode 100644 packages/hoppscotch-backend/prisma/migrations/20221213074249_create_user_environments/migration.sql delete mode 100644 packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql delete mode 100644 packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql diff --git a/packages/hoppscotch-backend/prisma/migrations/20221213074249_create_user_environments/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20221213074249_create_user_environments/migration.sql deleted file mode 100644 index 09af18a03..000000000 --- a/packages/hoppscotch-backend/prisma/migrations/20221213074249_create_user_environments/migration.sql +++ /dev/null @@ -1,129 +0,0 @@ --- CreateEnum -CREATE TYPE "TeamMemberRole" AS ENUM ('OWNER', 'VIEWER', 'EDITOR'); - --- CreateTable -CREATE TABLE "Team" ( - "id" TEXT NOT NULL, - "name" TEXT NOT NULL, - - CONSTRAINT "Team_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "TeamMember" ( - "id" TEXT NOT NULL, - "role" "TeamMemberRole" NOT NULL, - "userUid" TEXT NOT NULL, - "teamID" TEXT NOT NULL, - - CONSTRAINT "TeamMember_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "TeamInvitation" ( - "id" TEXT NOT NULL, - "teamID" TEXT NOT NULL, - "creatorUid" TEXT NOT NULL, - "inviteeEmail" TEXT NOT NULL, - "inviteeRole" "TeamMemberRole" NOT NULL, - - CONSTRAINT "TeamInvitation_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "TeamCollection" ( - "id" TEXT NOT NULL, - "parentID" TEXT, - "teamID" TEXT NOT NULL, - "title" TEXT NOT NULL, - - CONSTRAINT "TeamCollection_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "TeamRequest" ( - "id" TEXT NOT NULL, - "collectionID" TEXT NOT NULL, - "teamID" TEXT NOT NULL, - "title" TEXT NOT NULL, - "request" JSONB NOT NULL, - - CONSTRAINT "TeamRequest_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "Shortcode" ( - "id" TEXT NOT NULL, - "request" JSONB NOT NULL, - "creatorUid" TEXT, - "createdOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "Shortcode_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "TeamEnvironment" ( - "id" TEXT NOT NULL, - "teamID" TEXT NOT NULL, - "name" TEXT NOT NULL, - "variables" JSONB NOT NULL, - - CONSTRAINT "TeamEnvironment_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "User" ( - "uid" TEXT NOT NULL, - "displayName" TEXT, - "email" TEXT, - "photoURL" TEXT, - - CONSTRAINT "User_pkey" PRIMARY KEY ("uid") -); - --- CreateTable -CREATE TABLE "UserEnvironment" ( - "id" TEXT NOT NULL, - "userUid" TEXT NOT NULL, - "name" TEXT, - "variables" JSONB NOT NULL, - "isGlobal" BOOLEAN NOT NULL, - - CONSTRAINT "UserEnvironment_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "TeamMember_teamID_userUid_key" ON "TeamMember"("teamID", "userUid"); - --- CreateIndex -CREATE INDEX "TeamInvitation_teamID_idx" ON "TeamInvitation"("teamID"); - --- CreateIndex -CREATE UNIQUE INDEX "TeamInvitation_teamID_inviteeEmail_key" ON "TeamInvitation"("teamID", "inviteeEmail"); - --- CreateIndex -CREATE UNIQUE INDEX "Shortcode_id_creatorUid_key" ON "Shortcode"("id", "creatorUid"); - --- AddForeignKey -ALTER TABLE "TeamMember" ADD CONSTRAINT "TeamMember_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "TeamInvitation" ADD CONSTRAINT "TeamInvitation_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "TeamCollection" ADD CONSTRAINT "TeamCollection_parentID_fkey" FOREIGN KEY ("parentID") REFERENCES "TeamCollection"("id") ON DELETE SET NULL ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "TeamCollection" ADD CONSTRAINT "TeamCollection_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "TeamRequest" ADD CONSTRAINT "TeamRequest_collectionID_fkey" FOREIGN KEY ("collectionID") REFERENCES "TeamCollection"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "TeamRequest" ADD CONSTRAINT "TeamRequest_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "TeamEnvironment" ADD CONSTRAINT "TeamEnvironment_teamID_fkey" FOREIGN KEY ("teamID") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "UserEnvironment" ADD CONSTRAINT "UserEnvironment_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql deleted file mode 100644 index 6a044925a..000000000 --- a/packages/hoppscotch-backend/prisma/migrations/20230123184916_sdfv/migration.sql +++ /dev/null @@ -1,18 +0,0 @@ --- CreateEnum -CREATE TYPE "ReqType" AS ENUM ('REST', 'GQL'); - --- CreateTable -CREATE TABLE "UserHistory" ( - "id" TEXT NOT NULL, - "userUid" TEXT NOT NULL, - "type" "ReqType" NOT NULL, - "request" JSONB NOT NULL, - "responseMetadata" JSONB NOT NULL, - "isStarred" BOOLEAN NOT NULL, - "executedOn" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "UserHistory_pkey" PRIMARY KEY ("id") -); - --- AddForeignKey -ALTER TABLE "UserHistory" ADD CONSTRAINT "UserHistory_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql b/packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql deleted file mode 100644 index 94710bca8..000000000 --- a/packages/hoppscotch-backend/prisma/migrations/20230123195828_dfgdbfgb/migration.sql +++ /dev/null @@ -1,10 +0,0 @@ -/* - Warnings: - - - You are about to drop the column `type` on the `UserHistory` table. All the data in the column will be lost. - - Added the required column `reqType` to the `UserHistory` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable -ALTER TABLE "UserHistory" DROP COLUMN "type", -ADD COLUMN "reqType" "ReqType" NOT NULL;