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`); } }