From faa0bf77146e21fa45763ccfed5f050e64fc5785 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Thu, 15 Feb 2024 10:55:54 +0530 Subject: [PATCH] fix: updates to collection level authorization and headers reflect at the request level straightaway --- .../components/new-collections/rest/index.vue | 2 - .../src/helpers/collection/collection.ts | 107 +++++++++++++++--- 2 files changed, 89 insertions(+), 20 deletions(-) diff --git a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue index 8b9a74183..1b2d88c46 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -402,8 +402,6 @@ const onRemoveRootCollection = async () => { parentCollectionIndexPath.split("/").map((id) => parseInt(id)) ) - console.log(`Parent collection is `, parentCollection) - if (!parentCollection) { tab.document.saveContext = null tab.document.isDirty = true diff --git a/packages/hoppscotch-common/src/helpers/collection/collection.ts b/packages/hoppscotch-common/src/helpers/collection/collection.ts index a48f1cad0..c675796cd 100644 --- a/packages/hoppscotch-common/src/helpers/collection/collection.ts +++ b/packages/hoppscotch-common/src/helpers/collection/collection.ts @@ -1,12 +1,12 @@ -import { HoppCollection } from "@hoppscotch/data" -import { getAffectedIndexes } from "./affectedIndex" -import { GetSingleRequestDocument } from "../backend/graphql" -import { runGQLQuery } from "../backend/GQLClient" import * as E from "fp-ts/Either" +import { ref } from "vue" import { getService } from "~/modules/dioc" -import { RESTTabService } from "~/services/tab/rest" -import { HoppInheritedProperty } from "../types/HoppInheritedProperties" import { GQLTabService } from "~/services/tab/graphql" +import { RESTTabService } from "~/services/tab/rest" +import { runGQLQuery } from "../backend/GQLClient" +import { GetSingleRequestDocument } from "../backend/graphql" +import { HoppInheritedProperty } from "../types/HoppInheritedProperties" +import { getAffectedIndexes } from "./affectedIndex" /** * Resolve save context on reorder @@ -174,25 +174,96 @@ export function updateInheritedPropertiesForAffectedRequests( const tabService = type === "rest" ? getService(RESTTabService) : getService(GQLTabService) - const effectedTabs = tabService.getTabsRefTo((tab) => { - const saveContext = tab.document.saveContext + const tabs = tabService.getTabsRefTo((tab) => { + if (tab.document.saveContext?.originLocation === "user-collection") { + return tab.document.saveContext.folderPath.startsWith(path) + } - const saveContextPath = - saveContext?.originLocation === "team-collection" - ? saveContext.collectionID - : saveContext?.folderPath + if (tab.document.saveContext?.originLocation === "team-collection") { + return Boolean(tab.document.saveContext.collectionID?.startsWith(path)) + } - return saveContextPath?.startsWith(path) ?? false + if ( + tab.document.saveContext?.originLocation !== "workspace-user-collection" + ) { + return false + } + + const requestHandle = ref(tab.document.saveContext.requestHandle) + + return ( + tab.document.saveContext?.originLocation === + "workspace-user-collection" && + requestHandle.value.type === "ok" && + requestHandle.value.data.collectionID.startsWith(path) + ) }) - effectedTabs.map((tab) => { + const tabsEffectedByAuth = tabs.filter((tab) => { + if (tab.value.document.saveContext?.originLocation === "user-collection") { + return ( + tab.value.document.saveContext.folderPath.startsWith(path) && + path === + folderPathCloseToSaveContext( + tab.value.document.inheritedProperties?.auth.parentID, + path, + tab.value.document.saveContext.folderPath + ) + ) + } + + if (tab.value.document.saveContext?.originLocation === "team-collection") { + return ( + tab.value.document.saveContext.collectionID?.startsWith(path) && + path === + folderPathCloseToSaveContext( + tab.value.document.inheritedProperties?.auth.parentID, + path, + tab.value.document.saveContext.collectionID + ) + ) + } + + if ( + tab.value.document.saveContext?.originLocation !== + "workspace-user-collection" + ) { + return false + } + + const requestHandle = ref(tab.value.document.saveContext.requestHandle) + + return ( + requestHandle.value.type === "ok" && + requestHandle.value.data.collectionID.startsWith(path) && + path === + folderPathCloseToSaveContext( + tab.value.document.inheritedProperties?.auth.parentID, + path, + requestHandle.value.data.collectionID + ) + ) + }) + + tabsEffectedByAuth.map((tab) => { const inheritedParentID = tab.value.document.inheritedProperties?.auth.parentID - const contextPath = - tab.value.document.saveContext?.originLocation === "team-collection" - ? tab.value.document.saveContext.collectionID - : tab.value.document.saveContext?.folderPath + let contextPath = "" + + if (tab.value.document.saveContext?.originLocation === "user-collection") { + contextPath = tab.value.document.saveContext.folderPath + } else if ( + tab.value.document.saveContext?.originLocation === + "workspace-user-collection" + ) { + const requestHandle = ref(tab.value.document.saveContext.requestHandle) + if (requestHandle.value.type === "ok") { + contextPath = requestHandle.value.data.collectionID + } + } else { + contextPath = tab.value.document.saveContext?.collectionID ?? "" + } const effectedPath = folderPathCloseToSaveContext( inheritedParentID,