From d066b9c9136d1c8f4711d71130c7bbc4b92818d2 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:28:15 +0530 Subject: [PATCH 01/21] 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 02/21] 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 b677aa1715d0a35897627d785a6b3c72762bc78a Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 14:30:14 +0530 Subject: [PATCH 03/21] 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 04/21] 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 05/21] 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 f7dadda52abab58bb2655f9ffb2ff7c5acf9fa23 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 20 Dec 2022 21:05:58 +0530 Subject: [PATCH 06/21] 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 07/21] 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 08/21] 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 09/21] 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 523c650c9d4f3a6aa28e8d927e6db083a191fc50 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 13:13:19 +0530 Subject: [PATCH 10/21] 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 11/21] 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 12/21] 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 13/21] 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 14/21] 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 15/21] 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 a5a14f6c76686e5b7eaafad683be8ad34ecef1ff Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 20:21:44 +0530 Subject: [PATCH 16/21] 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 ebf236b387bd5e61ce2bfc9b1e30007eb917cabc Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Mon, 23 Jan 2023 22:51:37 +0530 Subject: [PATCH 17/21] 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 18/21] 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 19/21] 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 97eedb568cf350638afd7e913a3f17cd906ffba7 Mon Sep 17 00:00:00 2001 From: ankitsridhar16 Date: Tue, 24 Jan 2023 12:53:44 +0530 Subject: [PATCH 20/21] 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 21/21] 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;