From e712d1e3ae77e5c763b9940b46991ed238052933 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 1 Dec 2023 16:22:22 +0530 Subject: [PATCH] feat: completed addition of new data field in UserCollections --- packages/hoppscotch-backend/src/errors.ts | 7 +++ .../src/pubsub/topicsDefs.ts | 2 +- .../src/user-collection/input-type.args.ts | 12 ++++ .../user-collection.resolver.ts | 12 +++- .../user-collection.service.ts | 59 +++++++++++++++---- .../user-collection/user-collections.model.ts | 6 ++ 6 files changed, 83 insertions(+), 15 deletions(-) diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 8f305b9b2..df81a5b85 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -592,6 +592,13 @@ export const USER_COLL_REORDERING_FAILED = export const USER_COLL_SAME_NEXT_COLL = 'user_coll/user_collection_and_next_user_collection_are_same' as const; +/** + * The User Collection data is not valid + * (UserCollectionService) + */ +export const USER_COLL_DATA_INVALID = + 'user_coll/user_coll_data_invalid' as const; + /** * The User Collection does not belong to the logged-in user * (UserCollectionService) diff --git a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts index b0cc8a854..49506c67e 100644 --- a/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts +++ b/packages/hoppscotch-backend/src/pubsub/topicsDefs.ts @@ -21,8 +21,8 @@ import { } from 'src/team-request/team-request.model'; import { TeamInvitation } from 'src/team-invitation/team-invitation.model'; import { InvitedUser } from '../admin/invited-user.model'; -import { UserCollection } from '@prisma/client'; import { + UserCollection, UserCollectionRemovedData, UserCollectionReorderData, } from 'src/user-collection/user-collections.model'; diff --git a/packages/hoppscotch-backend/src/user-collection/input-type.args.ts b/packages/hoppscotch-backend/src/user-collection/input-type.args.ts index 07e612bf0..0280e0785 100644 --- a/packages/hoppscotch-backend/src/user-collection/input-type.args.ts +++ b/packages/hoppscotch-backend/src/user-collection/input-type.args.ts @@ -6,6 +6,12 @@ import { PaginationArgs } from 'src/types/input-types.args'; export class CreateRootUserCollectionArgs { @Field({ name: 'title', description: 'Title of the new user collection' }) title: string; + + @Field({ + name: 'data', + description: 'JSON string representing the collection data', + }) + data: string; } @ArgsType() export class CreateChildUserCollectionArgs { @@ -17,6 +23,12 @@ export class CreateChildUserCollectionArgs { description: 'ID of the parent to the new user collection', }) parentUserCollectionID: string; + + @Field({ + name: 'data', + description: 'JSON string representing the collection data', + }) + data: string; } @ArgsType() 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 48196e48e..95a5356d4 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.resolver.ts @@ -142,7 +142,13 @@ export class UserCollectionResolver { ); if (E.isLeft(userCollection)) throwErr(userCollection.left); - return userCollection.right; + return { + ...userCollection.right, + userID: userCollection.right.userUid, + data: !userCollection.right.data + ? null + : JSON.stringify(userCollection.right.data), + }; } @Query(() => UserCollectionExportJSONData, { @@ -191,6 +197,7 @@ export class UserCollectionResolver { await this.userCollectionService.createUserCollection( user, args.title, + args.data, null, ReqType.REST, ); @@ -212,6 +219,7 @@ export class UserCollectionResolver { await this.userCollectionService.createUserCollection( user, args.title, + args.data, null, ReqType.GQL, ); @@ -232,6 +240,7 @@ export class UserCollectionResolver { await this.userCollectionService.createUserCollection( user, args.title, + args.data, args.parentUserCollectionID, ReqType.GQL, ); @@ -252,6 +261,7 @@ export class UserCollectionResolver { await this.userCollectionService.createUserCollection( user, args.title, + args.data, args.parentUserCollectionID, ReqType.REST, ); 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 169f47aa6..445e0377e 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.service.ts @@ -12,6 +12,7 @@ import { USER_NOT_FOUND, USER_NOT_OWNER, USER_COLL_INVALID_JSON, + USER_COLL_DATA_INVALID, } from 'src/errors'; import { PrismaService } from 'src/prisma/prisma.service'; import { AuthUser } from 'src/types/AuthUser'; @@ -45,6 +46,7 @@ export class UserCollectionService { return { ...collection, userID: collection.userUid, + data: !collection.data ? null : JSON.stringify(collection.data), }; } @@ -146,7 +148,7 @@ export class UserCollectionService { }, }); - return parent; + return !parent ? null : this.cast(parent); } /** @@ -164,7 +166,7 @@ export class UserCollectionService { take: number, type: ReqType, ) { - return this.prisma.userCollection.findMany({ + const res = await this.prisma.userCollection.findMany({ where: { parentID: collectionID, type: type, @@ -176,6 +178,12 @@ export class UserCollectionService { skip: cursor ? 1 : 0, cursor: cursor ? { id: cursor } : undefined, }); + + const childCollections = res.map((childCollection) => + this.cast(childCollection), + ); + + return childCollections; } /** @@ -211,12 +219,17 @@ export class UserCollectionService { async createUserCollection( user: AuthUser, title: string, + data: string, parentUserCollectionID: string | null, type: ReqType, ) { const isTitleValid = isValidLength(title, this.TITLE_LENGTH); if (!isTitleValid) return E.left(USER_COLL_SHORT_TITLE); + console.log(data); + const collectionData = stringToJson(data); + if (E.isLeft(collectionData)) return E.left(USER_COLL_DATA_INVALID); + // If creating a child collection if (parentUserCollectionID !== null) { const parentCollection = await this.getUserCollection( @@ -251,15 +264,19 @@ export class UserCollectionService { }, }, parent: isParent, + data: collectionData.right, orderIndex: !parentUserCollectionID ? (await this.getRootCollectionsCount(user.uid)) + 1 : (await this.getChildCollectionsCount(parentUserCollectionID)) + 1, }, }); - await this.pubsub.publish(`user_coll/${user.uid}/created`, userCollection); + await this.pubsub.publish( + `user_coll/${user.uid}/created`, + this.cast(userCollection), + ); - return E.right(userCollection); + return E.right(this.cast(userCollection)); } /** @@ -276,7 +293,7 @@ export class UserCollectionService { take: number, type: ReqType, ) { - return this.prisma.userCollection.findMany({ + const res = await this.prisma.userCollection.findMany({ where: { userUid: user.uid, parentID: null, @@ -289,6 +306,12 @@ export class UserCollectionService { skip: cursor ? 1 : 0, cursor: cursor ? { id: cursor } : undefined, }); + + const userCollections = res.map((childCollection) => + this.cast(childCollection), + ); + + return userCollections; } /** @@ -307,7 +330,7 @@ export class UserCollectionService { take: number, type: ReqType, ) { - return this.prisma.userCollection.findMany({ + const res = await this.prisma.userCollection.findMany({ where: { userUid: user.uid, parentID: userCollectionID, @@ -317,9 +340,16 @@ export class UserCollectionService { skip: cursor ? 1 : 0, cursor: cursor ? { id: cursor } : undefined, }); + + const childCollections = res.map((childCollection) => + this.cast(childCollection), + ); + + return childCollections; } /** + * @deprecated Use updateUserCollection method instead * Update the title of a UserCollection * * @param newTitle The new title of collection @@ -351,10 +381,10 @@ export class UserCollectionService { this.pubsub.publish( `user_coll/${updatedUserCollection.userUid}/updated`, - updatedUserCollection, + this.cast(updatedUserCollection), ); - return E.right(updatedUserCollection); + return E.right(this.cast(updatedUserCollection)); } catch (error) { return E.left(USER_COLL_NOT_FOUND); } @@ -591,10 +621,10 @@ export class UserCollectionService { this.pubsub.publish( `user_coll/${collection.right.userUid}/moved`, - updatedCollection.right, + this.cast(updatedCollection.right), ); - return E.right(updatedCollection.right); + return E.right(this.cast(updatedCollection.right)); } // destCollectionID != null i.e move into another collection @@ -642,10 +672,10 @@ export class UserCollectionService { this.pubsub.publish( `user_coll/${collection.right.userUid}/moved`, - updatedCollection.right, + this.cast(updatedCollection.right), ); - return E.right(updatedCollection.right); + return E.right(this.cast(updatedCollection.right)); } /** @@ -846,6 +876,7 @@ export class UserCollectionService { ...(x.request as Record), // type casting x.request of type Prisma.JSONValue to an object to enable spread }; }), + data: JSON.stringify(collection.right.data), }; return E.right(result); @@ -918,6 +949,7 @@ export class UserCollectionService { ...(x.request as Record), // type casting x.request of type Prisma.JSONValue to an object to enable spread }; }), + data: JSON.stringify(parentCollection.right.data), }), collectionType: parentCollection.right.type, }); @@ -971,6 +1003,7 @@ export class UserCollectionService { this.generatePrismaQueryObj(f, userID, index + 1, reqType), ), }, + data: folder.data ?? undefined, }; } @@ -1041,7 +1074,7 @@ export class UserCollectionService { ); userCollections.forEach((x) => - this.pubsub.publish(`user_coll/${userID}/created`, x), + this.pubsub.publish(`user_coll/${userID}/created`, this.cast(x)), ); return E.right(true); 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 21a6b6242..89c26861c 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collections.model.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collections.model.ts @@ -13,6 +13,12 @@ export class UserCollection { }) title: string; + @Field({ + description: 'JSON string representing the collection data', + nullable: true, + }) + data: string; + @Field(() => ReqType, { description: 'Type of the user collection', })