From 509775878a085c63ffeac6118f60f26d0f1dee3f Mon Sep 17 00:00:00 2001 From: nivedin Date: Mon, 11 Dec 2023 21:40:52 +0530 Subject: [PATCH] refactor: import-export team coll in new schema --- .../components/collections/ImportExport.vue | 28 ++++++++++++++-- .../gql/queries/GetCollectionTitle.graphql | 5 --- .../queries/GetCollectionTitleAndData.graphql | 6 ++++ .../gql/queries/GetSingleCollection.graphql | 1 + .../subscriptions/TeamCollectionAdded.graphql | 1 + .../src/helpers/backend/helpers.ts | 33 ++++++++++++++----- .../src/helpers/import-export/import/hopp.ts | 1 + .../helpers/teams/TeamCollectionAdapter.ts | 2 +- .../collections/collections.platform.ts | 10 +++--- 9 files changed, 66 insertions(+), 21 deletions(-) delete mode 100644 packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitle.graphql create mode 100644 packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitleAndData.graphql diff --git a/packages/hoppscotch-common/src/components/collections/ImportExport.vue b/packages/hoppscotch-common/src/components/collections/ImportExport.vue index 95544f205..6413d310a 100644 --- a/packages/hoppscotch-common/src/components/collections/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/collections/ImportExport.vue @@ -108,6 +108,27 @@ const importToPersonalWorkspace = (collections: HoppCollection[]) => { }) } +function translateToTeamCollectionFormat(x: HoppCollection) { + const folders: HoppCollection[] = (x.folders ?? []).map( + translateToTeamCollectionFormat + ) + + const data = { + auth: x.auth, + headers: x.headers, + } + + const obj = { + ...x, + folders, + data, + } + + if (x.id) obj.id = x.id + + return obj +} + const importToTeamsWorkspace = async (collections: HoppCollection[]) => { if (!hasTeamWriteAccess.value || !selectedTeamID.value) { return E.left({ @@ -115,8 +136,12 @@ const importToTeamsWorkspace = async (collections: HoppCollection[]) => { }) } + const transformedCollection = collections.map((collection) => + translateToTeamCollectionFormat(collection) + ) + const res = await toTeamsImporter( - JSON.stringify(collections), + JSON.stringify(transformedCollection), selectedTeamID.value )() @@ -400,7 +425,6 @@ const HoppTeamCollectionsExporter: ImporterOrExporter = { }, action: async () => { isHoppTeamCollectionExporterInProgress.value = true - if ( props.collectionsType.type === "team-collections" && props.collectionsType.selectedTeam diff --git a/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitle.graphql b/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitle.graphql deleted file mode 100644 index 6e4030d34..000000000 --- a/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitle.graphql +++ /dev/null @@ -1,5 +0,0 @@ -query GetCollectionTitle($collectionID: ID!) { - collection(collectionID: $collectionID) { - title - } -} diff --git a/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitleAndData.graphql b/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitleAndData.graphql new file mode 100644 index 000000000..cbde01b0d --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetCollectionTitleAndData.graphql @@ -0,0 +1,6 @@ +query GetCollectionTitleAndData($collectionID: ID!) { + collection(collectionID: $collectionID) { + title + data + } +} diff --git a/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetSingleCollection.graphql b/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetSingleCollection.graphql index 50505d6f5..398da3b8b 100644 --- a/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetSingleCollection.graphql +++ b/packages/hoppscotch-common/src/helpers/backend/gql/queries/GetSingleCollection.graphql @@ -2,6 +2,7 @@ query GetSingleCollection($collectionID: ID!) { collection(collectionID: $collectionID) { id title + data parent { id } diff --git a/packages/hoppscotch-common/src/helpers/backend/gql/subscriptions/TeamCollectionAdded.graphql b/packages/hoppscotch-common/src/helpers/backend/gql/subscriptions/TeamCollectionAdded.graphql index 70c9c3705..7cd38bba9 100644 --- a/packages/hoppscotch-common/src/helpers/backend/gql/subscriptions/TeamCollectionAdded.graphql +++ b/packages/hoppscotch-common/src/helpers/backend/gql/subscriptions/TeamCollectionAdded.graphql @@ -2,6 +2,7 @@ subscription TeamCollectionAdded($teamID: ID!) { teamCollectionAdded(teamID: $teamID) { id title + data parent { id } diff --git a/packages/hoppscotch-common/src/helpers/backend/helpers.ts b/packages/hoppscotch-common/src/helpers/backend/helpers.ts index edb61cc7f..587fb4dd0 100644 --- a/packages/hoppscotch-common/src/helpers/backend/helpers.ts +++ b/packages/hoppscotch-common/src/helpers/backend/helpers.ts @@ -14,7 +14,7 @@ import { ExportAsJsonDocument, GetCollectionChildrenIDsDocument, GetCollectionRequestsDocument, - GetCollectionTitleDocument, + GetCollectionTitleAndDataDocument, } from "./graphql" export const BACKEND_PAGE_SIZE = 10 @@ -84,16 +84,19 @@ export const getCompleteCollectionTree = ( pipe( TE.Do, - TE.bind("title", () => + TE.bind("titleAndData", () => pipe( () => runGQLQuery({ - query: GetCollectionTitleDocument, + query: GetCollectionTitleAndDataDocument, variables: { collectionID: collID, }, }), - TE.map((x) => x.collection!.title) + TE.map((result) => ({ + title: result.collection!.title, + data: result.collection!.data, + })) ) ), TE.bind("children", () => @@ -107,22 +110,36 @@ export const getCompleteCollectionTree = ( TE.bind("requests", () => () => getCollectionRequests(collID)), TE.map( - ({ title, children, requests }) => + ({ titleAndData, children, requests }) => { id: collID, children, requests, - title, + title: titleAndData.title, + data: titleAndData.data, } ) ) -export const teamCollToHoppRESTColl = (coll: TeamCollection): HoppCollection => - makeCollection({ +export const teamCollToHoppRESTColl = ( + coll: TeamCollection +): HoppCollection => { + const data = + coll.data && coll.data !== "null" + ? JSON.parse(coll.data) + : { + auth: { authType: "inherit", authActive: true }, + headers: [], + } + + return makeCollection({ name: coll.title, folders: coll.children?.map(teamCollToHoppRESTColl) ?? [], requests: coll.requests?.map((x) => x.request) ?? [], + auth: data.auth ?? { authType: "inherit", authActive: true }, + headers: data.headers ?? [], }) +} /** * Get the JSON string of all the collection of the specified team diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts index 73045ca33..887e22e72 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts @@ -29,6 +29,7 @@ const isPlainObject = (value: any): value is object => _isPlainObject(value) /** * checks if a collection matches the schema for a hoppscotch collection. + * here 2 is the latest version of the schema. */ const isValidCollection = (collection: unknown): collection is HoppCollection => isPlainObject(collection) && "v" in collection && collection.v === 2 diff --git a/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts b/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts index ff313760f..9bcddc022 100644 --- a/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts +++ b/packages/hoppscotch-common/src/helpers/teams/TeamCollectionAdapter.ts @@ -699,7 +699,7 @@ export default class NewTeamCollectionAdapter { children: null, requests: null, title: result.right.teamCollectionAdded.title, - data: null, + data: result.right.teamCollectionAdded.data ?? null, }, result.right.teamCollectionAdded.parent?.id ?? null ) diff --git a/packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts b/packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts index 23e55e85a..747f67f1b 100644 --- a/packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts +++ b/packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts @@ -107,7 +107,7 @@ function exportedCollectionToHoppCollection( if (collectionType == "REST") { const restCollection = collection as ExportedUserCollectionREST const data = - restCollection.data && restCollection.data != "null" + restCollection.data && restCollection.data !== "null" ? JSON.parse(restCollection.data) : { auth: { authType: "inherit", authActive: false }, @@ -115,7 +115,7 @@ function exportedCollectionToHoppCollection( } return { id: restCollection.id, - v: 1, + v: 2, name: restCollection.name, folders: restCollection.folders.map((folder) => exportedCollectionToHoppCollection(folder, collectionType) @@ -162,7 +162,7 @@ function exportedCollectionToHoppCollection( return { id: gqlCollection.id, - v: 1, + v: 2, name: gqlCollection.name, folders: gqlCollection.folders.map((folder) => exportedCollectionToHoppCollection(folder, collectionType) @@ -324,7 +324,7 @@ function setupUserCollectionCreatedSubscription() { name: res.right.userCollectionCreated.title, folders: [], requests: [], - v: 1, + v: 2, auth: data.auth, headers: data.headers, }) @@ -332,7 +332,7 @@ function setupUserCollectionCreatedSubscription() { name: res.right.userCollectionCreated.title, folders: [], requests: [], - v: 1, + v: 2, auth: data.auth, headers: data?.headers, })