Repository: aipotheosis-labs/aci
File: backend/aci/server/routes/webhooks.py (Python)

Code before:
        "created a default personal org for new user",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
        },
    )

    auth.update_org_metadata(org_id=org.org_id, metadata={"personal": True})
    logger.info(
        "updated org metadata (personal=True) for default personal org",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
        },
    )

    auth.add_user_to_org(user_id=user.user_id, org_id=org.org_id, role=OrganizationRole.OWNER)
    logger.info(
        "added new user to default personal org",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
        },
    )

    project = crud.projects.create_project(db_session, UUID(org.org_id), "Default Project")

    # Create a default Agent for the project
    agent = crud.projects.create_agent(
        db_session,
        project.id,
        name="Default Agent",
        description="Default Agent",
        allowed_apps=[],
        custom_instructions={},
    )
    db_session.commit()

    logger.info(
        "created default project and agent for new user",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
            "project_id": project.id,
            "agent_id": agent.id,
        },
    )


def _generate_secure_random_alphanumeric_string(length: int = 6) -> str:
    charset = string.ascii_letters + string.digits

Review comment (security):
if we are worried about propelauth change `org_id` type, maybe check here if it's is UUID first? or create a function `convert_to_uuid(any) (if type(str); if type(UUID); else...) .`
because if `org_id` is a uuid, UUID(org_id) will throw exception.

Code after:
        "created a default personal org for new user",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
        },
    )

    auth.update_org_metadata(org_id=org.org_id, metadata={"personal": True})
    logger.info(
        "updated org metadata (personal=True) for default personal org",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
        },
    )

    auth.add_user_to_org(user_id=user.user_id, org_id=org.org_id, role=OrganizationRole.OWNER)
    logger.info(
        "added new user to default personal org",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
        },
    )

    org_id_uuid = _convert_org_id_to_uuid(org.org_id)
    project = crud.projects.create_project(db_session, org_id_uuid, "Default Project")

    # Create a default Agent for the project
    agent = crud.projects.create_agent(
        db_session,
        project.id,
        name="Default Agent",
        description="Default Agent",
        allowed_apps=[],
        custom_instructions={},
    )
    db_session.commit()

    logger.info(
        "created default project and agent for new user",
        extra={
            "user_id": user.user_id,
            "org_id": org.org_id,
            "project_id": project.id,
            "agent_id": agent.id,
        },
    )


def _generate_secure_random_alphanumeric_string(length: int = 6) -> str: