From b4b63f86d9c4436b65651a6389b4539b29bab679 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 13 Dec 2022 13:14:13 +0530 Subject: [PATCH 01/95] feat: added user environment prisma schema --- .../migration.sql | 129 ++++++++++++++++++ .../prisma/migrations/migration_lock.toml | 3 + .../hoppscotch-backend/prisma/schema.prisma | 10 ++ 3 files changed, 142 insertions(+) create mode 100644 packages/hoppscotch-backend/prisma/migrations/20221213074249_create_user_environments/migration.sql create mode 100644 packages/hoppscotch-backend/prisma/migrations/migration_lock.toml 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 new file mode 100644 index 000000000..09af18a03 --- /dev/null +++ b/packages/hoppscotch-backend/prisma/migrations/20221213074249_create_user_environments/migration.sql @@ -0,0 +1,129 @@ +-- 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/migration_lock.toml b/packages/hoppscotch-backend/prisma/migrations/migration_lock.toml new file mode 100644 index 000000000..fbffa92c2 --- /dev/null +++ b/packages/hoppscotch-backend/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 947d158a8..0d6e7c56d 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -83,6 +83,16 @@ model User { displayName String? email String? photoURL String? + UserEnvironments UserEnvironment[] +} + +model UserEnvironment { + id String @id @default(cuid()) + userUid String + user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) + name String? + variables Json + isGlobal Boolean } enum TeamMemberRole { From ce94255a9edd41fe7b119217207775eec9ae9c82 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 13 Dec 2022 13:27:51 +0530 Subject: [PATCH 02/95] feat: added user environment user environments resolvers, service files --- .../user-environments.model.ts | 30 ++ .../user-environments.module.ts | 18 ++ .../user-environments.resolver.ts | 187 +++++++++++++ .../user-environments.service.ts | 264 ++++++++++++++++++ .../src/user-environment/user.resolver.ts | 27 ++ 5 files changed, 526 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user-environment/user-environments.model.ts create mode 100644 packages/hoppscotch-backend/src/user-environment/user-environments.module.ts create mode 100644 packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts create mode 100644 packages/hoppscotch-backend/src/user-environment/user-environments.service.ts create mode 100644 packages/hoppscotch-backend/src/user-environment/user.resolver.ts diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts new file mode 100644 index 000000000..552299f60 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts @@ -0,0 +1,30 @@ +import { Field, ID, ObjectType } from '@nestjs/graphql'; + +@ObjectType() +export class UserEnvironment { + @Field(() => ID, { + description: 'ID of the User Environment', + }) + id: string; + + @Field(() => ID, { + description: 'ID of the user this environment belongs to', + }) + userUid: string; + + @Field(() => String, { + nullable: true, + description: 'Name of the environment', + }) + name: string | null | undefined; + + @Field({ + description: 'All variables present in the environment', + }) + variables: string; // JSON string of the variables object (format:[{ key: "bla", value: "bla_val" }, ...] ) which will be received from the client + + @Field({ + description: 'isGlobal flag to indicate the environment is global or not', + }) + isGlobal: boolean; +} diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts new file mode 100644 index 000000000..04f391682 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts @@ -0,0 +1,18 @@ +import { Module } from '@nestjs/common'; +import { PrismaModule } from '../prisma/prisma.module'; +import { PubSubModule } from '../pubsub/pubsub.module'; +import { UserModule } from '../user/user.module'; +import { UserEnvsUserResolver } from './user.resolver'; +import { UserEnvironmentsResolver } from './user-environments.resolver'; +import { UserEnvironmentsService } from './user-environments.service'; + +@Module({ + imports: [PrismaModule, PubSubModule, UserModule], + providers: [ + UserEnvironmentsResolver, + UserEnvironmentsService, + UserEnvsUserResolver, + ], + exports: [UserEnvironmentsService], +}) +export class UserEnvironmentsModule {} diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts new file mode 100644 index 000000000..bf1383f7d --- /dev/null +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -0,0 +1,187 @@ +import { Args, ID, Mutation, Resolver, Subscription } from '@nestjs/graphql'; +import { PubSubService } from '../pubsub/pubsub.service'; +import { UserEnvironment } from './user-environments.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'; +import { UserEnvironmentsService } from './user-environments.service'; +import * as E from 'fp-ts/Either'; +import { throwErr } from 'src/utils'; + +@Resolver() +export class UserEnvironmentsResolver { + constructor( + private readonly userEnvironmentsService: UserEnvironmentsService, + private readonly pubsub: PubSubService, + ) {} + + /* Mutations */ + + @Mutation(() => UserEnvironment, { + description: + 'Create a new personal or global user environment for given user uid', + }) + @UseGuards(GqlAuthGuard) + async createUserEnvironment( + @GqlUser() user: User, + @Args({ + name: 'name', + description: + 'Name of the User Environment, if global send an empty string', + }) + name: string, + @Args({ + name: 'variables', + description: 'JSON string of the variables object', + }) + variables: string, + @Args({ + name: 'isGlobal', + description: 'isGlobal flag to indicate personal or global environment', + }) + isGlobal: boolean, + ): Promise { + const userEnvironment = + await this.userEnvironmentsService.createUserEnvironment( + user.uid, + name, + variables, + isGlobal, + ); + if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); + return userEnvironment.right; + } + + @Mutation(() => UserEnvironment, { + description: + 'Update a users personal or global environment based on environment id', + }) + @UseGuards(GqlAuthGuard) + async updateUserEnvironment( + @Args({ + name: 'id', + description: 'ID of the user environment', + type: () => ID, + }) + id: string, + @Args({ + name: 'name', + description: + 'Name of the User Environment, if global send an empty string', + }) + name: string, + @Args({ + name: 'variables', + description: 'JSON string of the variables object', + }) + variables: string, + ): Promise { + const userEnvironment = + await this.userEnvironmentsService.updateUserEnvironment( + id, + name, + variables, + ); + if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); + return userEnvironment.right; + } + + @Mutation(() => UserEnvironment, { + description: 'Deletes a users personal environment based on environment id', + }) + @UseGuards(GqlAuthGuard) + async deleteUserEnvironment( + @Args({ + name: 'id', + description: 'ID of the user environment', + type: () => ID, + }) + id: string, + ): Promise { + const userEnvironment = + await this.userEnvironmentsService.deleteUserEnvironment(id); + if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); + return userEnvironment.right; + } + + @Mutation(() => Number, { + description: 'Deletes users all personal environments', + }) + @UseGuards(GqlAuthGuard) + async deleteUserEnvironments(@GqlUser() user: User): Promise { + return await this.userEnvironmentsService.deleteUserEnvironments(user.uid); + } + + @Mutation(() => UserEnvironment, { + description: 'Deletes all variables inside a users global environment', + }) + @UseGuards(GqlAuthGuard) + async deleteAllVariablesFromUsersGlobalEnvironment( + @GqlUser() user: User, + @Args({ + name: 'id', + description: 'ID of the users global environment', + type: () => ID, + }) + id: string, + ): Promise { + const userEnvironment = + await this.userEnvironmentsService.deleteAllVariablesFromUsersGlobalEnvironment( + user.uid, + id, + ); + if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); + return userEnvironment.right; + } + + /* Subscriptions */ + + @Subscription(() => UserEnvironment, { + description: 'Listen for User Environment Creation', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userEnvironmentCreated( + @Args({ + name: 'userUid', + description: 'users uid', + type: () => ID, + }) + userUid: string, + ) { + return this.pubsub.asyncIterator(`user_environment/${userUid}/created`); + } + + @Subscription(() => UserEnvironment, { + description: 'Listen for User Environment updates', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userEnvironmentUpdated( + @Args({ + name: 'id', + description: 'environment id', + type: () => ID, + }) + id: string, + ) { + return this.pubsub.asyncIterator(`user_environment/${id}/updated`); + } + + @Subscription(() => UserEnvironment, { + description: 'Listen for User Environment updates', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userEnvironmentDeleted( + @Args({ + name: 'id', + description: 'environment id', + type: () => ID, + }) + id: string, + ) { + return this.pubsub.asyncIterator(`user_environment/${id}/deleted`); + } +} diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts new file mode 100644 index 000000000..fa322602e --- /dev/null +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -0,0 +1,264 @@ +import { Injectable } from '@nestjs/common'; + +import { UserEnvironment } from './user-environments.model'; +import { PrismaService } from '../prisma/prisma.service'; +import { PubSubService } from '../pubsub/pubsub.service'; +import * as E from 'fp-ts/Either'; + +enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', +} + +@Injectable() +export class UserEnvironmentsService { + constructor( + private readonly prisma: PrismaService, + private readonly pubsub: PubSubService, + ) {} + + /** + * Fetch personal and global user environments based on `isGlobal` flag + * @param uid Users uid + * @returns array of users personal and global environments + */ + async fetchUserEnvironments(uid: string) { + const environments = await this.prisma.userEnvironment.findMany({ + where: { + userUid: uid, + isGlobal: false, + }, + }); + + const userEnvironments: UserEnvironment[] = []; + environments.forEach((environment) => { + userEnvironments.push({ + userUid: environment.userUid, + id: environment.id, + name: environment.name, + variables: JSON.stringify(environment.variables), + isGlobal: environment.isGlobal, + }); + }); + return userEnvironments; + } + + /** + * Create a personal or global user environment + * @param uid Users uid + * @param name environments name + * @param variables environment variables + * @param isGlobal flag to indicate type of environment to create + * @returns an `UserEnvironment` object + */ + async createUserEnvironment( + uid: string, + name: string, + variables: string, + isGlobal: boolean, + ) { + if (isGlobal) { + const globalEnvExists = await this.checkForExistingGlobalEnv(uid); + if (E.isRight(globalEnvExists)) return E.left('global env exits'); + } + + const createdEnvironment = await this.prisma.userEnvironment.create({ + data: { + userUid: uid, + name: name, + variables: JSON.parse(variables), + isGlobal: isGlobal, + }, + }); + + const userEnvironment: UserEnvironment = { + userUid: createdEnvironment.userUid, + id: createdEnvironment.id, + name: createdEnvironment.name, + variables: JSON.stringify(createdEnvironment.variables), + isGlobal: createdEnvironment.isGlobal, + }; + // Publish subscription for environment creation + await this.publishUserEnvironmentCreatedSubscription( + userEnvironment, + SubscriptionType.Created, + ); + + return E.right(userEnvironment); + } + + /** + * Update an existing personal or global user environment + * @param id environment id + * @param name environments name + * @param variables environment variables + * @returns an Either of `UserEnvironment` or error + */ + async updateUserEnvironment(id: string, name: string, variables: string) { + try { + const updatedEnvironment = await this.prisma.userEnvironment.update({ + where: { id: id }, + data: { + name: name, + variables: JSON.parse(variables), + }, + }); + + const updatedUserEnvironment: UserEnvironment = { + userUid: updatedEnvironment.userUid, + id: updatedEnvironment.id, + name: updatedEnvironment.name, + variables: JSON.stringify(updatedEnvironment.variables), + isGlobal: updatedEnvironment.isGlobal, + }; + // Publish subscription for environment creation + await this.publishUserEnvironmentCreatedSubscription( + updatedUserEnvironment, + SubscriptionType.Updated, + ); + return E.right(updatedUserEnvironment); + } catch (e) { + return E.left('user_env not found'); + } + } + + /** + * Delete an existing personal user environment based on environment id + * @param id environment id + * @returns an Either of deleted `UserEnvironment` or error + */ + async deleteUserEnvironment(id: string) { + try { + const deletedEnvironment = await this.prisma.userEnvironment.delete({ + where: { + id: id, + }, + }); + + const deletedUserEnvironment: UserEnvironment = { + userUid: deletedEnvironment.userUid, + id: deletedEnvironment.id, + name: deletedEnvironment.name, + variables: JSON.stringify(deletedEnvironment.variables), + isGlobal: deletedEnvironment.isGlobal, + }; + // Publish subscription for environment creation + await this.publishUserEnvironmentCreatedSubscription( + deletedUserEnvironment, + SubscriptionType.Deleted, + ); + return E.right(deletedUserEnvironment); + } catch (e) { + return E.left('user_env not found'); + } + } + + /** + * Deletes all existing personal user environments + * @param id environment id + * @param isGlobal flag to indicate type of environment to delete + * @returns a count of environments deleted + */ + async deleteUserEnvironments(uid: string) { + const deletedEnvironments = await this.prisma.userEnvironment.deleteMany({ + where: { + userUid: uid, + isGlobal: false, + }, + }); + return deletedEnvironments.count; + } + + async deleteAllVariablesFromUsersGlobalEnvironment(uid: string, id: string) { + const globalEnvExists = await this.checkForExistingGlobalEnv(uid); + if (E.isRight(globalEnvExists) && !E.isLeft(globalEnvExists)) { + const env = globalEnvExists.right; + if (env.id === id) { + try { + const updatedEnvironment = await this.prisma.userEnvironment.update({ + where: { id: id }, + data: { + variables: [], + }, + }); + const updatedUserEnvironment: UserEnvironment = { + userUid: updatedEnvironment.userUid, + id: updatedEnvironment.id, + name: updatedEnvironment.name, + variables: JSON.stringify(updatedEnvironment.variables), + isGlobal: updatedEnvironment.isGlobal, + }; + // Publish subscription for environment creation + await this.publishUserEnvironmentCreatedSubscription( + updatedUserEnvironment, + SubscriptionType.Updated, + ); + return E.right(updatedUserEnvironment); + } catch (e) { + return E.left('user_env not found'); + } + } + } + return E.left('mismatch'); + } + + // Method to publish subscriptions based on the subscription type of the environment + async publishUserEnvironmentCreatedSubscription( + userEnv: UserEnvironment, + subscriptionType: SubscriptionType, + ) { + switch (subscriptionType) { + case SubscriptionType.Created: + await this.pubsub.publish( + `user_environment/${userEnv.userUid}/created`, + userEnv, + ); + break; + case SubscriptionType.Updated: + await this.pubsub.publish( + `user_environment/${userEnv.id}/updated`, + userEnv, + ); + break; + case SubscriptionType.Deleted: + await this.pubsub.publish( + `user_environment/${userEnv.id}/deleted`, + userEnv, + ); + break; + default: + break; + } + } + + private async checkForExistingGlobalEnv(uid: string) { + const globalEnv = await this.prisma.userEnvironment.findFirst({ + where: { + userUid: uid, + isGlobal: true, + }, + }); + if (globalEnv === null) return E.left('global env not exist'); + + return E.right(globalEnv); + } + + async fetchUserGlobalEnvironments(uid: string) { + const globalEnvironment = await this.prisma.userEnvironment.findFirst({ + where: { + userUid: uid, + isGlobal: true, + }, + rejectOnNotFound: true, + }); + + return { + userUid: globalEnvironment.userUid, + id: globalEnvironment.id, + name: globalEnvironment.name, + variables: JSON.stringify(globalEnvironment.variables), + isGlobal: globalEnvironment.isGlobal, + }; + } +} diff --git a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts new file mode 100644 index 000000000..97acdac2c --- /dev/null +++ b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts @@ -0,0 +1,27 @@ +import { Parent, ResolveField, Resolver } from '@nestjs/graphql'; +import { User } from 'src/user/user.model'; +import { UserEnvironment } from './user-environments.model'; +import { UserEnvironmentsService } from './user-environments.service'; + +@Resolver(() => User) +export class UserEnvsUserResolver { + constructor(private userEnvironmentsService: UserEnvironmentsService) {} + @ResolveField(() => [UserEnvironment], { + description: 'Returns a list of users personal environments', + }) + async environments(@Parent() user: User): Promise { + return await this.userEnvironmentsService.fetchUserEnvironments(user.uid); + } + + @ResolveField(() => UserEnvironment, { + description: + 'Returns a list of user variables inside a global environments', + }) + async globalEnvironments( + @Parent() user: User, + ): Promise { + return await this.userEnvironmentsService.fetchUserGlobalEnvironments( + user.uid, + ); + } +} From 8e038f6944e42b0c4b2cb6740d14b637dcdabcb1 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 13 Dec 2022 16:10:58 +0530 Subject: [PATCH 03/95] feat: added user environments to app module --- packages/hoppscotch-backend/docker-compose.yml | 6 +++--- packages/hoppscotch-backend/src/app.module.ts | 2 ++ packages/hoppscotch-backend/src/prisma/prisma.service.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/hoppscotch-backend/docker-compose.yml b/packages/hoppscotch-backend/docker-compose.yml index 9b22e6f75..72bd1e8ac 100644 --- a/packages/hoppscotch-backend/docker-compose.yml +++ b/packages/hoppscotch-backend/docker-compose.yml @@ -6,9 +6,9 @@ services: - PRODUCTION=false - DATABASE_URL=postgresql://postgres:testpass@dev-db:5432/hoppscotch?connect_timeout=300 - PORT=3000 - volumes: - - .:/usr/src/app - - /usr/src/app/node_modules/ +# volumes: +# - .:/usr/src/app +# - /usr/src/app/node_modules/ depends_on: - dev-db ports: diff --git a/packages/hoppscotch-backend/src/app.module.ts b/packages/hoppscotch-backend/src/app.module.ts index 7614320b9..b9281e55d 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 { UserEnvironmentsModule } from './user-environment/user-environments.module'; @Module({ imports: [ @@ -44,6 +45,7 @@ import { GQLComplexityPlugin } from './plugins/GQLComplexityPlugin'; driver: ApolloDriver, }), UserModule, + UserEnvironmentsModule, ], providers: [GQLComplexityPlugin], }) diff --git a/packages/hoppscotch-backend/src/prisma/prisma.service.ts b/packages/hoppscotch-backend/src/prisma/prisma.service.ts index 5b962c430..70d38eebb 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/prisma-client/scripts/default-index'; @Injectable() export class PrismaService From 08cc7114ac421584c20568fc64818d0f6818ae7a Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:21:21 +0530 Subject: [PATCH 04/95] chore: minor changes to Dockerfile --- packages/hoppscotch-backend/Dockerfile | 4 ++-- packages/hoppscotch-backend/docker-compose.yml | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/Dockerfile b/packages/hoppscotch-backend/Dockerfile index ab0c88f42..ed9736551 100644 --- a/packages/hoppscotch-backend/Dockerfile +++ b/packages/hoppscotch-backend/Dockerfile @@ -6,10 +6,10 @@ WORKDIR /usr/src/app RUN npm i -g pnpm # Prisma bits -COPY prisma ./ +COPY prisma ./prisma/ RUN pnpx prisma generate -# # NPM package install +# # PNPM package install COPY . . RUN pnpm i diff --git a/packages/hoppscotch-backend/docker-compose.yml b/packages/hoppscotch-backend/docker-compose.yml index 72bd1e8ac..9b22e6f75 100644 --- a/packages/hoppscotch-backend/docker-compose.yml +++ b/packages/hoppscotch-backend/docker-compose.yml @@ -6,9 +6,9 @@ services: - PRODUCTION=false - DATABASE_URL=postgresql://postgres:testpass@dev-db:5432/hoppscotch?connect_timeout=300 - PORT=3000 -# volumes: -# - .:/usr/src/app -# - /usr/src/app/node_modules/ + volumes: + - .:/usr/src/app + - /usr/src/app/node_modules/ depends_on: - dev-db ports: From 3392b1a1caab6701282d17650470db3d445a903e Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:21:57 +0530 Subject: [PATCH 05/95] chore: minor changes to Dockerfilefor prisma service --- 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 70d38eebb..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/prisma-client/scripts/default-index'; +import { PrismaClient } from '@prisma/client'; @Injectable() export class PrismaService From 73532e41c5876c2951900bfaa76b0cc6db48193e Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:23:13 +0530 Subject: [PATCH 06/95] chore: added jest and jest related setup files to support jest fp-ts --- packages/hoppscotch-backend/global.d.ts | 1 + packages/hoppscotch-backend/jest.setup.js | 1 + packages/hoppscotch-backend/package.json | 12 +++++++++--- packages/hoppscotch-backend/tsconfig.json | 3 ++- 4 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 packages/hoppscotch-backend/global.d.ts create mode 100644 packages/hoppscotch-backend/jest.setup.js diff --git a/packages/hoppscotch-backend/global.d.ts b/packages/hoppscotch-backend/global.d.ts new file mode 100644 index 000000000..78769ebc0 --- /dev/null +++ b/packages/hoppscotch-backend/global.d.ts @@ -0,0 +1 @@ +import '@relmify/jest-fp-ts'; diff --git a/packages/hoppscotch-backend/jest.setup.js b/packages/hoppscotch-backend/jest.setup.js new file mode 100644 index 000000000..562234114 --- /dev/null +++ b/packages/hoppscotch-backend/jest.setup.js @@ -0,0 +1 @@ +require('@relmify/jest-fp-ts'); \ No newline at end of file diff --git a/packages/hoppscotch-backend/package.json b/packages/hoppscotch-backend/package.json index 6e3a31005..4f67b07a8 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,7 @@ "json", "ts" ], + "setupFilesAfterEnv": ["@relmify/jest-fp-ts"], "rootDir": "src", "testRegex": ".*\\.spec\\.ts$", "transform": { diff --git a/packages/hoppscotch-backend/tsconfig.json b/packages/hoppscotch-backend/tsconfig.json index adb614cab..22cc39ab4 100644 --- a/packages/hoppscotch-backend/tsconfig.json +++ b/packages/hoppscotch-backend/tsconfig.json @@ -12,10 +12,11 @@ "baseUrl": "./", "incremental": true, "skipLibCheck": true, + "strict": false, "strictNullChecks": false, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, } } From 164c2463f556e13b9d7842fdf208ee158cca5b8e Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:24:09 +0530 Subject: [PATCH 07/95] chore: added error messages for user environment related errors --- packages/hoppscotch-backend/src/errors.ts | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 794a06db5..62664d3f7 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -155,6 +155,52 @@ export const TEAM_ENVIRONMMENT_NOT_FOUND = export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = 'team_environment/not_team_member' as const; +/** + * Global environment doesnt exists for the user + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS = + 'user_environment/global_env_doesnt_exists' as const; +/* + +/** + * Global environment already exists for the user + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_GLOBAL_ENV_EXISTS = + 'user_environment/global_env_already_exists' as const; +/* + +/** + * User environment doesn't exist for the user + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_ENV_DOESNT_EXISTS = + 'user_environment/user_env_doesnt_exists' as const; +/* + +/** + * Cannot delete the global user environment + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED = + 'user_environment/user_env_global_env_deletion_failed' as const; +/* + +/** + * User environment is not a global environment + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_IS_NOT_GLOBAL = + 'user_environment/user_env_is_not_global' as const; +/* + +/** + * User environment update failed + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_UPDATE_FAILED = + 'user_environment/user_env_update_failed' as const; /* |------------------------------------| From 6bd4fd91ffdcb8542a16bad122dfef2fcce07779 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:25:21 +0530 Subject: [PATCH 08/95] chore: updated user resolver with updated service methods --- .../src/user-environment/user.resolver.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts index 97acdac2c..3dbfc949b 100644 --- a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts @@ -2,6 +2,8 @@ import { Parent, ResolveField, Resolver } from '@nestjs/graphql'; import { User } from 'src/user/user.model'; import { UserEnvironment } from './user-environments.model'; import { UserEnvironmentsService } from './user-environments.service'; +import * as E from 'fp-ts/Either'; +import { throwErr } from '../utils'; @Resolver(() => User) export class UserEnvsUserResolver { @@ -20,8 +22,9 @@ export class UserEnvsUserResolver { async globalEnvironments( @Parent() user: User, ): Promise { - return await this.userEnvironmentsService.fetchUserGlobalEnvironments( - user.uid, - ); + const userEnvironment = + await this.userEnvironmentsService.fetchUserGlobalEnvironment(user.uid); + if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); + return userEnvironment.right; } } From 9fb9fd4568172cdf10f1395376933e4a52e5de87 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:29:51 +0530 Subject: [PATCH 09/95] chore: added test files for user environment service --- .../user-environments.service.spec.ts | 515 ++++++++++++++++++ 1 file changed, 515 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts new file mode 100644 index 000000000..ca5078005 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts @@ -0,0 +1,515 @@ +import { UserEnvironment } from './user-environments.model'; +import { mockDeep, mockReset } from 'jest-mock-extended'; +import { PrismaService } from '../prisma/prisma.service'; +import { UserEnvironmentsService } from './user-environments.service'; +import { + USER_ENVIRONMENT_ENV_DOESNT_EXISTS, + USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED, + USER_ENVIRONMENT_GLOBAL_ENV_EXISTS, +} from '../errors'; +import { PubSubService } from '../pubsub/pubsub.service'; + +const mockPrisma = mockDeep(); +const mockPubSub = mockDeep(); + +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +const userEnvironmentsService = new UserEnvironmentsService( + mockPrisma, + mockPubSub as any, +); + +enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', +} + +const userPersonalEnvironments = [ + { + userUiD: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }, + { + userUiD: 'abc123', + id: '1234', + name: 'test2', + variables: [{}], + isGlobal: false, + }, +]; + +beforeEach(() => { + mockReset(mockPrisma); + mockPubSub.publish.mockClear(); +}); + +describe('UserEnvironmentsService', () => { + describe('fetchUserEnvironments', () => { + test('Should return a list of users personal environments', async () => { + mockPrisma.userEnvironment.findMany.mockResolvedValueOnce([ + { + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }, + { + userUid: 'abc123', + id: '1234', + name: 'test2', + variables: [{}], + isGlobal: false, + }, + ]); + + const userEnvironments: UserEnvironment[] = [ + { + userUid: userPersonalEnvironments[0].userUiD, + id: userPersonalEnvironments[0].id, + name: userPersonalEnvironments[0].name, + variables: JSON.stringify(userPersonalEnvironments[0].variables), + isGlobal: userPersonalEnvironments[0].isGlobal, + }, + { + userUid: userPersonalEnvironments[1].userUiD, + id: userPersonalEnvironments[1].id, + name: userPersonalEnvironments[1].name, + variables: JSON.stringify(userPersonalEnvironments[1].variables), + isGlobal: userPersonalEnvironments[1].isGlobal, + }, + ]; + return expect( + await userEnvironmentsService.fetchUserEnvironments('abc123'), + ).toEqual(userEnvironments); + }); + + test('Should return an empty list of users personal environments', async () => { + mockPrisma.userEnvironment.findMany.mockResolvedValueOnce([]); + + return expect( + await userEnvironmentsService.fetchUserEnvironments('testuser'), + ).toEqual([]); + }); + + test('Should return an empty list of users personal environments if user uid is invalid', async () => { + mockPrisma.userEnvironment.findMany.mockResolvedValueOnce([]); + + return expect( + await userEnvironmentsService.fetchUserEnvironments('invaliduid'), + ).toEqual([]); + }); + }); + + describe('fetchUserGlobalEnvironment', () => { + test('Should resolve right and return a Global Environment for the uid', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + id: 'genv1', + userUid: 'abc', + name: '', + variables: [{}], + isGlobal: true, + }); + + expect( + await userEnvironmentsService.fetchUserGlobalEnvironment('abc'), + ).toEqualRight({ + id: 'genv1', + userUid: 'abc', + name: '', + variables: JSON.stringify([{}]), + isGlobal: true, + }); + }); + + test('Should resolve left and return an error if global env it doesnt exists', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); + + expect( + await userEnvironmentsService.fetchUserGlobalEnvironment('abc'), + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + }); + }); + + describe('createUserEnvironment', () => { + test( + 'Should resolve right and create a users personal environment and return a `UserEnvironment` object ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.create.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'test', + variables: JSON.stringify([{}]), + isGlobal: false, + }; + + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Created, + ); + + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + 'test', + '[{}]', + false, + ), + ).toEqualRight(result); + }, + ); + + test( + 'Should resolve right and create a new users global environment and return a `UserEnvironment` object ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); + + mockPrisma.userEnvironment.create.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'testgenv', + variables: [{}], + isGlobal: true, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'testgenv', + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Created, + ); + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + 'test', + '[{}]', + true, + ), + ).toEqualRight(result); + }, + ); + + test( + 'Should resolve left and not create a new users global environment if existing global env exists ' + + 'and not publish a subscription', + async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'testgenv', + variables: [{}], + isGlobal: true, + }); + + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + 'test', + '[{}]', + true, + ), + ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS); + }, + ); + }); + + describe('UpdateUserEnvironment', () => { + test( + 'should resolve right and update a users personal or environment and return a `UserEnvironment` object ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'test', + variables: JSON.stringify([{}]), + isGlobal: false, + }; + + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Updated, + ); + return expect( + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + 'test', + '[{}]', + ), + ).toEqualRight(result); + }, + ); + + test( + 'should resolve right and update a users global environment and return a `UserEnvironment` object ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: '', + variables: [{}], + isGlobal: true, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: '', + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Updated, + ); + return expect( + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + '', + '[{}]', + ), + ).toEqualRight(result); + }, + ); + + test( + 'should resolve left and not update a users environment if env doesnt exist ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.update.mockRejectedValueOnce({}); + + return expect( + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + 'test', + '[{}]', + ), + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + }, + ); + }); + + describe('deleteUserEnvironment', () => { + test( + 'Should resolve right and delete a users personal environment and return a `UserEnvironment` object ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); + mockPrisma.userEnvironment.delete.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [{}], + isGlobal: false, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: JSON.stringify([{}]), + isGlobal: false, + }; + + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Deleted, + ); + + return expect( + await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'), + ).toEqualRight(result); + }, + ); + + test('Should resolve left and return an error when deleting a global user environment ', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'genv1', + name: 'en1', + variables: [{}], + isGlobal: true, + }); + + return expect( + await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'), + ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED); + }); + + test('Should resolve left and return an error when deleting an invalid user environment ', async () => { + mockPrisma.userEnvironment.delete.mockResolvedValueOnce(null); + + return expect( + await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'), + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + }); + }); + + describe('deleteUserEnvironments', () => { + test('Should return a count of users personal environment deleted', async () => { + mockPrisma.userEnvironment.deleteMany.mockResolvedValueOnce({ + count: 1, + }); + + return expect( + await userEnvironmentsService.deleteUserEnvironments('abc123'), + ).toEqual(1); + }); + }); + + describe('deleteAllVariablesFromUsersGlobalEnvironment', () => { + test( + 'Should resolve right and delete all variables inside users global environment and return a `UserEnvironment` object ' + + 'and publish a subscription', + async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [{}], + isGlobal: true, + }); + + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [], + isGlobal: true, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: JSON.stringify([]), + isGlobal: true, + }; + + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Updated, + ); + + return expect( + await userEnvironmentsService.deleteAllVariablesFromUsersGlobalEnvironment( + 'abc123', + 'env1', + ), + ).toEqualRight(result); + }, + ); + + test('Should resolve left and return an error if global environment id and passed id dont match ', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'genv2', + name: 'en1', + variables: [{}], + isGlobal: true, + }); + + return expect( + await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'), + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + }); + }); + + describe('publishUserEnvironmentSubscription', () => { + test('Should publish a created subscription', async () => { + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: '', + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await mockPubSub.publish( + `user_environment/${result.userUid}/created`, + result, + ); + + return expect( + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Created, + ), + ).toBeUndefined(); + }); + + test('Should publish a updated subscription', async () => { + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: '', + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await mockPubSub.publish( + `user_environment/${result.userUid}/updated`, + result, + ); + + return expect( + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Updated, + ), + ).toBeUndefined(); + }); + + test('Should publish a deleted subscription', async () => { + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: '', + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await mockPubSub.publish( + `user_environment/${result.userUid}/deleted`, + result, + ); + + return expect( + await userEnvironmentsService.publishUserEnvironmentSubscription( + result, + SubscriptionType.Deleted, + ), + ).toBeUndefined(); + }); + }); +}); From c87690f378650756559ec176fc5349f3221e243b Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 15 Dec 2022 23:31:12 +0530 Subject: [PATCH 10/95] chore: update resolvers and service files --- .../user-environments.resolver.ts | 5 +- .../user-environments.service.ts | 110 ++++++++++++------ 2 files changed, 75 insertions(+), 40 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index bf1383f7d..76af01688 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -92,6 +92,7 @@ export class UserEnvironmentsResolver { }) @UseGuards(GqlAuthGuard) async deleteUserEnvironment( + @GqlUser() user: User, @Args({ name: 'id', description: 'ID of the user environment', @@ -100,7 +101,7 @@ export class UserEnvironmentsResolver { id: string, ): Promise { const userEnvironment = - await this.userEnvironmentsService.deleteUserEnvironment(id); + await this.userEnvironmentsService.deleteUserEnvironment(user.uid, id); if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); return userEnvironment.right; } @@ -170,7 +171,7 @@ export class UserEnvironmentsResolver { } @Subscription(() => UserEnvironment, { - description: 'Listen for User Environment updates', + description: 'Listen for User Environment deletion', resolve: (value) => value, }) @UseGuards(GqlAuthGuard) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index fa322602e..b09d9a5e3 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -1,10 +1,18 @@ import { Injectable } from '@nestjs/common'; - import { UserEnvironment } from './user-environments.model'; import { PrismaService } from '../prisma/prisma.service'; import { PubSubService } from '../pubsub/pubsub.service'; import * as E from 'fp-ts/Either'; +import { + USER_ENVIRONMENT_ENV_DOESNT_EXISTS, + USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED, + USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS, + USER_ENVIRONMENT_GLOBAL_ENV_EXISTS, + USER_ENVIRONMENT_IS_NOT_GLOBAL, + USER_ENVIRONMENT_UPDATE_FAILED, +} from '../errors'; +// Contains constants for the subscription types we send to pubsub service enum SubscriptionType { Created = 'created', Updated = 'updated', @@ -19,9 +27,9 @@ export class UserEnvironmentsService { ) {} /** - * Fetch personal and global user environments based on `isGlobal` flag + * Fetch personal user environments * @param uid Users uid - * @returns array of users personal and global environments + * @returns array of users personal environments */ async fetchUserEnvironments(uid: string) { const environments = await this.prisma.userEnvironment.findMany({ @@ -44,6 +52,32 @@ export class UserEnvironmentsService { return userEnvironments; } + /** + * Fetch users global environment + * @param uid Users uid + * @returns an `UserEnvironment` object + */ + async fetchUserGlobalEnvironment(uid: string) { + const globalEnvironment = await this.prisma.userEnvironment.findFirst({ + where: { + userUid: uid, + isGlobal: true, + }, + }); + + if (globalEnvironment != null) { + return E.right({ + userUid: globalEnvironment.userUid, + id: globalEnvironment.id, + name: globalEnvironment.name, + variables: JSON.stringify(globalEnvironment.variables), + isGlobal: globalEnvironment.isGlobal, + }); + } + + return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + } + /** * Create a personal or global user environment * @param uid Users uid @@ -58,9 +92,11 @@ export class UserEnvironmentsService { variables: string, isGlobal: boolean, ) { + // Check for existing global env for a user if exists error out to avoid recreation if (isGlobal) { const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) return E.left('global env exits'); + if (E.isRight(globalEnvExists)) + return E.left(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS); } const createdEnvironment = await this.prisma.userEnvironment.create({ @@ -80,7 +116,7 @@ export class UserEnvironmentsService { isGlobal: createdEnvironment.isGlobal, }; // Publish subscription for environment creation - await this.publishUserEnvironmentCreatedSubscription( + await this.publishUserEnvironmentSubscription( userEnvironment, SubscriptionType.Created, ); @@ -112,24 +148,33 @@ export class UserEnvironmentsService { variables: JSON.stringify(updatedEnvironment.variables), isGlobal: updatedEnvironment.isGlobal, }; - // Publish subscription for environment creation - await this.publishUserEnvironmentCreatedSubscription( + // Publish subscription for environment update + await this.publishUserEnvironmentSubscription( updatedUserEnvironment, SubscriptionType.Updated, ); return E.right(updatedUserEnvironment); } catch (e) { - return E.left('user_env not found'); + return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); } } /** * Delete an existing personal user environment based on environment id + * @param uid users uid * @param id environment id * @returns an Either of deleted `UserEnvironment` or error */ - async deleteUserEnvironment(id: string) { + async deleteUserEnvironment(uid: string, id: string) { try { + // check if id is of a global environment if it is, don't delete and error out + const globalEnvExists = await this.checkForExistingGlobalEnv(uid); + if (E.isRight(globalEnvExists)) { + const globalEnv = globalEnvExists.right; + if (globalEnv.id === id) { + return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED); + } + } const deletedEnvironment = await this.prisma.userEnvironment.delete({ where: { id: id, @@ -144,20 +189,19 @@ export class UserEnvironmentsService { isGlobal: deletedEnvironment.isGlobal, }; // Publish subscription for environment creation - await this.publishUserEnvironmentCreatedSubscription( + await this.publishUserEnvironmentSubscription( deletedUserEnvironment, SubscriptionType.Deleted, ); return E.right(deletedUserEnvironment); } catch (e) { - return E.left('user_env not found'); + return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); } } /** * Deletes all existing personal user environments - * @param id environment id - * @param isGlobal flag to indicate type of environment to delete + * @param uid user uid * @returns a count of environments deleted */ async deleteUserEnvironments(uid: string) { @@ -170,9 +214,15 @@ export class UserEnvironmentsService { return deletedEnvironments.count; } + /** + * Deletes all existing variables in a users global environment + * @param uid users uid + * @param id environment id + * @returns an `` of environments deleted + */ async deleteAllVariablesFromUsersGlobalEnvironment(uid: string, id: string) { const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists) && !E.isLeft(globalEnvExists)) { + if (E.isRight(globalEnvExists)) { const env = globalEnvExists.right; if (env.id === id) { try { @@ -190,21 +240,21 @@ export class UserEnvironmentsService { isGlobal: updatedEnvironment.isGlobal, }; // Publish subscription for environment creation - await this.publishUserEnvironmentCreatedSubscription( + await this.publishUserEnvironmentSubscription( updatedUserEnvironment, SubscriptionType.Updated, ); return E.right(updatedUserEnvironment); } catch (e) { - return E.left('user_env not found'); + return E.left(USER_ENVIRONMENT_UPDATE_FAILED); } - } + } else return E.left(USER_ENVIRONMENT_IS_NOT_GLOBAL); } - return E.left('mismatch'); + return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); } // Method to publish subscriptions based on the subscription type of the environment - async publishUserEnvironmentCreatedSubscription( + async publishUserEnvironmentSubscription( userEnv: UserEnvironment, subscriptionType: SubscriptionType, ) { @@ -232,6 +282,7 @@ export class UserEnvironmentsService { } } + // Method to check for existing global environments for a given user uid private async checkForExistingGlobalEnv(uid: string) { const globalEnv = await this.prisma.userEnvironment.findFirst({ where: { @@ -239,26 +290,9 @@ export class UserEnvironmentsService { isGlobal: true, }, }); - if (globalEnv === null) return E.left('global env not exist'); + if (globalEnv === null) + return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS); return E.right(globalEnv); } - - async fetchUserGlobalEnvironments(uid: string) { - const globalEnvironment = await this.prisma.userEnvironment.findFirst({ - where: { - userUid: uid, - isGlobal: true, - }, - rejectOnNotFound: true, - }); - - return { - userUid: globalEnvironment.userUid, - id: globalEnvironment.id, - name: globalEnvironment.name, - variables: JSON.stringify(globalEnvironment.variables), - isGlobal: globalEnvironment.isGlobal, - }; - } } From 4affb2bc5bb8e7995c4ed96fe66a410a3ace9be7 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Mon, 19 Dec 2022 17:38:46 +0600 Subject: [PATCH 11/95] 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 12/95] 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 13/95] 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 14/95] 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 15/95] 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 16/95] 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 17/95] 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 18/95] 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 19/95] 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 20/95] 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 21/95] 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 22/95] 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 23/95] 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 24/95] 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 25/95] 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 26/95] 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 27/95] 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 28/95] 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 c42b6e2fdb91130b99d3c2d7640622160e428d7a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 22 Dec 2022 12:46:03 +0600 Subject: [PATCH 29/95] feat: added fields for user-session of rest and gql --- packages/hoppscotch-backend/prisma/schema.prisma | 10 ++++++---- packages/hoppscotch-backend/src/user/user.model.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 947d158a8..7d88dbcf2 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -79,10 +79,12 @@ 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? + currentRESTSession String? + currentGQLSession String? } enum TeamMemberRole { diff --git a/packages/hoppscotch-backend/src/user/user.model.ts b/packages/hoppscotch-backend/src/user/user.model.ts index 779dbc4db..e2f2eb4a4 100644 --- a/packages/hoppscotch-backend/src/user/user.model.ts +++ b/packages/hoppscotch-backend/src/user/user.model.ts @@ -24,4 +24,16 @@ export class User { description: 'URL to the profile photo of the user (if given)', }) photoURL?: string; + + @Field({ + nullable: true, + description: 'JSON string of current REST session for logged-in User', + }) + currentRESTSession?: string; + + @Field({ + nullable: true, + description: 'JSON string of current GraphQL session for logged-in User', + }) + currentGQLSession?: string; } From 3cc22575cb40cfbb88966e86656c30ac57bc5658 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 22 Dec 2022 17:35:44 +0600 Subject: [PATCH 30/95] feat: user update and subscribers added --- .../src/prisma/prisma.service.ts | 4 +- .../src/user/dtos/update-user-input.dto.ts | 18 +++++++++ .../src/user/user.module.ts | 6 ++- .../src/user/user.resolver.ts | 38 ++++++++++++++++++- .../src/user/user.service.ts | 34 +++++++++++++++++ 5 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts create mode 100644 packages/hoppscotch-backend/src/user/user.service.ts diff --git a/packages/hoppscotch-backend/src/prisma/prisma.service.ts b/packages/hoppscotch-backend/src/prisma/prisma.service.ts index 5b962c430..be48954ef 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 @@ -16,4 +16,4 @@ export class PrismaService async onModuleDestroy() { await this.$disconnect(); } -} +} \ No newline at end of file diff --git a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts new file mode 100644 index 000000000..2747c9e3d --- /dev/null +++ b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts @@ -0,0 +1,18 @@ +import { Field, InputType } from '@nestjs/graphql'; + +@InputType() +export class UpdateUserInput { + @Field({ + nullable: true, + name: 'currentRESTSession', + description: 'JSON string of the session', + }) + currentRESTSession?: string; + + @Field({ + nullable: true, + name: 'currentGQLSession', + description: 'JSON string of the session', + }) + currentGQLSession?: string; +} diff --git a/packages/hoppscotch-backend/src/user/user.module.ts b/packages/hoppscotch-backend/src/user/user.module.ts index 3681faad5..a5f9eda17 100644 --- a/packages/hoppscotch-backend/src/user/user.module.ts +++ b/packages/hoppscotch-backend/src/user/user.module.ts @@ -1,10 +1,12 @@ import { Module } from '@nestjs/common'; import { UserResolver } from './user.resolver'; import { PubSubModule } from 'src/pubsub/pubsub.module'; +import { PrismaModule } from 'src/prisma/prisma.module'; +import { UserService } from './user.service'; @Module({ - imports: [PubSubModule], - providers: [UserResolver], + imports: [PubSubModule, PrismaModule], + providers: [UserResolver, UserService], exports: [], }) export class UserModule {} diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index d913be01d..3358b187b 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -1,14 +1,22 @@ -import { Resolver, Query } from '@nestjs/graphql'; +import { Resolver, Query, Mutation, Args, Subscription } from '@nestjs/graphql'; import { User } from './user.model'; import { UseGuards } from '@nestjs/common'; import { GqlAuthGuard } from '../guards/gql-auth.guard'; import { GqlUser } from '../decorators/gql-user.decorator'; +import { UserService } from './user.service'; +import { throwErr } from 'src/utils'; +import * as E from 'fp-ts/lib/Either'; +import { UpdateUserInput } from './dtos/update-user-input.dto'; +import { PubSubService } from 'src/pubsub/pubsub.service'; @Resolver(() => User) export class UserResolver { // TODO: remove the eslint-disable line below once dependencies are added to user.service file // eslint-disable-next-line @typescript-eslint/no-empty-function - constructor() {} + constructor( + private readonly userService: UserService, + private readonly pubsub: PubSubService, + ) {} @Query(() => User, { description: @@ -27,4 +35,30 @@ export class UserResolver { me2(@GqlUser() user: User): User { return user; } + + /* Mutations */ + + @Mutation(() => User, { + description: 'Update user information', + }) + @UseGuards(GqlAuthGuard) + async updateUser( + @GqlUser() user: User, + @Args('userInput') userInput: UpdateUserInput, + ): Promise { + const updatedUser = await this.userService.updateUser(user, userInput); + if (E.isLeft(updatedUser)) throwErr(updatedUser.left); + return updatedUser.right; + } + + /* Subscriptions */ + + @Subscription(() => User, { + description: 'Listen for user updates', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userSettingsUpdated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user/${user.uid}/updated`); + } } diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts new file mode 100644 index 000000000..df8a60aff --- /dev/null +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -0,0 +1,34 @@ +import { Injectable } from '@nestjs/common'; +import { PrismaService } from '../prisma/prisma.service'; +import { User } from './user.model'; +import * as E from 'fp-ts/lib/Either'; +import { USER_NOT_FOUND } from 'src/errors'; +import { UpdateUserInput } from './dtos/update-user-input.dto'; +import { PubSubService } from 'src/pubsub/pubsub.service'; + +@Injectable() +export class UserService { + constructor( + private readonly prisma: PrismaService, + private readonly pubsub: PubSubService, + ) {} + + async updateUser(user: User, updateUserDto: UpdateUserInput) { + try { + const updatedUser = await this.prisma.user.update({ + where: { uid: user.uid }, + data: updateUserDto, + }); + + // Publish subscription for user updates + await this.pubsub.publish( + `user_settings/${user.uid}/updated`, + updatedUser, + ); + + return E.right(updatedUser); + } catch (e) { + return E.left(USER_NOT_FOUND); + } + } +} From c11a219c62ffc455da3085447283ae149e88289a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 22 Dec 2022 18:41:35 +0600 Subject: [PATCH 31/95] fix: typo of pubsub message --- packages/hoppscotch-backend/src/user/user.service.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index df8a60aff..ec62e0778 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -21,10 +21,7 @@ export class UserService { }); // Publish subscription for user updates - await this.pubsub.publish( - `user_settings/${user.uid}/updated`, - updatedUser, - ); + await this.pubsub.publish(`user/${user.uid}/updated`, updatedUser); return E.right(updatedUser); } catch (e) { From 9e304b947b62908706008ab70d377e28037951ba Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 22 Dec 2022 18:47:42 +0600 Subject: [PATCH 32/95] test: added user update unit tests --- .../src/user/user.service.spec.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 packages/hoppscotch-backend/src/user/user.service.spec.ts diff --git a/packages/hoppscotch-backend/src/user/user.service.spec.ts b/packages/hoppscotch-backend/src/user/user.service.spec.ts new file mode 100644 index 000000000..e5f293519 --- /dev/null +++ b/packages/hoppscotch-backend/src/user/user.service.spec.ts @@ -0,0 +1,53 @@ +import { DeepMockProxy, mockDeep, mockReset } from 'jest-mock-extended'; +import { PrismaService } from 'src/prisma/prisma.service'; +import { PubSubService } from 'src/pubsub/pubsub.service'; +import { UserService } from './user.service'; + +const mockPrisma = mockDeep(); +const mockPubSub = mockDeep(); + +// @ts-ignore +const userService = new UserService(mockPrisma, mockPubSub as any); + +const user = { + uid: '123', + displayName: 'John Doe', + email: 'test@hoppscotch.io', + photoURL: 'https://example.com/avatar.png', + currentRESTSession: JSON.stringify({}), + currentGQLSession: JSON.stringify({}), +}; + +beforeEach(() => { + mockReset(mockPrisma); + mockPubSub.publish.mockClear(); +}); + +describe('UserService', () => { + describe('updateUser', () => { + test('should update user', async () => { + mockPrisma.user.update.mockResolvedValue(user); + + const result = await userService.updateUser(user, { + currentGQLSession: user.currentGQLSession, + currentRESTSession: user.currentRESTSession, + }); + + expect(result).toEqualRight(user); + }); + test('should publish user update subscription', async () => { + mockPrisma.user.update.mockResolvedValue(user); + + await userService.updateUser(user, { + currentGQLSession: user.currentGQLSession, + currentRESTSession: user.currentRESTSession, + }); + + expect(mockPubSub.publish).toHaveBeenCalledTimes(1); + expect(mockPubSub.publish).toHaveBeenCalledWith( + `user/${user.uid}/updated`, + user, + ); + }); + }); +}); From f9de546d1462b0380c173d9e2f5766677e348a42 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 22 Dec 2022 18:56:18 +0600 Subject: [PATCH 33/95] feat: more property added in userInputDto and key updated --- .../src/user/dtos/update-user-input.dto.ts | 14 ++++++++++++++ .../hoppscotch-backend/src/user/user.resolver.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts index 2747c9e3d..41bd3e9ec 100644 --- a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts +++ b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts @@ -2,6 +2,20 @@ import { Field, InputType } from '@nestjs/graphql'; @InputType() export class UpdateUserInput { + @Field({ + nullable: true, + name: 'displayName', + description: 'Displayed name of the user (if given)', + }) + displayName?: string; + + @Field({ + nullable: true, + name: 'photoURL', + description: 'URL to the profile photo of the user (if given)', + }) + photoURL?: string; + @Field({ nullable: true, name: 'currentRESTSession', diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index 3358b187b..99475d0d5 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -44,7 +44,7 @@ export class UserResolver { @UseGuards(GqlAuthGuard) async updateUser( @GqlUser() user: User, - @Args('userInput') userInput: UpdateUserInput, + @Args('user') userInput: UpdateUserInput, ): Promise { const updatedUser = await this.userService.updateUser(user, userInput); if (E.isLeft(updatedUser)) throwErr(updatedUser.left); From 6b59b9988c56907ae1a1f86cdd754c8370f8dabe Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 22 Dec 2022 22:25:16 +0600 Subject: [PATCH 34/95] docs: developer guide text updated --- packages/hoppscotch-backend/src/user/user.model.ts | 12 ++++++------ packages/hoppscotch-backend/src/user/user.service.ts | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/user.model.ts b/packages/hoppscotch-backend/src/user/user.model.ts index e2f2eb4a4..6db48b3e1 100644 --- a/packages/hoppscotch-backend/src/user/user.model.ts +++ b/packages/hoppscotch-backend/src/user/user.model.ts @@ -3,37 +3,37 @@ import { ObjectType, ID, Field } from '@nestjs/graphql'; @ObjectType() export class User { @Field(() => ID, { - description: 'Firebase UID of the user', + description: 'UID of the user', }) uid: string; @Field({ nullable: true, - description: 'Displayed name of the user (if given)', + description: 'Displayed name of the user', }) displayName?: string; @Field({ nullable: true, - description: 'Email of the user (if given)', + description: 'Email of the user', }) email?: string; @Field({ nullable: true, - description: 'URL to the profile photo of the user (if given)', + description: 'URL to the profile photo of the user', }) photoURL?: string; @Field({ nullable: true, - description: 'JSON string of current REST session for logged-in User', + description: 'Stringified current REST session for logged-in User', }) currentRESTSession?: string; @Field({ nullable: true, - description: 'JSON string of current GraphQL session for logged-in User', + description: 'Stringified current GraphQL session for logged-in User', }) currentGQLSession?: string; } diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index ec62e0778..3b563476f 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -13,6 +13,12 @@ export class UserService { private readonly pubsub: PubSubService, ) {} + /** + * Update a user's information + * @param user User object + * @param updateUserDto Properties to update + * @returns a Either of User or error + */ async updateUser(user: User, updateUserDto: UpdateUserInput) { try { const updatedUser = await this.prisma.user.update({ From 9b5734f2ff78cbdf63de4c106ee83acf08a4b35b Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Fri, 23 Dec 2022 12:28:22 +0600 Subject: [PATCH 35/95] 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 36/95] 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 37/95] 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 38/95] 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 39/95] 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 40/95] 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 41/95] 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 9d20c4c4a94a45dad5310e1d40cd6f9ab30824ca Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 5 Jan 2023 15:52:43 +0600 Subject: [PATCH 42/95] chore: updated variable names and comments --- .../src/user/dtos/update-user-input.dto.ts | 4 ++-- packages/hoppscotch-backend/src/user/user.service.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts index 41bd3e9ec..b5fd7d0a6 100644 --- a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts +++ b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts @@ -19,14 +19,14 @@ export class UpdateUserInput { @Field({ nullable: true, name: 'currentRESTSession', - description: 'JSON string of the session', + description: 'JSON string of the saved REST session', }) currentRESTSession?: string; @Field({ nullable: true, name: 'currentGQLSession', - description: 'JSON string of the session', + description: 'JSON string of the saved GQL session', }) currentGQLSession?: string; } diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index 3b563476f..cdb1f64e2 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -16,14 +16,14 @@ export class UserService { /** * Update a user's information * @param user User object - * @param updateUserDto Properties to update + * @param updateUserData Properties to update * @returns a Either of User or error */ - async updateUser(user: User, updateUserDto: UpdateUserInput) { + async updateUser(user: User, updateUserData: UpdateUserInput) { try { const updatedUser = await this.prisma.user.update({ where: { uid: user.uid }, - data: updateUserDto, + data: updateUserData, }); // Publish subscription for user updates From f58d5d28cfbd60e4469596e46626132e33407921 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 01:31:38 +0530 Subject: [PATCH 43/95] chore: added error messages and updated existing error messages --- packages/hoppscotch-backend/src/errors.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 62664d3f7..b95e25b14 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -159,7 +159,7 @@ export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = * Global environment doesnt exists for the user * (UserEnvironmentsService) */ -export const USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS = +export const USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS = 'user_environment/global_env_doesnt_exists' as const; /* @@ -175,7 +175,7 @@ export const USER_ENVIRONMENT_GLOBAL_ENV_EXISTS = * User environment doesn't exist for the user * (UserEnvironmentsService) */ -export const USER_ENVIRONMENT_ENV_DOESNT_EXISTS = +export const USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS = 'user_environment/user_env_doesnt_exists' as const; /* @@ -203,6 +203,14 @@ export const USER_ENVIRONMENT_UPDATE_FAILED = 'user_environment/user_env_update_failed' as const; /* +/** + * User environment invalid environment name + * (UserEnvironmentsService) + */ +export const USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME = + 'user_environment/user_env_invalid_env_name' as const; +/* + |------------------------------------| |Server errors that are actually bugs| |------------------------------------| From 9e25aa1f9f329b2e293bc7879e32f7fb7003549a Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 01:34:11 +0530 Subject: [PATCH 44/95] feat: introducing new subscription handler interface and user defined/primitive types --- .../src/subscription-handler.ts | 49 +++++++++++++++++++ .../src/types/module-types.ts | 4 ++ .../src/types/primitive-types.ts | 1 + 3 files changed, 54 insertions(+) create mode 100644 packages/hoppscotch-backend/src/subscription-handler.ts create mode 100644 packages/hoppscotch-backend/src/types/module-types.ts create mode 100644 packages/hoppscotch-backend/src/types/primitive-types.ts diff --git a/packages/hoppscotch-backend/src/subscription-handler.ts b/packages/hoppscotch-backend/src/subscription-handler.ts new file mode 100644 index 000000000..f07e62c89 --- /dev/null +++ b/packages/hoppscotch-backend/src/subscription-handler.ts @@ -0,0 +1,49 @@ +import { Injectable } from '@nestjs/common'; +import { PubSubService } from './pubsub/pubsub.service'; +import { PrimitiveTypes } from './types/primitive-types'; +import { ModuleTypes } from './types/module-types'; + +// Custom generic type to indicate the type of module +type ModuleType = PrimitiveTypes | ModuleTypes; + +// Contains constants for the subscription types we send to pubsub service +enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', + DeleteMany = 'delete_many', +} + +@Injectable() +export class SubscriptionHandler { + constructor(private readonly pubsub: PubSubService) {} + + /** + * Publishes a subscription using the pubsub module + * @param topic a string containing the module name, a uid and the type of subscription + * @param moduleType type of the module model being called + * @returns a promise of type void + */ + async publish( + topic: string, + subscriptionType: SubscriptionType, + moduleType: ModuleType, + ) { + switch (subscriptionType) { + case SubscriptionType.Created: + await this.pubsub.publish(`${topic}/created`, moduleType); + break; + case SubscriptionType.Updated: + await this.pubsub.publish(`${topic}/updated`, moduleType); + break; + case SubscriptionType.Deleted: + await this.pubsub.publish(`${topic}/deleted`, moduleType); + break; + case SubscriptionType.DeleteMany: + await this.pubsub.publish(`${topic}/delete_many`, moduleType); + break; + default: + break; + } + } +} diff --git a/packages/hoppscotch-backend/src/types/module-types.ts b/packages/hoppscotch-backend/src/types/module-types.ts new file mode 100644 index 000000000..356fcc816 --- /dev/null +++ b/packages/hoppscotch-backend/src/types/module-types.ts @@ -0,0 +1,4 @@ +import { UserEnvironment } from '../user-environment/user-environments.model'; +import { User } from '../user/user.model'; + +export type ModuleTypes = UserEnvironment | User; diff --git a/packages/hoppscotch-backend/src/types/primitive-types.ts b/packages/hoppscotch-backend/src/types/primitive-types.ts new file mode 100644 index 000000000..11918cf33 --- /dev/null +++ b/packages/hoppscotch-backend/src/types/primitive-types.ts @@ -0,0 +1 @@ +export type PrimitiveTypes = number | string | boolean; From 80fdc6005bbc53048ad47436ca9535db8da19861 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 01:35:21 +0530 Subject: [PATCH 45/95] chore: added comments to certain fields --- .../src/user-environment/user-environments.model.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts index 552299f60..be99821e3 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.model.ts @@ -16,7 +16,7 @@ export class UserEnvironment { nullable: true, description: 'Name of the environment', }) - name: string | null | undefined; + name: string | null | undefined; // types have a union to avoid TS warnings and field is nullable when it is global env @Field({ description: 'All variables present in the environment', @@ -24,7 +24,7 @@ export class UserEnvironment { variables: string; // JSON string of the variables object (format:[{ key: "bla", value: "bla_val" }, ...] ) which will be received from the client @Field({ - description: 'isGlobal flag to indicate the environment is global or not', + description: 'Flag to indicate the environment is global or not', }) isGlobal: boolean; } From 29e74a2c9ec2f631f573fdb749df212e9971a4c3 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 01:36:31 +0530 Subject: [PATCH 46/95] feat: added subscription handler as a provider for user environment module --- .../src/user-environment/user-environments.module.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts index 04f391682..d82a1e899 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts @@ -5,6 +5,7 @@ import { UserModule } from '../user/user.module'; import { UserEnvsUserResolver } from './user.resolver'; import { UserEnvironmentsResolver } from './user-environments.resolver'; import { UserEnvironmentsService } from './user-environments.service'; +import { SubscriptionHandler } from '../subscription-handler'; @Module({ imports: [PrismaModule, PubSubModule, UserModule], @@ -12,6 +13,7 @@ import { UserEnvironmentsService } from './user-environments.service'; UserEnvironmentsResolver, UserEnvironmentsService, UserEnvsUserResolver, + SubscriptionHandler, ], exports: [UserEnvironmentsService], }) From 6aa66e99b51e5914e4174c43a341d2cd1f75bd22 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 12:48:25 +0530 Subject: [PATCH 47/95] chore: added review changes for description --- .../hoppscotch-backend/src/user-environment/user.resolver.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts index 3dbfc949b..0d6d31cf4 100644 --- a/packages/hoppscotch-backend/src/user-environment/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user.resolver.ts @@ -16,8 +16,7 @@ export class UserEnvsUserResolver { } @ResolveField(() => UserEnvironment, { - description: - 'Returns a list of user variables inside a global environments', + description: 'Returns the users global environments', }) async globalEnvironments( @Parent() user: User, From 3e9295f31303c5a963ba80461446dd14001eacb5 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 12:50:20 +0530 Subject: [PATCH 48/95] chore: added review changes for updating mutations and naming for descriptions --- .../user-environments.resolver.ts | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index 76af01688..c73156600 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -19,8 +19,7 @@ export class UserEnvironmentsResolver { /* Mutations */ @Mutation(() => UserEnvironment, { - description: - 'Create a new personal or global user environment for given user uid', + description: 'Create a new personal user environment for given user uid', }) @UseGuards(GqlAuthGuard) async createUserEnvironment( @@ -36,12 +35,8 @@ export class UserEnvironmentsResolver { description: 'JSON string of the variables object', }) variables: string, - @Args({ - name: 'isGlobal', - description: 'isGlobal flag to indicate personal or global environment', - }) - isGlobal: boolean, ): Promise { + const isGlobal = false; const userEnvironment = await this.userEnvironmentsService.createUserEnvironment( user.uid, @@ -54,8 +49,32 @@ export class UserEnvironmentsResolver { } @Mutation(() => UserEnvironment, { - description: - 'Update a users personal or global environment based on environment id', + description: 'Create a new global user environment for given user uid', + }) + @UseGuards(GqlAuthGuard) + async createUserGlobalEnvironment( + @GqlUser() user: User, + @Args({ + name: 'variables', + description: 'JSON string of the variables object', + }) + variables: string, + ): Promise { + const isGlobal = true; + const globalEnvName: string = null; + const userEnvironment = + await this.userEnvironmentsService.createUserEnvironment( + user.uid, + globalEnvName, + variables, + isGlobal, + ); + if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); + return userEnvironment.right; + } + + @Mutation(() => UserEnvironment, { + description: 'Updates a users personal or global environment', }) @UseGuards(GqlAuthGuard) async updateUserEnvironment( @@ -88,7 +107,7 @@ export class UserEnvironmentsResolver { } @Mutation(() => UserEnvironment, { - description: 'Deletes a users personal environment based on environment id', + description: 'Deletes a users personal environment', }) @UseGuards(GqlAuthGuard) async deleteUserEnvironment( @@ -99,7 +118,7 @@ export class UserEnvironmentsResolver { type: () => ID, }) id: string, - ): Promise { + ): Promise { const userEnvironment = await this.userEnvironmentsService.deleteUserEnvironment(user.uid, id); if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); @@ -110,7 +129,7 @@ export class UserEnvironmentsResolver { description: 'Deletes users all personal environments', }) @UseGuards(GqlAuthGuard) - async deleteUserEnvironments(@GqlUser() user: User): Promise { + async deleteUserEnvironments(@GqlUser() user: User): Promise { return await this.userEnvironmentsService.deleteUserEnvironments(user.uid); } @@ -118,7 +137,7 @@ export class UserEnvironmentsResolver { description: 'Deletes all variables inside a users global environment', }) @UseGuards(GqlAuthGuard) - async deleteAllVariablesFromUsersGlobalEnvironment( + async clearGlobalEnvironments( @GqlUser() user: User, @Args({ name: 'id', @@ -128,10 +147,7 @@ export class UserEnvironmentsResolver { id: string, ): Promise { const userEnvironment = - await this.userEnvironmentsService.deleteAllVariablesFromUsersGlobalEnvironment( - user.uid, - id, - ); + await this.userEnvironmentsService.clearGlobalEnvironments(user.uid, id); if (E.isLeft(userEnvironment)) throwErr(userEnvironment.left); return userEnvironment.right; } @@ -146,7 +162,7 @@ export class UserEnvironmentsResolver { userEnvironmentCreated( @Args({ name: 'userUid', - description: 'users uid', + description: 'Users uid', type: () => ID, }) userUid: string, @@ -162,7 +178,7 @@ export class UserEnvironmentsResolver { userEnvironmentUpdated( @Args({ name: 'id', - description: 'environment id', + description: 'Environment id', type: () => ID, }) id: string, @@ -178,7 +194,7 @@ export class UserEnvironmentsResolver { userEnvironmentDeleted( @Args({ name: 'id', - description: 'environment id', + description: 'Environment id', type: () => ID, }) id: string, From 4aad8d36a999379be958db0c1ff265bdda4b66fd Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 12:53:40 +0530 Subject: [PATCH 49/95] chore: updated comment for Subscription Type and updated JSDoc for publish --- packages/hoppscotch-backend/src/subscription-handler.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/subscription-handler.ts b/packages/hoppscotch-backend/src/subscription-handler.ts index f07e62c89..fba5551a4 100644 --- a/packages/hoppscotch-backend/src/subscription-handler.ts +++ b/packages/hoppscotch-backend/src/subscription-handler.ts @@ -6,7 +6,7 @@ import { ModuleTypes } from './types/module-types'; // Custom generic type to indicate the type of module type ModuleType = PrimitiveTypes | ModuleTypes; -// Contains constants for the subscription types we send to pubsub service +// Contains constants for the subscription types we use in Subscription Handler enum SubscriptionType { Created = 'created', Updated = 'updated', @@ -20,7 +20,8 @@ export class SubscriptionHandler { /** * Publishes a subscription using the pubsub module - * @param topic a string containing the module name, a uid and the type of subscription + * @param topic a string containing the module name, an uid and the type of subscription + * @param subscriptionType type of subscription being called * @param moduleType type of the module model being called * @returns a promise of type void */ From d10ed664bfb7d362d1f7481a524c2be1617e181f Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 12:56:41 +0530 Subject: [PATCH 50/95] chore: updated test cases with requested changes to handle publishing seperately --- .../user-environments.service.spec.ts | 667 ++++++++++-------- 1 file changed, 366 insertions(+), 301 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts index ca5078005..f41287f1d 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts @@ -3,26 +3,31 @@ import { mockDeep, mockReset } from 'jest-mock-extended'; import { PrismaService } from '../prisma/prisma.service'; import { UserEnvironmentsService } from './user-environments.service'; import { - USER_ENVIRONMENT_ENV_DOESNT_EXISTS, + USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS, USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED, USER_ENVIRONMENT_GLOBAL_ENV_EXISTS, + USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, } from '../errors'; import { PubSubService } from '../pubsub/pubsub.service'; +import { SubscriptionHandler } from '../subscription-handler'; const mockPrisma = mockDeep(); const mockPubSub = mockDeep(); +const mockSubscriptionHandler = mockDeep(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const userEnvironmentsService = new UserEnvironmentsService( mockPrisma, mockPubSub as any, + mockSubscriptionHandler, ); enum SubscriptionType { Created = 'created', Updated = 'updated', Deleted = 'deleted', + DeleteMany = 'delete_many', } const userPersonalEnvironments = [ @@ -131,228 +136,303 @@ describe('UserEnvironmentsService', () => { expect( await userEnvironmentsService.fetchUserGlobalEnvironment('abc'), - ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); }); }); describe('createUserEnvironment', () => { - test( - 'Should resolve right and create a users personal environment and return a `UserEnvironment` object ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.create.mockResolvedValueOnce({ - userUid: 'abc123', - id: '123', - name: 'test', - variables: [{}], - isGlobal: false, - }); + test('Should resolve right and create a users personal environment and return a `UserEnvironment` object ', async () => { + mockPrisma.userEnvironment.create.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }); - const result: UserEnvironment = { - userUid: 'abc123', - id: '123', - name: 'test', - variables: JSON.stringify([{}]), - isGlobal: false, - }; + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'test', + variables: JSON.stringify([{}]), + isGlobal: false, + }; - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Created, - ); + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + 'test', + '[{}]', + false, + ), + ).toEqualRight(result); + }); - return expect( - await userEnvironmentsService.createUserEnvironment( - 'abc123', - 'test', - '[{}]', - false, - ), - ).toEqualRight(result); - }, - ); + test('Should resolve right and create a new users global environment and return a `UserEnvironment` object ', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); - test( - 'Should resolve right and create a new users global environment and return a `UserEnvironment` object ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); + mockPrisma.userEnvironment.create.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: null, + variables: [{}], + isGlobal: true, + }); - mockPrisma.userEnvironment.create.mockResolvedValueOnce({ - userUid: 'abc123', - id: '123', - name: 'testgenv', - variables: [{}], - isGlobal: true, - }); + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: null, + variables: JSON.stringify([{}]), + isGlobal: true, + }; - const result: UserEnvironment = { - userUid: 'abc123', - id: '123', - name: 'testgenv', - variables: JSON.stringify([{}]), - isGlobal: true, - }; + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + null, + '[{}]', + true, + ), + ).toEqualRight(result); + }); - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Created, - ); - return expect( - await userEnvironmentsService.createUserEnvironment( - 'abc123', - 'test', - '[{}]', - true, - ), - ).toEqualRight(result); - }, - ); + test('Should resolve left and not create a new users global environment if existing global env exists ', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: null, + variables: [{}], + isGlobal: true, + }); - test( - 'Should resolve left and not create a new users global environment if existing global env exists ' + - 'and not publish a subscription', - async () => { - mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ - userUid: 'abc123', - id: '123', - name: 'testgenv', - variables: [{}], - isGlobal: true, - }); + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + null, + '[{}]', + true, + ), + ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS); + }); - return expect( - await userEnvironmentsService.createUserEnvironment( - 'abc123', - 'test', - '[{}]', - true, - ), - ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS); - }, - ); + test('Should resolve left when an invalid personal environment name has been passed', async () => { + return expect( + await userEnvironmentsService.createUserEnvironment( + 'abc123', + null, + '[{}]', + false, + ), + ).toEqualLeft(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME); + }); + + test('Should create a users personal environment and publish a created subscription', async () => { + mockPrisma.userEnvironment.create.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'test', + variables: JSON.stringify([{}]), + isGlobal: false, + }; + + await userEnvironmentsService.createUserEnvironment( + 'abc123', + 'test', + '[{}]', + false, + ); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/${result.userUid}`, + SubscriptionType.Created, + result, + ); + }); + + test('Should create a users global environment and publish a created subscription', async () => { + mockPrisma.userEnvironment.create.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: '', + variables: [{}], + isGlobal: true, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: '', + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await userEnvironmentsService.createUserEnvironment( + 'abc123', + '', + '[{}]', + true, + ); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/${result.userUid}`, + SubscriptionType.Created, + result, + ); + }); }); describe('UpdateUserEnvironment', () => { - test( - 'should resolve right and update a users personal or environment and return a `UserEnvironment` object ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.update.mockResolvedValueOnce({ - userUid: 'abc123', - id: '123', - name: 'test', - variables: [{}], - isGlobal: false, - }); + test('Should resolve right and update a users personal or environment and return a `UserEnvironment` object ', async () => { + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }); - const result: UserEnvironment = { - userUid: 'abc123', - id: '123', - name: 'test', - variables: JSON.stringify([{}]), - isGlobal: false, - }; + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'test', + variables: JSON.stringify([{}]), + isGlobal: false, + }; - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Updated, - ); - return expect( - await userEnvironmentsService.updateUserEnvironment( - 'abc123', - 'test', - '[{}]', - ), - ).toEqualRight(result); - }, - ); + return expect( + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + 'test', + '[{}]', + ), + ).toEqualRight(result); + }); - test( - 'should resolve right and update a users global environment and return a `UserEnvironment` object ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.update.mockResolvedValueOnce({ - userUid: 'abc123', - id: '123', - name: '', - variables: [{}], - isGlobal: true, - }); + test('Should resolve right and update a users global environment and return a `UserEnvironment` object ', async () => { + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: null, + variables: [{}], + isGlobal: true, + }); - const result: UserEnvironment = { - userUid: 'abc123', - id: '123', - name: '', - variables: JSON.stringify([{}]), - isGlobal: true, - }; + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: null, + variables: JSON.stringify([{}]), + isGlobal: true, + }; - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Updated, - ); - return expect( - await userEnvironmentsService.updateUserEnvironment( - 'abc123', - '', - '[{}]', - ), - ).toEqualRight(result); - }, - ); + return expect( + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + null, + '[{}]', + ), + ).toEqualRight(result); + }); - test( - 'should resolve left and not update a users environment if env doesnt exist ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.update.mockRejectedValueOnce({}); + test('Should resolve left and not update a users environment if env doesnt exist ', async () => { + mockPrisma.userEnvironment.update.mockRejectedValueOnce({}); - return expect( - await userEnvironmentsService.updateUserEnvironment( - 'abc123', - 'test', - '[{}]', - ), - ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); - }, - ); + return expect( + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + 'test', + '[{}]', + ), + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); + }); + + test('Should resolve right, update a users personal environment and publish an updated subscription ', async () => { + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: 'test', + variables: [{}], + isGlobal: false, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: 'test', + variables: JSON.stringify([{}]), + isGlobal: false, + }; + + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + 'test', + '[{}]', + ); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/${result.id}`, + SubscriptionType.Updated, + result, + ); + }); + + test('Should resolve right, update a users global environment and publish an updated subscription ', async () => { + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: '123', + name: null, + variables: [{}], + isGlobal: true, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: '123', + name: null, + variables: JSON.stringify([{}]), + isGlobal: true, + }; + + await userEnvironmentsService.updateUserEnvironment( + 'abc123', + null, + '[{}]', + ); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/${result.id}`, + SubscriptionType.Updated, + result, + ); + }); }); describe('deleteUserEnvironment', () => { - test( - 'Should resolve right and delete a users personal environment and return a `UserEnvironment` object ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); - mockPrisma.userEnvironment.delete.mockResolvedValueOnce({ - userUid: 'abc123', - id: 'env1', - name: 'en1', - variables: [{}], - isGlobal: false, - }); + test('Should resolve right and delete a users personal environment and return a `UserEnvironment` object ', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); + mockPrisma.userEnvironment.delete.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [{}], + isGlobal: false, + }); - const result: UserEnvironment = { - userUid: 'abc123', - id: 'env1', - name: 'en1', - variables: JSON.stringify([{}]), - isGlobal: false, - }; + return expect( + await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'), + ).toEqualRight(true); + }); - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Deleted, - ); - - return expect( - await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'), - ).toEqualRight(result); - }, - ); - - test('Should resolve left and return an error when deleting a global user environment ', async () => { + test('Should resolve left and return an error when deleting a global user environment', async () => { mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ userUid: 'abc123', id: 'genv1', @@ -366,71 +446,90 @@ describe('UserEnvironmentsService', () => { ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED); }); - test('Should resolve left and return an error when deleting an invalid user environment ', async () => { + test('Should resolve left and return an error when deleting an invalid user environment', async () => { mockPrisma.userEnvironment.delete.mockResolvedValueOnce(null); return expect( await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'), - ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); + }); + + test('Should resolve right, delete a users personal environment and publish a deleted subscription', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null); + mockPrisma.userEnvironment.delete.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [{}], + isGlobal: false, + }); + + const result: UserEnvironment = { + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: JSON.stringify([{}]), + isGlobal: false, + }; + + await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/${result.id}`, + SubscriptionType.Deleted, + result, + ); }); }); describe('deleteUserEnvironments', () => { - test('Should return a count of users personal environment deleted', async () => { + test('Should publish a subscription with a count of deleted environments', async () => { mockPrisma.userEnvironment.deleteMany.mockResolvedValueOnce({ count: 1, }); - return expect( - await userEnvironmentsService.deleteUserEnvironments('abc123'), - ).toEqual(1); + await userEnvironmentsService.deleteUserEnvironments('abc123'); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/abc123`, + SubscriptionType.DeleteMany, + 1, + ); }); }); - describe('deleteAllVariablesFromUsersGlobalEnvironment', () => { - test( - 'Should resolve right and delete all variables inside users global environment and return a `UserEnvironment` object ' + - 'and publish a subscription', - async () => { - mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ - userUid: 'abc123', - id: 'env1', - name: 'en1', - variables: [{}], - isGlobal: true, - }); + describe('clearGlobalEnvironments', () => { + test('Should resolve right and delete all variables inside users global environment and return a `UserEnvironment` object', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [{}], + isGlobal: true, + }); - mockPrisma.userEnvironment.update.mockResolvedValueOnce({ - userUid: 'abc123', - id: 'env1', - name: 'en1', - variables: [], - isGlobal: true, - }); + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [], + isGlobal: true, + }); - const result: UserEnvironment = { - userUid: 'abc123', - id: 'env1', - name: 'en1', - variables: JSON.stringify([]), - isGlobal: true, - }; + const result: UserEnvironment = { + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: JSON.stringify([]), + isGlobal: true, + }; - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Updated, - ); + return expect( + await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1'), + ).toEqualRight(result); + }); - return expect( - await userEnvironmentsService.deleteAllVariablesFromUsersGlobalEnvironment( - 'abc123', - 'env1', - ), - ).toEqualRight(result); - }, - ); - - test('Should resolve left and return an error if global environment id and passed id dont match ', async () => { + test('Should resolve left and return an error if global environment id and passed id dont match', async () => { mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ userUid: 'abc123', id: 'genv2', @@ -441,75 +540,41 @@ describe('UserEnvironmentsService', () => { return expect( await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'), - ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); }); - }); - describe('publishUserEnvironmentSubscription', () => { - test('Should publish a created subscription', async () => { + test('Should resolve right,delete all variables inside users global environment and publish an updated subscription', async () => { + mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [{}], + isGlobal: true, + }); + + mockPrisma.userEnvironment.update.mockResolvedValueOnce({ + userUid: 'abc123', + id: 'env1', + name: 'en1', + variables: [], + isGlobal: true, + }); + const result: UserEnvironment = { userUid: 'abc123', - id: '123', - name: '', - variables: JSON.stringify([{}]), + id: 'env1', + name: 'en1', + variables: JSON.stringify([]), isGlobal: true, }; - await mockPubSub.publish( - `user_environment/${result.userUid}/created`, + await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1'); + + return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( + `user_environment/${result.id}`, + SubscriptionType.Updated, result, ); - - return expect( - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Created, - ), - ).toBeUndefined(); - }); - - test('Should publish a updated subscription', async () => { - const result: UserEnvironment = { - userUid: 'abc123', - id: '123', - name: '', - variables: JSON.stringify([{}]), - isGlobal: true, - }; - - await mockPubSub.publish( - `user_environment/${result.userUid}/updated`, - result, - ); - - return expect( - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Updated, - ), - ).toBeUndefined(); - }); - - test('Should publish a deleted subscription', async () => { - const result: UserEnvironment = { - userUid: 'abc123', - id: '123', - name: '', - variables: JSON.stringify([{}]), - isGlobal: true, - }; - - await mockPubSub.publish( - `user_environment/${result.userUid}/deleted`, - result, - ); - - return expect( - await userEnvironmentsService.publishUserEnvironmentSubscription( - result, - SubscriptionType.Deleted, - ), - ).toBeUndefined(); }); }); }); From 0a469f4ccf7c2c6fa64536ceedbbd69c090f4257 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 12:57:59 +0530 Subject: [PATCH 51/95] chore: introduced subscription handler and fixed requested review changes --- .../user-environments.service.ts | 149 ++++++++---------- 1 file changed, 68 insertions(+), 81 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index b09d9a5e3..7bf8c7ae1 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -3,20 +3,24 @@ import { UserEnvironment } from './user-environments.model'; import { PrismaService } from '../prisma/prisma.service'; import { PubSubService } from '../pubsub/pubsub.service'; import * as E from 'fp-ts/Either'; +import * as O from 'fp-ts/Option'; import { - USER_ENVIRONMENT_ENV_DOESNT_EXISTS, + USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS, + USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS, USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED, - USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS, USER_ENVIRONMENT_GLOBAL_ENV_EXISTS, USER_ENVIRONMENT_IS_NOT_GLOBAL, USER_ENVIRONMENT_UPDATE_FAILED, + USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, } from '../errors'; +import { SubscriptionHandler } from '../subscription-handler'; -// Contains constants for the subscription types we send to pubsub service +// Contains constants for the subscription types we send to subscription handler enum SubscriptionType { Created = 'created', Updated = 'updated', Deleted = 'deleted', + DeleteMany = 'delete_many', } @Injectable() @@ -24,6 +28,7 @@ export class UserEnvironmentsService { constructor( private readonly prisma: PrismaService, private readonly pubsub: PubSubService, + private readonly subscriptionHandler: SubscriptionHandler, ) {} /** @@ -75,13 +80,13 @@ export class UserEnvironmentsService { }); } - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); } /** * Create a personal or global user environment * @param uid Users uid - * @param name environments name + * @param name environments name, null if the environment is global * @param variables environment variables * @param isGlobal flag to indicate type of environment to create * @returns an `UserEnvironment` object @@ -95,9 +100,11 @@ export class UserEnvironmentsService { // Check for existing global env for a user if exists error out to avoid recreation if (isGlobal) { const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) + if (!O.isNone(globalEnvExists)) return E.left(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS); } + if (name === null && !isGlobal) + return E.left(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME); const createdEnvironment = await this.prisma.userEnvironment.create({ data: { @@ -116,11 +123,11 @@ export class UserEnvironmentsService { isGlobal: createdEnvironment.isGlobal, }; // Publish subscription for environment creation - await this.publishUserEnvironmentSubscription( - userEnvironment, + await this.subscriptionHandler.publish( + `user_environment/${userEnvironment.userUid}`, SubscriptionType.Created, + userEnvironment, ); - return E.right(userEnvironment); } @@ -149,13 +156,14 @@ export class UserEnvironmentsService { isGlobal: updatedEnvironment.isGlobal, }; // Publish subscription for environment update - await this.publishUserEnvironmentSubscription( - updatedUserEnvironment, + await this.subscriptionHandler.publish( + `user_environment/${updatedUserEnvironment.id}`, SubscriptionType.Updated, + updatedUserEnvironment, ); return E.right(updatedUserEnvironment); } catch (e) { - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); } } @@ -169,8 +177,8 @@ export class UserEnvironmentsService { try { // check if id is of a global environment if it is, don't delete and error out const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) { - const globalEnv = globalEnvExists.right; + if (O.isSome(globalEnvExists)) { + const globalEnv = globalEnvExists.value; if (globalEnv.id === id) { return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED); } @@ -188,14 +196,16 @@ export class UserEnvironmentsService { variables: JSON.stringify(deletedEnvironment.variables), isGlobal: deletedEnvironment.isGlobal, }; - // Publish subscription for environment creation - await this.publishUserEnvironmentSubscription( - deletedUserEnvironment, + + // Publish subscription for environment deletion + await this.subscriptionHandler.publish( + `user_environment/${deletedUserEnvironment.id}`, SubscriptionType.Deleted, + deletedUserEnvironment, ); - return E.right(deletedUserEnvironment); + return E.right(true); } catch (e) { - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); + return E.left(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); } } @@ -211,75 +221,53 @@ export class UserEnvironmentsService { isGlobal: false, }, }); - return deletedEnvironments.count; + + await this.subscriptionHandler.publish( + `user_environment/${uid}`, + SubscriptionType.DeleteMany, + deletedEnvironments.count, + ); } /** - * Deletes all existing variables in a users global environment + * Removes all existing variables in a users global environment * @param uid users uid * @param id environment id * @returns an `` of environments deleted */ - async deleteAllVariablesFromUsersGlobalEnvironment(uid: string, id: string) { + async clearGlobalEnvironments(uid: string, id: string) { const globalEnvExists = await this.checkForExistingGlobalEnv(uid); - if (E.isRight(globalEnvExists)) { - const env = globalEnvExists.right; - if (env.id === id) { - try { - const updatedEnvironment = await this.prisma.userEnvironment.update({ - where: { id: id }, - data: { - variables: [], - }, - }); - const updatedUserEnvironment: UserEnvironment = { - userUid: updatedEnvironment.userUid, - id: updatedEnvironment.id, - name: updatedEnvironment.name, - variables: JSON.stringify(updatedEnvironment.variables), - isGlobal: updatedEnvironment.isGlobal, - }; - // Publish subscription for environment creation - await this.publishUserEnvironmentSubscription( - updatedUserEnvironment, - SubscriptionType.Updated, - ); - return E.right(updatedUserEnvironment); - } catch (e) { - return E.left(USER_ENVIRONMENT_UPDATE_FAILED); - } - } else return E.left(USER_ENVIRONMENT_IS_NOT_GLOBAL); - } - return E.left(USER_ENVIRONMENT_ENV_DOESNT_EXISTS); - } + if (O.isNone(globalEnvExists)) + return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS); - // Method to publish subscriptions based on the subscription type of the environment - async publishUserEnvironmentSubscription( - userEnv: UserEnvironment, - subscriptionType: SubscriptionType, - ) { - switch (subscriptionType) { - case SubscriptionType.Created: - await this.pubsub.publish( - `user_environment/${userEnv.userUid}/created`, - userEnv, + const env = globalEnvExists.value; + if (env.id === id) { + try { + const updatedEnvironment = await this.prisma.userEnvironment.update({ + where: { id: id }, + data: { + variables: [], + }, + }); + const updatedUserEnvironment: UserEnvironment = { + userUid: updatedEnvironment.userUid, + id: updatedEnvironment.id, + name: updatedEnvironment.name, + variables: JSON.stringify(updatedEnvironment.variables), + isGlobal: updatedEnvironment.isGlobal, + }; + + // Publish subscription for environment update + await this.subscriptionHandler.publish( + `user_environment/${updatedUserEnvironment.id}`, + SubscriptionType.Updated, + updatedUserEnvironment, ); - break; - case SubscriptionType.Updated: - await this.pubsub.publish( - `user_environment/${userEnv.id}/updated`, - userEnv, - ); - break; - case SubscriptionType.Deleted: - await this.pubsub.publish( - `user_environment/${userEnv.id}/deleted`, - userEnv, - ); - break; - default: - break; - } + return E.right(updatedUserEnvironment); + } catch (e) { + return E.left(USER_ENVIRONMENT_UPDATE_FAILED); + } + } else return E.left(USER_ENVIRONMENT_IS_NOT_GLOBAL); } // Method to check for existing global environments for a given user uid @@ -290,9 +278,8 @@ export class UserEnvironmentsService { isGlobal: true, }, }); - if (globalEnv === null) - return E.left(USER_ENVIRONMENT_GLOBAL_ENV_DOESNT_EXISTS); - return E.right(globalEnv); + if (globalEnv == null) return O.none; + return O.some(globalEnv); } } From 2252048d2e4c04d7a1efa4cdfbb31d52f8cc774f Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 16:20:12 +0530 Subject: [PATCH 52/95] chore: updated module type name --- .../src/types/{module-types.ts => custom-module-types.ts} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename packages/hoppscotch-backend/src/types/{module-types.ts => custom-module-types.ts} (68%) diff --git a/packages/hoppscotch-backend/src/types/module-types.ts b/packages/hoppscotch-backend/src/types/custom-module-types.ts similarity index 68% rename from packages/hoppscotch-backend/src/types/module-types.ts rename to packages/hoppscotch-backend/src/types/custom-module-types.ts index 356fcc816..5d669e075 100644 --- a/packages/hoppscotch-backend/src/types/module-types.ts +++ b/packages/hoppscotch-backend/src/types/custom-module-types.ts @@ -1,4 +1,4 @@ import { UserEnvironment } from '../user-environment/user-environments.model'; import { User } from '../user/user.model'; -export type ModuleTypes = UserEnvironment | User; +export type CustomModuleTypes = UserEnvironment | User; From 86aa0251abd34c820a723b01ff53b8221d6c02c4 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 16:21:05 +0530 Subject: [PATCH 53/95] chore: made review changes --- packages/hoppscotch-backend/src/errors.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index b95e25b14..a03e437c2 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) @@ -160,7 +166,7 @@ export const TEAM_ENVIRONMENT_NOT_TEAM_MEMBER = * (UserEnvironmentsService) */ export const USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS = - 'user_environment/global_env_doesnt_exists' as const; + 'user_environment/global_env_does_not_exists' as const; /* /** @@ -176,7 +182,7 @@ export const USER_ENVIRONMENT_GLOBAL_ENV_EXISTS = * (UserEnvironmentsService) */ export const USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS = - 'user_environment/user_env_doesnt_exists' as const; + 'user_environment/user_env_does_not_exists' as const; /* /** From 669f8b043116f9be4fd09a79cfad331262cdbe61 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 16:22:49 +0530 Subject: [PATCH 54/95] chore: made review changes for resolvers and introduced stringToJson from user-settings --- .../user-environments.resolver.ts | 27 ++++++++++--------- .../user-environments.service.spec.ts | 16 +++++------ .../user-environments.service.ts | 18 ++++++------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index c73156600..17cda8902 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -61,11 +61,10 @@ export class UserEnvironmentsResolver { variables: string, ): Promise { const isGlobal = true; - const globalEnvName: string = null; const userEnvironment = await this.userEnvironmentsService.createUserEnvironment( user.uid, - globalEnvName, + null, variables, isGlobal, ); @@ -126,7 +125,7 @@ export class UserEnvironmentsResolver { } @Mutation(() => Number, { - description: 'Deletes users all personal environments', + description: 'Deletes all of users personal environments', }) @UseGuards(GqlAuthGuard) async deleteUserEnvironments(@GqlUser() user: User): Promise { @@ -159,15 +158,8 @@ export class UserEnvironmentsResolver { resolve: (value) => value, }) @UseGuards(GqlAuthGuard) - userEnvironmentCreated( - @Args({ - name: 'userUid', - description: 'Users uid', - type: () => ID, - }) - userUid: string, - ) { - return this.pubsub.asyncIterator(`user_environment/${userUid}/created`); + userEnvironmentCreated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_environment/${user.uid}/created`); } @Subscription(() => UserEnvironment, { @@ -201,4 +193,15 @@ export class UserEnvironmentsResolver { ) { return this.pubsub.asyncIterator(`user_environment/${id}/deleted`); } + + @Subscription(() => UserEnvironment, { + description: 'Listen for User Environment DeleteMany', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userEnvironmentDeleteMany(@GqlUser() user: User) { + return this.pubsub.asyncIterator( + `user_environment/${user.uid}/delete_many`, + ); + } } diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts index f41287f1d..894872cde 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts @@ -10,6 +10,7 @@ import { } from '../errors'; import { PubSubService } from '../pubsub/pubsub.service'; import { SubscriptionHandler } from '../subscription-handler'; +import { SubscriptionType } from '../types/subscription-types'; const mockPrisma = mockDeep(); const mockPubSub = mockDeep(); @@ -23,13 +24,6 @@ const userEnvironmentsService = new UserEnvironmentsService( mockSubscriptionHandler, ); -enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', - DeleteMany = 'delete_many', -} - const userPersonalEnvironments = [ { userUiD: 'abc123', @@ -344,7 +338,9 @@ describe('UserEnvironmentsService', () => { }); test('Should resolve left and not update a users environment if env doesnt exist ', async () => { - mockPrisma.userEnvironment.update.mockRejectedValueOnce({}); + mockPrisma.userEnvironment.update.mockRejectedValueOnce( + 'RejectOnNotFound', + ); return expect( await userEnvironmentsService.updateUserEnvironment( @@ -355,7 +351,7 @@ describe('UserEnvironmentsService', () => { ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS); }); - test('Should resolve right, update a users personal environment and publish an updated subscription ', async () => { + test('Should update a users personal environment and publish an updated subscription ', async () => { mockPrisma.userEnvironment.update.mockResolvedValueOnce({ userUid: 'abc123', id: '123', @@ -385,7 +381,7 @@ describe('UserEnvironmentsService', () => { ); }); - test('Should resolve right, update a users global environment and publish an updated subscription ', async () => { + test('Should update a users global environment and publish an updated subscription ', async () => { mockPrisma.userEnvironment.update.mockResolvedValueOnce({ userUid: 'abc123', id: '123', diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index 7bf8c7ae1..290b414da 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -14,14 +14,8 @@ import { USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, } from '../errors'; import { SubscriptionHandler } from '../subscription-handler'; - -// Contains constants for the subscription types we send to subscription handler -enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', - DeleteMany = 'delete_many', -} +import { SubscriptionType } from '../types/subscription-types'; +import { stringToJson } from '../utils'; @Injectable() export class UserEnvironmentsService { @@ -106,11 +100,13 @@ export class UserEnvironmentsService { if (name === null && !isGlobal) return E.left(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME); + const envVariables = stringToJson(variables); + if (E.isLeft(envVariables)) return E.left(envVariables.left); const createdEnvironment = await this.prisma.userEnvironment.create({ data: { userUid: uid, name: name, - variables: JSON.parse(variables), + variables: envVariables.right, isGlobal: isGlobal, }, }); @@ -139,12 +135,14 @@ export class UserEnvironmentsService { * @returns an Either of `UserEnvironment` or error */ async updateUserEnvironment(id: string, name: string, variables: string) { + const envVariables = stringToJson(variables); + if (E.isLeft(envVariables)) return E.left(envVariables.left); try { const updatedEnvironment = await this.prisma.userEnvironment.update({ where: { id: id }, data: { name: name, - variables: JSON.parse(variables), + variables: envVariables.right, }, }); From f6f4547af364b5eff70ac83b9fa5e2bed28be2e7 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 16:24:31 +0530 Subject: [PATCH 55/95] chore: introduced string to json from user-settings and moved types --- .../src/subscription-handler.ts | 17 +++++------------ .../src/types/subscription-types.ts | 7 +++++++ packages/hoppscotch-backend/src/utils.ts | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 packages/hoppscotch-backend/src/types/subscription-types.ts diff --git a/packages/hoppscotch-backend/src/subscription-handler.ts b/packages/hoppscotch-backend/src/subscription-handler.ts index fba5551a4..a47730c3b 100644 --- a/packages/hoppscotch-backend/src/subscription-handler.ts +++ b/packages/hoppscotch-backend/src/subscription-handler.ts @@ -1,18 +1,11 @@ import { Injectable } from '@nestjs/common'; import { PubSubService } from './pubsub/pubsub.service'; import { PrimitiveTypes } from './types/primitive-types'; -import { ModuleTypes } from './types/module-types'; +import { CustomModuleTypes } from './types/custom-module-types'; +import { SubscriptionType } from './types/subscription-types'; // Custom generic type to indicate the type of module -type ModuleType = PrimitiveTypes | ModuleTypes; - -// Contains constants for the subscription types we use in Subscription Handler -enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', - DeleteMany = 'delete_many', -} +type ModuleType = PrimitiveTypes | CustomModuleTypes; @Injectable() export class SubscriptionHandler { @@ -20,8 +13,8 @@ export class SubscriptionHandler { /** * Publishes a subscription using the pubsub module - * @param topic a string containing the module name, an uid and the type of subscription - * @param subscriptionType type of subscription being called + * @param topic a string containing the "module_name/identifier" + * @param subscriptionType type of subscription being published * @param moduleType type of the module model being called * @returns a promise of type void */ diff --git a/packages/hoppscotch-backend/src/types/subscription-types.ts b/packages/hoppscotch-backend/src/types/subscription-types.ts new file mode 100644 index 000000000..ee7ab75f2 --- /dev/null +++ b/packages/hoppscotch-backend/src/types/subscription-types.ts @@ -0,0 +1,7 @@ +// Contains constants for the subscription types we use in Subscription Handler +export enum SubscriptionType { + Created = 'created', + Updated = 'updated', + Deleted = 'deleted', + DeleteMany = 'delete_many', +} diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts index a4756f5e6..570313640 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. @@ -108,3 +110,18 @@ export const taskEitherValidateArraySeq = ( TE.getApplicativeTaskValidation(T.ApplicativeSeq, A.getMonoid()), ), ); + +/** + * 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); + } +} From ca5404a93b61c3361379bb74833110bdb29efbff Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Thu, 19 Jan 2023 16:25:36 +0530 Subject: [PATCH 56/95] chore: updated ts config with review changes --- packages/hoppscotch-backend/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/tsconfig.json b/packages/hoppscotch-backend/tsconfig.json index 22cc39ab4..02420b5f7 100644 --- a/packages/hoppscotch-backend/tsconfig.json +++ b/packages/hoppscotch-backend/tsconfig.json @@ -17,6 +17,6 @@ "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false, + "noFallthroughCasesInSwitch": true, } } From 08ac9680d798c47cc39f235c4c7ff07dcdc4beaf Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Thu, 19 Jan 2023 17:11:32 +0600 Subject: [PATCH 57/95] 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 8929b37dbe685d06683874f0fd13ce7581375e4f Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 20 Jan 2023 04:26:05 +0530 Subject: [PATCH 58/95] fix: changed return type of deleteUserEnvironment mutation to boolean in user-environments resolver --- .../src/user-environment/user-environments.resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index 17cda8902..bdb18ab1f 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -105,7 +105,7 @@ export class UserEnvironmentsResolver { return userEnvironment.right; } - @Mutation(() => UserEnvironment, { + @Mutation(() => Boolean, { description: 'Deletes a users personal environment', }) @UseGuards(GqlAuthGuard) From da9fcd10876d3b9cfaf7ddca65bac9189c092e27 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 20 Jan 2023 04:36:30 +0530 Subject: [PATCH 59/95] refactor: changed the return type of deleteUserEnvironments method to number from void --- .../src/user-environment/user-environments.resolver.ts | 2 +- .../src/user-environment/user-environments.service.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index bdb18ab1f..45530694d 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -128,7 +128,7 @@ export class UserEnvironmentsResolver { description: 'Deletes all of users personal environments', }) @UseGuards(GqlAuthGuard) - async deleteUserEnvironments(@GqlUser() user: User): Promise { + async deleteUserEnvironments(@GqlUser() user: User): Promise { return await this.userEnvironmentsService.deleteUserEnvironments(user.uid); } diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index 290b414da..a08549e0c 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -225,6 +225,8 @@ export class UserEnvironmentsService { SubscriptionType.DeleteMany, deletedEnvironments.count, ); + + return deletedEnvironments.count; } /** From cde0ba11fa70833975b920d190586a035c7dce45 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 20 Jan 2023 04:55:35 +0530 Subject: [PATCH 60/95] fix: changed the return type of delete many subscription to number from user-environment type --- .../src/user-environment/user-environments.resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index 45530694d..f053905bb 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -194,7 +194,7 @@ export class UserEnvironmentsResolver { return this.pubsub.asyncIterator(`user_environment/${id}/deleted`); } - @Subscription(() => UserEnvironment, { + @Subscription(() => Number, { description: 'Listen for User Environment DeleteMany', resolve: (value) => value, }) From a0006f73acc2364ac5528f96cb2cff329eb700f3 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Fri, 20 Jan 2023 14:55:26 +0530 Subject: [PATCH 61/95] chore: added message types to PubSub Service --- .../src/pubsub/pubsub.service.ts | 6 +++++- .../src/pubsub/subscriptionTopicsDefs.ts | 14 ++++++++++++++ .../{primitive-types.ts => primitiveTypes.ts} | 0 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts rename packages/hoppscotch-backend/src/types/{primitive-types.ts => primitiveTypes.ts} (100%) diff --git a/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts b/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts index 475768712..854ef2e92 100644 --- a/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts +++ b/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts @@ -4,6 +4,7 @@ import { default as Redis, RedisOptions } from 'ioredis'; import { RedisPubSub } from 'graphql-redis-subscriptions'; import { PubSub as LocalPubSub } from 'graphql-subscriptions'; +import { MessageType } from './subscriptionTopicsDefs'; /** * RedisPubSub uses JSON parsing for back and forth conversion, which loses Date objects, hence this reviver brings them back @@ -70,7 +71,10 @@ export class PubSubService implements OnModuleInit { return this.pubsub.asyncIterator(topic, options); } - async publish(topic: string, payload: any) { + async publish( + topic: T, + payload: MessageType[T], + ) { await this.pubsub.publish(topic, payload); } } diff --git a/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts new file mode 100644 index 000000000..c860d55a4 --- /dev/null +++ b/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts @@ -0,0 +1,14 @@ +import { UserEnvironment } from '../user-environment/user-environments.model'; +import { PrimitiveTypes } from '../types/primitiveTypes'; + +// 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. +export type MessageType = { + [ + topic: `user_environment/${string}/${ + | 'created' + | 'updated' + | 'deleted' + | 'deleted_many'}` + ]: UserEnvironment | PrimitiveTypes; // Returning a number hence having a union with `PrimitiveTypes`. +}; diff --git a/packages/hoppscotch-backend/src/types/primitive-types.ts b/packages/hoppscotch-backend/src/types/primitiveTypes.ts similarity index 100% rename from packages/hoppscotch-backend/src/types/primitive-types.ts rename to packages/hoppscotch-backend/src/types/primitiveTypes.ts From bc55af27a725f08aab75dc643420eeb033f8e3ff Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Fri, 20 Jan 2023 14:56:28 +0530 Subject: [PATCH 62/95] chore: updated user environment to use PubSub instead of SubscriptionHandler --- .../user-environments.module.ts | 2 - .../user-environments.service.spec.ts | 39 +++++++------------ .../user-environments.service.ts | 33 +++++++--------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts index d82a1e899..04f391682 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.module.ts @@ -5,7 +5,6 @@ import { UserModule } from '../user/user.module'; import { UserEnvsUserResolver } from './user.resolver'; import { UserEnvironmentsResolver } from './user-environments.resolver'; import { UserEnvironmentsService } from './user-environments.service'; -import { SubscriptionHandler } from '../subscription-handler'; @Module({ imports: [PrismaModule, PubSubModule, UserModule], @@ -13,7 +12,6 @@ import { SubscriptionHandler } from '../subscription-handler'; UserEnvironmentsResolver, UserEnvironmentsService, UserEnvsUserResolver, - SubscriptionHandler, ], exports: [UserEnvironmentsService], }) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts index 894872cde..849b589e1 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts @@ -9,19 +9,15 @@ import { USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, } from '../errors'; import { PubSubService } from '../pubsub/pubsub.service'; -import { SubscriptionHandler } from '../subscription-handler'; -import { SubscriptionType } from '../types/subscription-types'; const mockPrisma = mockDeep(); const mockPubSub = mockDeep(); -const mockSubscriptionHandler = mockDeep(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const userEnvironmentsService = new UserEnvironmentsService( mockPrisma, mockPubSub as any, - mockSubscriptionHandler, ); const userPersonalEnvironments = [ @@ -245,9 +241,8 @@ describe('UserEnvironmentsService', () => { false, ); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/${result.userUid}`, - SubscriptionType.Created, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${result.userUid}/created`, result, ); }); @@ -276,9 +271,8 @@ describe('UserEnvironmentsService', () => { true, ); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/${result.userUid}`, - SubscriptionType.Created, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${result.userUid}/created`, result, ); }); @@ -374,9 +368,8 @@ describe('UserEnvironmentsService', () => { '[{}]', ); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/${result.id}`, - SubscriptionType.Updated, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${result.id}/updated`, result, ); }); @@ -404,9 +397,8 @@ describe('UserEnvironmentsService', () => { '[{}]', ); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/${result.id}`, - SubscriptionType.Updated, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${result.id}/updated`, result, ); }); @@ -470,9 +462,8 @@ describe('UserEnvironmentsService', () => { await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/${result.id}`, - SubscriptionType.Deleted, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${result.id}/deleted`, result, ); }); @@ -486,9 +477,8 @@ describe('UserEnvironmentsService', () => { await userEnvironmentsService.deleteUserEnvironments('abc123'); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/abc123`, - SubscriptionType.DeleteMany, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${'abc123'}/deleted_many`, 1, ); }); @@ -566,9 +556,8 @@ describe('UserEnvironmentsService', () => { await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1'); - return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith( - `user_environment/${result.id}`, - SubscriptionType.Updated, + return expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_environment/${result.id}/updated`, result, ); }); diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index a08549e0c..872395351 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -13,8 +13,6 @@ import { USER_ENVIRONMENT_UPDATE_FAILED, USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME, } from '../errors'; -import { SubscriptionHandler } from '../subscription-handler'; -import { SubscriptionType } from '../types/subscription-types'; import { stringToJson } from '../utils'; @Injectable() @@ -22,7 +20,6 @@ export class UserEnvironmentsService { constructor( private readonly prisma: PrismaService, private readonly pubsub: PubSubService, - private readonly subscriptionHandler: SubscriptionHandler, ) {} /** @@ -119,9 +116,8 @@ export class UserEnvironmentsService { isGlobal: createdEnvironment.isGlobal, }; // Publish subscription for environment creation - await this.subscriptionHandler.publish( - `user_environment/${userEnvironment.userUid}`, - SubscriptionType.Created, + await this.pubsub.publish( + `user_environment/${userEnvironment.userUid}/created`, userEnvironment, ); return E.right(userEnvironment); @@ -154,9 +150,8 @@ export class UserEnvironmentsService { isGlobal: updatedEnvironment.isGlobal, }; // Publish subscription for environment update - await this.subscriptionHandler.publish( - `user_environment/${updatedUserEnvironment.id}`, - SubscriptionType.Updated, + await this.pubsub.publish( + `user_environment/${updatedUserEnvironment.id}/updated`, updatedUserEnvironment, ); return E.right(updatedUserEnvironment); @@ -196,9 +191,8 @@ export class UserEnvironmentsService { }; // Publish subscription for environment deletion - await this.subscriptionHandler.publish( - `user_environment/${deletedUserEnvironment.id}`, - SubscriptionType.Deleted, + await this.pubsub.publish( + `user_environment/${deletedUserEnvironment.id}/deleted`, deletedUserEnvironment, ); return E.right(true); @@ -220,9 +214,13 @@ export class UserEnvironmentsService { }, }); - await this.subscriptionHandler.publish( - `user_environment/${uid}`, - SubscriptionType.DeleteMany, + // await this.subscriptionHandler.publish( + // `user_environment/${uid}`, + // SubscriptionType.DeleteMany, + // deletedEnvironments.count, + // ); + await this.pubsub.publish( + `user_environment/${uid}/deleted_many`, deletedEnvironments.count, ); @@ -258,9 +256,8 @@ export class UserEnvironmentsService { }; // Publish subscription for environment update - await this.subscriptionHandler.publish( - `user_environment/${updatedUserEnvironment.id}`, - SubscriptionType.Updated, + await this.pubsub.publish( + `user_environment/${updatedUserEnvironment.id}/updated`, updatedUserEnvironment, ); return E.right(updatedUserEnvironment); From 0bed5cd99a519ba39a7d08a8e15680c8291f5189 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Fri, 20 Jan 2023 14:57:47 +0530 Subject: [PATCH 63/95] chore: removed subscription handler related files --- .../src/subscription-handler.ts | 43 ------------------- .../src/types/custom-module-types.ts | 4 -- .../src/types/subscription-types.ts | 7 --- 3 files changed, 54 deletions(-) delete mode 100644 packages/hoppscotch-backend/src/subscription-handler.ts delete mode 100644 packages/hoppscotch-backend/src/types/custom-module-types.ts delete mode 100644 packages/hoppscotch-backend/src/types/subscription-types.ts diff --git a/packages/hoppscotch-backend/src/subscription-handler.ts b/packages/hoppscotch-backend/src/subscription-handler.ts deleted file mode 100644 index a47730c3b..000000000 --- a/packages/hoppscotch-backend/src/subscription-handler.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { PubSubService } from './pubsub/pubsub.service'; -import { PrimitiveTypes } from './types/primitive-types'; -import { CustomModuleTypes } from './types/custom-module-types'; -import { SubscriptionType } from './types/subscription-types'; - -// Custom generic type to indicate the type of module -type ModuleType = PrimitiveTypes | CustomModuleTypes; - -@Injectable() -export class SubscriptionHandler { - constructor(private readonly pubsub: PubSubService) {} - - /** - * Publishes a subscription using the pubsub module - * @param topic a string containing the "module_name/identifier" - * @param subscriptionType type of subscription being published - * @param moduleType type of the module model being called - * @returns a promise of type void - */ - async publish( - topic: string, - subscriptionType: SubscriptionType, - moduleType: ModuleType, - ) { - switch (subscriptionType) { - case SubscriptionType.Created: - await this.pubsub.publish(`${topic}/created`, moduleType); - break; - case SubscriptionType.Updated: - await this.pubsub.publish(`${topic}/updated`, moduleType); - break; - case SubscriptionType.Deleted: - await this.pubsub.publish(`${topic}/deleted`, moduleType); - break; - case SubscriptionType.DeleteMany: - await this.pubsub.publish(`${topic}/delete_many`, moduleType); - break; - default: - break; - } - } -} diff --git a/packages/hoppscotch-backend/src/types/custom-module-types.ts b/packages/hoppscotch-backend/src/types/custom-module-types.ts deleted file mode 100644 index 5d669e075..000000000 --- a/packages/hoppscotch-backend/src/types/custom-module-types.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { UserEnvironment } from '../user-environment/user-environments.model'; -import { User } from '../user/user.model'; - -export type CustomModuleTypes = UserEnvironment | User; diff --git a/packages/hoppscotch-backend/src/types/subscription-types.ts b/packages/hoppscotch-backend/src/types/subscription-types.ts deleted file mode 100644 index ee7ab75f2..000000000 --- a/packages/hoppscotch-backend/src/types/subscription-types.ts +++ /dev/null @@ -1,7 +0,0 @@ -// Contains constants for the subscription types we use in Subscription Handler -export enum SubscriptionType { - Created = 'created', - Updated = 'updated', - Deleted = 'deleted', - DeleteMany = 'delete_many', -} From 298b960ef75054cd13c83a6cac13edbc46919364 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Fri, 20 Jan 2023 15:18:04 +0530 Subject: [PATCH 64/95] chore: removed comment --- .../src/user-environment/user-environments.service.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts index 872395351..9daa9cb46 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.ts @@ -214,11 +214,7 @@ export class UserEnvironmentsService { }, }); - // await this.subscriptionHandler.publish( - // `user_environment/${uid}`, - // SubscriptionType.DeleteMany, - // deletedEnvironments.count, - // ); + // Publish subscription for multiple environment deletions await this.pubsub.publish( `user_environment/${uid}/deleted_many`, deletedEnvironments.count, From 6da85fd286b9aa06d6ce4ccc716d8b5104dbe0ce Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Fri, 20 Jan 2023 16:12:45 +0530 Subject: [PATCH 65/95] chore: updated types to def and changed deleted many as a new indexed type --- .../hoppscotch-backend/src/pubsub/pubsub.service.ts | 7 ++----- .../src/pubsub/subscriptionTopicsDefs.ts | 12 ++++-------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts b/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts index 854ef2e92..ba86342e1 100644 --- a/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts +++ b/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts @@ -4,7 +4,7 @@ import { default as Redis, RedisOptions } from 'ioredis'; import { RedisPubSub } from 'graphql-redis-subscriptions'; import { PubSub as LocalPubSub } from 'graphql-subscriptions'; -import { MessageType } from './subscriptionTopicsDefs'; +import { TopicDef } from './subscriptionTopicsDefs'; /** * RedisPubSub uses JSON parsing for back and forth conversion, which loses Date objects, hence this reviver brings them back @@ -71,10 +71,7 @@ export class PubSubService implements OnModuleInit { return this.pubsub.asyncIterator(topic, options); } - async publish( - topic: T, - payload: MessageType[T], - ) { + async publish(topic: T, payload: TopicDef[T]) { await this.pubsub.publish(topic, payload); } } diff --git a/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts index c860d55a4..4a1fbcb27 100644 --- a/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts +++ b/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts @@ -1,14 +1,10 @@ import { UserEnvironment } from '../user-environment/user-environments.model'; -import { PrimitiveTypes } from '../types/primitiveTypes'; // 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. -export type MessageType = { +export type TopicDef = { [ - topic: `user_environment/${string}/${ - | 'created' - | 'updated' - | 'deleted' - | 'deleted_many'}` - ]: UserEnvironment | PrimitiveTypes; // Returning a number hence having a union with `PrimitiveTypes`. + topic: `user_environment/${string}/${'created' | 'updated' | 'deleted'}` + ]: UserEnvironment; + [topic: `user_environment/${string}/deleted_many`]: number; }; From 606e0120ee3f9d9677eb0e80ade2a0dfba467fdd Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Fri, 20 Jan 2023 16:13:35 +0530 Subject: [PATCH 66/95] chore: removed the logic for primitiveTypes as it is unused --- packages/hoppscotch-backend/src/types/primitiveTypes.ts | 1 - 1 file changed, 1 deletion(-) delete mode 100644 packages/hoppscotch-backend/src/types/primitiveTypes.ts diff --git a/packages/hoppscotch-backend/src/types/primitiveTypes.ts b/packages/hoppscotch-backend/src/types/primitiveTypes.ts deleted file mode 100644 index 11918cf33..000000000 --- a/packages/hoppscotch-backend/src/types/primitiveTypes.ts +++ /dev/null @@ -1 +0,0 @@ -export type PrimitiveTypes = number | string | boolean; From 480a34c0f76d8c806a99d152b9d494f3fab1e941 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 09:54:20 +0530 Subject: [PATCH 67/95] fix: updated resolver pubsub topic name --- .../src/user-environment/user-environments.resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts index f053905bb..cccdf11cd 100644 --- a/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts +++ b/packages/hoppscotch-backend/src/user-environment/user-environments.resolver.ts @@ -201,7 +201,7 @@ export class UserEnvironmentsResolver { @UseGuards(GqlAuthGuard) userEnvironmentDeleteMany(@GqlUser() user: User) { return this.pubsub.asyncIterator( - `user_environment/${user.uid}/delete_many`, + `user_environment/${user.uid}/deleted_many`, ); } } From cb1b13bdb47689a90a7394b371a9e55cf9e512fe Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 11:49:41 +0530 Subject: [PATCH 68/95] chore: updated filename to topicDefs --- packages/hoppscotch-backend/src/pubsub/pubsub.service.ts | 2 +- .../src/pubsub/{subscriptionTopicsDefs.ts => topicsDefs.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/hoppscotch-backend/src/pubsub/{subscriptionTopicsDefs.ts => topicsDefs.ts} (100%) diff --git a/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts b/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts index ba86342e1..bec396028 100644 --- a/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts +++ b/packages/hoppscotch-backend/src/pubsub/pubsub.service.ts @@ -4,7 +4,7 @@ import { default as Redis, RedisOptions } from 'ioredis'; import { RedisPubSub } from 'graphql-redis-subscriptions'; import { PubSub as LocalPubSub } from 'graphql-subscriptions'; -import { TopicDef } from './subscriptionTopicsDefs'; +import { TopicDef } from './topicsDefs'; /** * RedisPubSub uses JSON parsing for back and forth conversion, which loses Date objects, hence this reviver brings them back diff --git a/packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts similarity index 100% rename from packages/hoppscotch-backend/src/pubsub/subscriptionTopicsDefs.ts rename to packages/hoppscotch-backend/src/pubsub/topicsDefs.ts From 523c650c9d4f3a6aa28e8d927e6db083a191fc50 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 13:13:19 +0530 Subject: [PATCH 69/95] 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 70/95] 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 71/95] 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 72/95] 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 73/95] 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 74/95] 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 75/95] 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 76/95] 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 77/95] 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 78/95] 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 79/95] 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 80/95] 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 81/95] 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 82/95] 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 83/95] 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 84/95] 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 85/95] 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 86/95] 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 87/95] 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 88/95] 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; From ebbe015bbc752aceb6e4b8602f21ce6c71f9111d Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 15:10:54 +0600 Subject: [PATCH 89/95] feat: db schema update from string to json column type --- .../hoppscotch-backend/prisma/schema.prisma | 4 +- packages/hoppscotch-backend/src/errors.ts | 6 +++ .../src/pubsub/topicsDefs.ts | 4 +- .../src/user/dtos/update-user-input.dto.ts | 32 ------------ .../hoppscotch-backend/src/user/user.model.ts | 33 +++++++++++- .../src/user/user.resolver.ts | 5 +- .../src/user/user.service.ts | 50 +++++++++++++++---- 7 files changed, 84 insertions(+), 50 deletions(-) delete mode 100644 packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 255781ec4..37247d244 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -83,8 +83,8 @@ model User { displayName String? email String? photoURL String? - currentRESTSession String? - currentGQLSession String? + currentRESTSession Json? + currentGQLSession Json? UserEnvironments UserEnvironment[] } diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index a03e437c2..fe2d6dd45 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -26,6 +26,12 @@ export const USER_FB_DOCUMENT_DELETION_FAILED = */ export const USER_NOT_FOUND = 'user/not_found' as const; +/** + * User update failure + * (UserService) + */ +export const USER_UPDATE_FAILED = 'user/update_failed' as const; + /** * User deletion failure * (UserService) diff --git a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts index d7bfa10d0..4d0cd959e 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/${string}/${'created' | 'updated' | 'deleted'}` - ]: User; + [topic: `user/${string}/${'updated'}`]: User; [topic: `user_environment/${string}/deleted_many`]: number; }; diff --git a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts b/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts deleted file mode 100644 index b5fd7d0a6..000000000 --- a/packages/hoppscotch-backend/src/user/dtos/update-user-input.dto.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Field, InputType } from '@nestjs/graphql'; - -@InputType() -export class UpdateUserInput { - @Field({ - nullable: true, - name: 'displayName', - description: 'Displayed name of the user (if given)', - }) - displayName?: string; - - @Field({ - nullable: true, - name: 'photoURL', - description: 'URL to the profile photo of the user (if given)', - }) - photoURL?: string; - - @Field({ - nullable: true, - name: 'currentRESTSession', - description: 'JSON string of the saved REST session', - }) - currentRESTSession?: string; - - @Field({ - nullable: true, - name: 'currentGQLSession', - description: 'JSON string of the saved GQL session', - }) - currentGQLSession?: string; -} diff --git a/packages/hoppscotch-backend/src/user/user.model.ts b/packages/hoppscotch-backend/src/user/user.model.ts index 6db48b3e1..da83a99c5 100644 --- a/packages/hoppscotch-backend/src/user/user.model.ts +++ b/packages/hoppscotch-backend/src/user/user.model.ts @@ -1,4 +1,4 @@ -import { ObjectType, ID, Field } from '@nestjs/graphql'; +import { ObjectType, ID, Field, InputType } from '@nestjs/graphql'; @ObjectType() export class User { @@ -37,3 +37,34 @@ export class User { }) currentGQLSession?: string; } + +@InputType() +export class UpdateUserInput { + @Field({ + nullable: true, + name: 'displayName', + description: 'Displayed name of the user (if given)', + }) + displayName?: string; + + @Field({ + nullable: true, + name: 'photoURL', + description: 'URL to the profile photo of the user (if given)', + }) + photoURL?: string; + + @Field({ + nullable: true, + name: 'currentRESTSession', + description: 'JSON string of the saved REST session', + }) + currentRESTSession?: string; + + @Field({ + nullable: true, + name: 'currentGQLSession', + description: 'JSON string of the saved GQL session', + }) + currentGQLSession?: string; +} diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index 99475d0d5..3d2abd9dc 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -1,12 +1,11 @@ import { Resolver, Query, Mutation, Args, Subscription } from '@nestjs/graphql'; -import { User } from './user.model'; +import { UpdateUserInput, User } from './user.model'; import { UseGuards } from '@nestjs/common'; import { GqlAuthGuard } from '../guards/gql-auth.guard'; import { GqlUser } from '../decorators/gql-user.decorator'; import { UserService } from './user.service'; import { throwErr } from 'src/utils'; import * as E from 'fp-ts/lib/Either'; -import { UpdateUserInput } from './dtos/update-user-input.dto'; import { PubSubService } from 'src/pubsub/pubsub.service'; @Resolver(() => User) @@ -58,7 +57,7 @@ export class UserResolver { resolve: (value) => value, }) @UseGuards(GqlAuthGuard) - userSettingsUpdated(@GqlUser() user: User) { + userUpdated(@GqlUser() user: User) { return this.pubsub.asyncIterator(`user/${user.uid}/updated`); } } diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index cdb1f64e2..a2ad712b4 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -1,10 +1,11 @@ import { Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; -import { User } from './user.model'; +import { UpdateUserInput, User } from './user.model'; +import { User as DbUser, Prisma } from '@prisma/client'; import * as E from 'fp-ts/lib/Either'; -import { USER_NOT_FOUND } from 'src/errors'; -import { UpdateUserInput } from './dtos/update-user-input.dto'; +import { USER_UPDATE_FAILED } from 'src/errors'; import { PubSubService } from 'src/pubsub/pubsub.service'; +import { stringToJson } from 'src/utils'; @Injectable() export class UserService { @@ -16,22 +17,53 @@ export class UserService { /** * Update a user's information * @param user User object - * @param updateUserData Properties to update + * @param updateData Properties to update * @returns a Either of User or error */ - async updateUser(user: User, updateUserData: UpdateUserInput) { + async updateUser( + user: User, + updateData: UpdateUserInput, + ): Promise> { + let { currentGQLSession, currentRESTSession, ...rest } = updateData; + let updateUserObj: Partial = rest; + + // Convert stringified JSON to JSON + if (updateData?.currentGQLSession !== undefined) { + const jsonGql = stringToJson(updateData.currentGQLSession); + if (E.isLeft(jsonGql)) return jsonGql; + + updateUserObj.currentGQLSession = jsonGql?.right ?? Prisma.DbNull; + } + if (updateData?.currentRESTSession !== undefined) { + const jsonRest = stringToJson(updateData.currentRESTSession); + if (E.isLeft(jsonRest)) return jsonRest; + + updateUserObj.currentRESTSession = jsonRest?.right ?? Prisma.DbNull; + } + + // Update user try { - const updatedUser = await this.prisma.user.update({ + const dbUpdatedUser = await this.prisma.user.update({ where: { uid: user.uid }, - data: updateUserData, + data: updateUserObj, }); + const updatedUser: User = { + ...dbUpdatedUser, + currentGQLSession: dbUpdatedUser.currentGQLSession + ? JSON.stringify(dbUpdatedUser.currentGQLSession) + : null, + currentRESTSession: dbUpdatedUser.currentRESTSession + ? JSON.stringify(dbUpdatedUser.currentRESTSession) + : null, + }; + // Publish subscription for user updates - await this.pubsub.publish(`user/${user.uid}/updated`, updatedUser); + await this.pubsub.publish(`user/${updatedUser.uid}/updated`, updatedUser); return E.right(updatedUser); } catch (e) { - return E.left(USER_NOT_FOUND); + return E.left(USER_UPDATE_FAILED); } } } From d7b02da719e38219ee8053f5504be919f8c13d3a Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 15:12:30 +0600 Subject: [PATCH 90/95] test: more test case added for user module --- .../src/user/user.service.spec.ts | 64 +++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/user.service.spec.ts b/packages/hoppscotch-backend/src/user/user.service.spec.ts index e5f293519..b9d4849ba 100644 --- a/packages/hoppscotch-backend/src/user/user.service.spec.ts +++ b/packages/hoppscotch-backend/src/user/user.service.spec.ts @@ -1,4 +1,5 @@ -import { DeepMockProxy, mockDeep, mockReset } from 'jest-mock-extended'; +import { mockDeep, mockReset } from 'jest-mock-extended'; +import { JSON_INVALID } from 'src/errors'; import { PrismaService } from 'src/prisma/prisma.service'; import { PubSubService } from 'src/pubsub/pubsub.service'; import { UserService } from './user.service'; @@ -25,8 +26,12 @@ beforeEach(() => { describe('UserService', () => { describe('updateUser', () => { - test('should update user', async () => { - mockPrisma.user.update.mockResolvedValue(user); + test('Should resolve and update user both GQL and REST session', async () => { + mockPrisma.user.update.mockResolvedValue({ + ...user, + currentGQLSession: JSON.parse(user.currentGQLSession), + currentRESTSession: JSON.parse(user.currentRESTSession), + }); const result = await userService.updateUser(user, { currentGQLSession: user.currentGQLSession, @@ -35,8 +40,57 @@ describe('UserService', () => { expect(result).toEqualRight(user); }); - test('should publish user update subscription', async () => { - mockPrisma.user.update.mockResolvedValue(user); + test('Should resolve and update user only with GQL session', async () => { + mockPrisma.user.update.mockResolvedValue({ + ...user, + currentGQLSession: JSON.parse(user.currentGQLSession), + currentRESTSession: undefined, + }); + + const result = await userService.updateUser(user, { + currentGQLSession: user.currentGQLSession, + }); + + expect(result).toEqualRight({ ...user, currentRESTSession: null }); + }); + test('Should reject update user for invalid GQL session', async () => { + const newGqlSession = null; + mockPrisma.user.update.mockResolvedValue({ + ...user, + currentGQLSession: newGqlSession, + currentRESTSession: undefined, + }); + + const result = await userService.updateUser(user, { + currentGQLSession: newGqlSession, + }); + + expect(result).toEqualRight({ + ...user, + currentGQLSession: newGqlSession, + currentRESTSession: null, + }); + }); + test('Should reject update user for invalid GQL session', async () => { + const newGqlSession = 'invalid json'; + mockPrisma.user.update.mockResolvedValue({ + ...user, + currentGQLSession: newGqlSession, + currentRESTSession: undefined, + }); + + const result = await userService.updateUser(user, { + currentGQLSession: newGqlSession, + }); + + expect(result).toEqualLeft(JSON_INVALID); + }); + test('Should publish pubsub message on user update', async () => { + mockPrisma.user.update.mockResolvedValue({ + ...user, + currentGQLSession: JSON.parse(user.currentGQLSession), + currentRESTSession: JSON.parse(user.currentRESTSession), + }); await userService.updateUser(user, { currentGQLSession: user.currentGQLSession, From 6627514e88844e342225ab33b1bbdd4fb5a8831e Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 15:24:11 +0600 Subject: [PATCH 91/95] chore: pulled from main --- packages/hoppscotch-backend/package.json | 2 +- .../hoppscotch-backend/prisma/schema.prisma | 26 + packages/hoppscotch-backend/src/app.module.ts | 4 + packages/hoppscotch-backend/src/errors.ts | 36 +- .../src/pubsub/topicsDefs.ts | 9 +- .../src/user-history/user-history.model.ts | 49 ++ .../src/user-history/user-history.module.ts | 14 + .../src/user-history/user-history.resolver.ts | 147 ++++++ .../user-history/user-history.service.spec.ts | 486 ++++++++++++++++++ .../src/user-history/user-history.service.ts | 209 ++++++++ .../src/user-history/user.resolver.ts | 27 + .../src/user-settings/user-settings.model.ts | 24 + .../src/user-settings/user-settings.module.ts | 17 + .../user-settings/user-settings.resolver.ts | 78 +++ .../user-settings.service.spec.ts | 125 +++++ .../user-settings/user-settings.service.ts | 110 ++++ .../src/user-settings/user.resolver.ts | 21 + 17 files changed, 1381 insertions(+), 3 deletions(-) create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.model.ts 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.spec.ts create mode 100644 packages/hoppscotch-backend/src/user-history/user-history.service.ts create mode 100644 packages/hoppscotch-backend/src/user-history/user.resolver.ts 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.spec.ts create mode 100644 packages/hoppscotch-backend/src/user-settings/user-settings.service.ts create mode 100644 packages/hoppscotch-backend/src/user-settings/user.resolver.ts diff --git a/packages/hoppscotch-backend/package.json b/packages/hoppscotch-backend/package.json index 9ee903f2e..9c442dbfe 100644 --- a/packages/hoppscotch-backend/package.json +++ b/packages/hoppscotch-backend/package.json @@ -88,4 +88,4 @@ "coverageDirectory": "../coverage", "testEnvironment": "node" } -} +} \ No newline at end of file diff --git a/packages/hoppscotch-backend/prisma/schema.prisma b/packages/hoppscotch-backend/prisma/schema.prisma index 37247d244..28e11e010 100644 --- a/packages/hoppscotch-backend/prisma/schema.prisma +++ b/packages/hoppscotch-backend/prisma/schema.prisma @@ -85,9 +85,35 @@ model User { photoURL String? currentRESTSession Json? currentGQLSession Json? + settings UserSettings? + UserHistory UserHistory[] UserEnvironments UserEnvironment[] } +model UserSettings { + 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 UserHistory { + id String @id @default(cuid()) + userUid String + user User @relation(fields: [userUid], references: [uid], onDelete: Cascade) + reqType ReqType + request Json + responseMetadata Json + isStarred Boolean + executedOn DateTime @default(now()) @db.Timestamp(3) +} + +enum ReqType { + REST + GQL +} + model UserEnvironment { id String @id @default(cuid()) userUid String diff --git a/packages/hoppscotch-backend/src/app.module.ts b/packages/hoppscotch-backend/src/app.module.ts index b9281e55d..80d1d83b4 100644 --- a/packages/hoppscotch-backend/src/app.module.ts +++ b/packages/hoppscotch-backend/src/app.module.ts @@ -3,7 +3,9 @@ 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'; import { UserEnvironmentsModule } from './user-environment/user-environments.module'; +import { UserHistoryModule } from './user-history/user-history.module'; @Module({ imports: [ @@ -45,7 +47,9 @@ import { UserEnvironmentsModule } from './user-environment/user-environments.mod driver: ApolloDriver, }), UserModule, + UserSettingsModule, UserEnvironmentsModule, + UserHistoryModule, ], providers: [GQLComplexityPlugin], }) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index fe2d6dd45..c78df062a 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -168,12 +168,29 @@ 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 already exists for a user + * (UserSettingsService) + */ +export const USER_SETTINGS_ALREADY_EXISTS = 'user_settings/settings_already_exists' as const; + +/** + * User setting invalid (null) settings + * (UserSettingsService) + */ +export const USER_SETTINGS_NULL_SETTINGS = 'user_settings/null_settings' as const; + +/* * Global environment doesnt exists for the user * (UserEnvironmentsService) */ export const USER_ENVIRONMENT_GLOBAL_ENV_DOES_NOT_EXISTS = 'user_environment/global_env_does_not_exists' as const; -/* /** * Global environment already exists for the user @@ -223,6 +240,23 @@ export const USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME = 'user_environment/user_env_invalid_env_name' 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; + +/* + |------------------------------------| |Server errors that are actually bugs| |------------------------------------| diff --git a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts index 4d0cd959e..cee338602 100644 --- a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts +++ b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts @@ -1,12 +1,19 @@ import { User } from 'src/user/user.model'; +import { UserSettings } from 'src/user-settings/user-settings.model'; 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. export type TopicDef = { + [topic: `user/${string}/${'updated'}`]: User; + [topic: `user_settings/${string}/${'created' | 'updated'}`]: UserSettings; [ topic: `user_environment/${string}/${'created' | 'updated' | 'deleted'}` ]: UserEnvironment; - [topic: `user/${string}/${'updated'}`]: User; [topic: `user_environment/${string}/deleted_many`]: number; + [ + topic: `user_history/${string}/${'created' | 'updated' | 'deleted'}` + ]: UserHistory; + [topic: `user_history/${string}/deleted_many`]: number; }; 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', +}); 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..65247c79f --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.resolver.ts @@ -0,0 +1,147 @@ +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'; +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 { + 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 createUserHistory( + @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: 'Request type, REST or GQL', + }) + reqType: string, + ): Promise { + const createdHistory = await this.userHistoryService.createUserHistory( + 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 toggleHistoryStarStatus( + @GqlUser() user: User, + @Args({ + name: 'id', + description: 'ID of User History', + }) + id: string, + ): Promise { + const updatedHistory = + await this.userHistoryService.toggleHistoryStarStatus(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: 'ID of User 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: 'Request type, 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(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_history/${user.uid}/created`); + } + + @Subscription(() => UserHistory, { + description: 'Listen for User History update', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + userHistoryUpdated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_history/${user.uid}/updated`); + } + + @Subscription(() => UserHistory, { + description: 'Listen for User History deletion', + resolve: (value) => value, + }) + @UseGuards(GqlAuthGuard) + 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`); + } +} 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..40e2343db --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts @@ -0,0 +1,486 @@ +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'; +import { ReqType as DBReqType } from '@prisma/client'; + +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, +); + +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: [{}], + reqType: ReqType.REST, + executedOn: executedOn, + isStarred: false, + }, + { + userUid: 'abc', + id: '2', + request: [{}], + responseMetadata: [{}], + reqType: 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: [{}], + reqType: ReqType.GQL, + executedOn: executedOn, + isStarred: false, + }, + { + userUid: 'abc', + id: '2', + request: [{}], + responseMetadata: [{}], + reqType: 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('createUserHistory', () => { + test('Should resolve right and create a REST request to users history and return a `UserHistory` object', async () => { + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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, + }; + + return expect( + await userHistoryService.createUserHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'REST', + ), + ).toEqualRight(userHistory); + }); + test('Should resolve right and create a GQL request to users history and return a `UserHistory` object', async () => { + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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, + }; + + return expect( + await userHistoryService.createUserHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'GQL', + ), + ).toEqualRight(userHistory); + }); + test('Should resolve left when invalid ReqType is passed', async () => { + return expect( + await userHistoryService.createUserHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'INVALID', + ), + ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE); + }); + test('Should create a GQL request to users history and publish a created subscription', async () => { + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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.createUserHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'GQL', + ); + + return expect(await mockPubSub.publish).toHaveBeenCalledWith( + `user_history/${userHistory.userUid}/created`, + userHistory, + ); + }); + test('Should create a REST request to users history and publish a created subscription', async () => { + mockPrisma.userHistory.create.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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.createUserHistory( + 'abc', + JSON.stringify([{}]), + JSON.stringify([{}]), + 'REST', + ); + + return expect(await mockPubSub.publish).toHaveBeenCalledWith( + `user_history/${userHistory.userUid}/created`, + userHistory, + ); + }); + }); + describe('toggleHistoryStarStatus', () => { + test('Should resolve right and star/unstar a request in the history', async () => { + mockPrisma.userHistory.findFirst.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + mockPrisma.userHistory.update.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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, + }; + + return expect( + await userHistoryService.toggleHistoryStarStatus('abc', '1'), + ).toEqualRight(userHistory); + }); + test('Should resolve left and error out due to invalid user history request ID', async () => { + mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null); + + return expect( + 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: [{}], + reqType: ReqType.REST, + executedOn: new Date(), + isStarred: false, + }); + + mockPrisma.userHistory.update.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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', async () => { + mockPrisma.userHistory.delete.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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, + }; + + 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); + }); + test('Should delete request from users history and publish deleted subscription', async () => { + mockPrisma.userHistory.delete.mockResolvedValueOnce({ + userUid: 'abc', + id: '1', + request: [{}], + responseMetadata: [{}], + reqType: 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 () => { + 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 () => { + 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 () => { + 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 () => { + 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 () => { + 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 () => { + 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, + ); + }); + }); +}); 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..8ee8da421 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -0,0 +1,209 @@ +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'; +import * as O from 'fp-ts/Option'; +import { + USER_HISTORY_INVALID_REQ_TYPE, + USER_HISTORY_NOT_FOUND, +} from '../errors'; + +@Injectable() +export class UserHistoryService { + constructor( + private readonly prisma: PrismaService, + private readonly pubsub: PubSubService, + ) {} + + /** + * 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 + */ + async fetchUserHistory(uid: string, reqType: ReqType) { + const userHistory = await this.prisma.userHistory.findMany({ + where: { + userUid: uid, + reqType: reqType, + }, + }); + + const userHistoryColl: UserHistory[] = userHistory.map( + (history) => + { + ...history, + request: JSON.stringify(history.request), + responseMetadata: JSON.stringify(history.responseMetadata), + }, + ); + + return userHistoryColl; + } + + /** + * 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 createUserHistory( + uid: string, + reqData: string, + resMetadata: string, + 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), + reqType: requestType.right, + isStarred: false, + }, + }); + + const userHistory = { + ...history, + reqType: history.reqType, + request: JSON.stringify(history.request), + responseMetadata: JSON.stringify(history.responseMetadata), + }; + + // Publish created user history subscription + await this.pubsub.publish( + `user_history/${userHistory.userUid}/created`, + userHistory, + ); + + return E.right(userHistory); + } + + /** + * 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 + */ + async toggleHistoryStarStatus(uid: string, id: string) { + 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.value.isStarred, + }, + }); + + const updatedUserHistory = { + ...updatedHistory, + request: JSON.stringify(updatedHistory.request), + responseMetadata: JSON.stringify(updatedHistory.responseMetadata), + }; + + // Publish updated user history subscription + await this.pubsub.publish( + `user_history/${updatedUserHistory.userUid}/updated`, + updatedUserHistory, + ); + return E.right(updatedUserHistory); + } catch (e) { + return E.left(USER_HISTORY_NOT_FOUND); + } + } + + /** + * 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 = { + ...delUserHistory, + request: JSON.stringify(delUserHistory.request), + responseMetadata: JSON.stringify(delUserHistory.responseMetadata), + }; + + // Publish deleted user history subscription + await this.pubsub.publish( + `user_history/${deletedUserHistory.userUid}/deleted`, + deletedUserHistory, + ); + return E.right(deletedUserHistory); + } catch (e) { + return E.left(USER_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); + if (E.isLeft(requestType)) return E.left(requestType.left); + + const deletedCount = await this.prisma.userHistory.deleteMany({ + where: { + userUid: uid, + reqType: requestType.right, + }, + }); + + // Publish multiple user history deleted subscription + await this.pubsub.publish( + `user_history/${uid}/deleted_many`, + deletedCount.count, + ); + 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 + * @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); + } +} 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, + ); + } +} 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..81d1af84a --- /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 Setting', + }) + id: string; + + @Field(() => ID, { + description: 'ID of the user this setting belongs to', + }) + userUid: string; + + @Field({ + description: 'Stringified JSON settings object', + }) + 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', + }) + 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..df3547243 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.module.ts @@ -0,0 +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, UserModule], + providers: [ + UserSettingsResolver, + UserSettingsService, + UserSettingsUserResolver, + ], +}) +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..74e103c70 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.resolver.ts @@ -0,0 +1,78 @@ +import { UseGuards } from '@nestjs/common'; +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'; +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, + private readonly pubsub: PubSubService, + ) {} + + /* Mutations */ + + @Mutation(() => UserSettings, { + description: 'Creates a new user setting', + }) + @UseGuards(GqlAuthGuard) + async createUserSettings( + @GqlUser() user: User, + @Args({ + name: 'properties', + description: 'Stringified JSON settings object', + }) + properties: string, + ) { + const createdUserSettings = + await this.userSettingsService.createUserSettings(user, properties); + + if (E.isLeft(createdUserSettings)) throwErr(createdUserSettings.left); + return createdUserSettings.right; + } + + @Mutation(() => UserSettings, { + description: 'Update user setting for a given user', + }) + @UseGuards(GqlAuthGuard) + async updateUserSettings( + @GqlUser() user: User, + @Args({ + name: 'properties', + description: 'Stringified JSON settings object', + }) + properties: string, + ) { + const updatedUserSettings = + await this.userSettingsService.updateUserSettings(user, properties); + + if (E.isLeft(updatedUserSettings)) throwErr(updatedUserSettings.left); + return updatedUserSettings.right; + } + + /* Subscriptions */ + + @Subscription(() => UserSettings, { + description: 'Listen for user setting creation', + 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, + }) + @UseGuards(GqlAuthGuard) + userSettingsUpdated(@GqlUser() user: User) { + return this.pubsub.asyncIterator(`user_settings/${user.uid}/updated`); + } +} 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..c8bc9705e --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.spec.ts @@ -0,0 +1,125 @@ +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_SETTINGS_NULL_SETTINGS } from 'src/errors'; +import { UserSettings } from './user-settings.model'; +import { User } from 'src/user/user.model'; + +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: User = { + uid: 'aabb22ccdd', + displayName: 'user-display-name', + email: 'user-email', + photoURL: 'user-photo-url', +}; +const settings: UserSettings = { + id: '1', + userUid: user.uid, + properties: JSON.stringify({ 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 resolve right and create an user setting with valid user and properties', async () => { + mockPrisma.userSettings.create.mockResolvedValue({ + ...settings, + properties: JSON.parse(settings.properties), + }); + + const result = await userSettingsService.createUserSettings( + user, + settings.properties, + ); + + expect(result).toEqualRight(settings); + }); + test('Should reject user settings creation for invalid properties', async () => { + const result = await userSettingsService.createUserSettings( + user, + 'invalid-settings', + ); + + expect(result).toEqualLeft(JSON_INVALID); + }); + test('Should reject user settings creation for null properties', async () => { + const result = await userSettingsService.createUserSettings( + user, + null as any, + ); + + expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); + }); + test('Should publish pubsub message 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 () => { + mockPrisma.userSettings.update.mockResolvedValue({ + ...settings, + properties: JSON.parse(settings.properties), + }); + + const result = await userSettingsService.updateUserSettings( + user, + settings.properties, + ); + + expect(result).toEqualRight(settings); + }); + test('Should reject user settings updation for invalid stringified JSON settings', async () => { + const result = await userSettingsService.updateUserSettings( + user, + 'invalid-settings', + ); + + expect(result).toEqualLeft(JSON_INVALID); + }); + test('Should reject user settings updation for null properties', async () => { + const result = await userSettingsService.updateUserSettings( + user, + null as any, + ); + expect(result).toEqualLeft(USER_SETTINGS_NULL_SETTINGS); + }); + test('Should publish pubsub message on successful user settings update', async () => { + mockPrisma.userSettings.update.mockResolvedValue({ + ...settings, + properties: JSON.parse(settings.properties), + }); + + await userSettingsService.updateUserSettings(user, settings.properties); + + expect(mockPubSub.publish).toBeCalledWith( + `user_settings/${user.uid}/updated`, + 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 new file mode 100644 index 000000000..40b630297 --- /dev/null +++ b/packages/hoppscotch-backend/src/user-settings/user-settings.service.ts @@ -0,0 +1,110 @@ +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'; +import { + USER_SETTINGS_ALREADY_EXISTS, + USER_SETTINGS_NULL_SETTINGS, + USER_SETTINGS_NOT_FOUND, +} from 'src/errors'; + +@Injectable() +export class UserSettingsService { + constructor( + private readonly prisma: PrismaService, + private readonly pubsub: PubSubService, + ) {} + + /** + * Fetch user settings for a given user + * @param user User object + * @returns Promise of an Either of `UserSettings` or error + */ + async fetchUserSettings(user: User) { + try { + const userSettings = await this.prisma.userSettings.findUniqueOrThrow({ + where: { userUid: user.uid }, + }); + + const settings: UserSettings = { + ...userSettings, + properties: JSON.stringify(userSettings.properties), + }; + + 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 stringified user settings properties + * @returns an Either of `UserSettings` or error + */ + async createUserSettings(user: User, properties: string) { + if (!properties) return E.left(USER_SETTINGS_NULL_SETTINGS); + + const jsonProperties = stringToJson(properties); + if (E.isLeft(jsonProperties)) return E.left(jsonProperties.left); + + try { + const userSettings = await this.prisma.userSettings.create({ + data: { + properties: jsonProperties.right, + userUid: user.uid, + }, + }); + + const settings: UserSettings = { + ...userSettings, + properties: JSON.stringify(userSettings.properties), + }; + + // Publish subscription for user settings creation + await this.pubsub.publish(`user_settings/${user.uid}/created`, settings); + + return E.right(settings); + } catch (e) { + return E.left(USER_SETTINGS_ALREADY_EXISTS); + } + } + + /** + * Update user setting for a given user + * @param user User object + * @param properties stringified user settings + * @returns Promise of an Either of `UserSettings` or error + */ + async updateUserSettings(user: User, properties: string) { + if (!properties) return E.left(USER_SETTINGS_NULL_SETTINGS); + + 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: { + properties: jsonProperties.right, + }, + }); + + const settings: UserSettings = { + ...updatedUserSettings, + properties: JSON.stringify(updatedUserSettings.properties), + }; + + // 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_NOT_FOUND); + } + } +} 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..b5f63de6e --- /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) { + const userSettings = await this.userSettingsService.fetchUserSettings(user); + + if (E.isLeft(userSettings)) throwErr(userSettings.left); + return userSettings.right; + } +} From 2221261ec22a64e5756e63e437149fd18287088d Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 15:27:03 +0600 Subject: [PATCH 92/95] fix: typo --- packages/hoppscotch-backend/src/user/user.resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index 3d2abd9dc..08b2bb1d0 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -43,7 +43,7 @@ export class UserResolver { @UseGuards(GqlAuthGuard) async updateUser( @GqlUser() user: User, - @Args('user') userInput: UpdateUserInput, + @Args('args') userInput: UpdateUserInput, ): Promise { const updatedUser = await this.userService.updateUser(user, userInput); if (E.isLeft(updatedUser)) throwErr(updatedUser.left); From 60a5acdb9d9277beea2179fc4018bc0cc08a680c Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 15:28:27 +0600 Subject: [PATCH 93/95] fix: typo --- packages/hoppscotch-backend/src/user/user.resolver.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index 08b2bb1d0..0120a499f 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -43,9 +43,9 @@ export class UserResolver { @UseGuards(GqlAuthGuard) async updateUser( @GqlUser() user: User, - @Args('args') userInput: UpdateUserInput, + @Args('args') args: UpdateUserInput, ): Promise { - const updatedUser = await this.userService.updateUser(user, userInput); + const updatedUser = await this.userService.updateUser(user, args); if (E.isLeft(updatedUser)) throwErr(updatedUser.left); return updatedUser.right; } From 08ca57cba27e3f4f257b646863ad7e6f848936ce Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 17:19:24 +0600 Subject: [PATCH 94/95] feat: user session resolver added for sessions updates --- .../hoppscotch-backend/src/user/user.model.ts | 44 +++------ .../src/user/user.resolver.ts | 23 ++++- .../src/user/user.service.spec.ts | 95 ++++++++++--------- .../src/user/user.service.ts | 60 +++++++----- 4 files changed, 115 insertions(+), 107 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/user.model.ts b/packages/hoppscotch-backend/src/user/user.model.ts index da83a99c5..d06319358 100644 --- a/packages/hoppscotch-backend/src/user/user.model.ts +++ b/packages/hoppscotch-backend/src/user/user.model.ts @@ -1,4 +1,10 @@ -import { ObjectType, ID, Field, InputType } from '@nestjs/graphql'; +import { + ObjectType, + ID, + Field, + InputType, + registerEnumType, +} from '@nestjs/graphql'; @ObjectType() export class User { @@ -38,33 +44,11 @@ export class User { currentGQLSession?: string; } -@InputType() -export class UpdateUserInput { - @Field({ - nullable: true, - name: 'displayName', - description: 'Displayed name of the user (if given)', - }) - displayName?: string; - - @Field({ - nullable: true, - name: 'photoURL', - description: 'URL to the profile photo of the user (if given)', - }) - photoURL?: string; - - @Field({ - nullable: true, - name: 'currentRESTSession', - description: 'JSON string of the saved REST session', - }) - currentRESTSession?: string; - - @Field({ - nullable: true, - name: 'currentGQLSession', - description: 'JSON string of the saved GQL session', - }) - currentGQLSession?: string; +export enum SessionType { + REST = 'REST', + GQL = 'GQL', } + +registerEnumType(SessionType, { + name: 'SessionType', +}); diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index 0120a499f..a5e288934 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -1,5 +1,5 @@ import { Resolver, Query, Mutation, Args, Subscription } from '@nestjs/graphql'; -import { UpdateUserInput, User } from './user.model'; +import { SessionType, User } from './user.model'; import { UseGuards } from '@nestjs/common'; import { GqlAuthGuard } from '../guards/gql-auth.guard'; import { GqlUser } from '../decorators/gql-user.decorator'; @@ -38,14 +38,27 @@ export class UserResolver { /* Mutations */ @Mutation(() => User, { - description: 'Update user information', + description: 'Update user sessions', }) @UseGuards(GqlAuthGuard) - async updateUser( + async updateUserSessions( @GqlUser() user: User, - @Args('args') args: UpdateUserInput, + @Args({ + name: 'currentSession', + description: 'JSON string of the saved REST/GQL session', + }) + currentSession: string, + @Args({ + name: 'sessionType', + description: 'Type of the session', + }) + sessionType: SessionType, ): Promise { - const updatedUser = await this.userService.updateUser(user, args); + const updatedUser = await this.userService.updateUserSessions( + user, + currentSession, + sessionType, + ); if (E.isLeft(updatedUser)) throwErr(updatedUser.left); return updatedUser.right; } diff --git a/packages/hoppscotch-backend/src/user/user.service.spec.ts b/packages/hoppscotch-backend/src/user/user.service.spec.ts index b9d4849ba..4561a6f61 100644 --- a/packages/hoppscotch-backend/src/user/user.service.spec.ts +++ b/packages/hoppscotch-backend/src/user/user.service.spec.ts @@ -25,77 +25,78 @@ beforeEach(() => { }); describe('UserService', () => { - describe('updateUser', () => { - test('Should resolve and update user both GQL and REST session', async () => { + describe('updateUserSessions', () => { + test('Should resolve right and update users GQL session', async () => { + const sessionData = user.currentGQLSession; mockPrisma.user.update.mockResolvedValue({ ...user, - currentGQLSession: JSON.parse(user.currentGQLSession), - currentRESTSession: JSON.parse(user.currentRESTSession), + currentGQLSession: JSON.parse(sessionData), + currentRESTSession: null, }); - const result = await userService.updateUser(user, { - currentGQLSession: user.currentGQLSession, - currentRESTSession: user.currentRESTSession, - }); - - expect(result).toEqualRight(user); - }); - test('Should resolve and update user only with GQL session', async () => { - mockPrisma.user.update.mockResolvedValue({ - ...user, - currentGQLSession: JSON.parse(user.currentGQLSession), - currentRESTSession: undefined, - }); - - const result = await userService.updateUser(user, { - currentGQLSession: user.currentGQLSession, - }); - - expect(result).toEqualRight({ ...user, currentRESTSession: null }); - }); - test('Should reject update user for invalid GQL session', async () => { - const newGqlSession = null; - mockPrisma.user.update.mockResolvedValue({ - ...user, - currentGQLSession: newGqlSession, - currentRESTSession: undefined, - }); - - const result = await userService.updateUser(user, { - currentGQLSession: newGqlSession, - }); + const result = await userService.updateUserSessions( + user, + sessionData, + 'GQL', + ); expect(result).toEqualRight({ ...user, - currentGQLSession: newGqlSession, + currentGQLSession: sessionData, currentRESTSession: null, }); }); - test('Should reject update user for invalid GQL session', async () => { - const newGqlSession = 'invalid json'; + test('Should resolve right and update users REST session', async () => { + const sessionData = user.currentGQLSession; mockPrisma.user.update.mockResolvedValue({ ...user, - currentGQLSession: newGqlSession, - currentRESTSession: undefined, + currentGQLSession: null, + currentRESTSession: JSON.parse(sessionData), }); - const result = await userService.updateUser(user, { - currentGQLSession: newGqlSession, + const result = await userService.updateUserSessions( + user, + sessionData, + 'REST', + ); + + expect(result).toEqualRight({ + ...user, + currentGQLSession: null, + currentRESTSession: sessionData, }); + }); + test('Should reject left and update user for invalid GQL session', async () => { + const sessionData = 'invalid json'; + + const result = await userService.updateUserSessions( + user, + sessionData, + 'GQL', + ); expect(result).toEqualLeft(JSON_INVALID); }); - test('Should publish pubsub message on user update', async () => { + test('Should reject left and update user for invalid REST session', async () => { + const sessionData = 'invalid json'; + + const result = await userService.updateUserSessions( + user, + sessionData, + 'REST', + ); + + expect(result).toEqualLeft(JSON_INVALID); + }); + + test('Should publish pubsub message on user update sessions', async () => { mockPrisma.user.update.mockResolvedValue({ ...user, currentGQLSession: JSON.parse(user.currentGQLSession), currentRESTSession: JSON.parse(user.currentRESTSession), }); - await userService.updateUser(user, { - currentGQLSession: user.currentGQLSession, - currentRESTSession: user.currentRESTSession, - }); + await userService.updateUserSessions(user, user.currentGQLSession, 'GQL'); expect(mockPubSub.publish).toHaveBeenCalledTimes(1); expect(mockPubSub.publish).toHaveBeenCalledWith( diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index a2ad712b4..9b0d6be1c 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -1,7 +1,6 @@ import { Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; -import { UpdateUserInput, User } from './user.model'; -import { User as DbUser, Prisma } from '@prisma/client'; +import { SessionType, User } from './user.model'; import * as E from 'fp-ts/lib/Either'; import { USER_UPDATE_FAILED } from 'src/errors'; import { PubSubService } from 'src/pubsub/pubsub.service'; @@ -15,37 +14,36 @@ export class UserService { ) {} /** - * Update a user's information + * Update a user's sessions * @param user User object - * @param updateData Properties to update + * @param currentRESTSession user's current REST session + * @param currentGQLSession user's current GQL session * @returns a Either of User or error */ - async updateUser( + async updateUserSessions( user: User, - updateData: UpdateUserInput, - ): Promise> { - let { currentGQLSession, currentRESTSession, ...rest } = updateData; - let updateUserObj: Partial = rest; + currentSession: string, + sessionType: string, + ): Promise | E.Left> { + const validatedSession = await this.validateSession(currentSession); + if (E.isLeft(validatedSession)) return E.left(validatedSession.left); - // Convert stringified JSON to JSON - if (updateData?.currentGQLSession !== undefined) { - const jsonGql = stringToJson(updateData.currentGQLSession); - if (E.isLeft(jsonGql)) return jsonGql; - - updateUserObj.currentGQLSession = jsonGql?.right ?? Prisma.DbNull; - } - if (updateData?.currentRESTSession !== undefined) { - const jsonRest = stringToJson(updateData.currentRESTSession); - if (E.isLeft(jsonRest)) return jsonRest; - - updateUserObj.currentRESTSession = jsonRest?.right ?? Prisma.DbNull; - } - - // Update user try { + const sessionObj = {}; + switch (sessionType) { + case SessionType.GQL: + sessionObj['currentGQLSession'] = validatedSession.right; + break; + case SessionType.REST: + sessionObj['currentRESTSession'] = validatedSession.right; + break; + default: + return E.left(USER_UPDATE_FAILED); + } + const dbUpdatedUser = await this.prisma.user.update({ where: { uid: user.uid }, - data: updateUserObj, + data: sessionObj, }); const updatedUser: User = { @@ -66,4 +64,16 @@ export class UserService { return E.left(USER_UPDATE_FAILED); } } + + /** + * Validate and parse currentRESTSession and currentGQLSession + * @param sessionData string of the session + * @returns a Either of JSON object or error + */ + async validateSession(sessionData: string) { + const jsonSession = stringToJson(sessionData); + if (E.isLeft(jsonSession)) return E.left(jsonSession.left); + + return E.right(jsonSession.right); + } } From 91adf379da54fbc5df91e91fe2e1a2f225f74922 Mon Sep 17 00:00:00 2001 From: Mir Arif Hasan Date: Tue, 24 Jan 2023 17:27:40 +0600 Subject: [PATCH 95/95] chore: removed redundant comment --- packages/hoppscotch-backend/src/user/user.resolver.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/hoppscotch-backend/src/user/user.resolver.ts b/packages/hoppscotch-backend/src/user/user.resolver.ts index a5e288934..a3c88fe2a 100644 --- a/packages/hoppscotch-backend/src/user/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user/user.resolver.ts @@ -10,8 +10,6 @@ import { PubSubService } from 'src/pubsub/pubsub.service'; @Resolver(() => User) export class UserResolver { - // TODO: remove the eslint-disable line below once dependencies are added to user.service file - // eslint-disable-next-line @typescript-eslint/no-empty-function constructor( private readonly userService: UserService, private readonly pubsub: PubSubService,