db_v1_queries_org - OmniCloudOrg/OmniOrchestrator GitHub Wiki
Path: src/db/v1/queries/org.rs
- async fn list_orgs
- async fn get_org_by_id
- async fn create_org
- async fn update_org
- async fn delete_org
- async fn add_org_member
- async fn remove_org_member
- async fn update_org_member_role
pub async fn list_orgs(pool: &Pool<MySql>) -> anyhow::Result<Vec<Org>> {
let orgs = sqlx::query_as::<_, Org>("SELECT * FROM orgs ORDER BY created_at DESC")
.fetch_all(pool)
.await
.context("Failed to fetch organizations")?;
Retrieves all organizations in the system, ordered by creation time. This function fetches all organization records from the database, with the most recently created organizations first. It provides a complete view of all organizations in the system.
-
pool
- Database connection pool for executing the query
-
Ok(Vec<Org>)
- Successfully retrieved list of organizations -
Err(anyhow::Error)
- Failed to fetch organizations
Common use cases include: - Administrative dashboards showing all organizations - System-wide reports and analytics - Multi-tenant application management
pub async fn get_org_by_id(pool: &Pool<MySql>, id: i64) -> anyhow::Result<Org> {
let org = sqlx::query_as::<_, Org>("SELECT * FROM orgs WHERE id = ?")
.bind(id)
.fetch_one(pool)
.await
.context("Failed to fetch organization")?;
Retrieves a specific organization by its unique identifier. This function fetches detailed information about a single organization record. It's typically used when specific organization details are needed, such as for displaying organization profiles or handling organization-specific operations.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the organization to retrieve
-
Ok(Org)
- Successfully retrieved organization information -
Err(anyhow::Error)
- Failed to fetch organization (including if not found)
Returns an error if no organization with the given ID exists or if a database error occurs during the query execution.
pub async fn create_org(pool: &Pool<MySql>, name: &str) -> anyhow::Result<Org> {
let mut tx = pool.begin().await?;
Creates a new organization in the system. This function inserts a new organization record with the provided name. It uses a transaction to ensure data consistency during the creation process.
-
pool
- Database connection pool for executing the query -
name
- Name of the new organization
-
Ok(Org)
- Successfully created organization record -
Err(anyhow::Error)
- Failed to create organization record
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
The created organization will have system-generated values for: - id
- Auto-incremented primary key - created_at
- Timestamp of creation - updated_at
- Initially same as creation timestamp
pub async fn update_org(pool: &Pool<MySql>, id: i64, name: &str) -> anyhow::Result<Org> {
let mut tx = pool.begin().await?;
Updates an existing organization's information. This function modifies an organization record with the provided name. It also updates the updated_at
timestamp to reflect the modification time.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the organization to update -
name
- New name for the organization
-
Ok(Org)
- Successfully updated organization record -
Err(anyhow::Error)
- Failed to update organization
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
Returns an error if no organization with the given ID exists or if a database error occurs during the update operation.
pub async fn delete_org(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
let mut tx = pool.begin().await?;
Deletes an organization from the system. This function permanently removes an organization record from the database. It should be used with caution, as it typically has significant implications for associated data and user access.
-
pool
- Database connection pool for executing the query -
id
- Unique identifier of the organization to delete
-
Ok(())
- Successfully deleted the organization -
Err(anyhow::Error)
- Failed to delete the organization
This operation is irreversible. Consider the implications before deleting organizations, especially those with active users or resources.
This function only deletes the organization record itself. It does not cascade delete related records such as organization members, applications, or other resources associated with the organization. Consider implementing cascading delete logic or foreign key constraints if needed.
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
pub async fn add_org_member(
pool: &Pool<MySql>,
org_id: i64,
user_id: i64,
role: &str,
) -> anyhow::Result<()> {
// ... function body
}
Adds a user as a member of an organization with a specific role. This function creates a relationship between a user and an organization, assigning the user a role within that organization. This role typically determines the user's permissions and access level within the organization.
-
pool
- Database connection pool for executing the query -
org_id
- Unique identifier of the organization -
user_id
- Unique identifier of the user to add -
role
- Role to assign to the user within the organization (e.g., "admin", "member")
-
Ok(())
- Successfully added the user to the organization -
Err(anyhow::Error)
- Failed to add the user to the organization
This function assumes that the combination of org_id
and user_id
must be unique in the orgmember table. If a user is already a member of the organization, the operation will fail with a unique constraint violation.
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
pub async fn remove_org_member(
pool: &Pool<MySql>,
org_id: i64,
user_id: i64,
) -> anyhow::Result<()> {
// ... function body
}
Removes a user from an organization. This function deletes the relationship between a user and an organization, effectively revoking the user's membership and access to the organization's resources.
-
pool
- Database connection pool for executing the query -
org_id
- Unique identifier of the organization -
user_id
- Unique identifier of the user to remove
-
Ok(())
- Successfully removed the user from the organization -
Err(anyhow::Error)
- Failed to remove the user from the organization
This function only removes the membership relationship. It does not perform any cleanup of resources owned by or associated with the user within the organization. Additional logic may be needed to handle resource ownership transfer or deletion.
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
pub async fn update_org_member_role(
pool: &Pool<MySql>,
org_id: i64,
user_id: i64,
role: &str,
) -> anyhow::Result<()> {
// ... function body
}
Updates a user's role within an organization. This function modifies the role assigned to a user within an organization, which typically affects their permissions and access level. This is useful for promoting or demoting users within the organization's hierarchy.
-
pool
- Database connection pool for executing the query -
org_id
- Unique identifier of the organization -
user_id
- Unique identifier of the user whose role to update -
role
- New role to assign to the user (e.g., "admin", "member")
-
Ok(())
- Successfully updated the user's role -
Err(anyhow::Error)
- Failed to update the user's role
Returns an error if the user is not a member of the organization or if a database error occurs during the update operation.
This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.
This function only performs the database operation to update a role. It does not implement any authorization logic to determine if the requesting user has permission to change roles. Such checks should be implemented in the business logic layer.