Skip to content

Commit

Permalink
Fix datasourceview root
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier committed Aug 9, 2024
1 parent addc829 commit e4c878f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 48 deletions.
72 changes: 34 additions & 38 deletions front/lib/api/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions front/pages/api/w/[wId]/vaults/[vId]/contentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getContentHandler = async (
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetDataSourceContentResponseBody>>,
dataSource: DataSourceResource,
parentIds: string[] | null
rootNodes: string[] | null
): Promise<void> => {
const viewType = req.query.viewType;
if (
Expand All @@ -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;
Expand All @@ -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, {
Expand Down

0 comments on commit e4c878f

Please sign in to comment.