Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! Permission-related query helpers.
//!
//! These queries read tables owned by other domains but are needed by the
//! permission outbound adapter. They are inlined here so this crate does not
//! depend on the monolithic db client crates.
use models_properties::EntityType;
use sqlx::{Pool, Postgres};
use uuid::Uuid;
#[cfg(test)]
mod test;
/// Gets the entity's owner and whether it's deleted.
/// Errors if the entity doesn't exist or the entity type is unsupported.
pub async fn get_owner_and_deleted(
pool: &Pool<Postgres>,
entity_id: &str,
entity_type: EntityType,
) -> anyhow::Result<(String, bool)> {
let result = match entity_type {
// The following entity types are either deleted immediately or simply
// unsupported by this owner lookup. Calls resolve access through their
// owning channel (see `access_entity_type`), so the caller skips this
// path for them entirely.
EntityType::CallRecord
| EntityType::Initiative
| EntityType::Channel
| EntityType::Company
| EntityType::Contact
| EntityType::User
| EntityType::Thread => {
anyhow::bail!("unsupported entity type")
}
EntityType::Document | EntityType::Task => {
sqlx::query!(
r#"SELECT owner, "deletedAt" as deleted_at FROM "Document" WHERE id=$1"#,
entity_id
)
.map(|r| (r.owner, r.deleted_at.is_some()))
.fetch_one(pool)
.await?
}
EntityType::CalendarEvent => {
let row = sqlx::query!(
"SELECT owner_id FROM calendar_events WHERE id = $1",
Uuid::parse_str(entity_id)?,
)
.fetch_one(pool)
.await?;
(row.owner_id, false)
}
EntityType::Chat => {
sqlx::query!(
r#"SELECT "userId" as user_id, "deletedAt" as deleted_at FROM "Chat" WHERE id=$1"#,
entity_id
)
.map(|r| (r.user_id, r.deleted_at.is_some()))
.fetch_one(pool)
.await?
}
EntityType::Project => sqlx::query!(
r#"SELECT "userId" as user_id, "deletedAt" as deleted_at FROM "Project" WHERE id=$1"#,
entity_id
)
.map(|r| (r.user_id, r.deleted_at.is_some()))
.fetch_one(pool)
.await?,
// A row has no owner or trash state of its own: both are its
// database's.
EntityType::DatabaseRow => {
sqlx::query!(
r#"
SELECT d.user_id AS owner_id, d.trashed_at
FROM database_rows r
JOIN database_tables t ON t.id = r.table_id
JOIN database_entities d ON d.database_id = t.database_id
WHERE r.id = $1
"#,
Uuid::parse_str(entity_id)?,
)
.map(|r| (r.owner_id, r.trashed_at.is_some()))
.fetch_one(pool)
.await?
}
};
Ok(result)
}
/// Gets the macro user id of the owner of an email thread via its link.
/// Returns `None` if the thread doesn't exist.
pub async fn get_macro_id_from_thread_id(
pool: &Pool<Postgres>,
thread_id: Uuid,
) -> anyhow::Result<Option<String>> {
let macro_id = sqlx::query_scalar!(
r#"
SELECT l.macro_id
FROM email_threads t
JOIN email_links l ON t.link_id = l.id
WHERE t.id = $1
"#,
thread_id
)
.fetch_optional(pool)
.await?;
Ok(macro_id)
}