From ea931620568b6bd8f911404edf5721442d26ba25 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Wed, 29 Mar 2023 15:50:48 +0530 Subject: [PATCH] refactor: modifed return types of mutation/subscriptions in UserCollections (#57) * refactor: modifed userCollectionRemoved subscription to return custom return type * chore: created new return type for export to JSON mutation in UserCollection * refactor: added reqType to exportUserCollectionsToJSON query * chore: remove duplicate enum in user-collection.model.ts file --- .../src/pubsub/topicsDefs.ts | 7 +++-- .../user-collection.resolver.ts | 13 +++++++-- .../user-collection.service.ts | 27 ++++++++++++++----- .../user-collection/user-collections.model.ts | 26 ++++++++++++++++++ 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts index ad085e728..bf34d415e 100644 --- a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts +++ b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts @@ -19,7 +19,10 @@ import { import { TeamInvitation } from 'src/team-invitation/team-invitation.model'; import { InvitedUser } from '../admin/invited-user.model'; import { UserCollection } from '@prisma/client'; -import { UserCollectionReorderData } from 'src/user-collection/user-collections.model'; +import { + UserCollectionRemovedData, + UserCollectionReorderData, +} from 'src/user-collection/user-collections.model'; import { Shortcode } from 'src/shortcode/shortcode.model'; // A custom message type that defines the topic and the corresponding payload. @@ -42,7 +45,7 @@ export type TopicDef = { [ topic: `user_coll/${string}/${'created' | 'updated' | 'moved'}` ]: UserCollection; - [topic: `user_coll/${string}/${'deleted'}`]: string; + [topic: `user_coll/${string}/${'deleted'}`]: UserCollectionRemovedData; [topic: `user_coll/${string}/${'order_updated'}`]: UserCollectionReorderData; [topic: `team/${string}/member_removed`]: string; [topic: `team/${string}/${'member_added' | 'member_updated'}`]: TeamMember; 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 ae8182cbf..48196e48e 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts @@ -16,6 +16,8 @@ import { AuthUser } from 'src/types/AuthUser'; import { UserCollectionService } from './user-collection.service'; import { UserCollection, + UserCollectionExportJSONData, + UserCollectionRemovedData, UserCollectionReorderData, } from './user-collections.model'; import { throwErr } from 'src/utils'; @@ -143,7 +145,7 @@ export class UserCollectionResolver { return userCollection.right; } - @Query(() => String, { + @Query(() => UserCollectionExportJSONData, { description: 'Returns the JSON string giving the collections and their contents of a user', }) @@ -158,11 +160,18 @@ export class UserCollectionResolver { defaultValue: null, }) collectionID: string, + @Args({ + type: () => ReqType, + name: 'collectionType', + description: 'Type of the user collection', + }) + collectionType: ReqType, ) { const jsonString = await this.userCollectionService.exportUserCollectionsToJSON( user.uid, collectionID, + collectionType, ); if (E.isLeft(jsonString)) throwErr(jsonString.left as string); @@ -371,7 +380,7 @@ export class UserCollectionResolver { return this.pubSub.asyncIterator(`user_coll/${user.uid}/updated`); } - @Subscription(() => ID, { + @Subscription(() => UserCollectionRemovedData, { description: 'Listen to when a User Collection has been deleted', resolve: (value) => value, }) 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 f1cb0a03d..de8fbf9dc 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts @@ -19,7 +19,10 @@ import * as E from 'fp-ts/Either'; import * as O from 'fp-ts/Option'; import { PubSubService } from 'src/pubsub/pubsub.service'; import { Prisma, UserCollection, ReqType as DBReqType } from '@prisma/client'; -import { UserCollection as UserCollectionModel } from './user-collections.model'; +import { + UserCollection as UserCollectionModel, + UserCollectionExportJSONData, +} from './user-collections.model'; import { ReqType } from 'src/types/RequestTypes'; import { isValidLength, stringToJson } from 'src/utils'; import { CollectionFolder } from 'src/types/CollectionFolder'; @@ -419,7 +422,10 @@ export class UserCollectionService { this.pubsub.publish( `user_coll/${deletedUserCollection.right.userUid}/deleted`, - deletedUserCollection.right.id, + { + id: deletedUserCollection.right.id, + type: ReqType[deletedUserCollection.right.type], + }, ); return E.right(true); @@ -852,12 +858,14 @@ export class UserCollectionService { async exportUserCollectionsToJSON( userUID: string, collectionID: string | null, + reqType: ReqType, ) { // Get all child collections details const childCollectionList = await this.prisma.userCollection.findMany({ where: { userUid: userUID, parentID: collectionID, + type: reqType, }, }); @@ -879,6 +887,9 @@ export class UserCollectionService { const parentCollection = await this.getUserCollection(collectionID); if (E.isLeft(parentCollection)) return E.left(parentCollection.left); + if (parentCollection.right.type !== reqType) + return E.left(USER_COLL_NOT_SAME_TYPE); + // Fetch all child requests that belong to collectionID const requests = await this.prisma.userRequest.findMany({ where: { @@ -890,8 +901,8 @@ export class UserCollectionService { }, }); - return E.right( - JSON.stringify({ + return E.right({ + exportedCollection: JSON.stringify({ id: parentCollection.right.id, name: parentCollection.right.title, folders: collectionListObjects, @@ -903,10 +914,14 @@ export class UserCollectionService { }; }), }), - ); + collectionType: parentCollection.right.type, + }); } - return E.right(JSON.stringify(collectionListObjects)); + return E.right({ + exportedCollection: JSON.stringify(collectionListObjects), + collectionType: reqType, + }); } /** diff --git a/packages/hoppscotch-backend/src/user-collection/user-collections.model.ts b/packages/hoppscotch-backend/src/user-collection/user-collections.model.ts index f1ad987bc..21a6b6242 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collections.model.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collections.model.ts @@ -38,6 +38,32 @@ export class UserCollectionReorderData { nextUserCollection?: UserCollection; } +@ObjectType() +export class UserCollectionRemovedData { + @Field(() => ID, { + description: 'ID of User Collection being removed', + }) + id: string; + + @Field(() => ReqType, { + description: 'Type of the user collection', + }) + type: ReqType; +} + registerEnumType(ReqType, { name: 'CollType', }); + +@ObjectType() +export class UserCollectionExportJSONData { + @Field(() => ID, { + description: 'Stringified contents of the collection', + }) + exportedCollection: string; + + @Field(() => ReqType, { + description: 'Type of the user collection', + }) + collectionType: ReqType; +}