diff --git a/js/sdk/__tests__/GraphsIntegrationSuperUser.test.ts b/js/sdk/__tests__/GraphsIntegrationSuperUser.test.ts index 490db1cc0..3341ca818 100644 --- a/js/sdk/__tests__/GraphsIntegrationSuperUser.test.ts +++ b/js/sdk/__tests__/GraphsIntegrationSuperUser.test.ts @@ -386,6 +386,24 @@ describe("r2rClient V3 Graphs Integration Tests", () => { expect(response.results.predicate).toBe("marries"); }); + test("Delete the community", async () => { + const response = await client.graphs.deleteCommunity({ + collectionId: collectionId, + communityId: communityId, + }); + + expect(response.results).toBeDefined(); + }); + + test("Check that the community was deleted", async () => { + const response = await client.graphs.listCommunities({ + collectionId: collectionId, + }); + + expect(response.results).toBeDefined(); + expect(response.results.entries).toHaveLength(0); + }); + test("Reset the graph", async () => { const response = await client.graphs.reset({ collectionId: collectionId, diff --git a/py/core/main/api/v3/graph_router.py b/py/core/main/api/v3/graph_router.py index ded054e81..cdd23c724 100644 --- a/py/core/main/api/v3/graph_router.py +++ b/py/core/main/api/v3/graph_router.py @@ -1150,7 +1150,37 @@ async def delete_relationship( client = R2RClient("http://localhost:7272") # when using auth, do client.login(...) - result = client.graphs.communities.create(collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1", communities=[community1, community2]) + result = client.graphs.create_community( + collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1", + name="My Community", + summary="A summary of the community", + findings=["Finding 1", "Finding 2"], + rating=5, + rating_explanation="This is a rating explanation", + ) + """ + ), + }, + { + "lang": "JavaScript", + "source": textwrap.dedent( + """ + const { r2rClient } = require("r2r-js"); + + const client = new r2rClient("http://localhost:7272"); + + function main() { + const response = await client.graphs.createCommunity({ + collectionId: "9fbe403b-c11c-5aae-8ade-ef22980c3ad1", + name: "My Community", + summary: "A summary of the community", + findings: ["Finding 1", "Finding 2"], + rating: 5, + ratingExplanation: "This is a rating explanation", + }); + } + + main(); """ ), }, @@ -1414,14 +1444,18 @@ async def delete_community( ), auth_user=Depends(self.providers.auth.auth_wrapper), ): - if not auth_user.is_superuser: + if ( + not auth_user.is_superuser + and collection_id not in auth_user.graph_ids + ): raise R2RException( - "Only superusers can access this endpoint.", 403 + "The currently authenticated user does not have access to the specified graph.", + 403, ) - await self.services["kg"].delete_community_v3( - graph_id=collection_id, + + await self.services["kg"].delete_community( + parent_id=collection_id, community_id=community_id, - auth_user=auth_user, ) return GenericBooleanResponse(success=True) # type: ignore diff --git a/py/core/main/services/kg_service.py b/py/core/main/services/kg_service.py index 57c5688fe..b4ea2aa91 100644 --- a/py/core/main/services/kg_service.py +++ b/py/core/main/services/kg_service.py @@ -315,10 +315,8 @@ async def create_relationship( @telemetry_event("delete_relationship_v3") async def delete_relationship_v3( self, - level: DataLevel, id: UUID, relationship_id: UUID, - **kwargs, ): return ( await self.providers.database.graph_handler.relationships.delete( @@ -437,18 +435,15 @@ async def update_community( rating_explanation=rating_explanation, ) - @telemetry_event("delete_community_v3") - async def delete_community_v3( + @telemetry_event("delete_community") + async def delete_community( self, - graph_id: UUID, + parent_id: UUID, community_id: UUID, - auth_user: Any, - **kwargs, - ): - return await self.providers.database.graph_handler.communities.delete( - graph_id=graph_id, + ) -> None: + await self.providers.database.graph_handler.communities.delete( + parent_id=parent_id, community_id=community_id, - auth_user=auth_user, ) @telemetry_event("list_communities_v3") diff --git a/py/core/providers/database/graph.py b/py/core/providers/database/graph.py index b613f2217..4896e651a 100644 --- a/py/core/providers/database/graph.py +++ b/py/core/providers/database/graph.py @@ -885,22 +885,28 @@ async def create( description_embedding, ] - result = await self.connection_manager.fetchrow_query( - query=query, - params=params, - ) + try: + result = await self.connection_manager.fetchrow_query( + query=query, + params=params, + ) - return Community( - id=result["id"], - community_id=result["community_id"], - name=result["name"], - summary=result["summary"], - findings=result["findings"], - rating=result["rating"], - rating_explanation=result["rating_explanation"], - created_at=result["created_at"], - updated_at=result["updated_at"], - ) + return Community( + id=result["id"], + community_id=result["community_id"], + name=result["name"], + summary=result["summary"], + findings=result["findings"], + rating=result["rating"], + rating_explanation=result["rating_explanation"], + created_at=result["created_at"], + updated_at=result["updated_at"], + ) + except Exception as e: + raise HTTPException( + status_code=500, + detail=f"An error occurred while creating the community: {e}", + ) async def update( self, @@ -950,14 +956,25 @@ async def update( async def delete( self, - graph_id: UUID, + parent_id: UUID, community_id: UUID, ) -> None: + table_name = "graph_community" - QUERY = f""" - DELETE FROM {self._get_table_name("graph_community")} WHERE id = $1 + query = f""" + DELETE FROM {self._get_table_name(table_name)} + WHERE id = $1 AND graph_id = $2 """ - await self.connection_manager.execute_query(QUERY, [community_id]) + + params = [community_id, parent_id] + + try: + await self.connection_manager.execute_query(query, params) + except Exception as e: + raise HTTPException( + status_code=500, + detail=f"An error occurred while deleting the community: {e}", + ) async def get( self,