Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/data/graph.py (Python)

Code before:
async def get_execution(user_id: str, execution_id: str) -> GraphExecution | None:
    execution = await AgentGraphExecution.prisma().find_first(
        where={"id": execution_id, "userId": user_id}
    )
    return GraphExecution.from_db(execution) if execution else None


async def get_graph(
    graph_id: str,
    version: int | None = None,
    user_id: str | None = None,
    for_export: bool = False,
) -> GraphModel | None:
    """
    Retrieves a graph from the DB.
    Defaults to the version with `is_active` if `version` is not passed,
    or the latest version with `is_template` if `template=True`.

    Returns `None` if the record is not found.
    """
    where_clause: AgentGraphWhereInput = {
        "id": graph_id,
    }

    if version is not None:
        where_clause["version"] = version

    graph = await AgentGraph.prisma().find_first(
        where=where_clause,
        include=AGENT_GRAPH_INCLUDE,
        order={"version": "desc"},
    )

    if not graph:
        return None

    if graph.userId == user_id:
        return GraphModel.from_db(graph, for_export) if graph else None

    # If the graph is not owned by the user, we need to check if it's a store listing.
    if not version:
        version = graph.version

    store_listing_where: StoreListingWhereInput = {
        "agentId": graph_id,
        "agentVersion": version,
    }

    store_listing = await StoreListing.prisma().find_first(where=store_listing_where)

    # If it is not a store listing, return None

Review comment (suggestion):
I understand removing the template check, but removing the `isActive` filter seems like an unintended behavior change?
```suggestion
    if version is not None:
        where_clause["version"] = version
    else:
        where_clause["isActive"] = True
```

Code after:
async def get_execution(user_id: str, execution_id: str) -> GraphExecution | None:
    execution = await AgentGraphExecution.prisma().find_first(
        where={"id": execution_id, "userId": user_id}
    )
    return GraphExecution.from_db(execution) if execution else None


async def get_graph(
    graph_id: str,
    version: int | None = None,
    user_id: str | None = None,
    for_export: bool = False,
) -> GraphModel | None:
    """
    Retrieves a graph from the DB.
    Defaults to the version with `is_active` if `version` is not passed,
    or the latest version with `is_template` if `template=True`.

    Returns `None` if the record is not found.
    """
    where_clause: AgentGraphWhereInput = {
        "id": graph_id,
    }

    if version is not None:
        where_clause["version"] = version
    else:
        where_clause["isActive"] = True

    graph = await AgentGraph.prisma().find_first(
        where=where_clause,
        include=AGENT_GRAPH_INCLUDE,
        order={"version": "desc"},
    )

    # The Graph has to be owned by the user or a store listing.
    if (
        graph is None
        or graph.userId != user_id
        and not (
            await StoreListingVersion.prisma().find_first(
                where=prisma.types.StoreListingVersionWhereInput(
                    agentId=graph_id,
                    agentVersion=version or graph.version,
                    isDeleted=False,
                    StoreListing={"is": {"isApproved": True}},
                )
            )
        )
    ):
        return None