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 c8ca3bb38..08abd099f 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 @@ -71,7 +71,7 @@ describe('UserHistoryService', () => { ]; return expect( - await userHistoryService.fetchUserHistory('abc', ReqType.REST), + await userHistoryService.fetchUserHistory('abc', 2, ReqType.REST), ).toEqual(userHistory); }); test('Should return a list of users GQL history if exists', async () => { @@ -118,7 +118,7 @@ describe('UserHistoryService', () => { }, ]; return expect( - await userHistoryService.fetchUserHistory('abc', ReqType.GQL), + await userHistoryService.fetchUserHistory('abc', 2, ReqType.GQL), ).toEqual(userHistory); }); test('Should return an empty list of users REST history if doesnt exists', async () => { @@ -126,7 +126,7 @@ describe('UserHistoryService', () => { const userHistory: UserHistory[] = []; return expect( - await userHistoryService.fetchUserHistory('abc', ReqType.REST), + await userHistoryService.fetchUserHistory('abc', 2, ReqType.REST), ).toEqual(userHistory); }); test('Should return an empty list of users GQL history if doesnt exists', async () => { @@ -134,7 +134,7 @@ describe('UserHistoryService', () => { const userHistory: UserHistory[] = []; return expect( - await userHistoryService.fetchUserHistory('abc', ReqType.GQL), + await userHistoryService.fetchUserHistory('abc', 2, ReqType.GQL), ).toEqual(userHistory); }); }); @@ -278,7 +278,7 @@ describe('UserHistoryService', () => { }); describe('toggleHistoryStarStatus', () => { test('Should resolve right and star/unstar a request in the history', async () => { - const createdOnDate = new Date() + const createdOnDate = new Date(); mockPrisma.userHistory.findFirst.mockResolvedValueOnce({ userUid: 'abc', id: '1', 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 8ee8da421..80df70853 100644 --- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts +++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts @@ -19,15 +19,20 @@ export class UserHistoryService { /** * Fetch users REST or GraphQL history based on ReqType param. * @param uid Users uid + * @param take items to fetch * @param reqType request Type to fetch i.e. GraphQL or REST * @returns an array of user history */ - async fetchUserHistory(uid: string, reqType: ReqType) { + async fetchUserHistory(uid: string, take: number, reqType: ReqType) { const userHistory = await this.prisma.userHistory.findMany({ where: { userUid: uid, reqType: reqType, }, + take: take, + orderBy: { + executedOn: 'desc', + }, }); const userHistoryColl: UserHistory[] = userHistory.map( diff --git a/packages/hoppscotch-backend/src/user-history/user.resolver.ts b/packages/hoppscotch-backend/src/user-history/user.resolver.ts index 12ec75c02..01f7d97ee 100644 --- a/packages/hoppscotch-backend/src/user-history/user.resolver.ts +++ b/packages/hoppscotch-backend/src/user-history/user.resolver.ts @@ -1,7 +1,8 @@ -import { Parent, ResolveField, Resolver } from '@nestjs/graphql'; +import { Args, 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'; +import { PaginationArgs } from '../types/input-types.args'; @Resolver(() => User) export class UserHistoryUserResolver { @@ -9,18 +10,26 @@ export class UserHistoryUserResolver { @ResolveField(() => [UserHistory], { description: 'Returns a users REST history', }) - async RESTHistory(@Parent() user: User): Promise { + async RESTHistory( + @Parent() user: User, + @Args() args: PaginationArgs, + ): Promise { return await this.userHistoryService.fetchUserHistory( user.uid, + args.take, ReqType.REST, ); } @ResolveField(() => [UserHistory], { description: 'Returns a users GraphQL history', }) - async GraphQLHistory(@Parent() user: User): Promise { + async GQLHistory( + @Parent() user: User, + @Args() args: PaginationArgs, + ): Promise { return await this.userHistoryService.fetchUserHistory( user.uid, + args.take, ReqType.GQL, ); }