Skip to content

Commit

Permalink
refactor: adding JSON import/export functions to UserCollections modu…
Browse files Browse the repository at this point in the history
…le (HBE-169) (#37)

* feat: created exportUserCollectionsToJSON mutation for UserCollection module

* chore: added comments to export functions

* chore: added type and user ownership checking to creation methods

* chore: replaced request property with spread request object instead

* chore: completed all changes requested in inital review of PR

* chore: explicitly exporting request title in export function

* chore: explicitly exporting request title in export function

* chore: added codegen folder to gitignore

* chore: removed gql-code gen file from repo
  • Loading branch information
balub committed Mar 14, 2023
1 parent e5002b4 commit be46ed2
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 14 deletions.
11 changes: 8 additions & 3 deletions packages/hoppscotch-backend/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ export const TEAM_REQ_INVALID_TARGET_COLL_ID =
* Tried to reorder team request but failed
* (TeamRequestService)
*/
export const TEAM_REQ_REORDERING_FAILED =
'team_req/reordering_failed' as const;
export const TEAM_REQ_REORDERING_FAILED = 'team_req/reordering_failed' as const;

/**
* No Postmark Sender Email defined
Expand Down Expand Up @@ -487,7 +486,7 @@ export const USER_COLL_NOT_FOUND = 'user_coll/not_found' as const;
* UserCollection is already a root collection
* (UserCollectionService)
*/
export const USER_COL_ALREADY_ROOT =
export const USER_COLL_ALREADY_ROOT =
'user_coll/target_user_collection_is_already_root_user_collection' as const;

/**
Expand Down Expand Up @@ -535,3 +534,9 @@ export const USER_COLL_SAME_NEXT_COLL =
* (UserCollectionService)
*/
export const USER_NOT_OWNER = 'user_coll/user_not_owner' as const;

/**
* The JSON used is not valid
* (UserCollectionService)
*/
export const USER_COLL_INVALID_JSON = 'user_coll/invalid_json';
3 changes: 1 addition & 2 deletions packages/hoppscotch-backend/src/types/CollectionFolder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Prisma } from '@prisma/client';

export interface CollectionFolder {
id?: string;
folders: CollectionFolder[];
requests: any[];
name: string;
Expand Down
22 changes: 22 additions & 0 deletions packages/hoppscotch-backend/src/user-collection/input-type.args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, ID, ArgsType } from '@nestjs/graphql';
import { ReqType } from '@prisma/client';
import { PaginationArgs } from 'src/types/input-types.args';

@ArgsType()
Expand Down Expand Up @@ -73,3 +74,24 @@ export class MoveUserCollectionArgs {
})
userCollectionID: string;
}

@ArgsType()
export class ImportUserCollectionsFromJSONArgs {
@Field({
name: 'jsonString',
description: 'JSON string to import',
})
jsonString: string;
@Field({
name: 'reqType',
description: 'Type of UserCollection',
})
reqType: ReqType;
@Field(() => ID, {
name: 'parentCollectionID',
description:
'ID to the collection to which to import into (null if to import into the root of the user)',
nullable: true,
})
parentCollectionID?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { PaginationArgs } from 'src/types/input-types.args';
import {
CreateChildUserCollectionArgs,
CreateRootUserCollectionArgs,
ImportUserCollectionsFromJSONArgs,
MoveUserCollectionArgs,
RenameUserCollectionsArgs,
UpdateUserCollectionArgs,
Expand Down Expand Up @@ -139,6 +140,32 @@ export class UserCollectionResolver {
return userCollection.right;
}

@Query(() => String, {
description:
'Returns the JSON string giving the collections and their contents of a user',
})
@UseGuards(GqlAuthGuard)
async exportUserCollectionsToJSON(
@GqlUser() user: AuthUser,
@Args({
type: () => ID,
name: 'collectionID',
description: 'ID of the user collection',
nullable: true,
defaultValue: null,
})
collectionID: string,
) {
const jsonString =
await this.userCollectionService.exportUserCollectionsToJSON(
user.uid,
collectionID,
);

if (E.isLeft(jsonString)) throwErr(jsonString.left as string);
return jsonString.right;
}

// Mutations
@Mutation(() => UserCollection, {
description: 'Creates root REST user collection(no parent user collection)',
Expand Down Expand Up @@ -300,6 +327,25 @@ export class UserCollectionResolver {
return res.right;
}

@Mutation(() => Boolean, {
description: 'Import collections from JSON string to the specified Team',
})
@UseGuards(GqlAuthGuard)
async importUserCollectionsFromJSON(
@Args() args: ImportUserCollectionsFromJSONArgs,
@GqlUser() user: AuthUser,
) {
const importedCollection =
await this.userCollectionService.importCollectionsFromJSON(
args.jsonString,
user.uid,
args.parentCollectionID,
args.reqType,
);
if (E.isLeft(importedCollection)) throwErr(importedCollection.left);
return importedCollection.right;
}

// Subscriptions
@Subscription(() => UserCollection, {
description: 'Listen for User Collection Creation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
USER_COLL_REORDERING_FAILED,
USER_COLL_SAME_NEXT_COLL,
USER_COLL_SHORT_TITLE,
USER_COL_ALREADY_ROOT,
USER_COLL_ALREADY_ROOT,
USER_NOT_OWNER,
} from 'src/errors';
import { PrismaService } from 'src/prisma/prisma.service';
Expand Down Expand Up @@ -997,7 +997,7 @@ describe('moveUserCollection', () => {
null,
user.uid,
);
expect(result).toEqualLeft(USER_COL_ALREADY_ROOT);
expect(result).toEqualLeft(USER_COLL_ALREADY_ROOT);
});
test('should successfully move a child user-collection into root', async () => {
// getUserCollection
Expand Down
Loading

0 comments on commit be46ed2

Please sign in to comment.