diff --git a/front/lib/api/vaults.ts b/front/lib/api/vaults.ts index da3eee112950..64f97651c99e 100644 --- a/front/lib/api/vaults.ts +++ b/front/lib/api/vaults.ts @@ -7,61 +7,57 @@ import logger from "@app/logger/logger"; export const getManagedDataSourceContent = async ( connectorId: string, permission: "read" | "write" | "read_write" | "none", - parentIds: string[] | null, + rootIds: string[] | null, + parentId: string | null, viewType: "tables" | "documents" ) => { const connectorsAPI = new ConnectorsAPI( config.getConnectorsAPIConfig(), logger ); - const contentNodes = []; - if (!parentIds) { - contentNodes.push( - await connectorsAPI.getConnectorPermissions({ - connectorId, - filterPermission: permission, - viewType, - }) - ); + if (parentId || !rootIds) { + const nodesResults = await connectorsAPI.getConnectorPermissions({ + connectorId, + filterPermission: permission, + parentId: parentId ?? undefined, + viewType, + }); + + if (nodesResults.isErr()) { + return nodesResults; + } + + contentNodes.push(...nodesResults.value.resources); } else { - for (const parentId of parentIds) { - contentNodes.push( - await connectorsAPI.getConnectorPermissions({ - connectorId, - parentId, - filterPermission: permission, - viewType, - }) - ); + const nodesResults = await connectorsAPI.getContentNodes({ + connectorId, + internalIds: rootIds, + viewType, + }); + + if (nodesResults.isErr()) { + return nodesResults; } - } - const err = contentNodes.find((r) => r.isErr()); - if (err?.isErr()) { - return err; + contentNodes.push(...nodesResults.value.nodes); } - const results = contentNodes.flatMap((r) => - r.isOk() - ? r.value.resources.map((r) => ({ - internalId: r.internalId, - parentInternalId: r.parentInternalId, - type: r.type, - title: r.title, - expandable: r.expandable, - preventSelection: r.preventSelection, - dustDocumentId: r.dustDocumentId, - lastUpdatedAt: r.lastUpdatedAt, - })) - : [] - ); + const results = contentNodes.map((r) => ({ + internalId: r.internalId, + parentInternalId: r.parentInternalId, + type: r.type, + title: r.title, + expandable: r.expandable, + preventSelection: r.preventSelection, + dustDocumentId: r.dustDocumentId, + lastUpdatedAt: r.lastUpdatedAt, + })); return new Ok(results); }; export const getUnmanagedDataSourceContent = async ( dataSource: DataSourceResource, - parentIds: string[] | null, viewType: "tables" | "documents", limit: number, offset: number diff --git a/front/pages/api/w/[wId]/vaults/[vId]/contentHandler.ts b/front/pages/api/w/[wId]/vaults/[vId]/contentHandler.ts index 4d08043d0854..3c8955955c36 100644 --- a/front/pages/api/w/[wId]/vaults/[vId]/contentHandler.ts +++ b/front/pages/api/w/[wId]/vaults/[vId]/contentHandler.ts @@ -27,7 +27,7 @@ export const getContentHandler = async ( req: NextApiRequest, res: NextApiResponse>, dataSource: DataSourceResource, - parentIds: string[] | null + rootNodes: string[] | null ): Promise => { const viewType = req.query.viewType; if ( @@ -44,8 +44,9 @@ export const getContentHandler = async ( }); } + let parentId: string | null = null; if (req.query.parentId && typeof req.query.parentId === "string") { - parentIds = [req.query.parentId]; + parentId = req.query.parentId; } const limit = req.query.limit ? parseInt(req.query.limit as string) : 10; @@ -55,16 +56,11 @@ export const getContentHandler = async ( ? await getManagedDataSourceContent( dataSource.connectorId, "read", - parentIds, + rootNodes, + parentId, viewType ) - : await getUnmanagedDataSourceContent( - dataSource, - parentIds, - viewType, - limit, - offset - ); + : await getUnmanagedDataSourceContent(dataSource, viewType, limit, offset); if (content.isErr()) { return apiError(req, res, {