From fc37196354d93195e86ce35186136bce5d2572c1 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 27 Sep 2024 16:55:47 +0530 Subject: [PATCH] chore: export single User/Team Collection (#4359) * chore: added query to export single TeamCollection data * chore: added query to export single UserCollection data * chore: fixed typo * chore: completed requested changes in PR review --- .../team-collection.resolver.ts | 30 +++++++++++++++++++ .../team-collection.service.ts | 4 +-- .../user-collection.resolver.ts | 24 +++++++++++++++ .../user-collection.service.ts | 2 +- packages/hoppscotch-backend/src/utils.ts | 11 +++++++ 5 files changed, 68 insertions(+), 3 deletions(-) diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts index 4c6b00ea6..1fc4c1086 100644 --- a/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts +++ b/packages/hoppscotch-backend/src/team-collection/team-collection.resolver.ts @@ -102,6 +102,36 @@ export class TeamCollectionResolver { return jsonString.right; } + @Query(() => String, { + description: + 'Returns a JSON string of all the contents of a Team Collection', + }) + @UseGuards(GqlAuthGuard, GqlTeamMemberGuard) + @RequiresTeamRole( + TeamMemberRole.VIEWER, + TeamMemberRole.EDITOR, + TeamMemberRole.OWNER, + ) + async exportCollectionToJSON( + @Args({ name: 'teamID', description: 'ID of the team', type: () => ID }) + teamID: string, + @Args({ + name: 'collectionID', + description: 'ID of the collection', + type: () => ID, + }) + collectionID: string, + ) { + const collectionData = + await this.teamCollectionService.exportCollectionToJSONObject( + teamID, + collectionID, + ); + + if (E.isLeft(collectionData)) throwErr(collectionData.left as string); + return JSON.stringify(collectionData.right); + } + @Query(() => [TeamCollection], { description: 'Returns the collections of a team', }) diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts index 093c25393..6779e64aa 100644 --- a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts +++ b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts @@ -103,10 +103,10 @@ export class TeamCollectionService { * @param collectionID The Collection ID * @returns A JSON string containing all the contents of a collection */ - private async exportCollectionToJSONObject( + async exportCollectionToJSONObject( teamID: string, collectionID: string, - ) { + ): Promise | E.Left> { const collection = await this.getCollection(collectionID); if (E.isLeft(collection)) return E.left(TEAM_INVALID_COLL_ID); diff --git a/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts b/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts index b5f3d2820..df530e3d7 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts @@ -186,6 +186,30 @@ export class UserCollectionResolver { return jsonString.right; } + @Query(() => String, { + description: + 'Returns a JSON string of all the contents of a User Collection', + }) + @UseGuards(GqlAuthGuard) + async exportUserCollectionToJSON( + @GqlUser() user: AuthUser, + @Args({ + type: () => ID, + name: 'collectionID', + description: 'ID of the user collection', + }) + collectionID: string, + ) { + const jsonString = + await this.userCollectionService.exportUserCollectionToJSONObject( + user.uid, + collectionID, + ); + + if (E.isLeft(jsonString)) throwErr(jsonString.left as string); + return JSON.stringify(jsonString.right); + } + // Mutations @Mutation(() => UserCollection, { description: 'Creates root REST user collection(no parent user collection)', diff --git a/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts b/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts index 0eeaee52f..4366caf43 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts @@ -836,7 +836,7 @@ export class UserCollectionService { * @param collectionID The Collection ID * @returns A JSON string containing all the contents of a collection */ - private async exportUserCollectionToJSONObject( + async exportUserCollectionToJSONObject( userUID: string, collectionID: string, ): Promise | E.Right> { diff --git a/packages/hoppscotch-backend/src/utils.ts b/packages/hoppscotch-backend/src/utils.ts index 820976cf9..23afc3fa3 100644 --- a/packages/hoppscotch-backend/src/utils.ts +++ b/packages/hoppscotch-backend/src/utils.ts @@ -116,6 +116,17 @@ export const getGqlArg = ( ); /** + * To the daring adventurer who has stumbled upon this relic of code... welcome. + * Many have gazed upon its depths, yet few have returned with answers. + * I could have deleted it, but that felt... too easy, too final. + * + * If you're still reading, perhaps you're the one destined to unravel its secrets. + * Or, maybe you're like me—content to let it linger, a puzzle for the ages. + * The choice is yours, but beware... once you start, there is no turning back. + * + * PLEASE, NO ONE KNOWS HOW THIS WORKS... + * -- Balu, whispering from the great beyond... probably still trying to understand this damn thing. + * * Sequences an array of TaskEither values while maintaining an array of all the error values * @param arr Array of TaskEithers * @returns A TaskEither saying all the errors possible on the left or all the success values on the right