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
use std::collections::HashSet;
use sqlx::{Pool, Postgres};
#[cfg(test)]
mod test;
/// Creates a new user in the database and attaches provided roles
///
/// `macro_user_customer_id` supplies the required legacy account key. Only an
/// actual Stripe customer belongs in the optional profile billing field.
#[expect(clippy::too_many_arguments, reason = "too annoying to fix")]
#[tracing::instrument(skip(db))]
pub async fn create_user(
db: &Pool<Postgres>,
id: &str,
username: &str,
email: &str,
is_verified: bool,
macro_user_customer_id: &str,
stripe_customer_id: Option<&str>,
organization_id: Option<i32>,
roles: HashSet<String>,
is_agent: bool,
) -> anyhow::Result<String> {
let id = macro_uuid::string_to_uuid(id)?;
let mut transaction = db.begin().await?;
// Create macro user
let macro_user_id = sqlx::query!(
r#"
INSERT INTO "macro_user" ("id", "username", "stripe_customer_id", "email", "has_trialed")
VALUES ($1, $2, $3, $4, $5)
RETURNING id
"#,
&id,
username,
macro_user_customer_id,
email,
false // has_trialed
)
.map(|row| row.id)
.fetch_one(&mut *transaction)
.await?;
sqlx::query!(
r#"
INSERT INTO "macro_user_email_verification" ("macro_user_id", "email", "is_verified")
VALUES ($1, $2, $3)
"#,
&id,
email,
is_verified
)
.execute(&mut *transaction)
.await?;
// Create user profile
let id = format!("macro|{}", email);
let user_id = sqlx::query!(
r#"
INSERT INTO "User" ("id", "email", "stripeCustomerId", "organizationId", "macro_user_id", "isAgent")
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id
"#,
&id,
email,
stripe_customer_id,
organization_id,
¯o_user_id,
is_agent
)
.map(|row| row.id)
.fetch_one(&mut *transaction)
.await?;
for role in roles {
sqlx::query!(
r#"
INSERT INTO "RolesOnUsers" ("userId", "roleId")
VALUES ($1, $2)
"#,
user_id,
role
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(id)
}
/// Creates a new user profile in the database and attaches provided roles
#[tracing::instrument(skip(db))]
pub async fn create_user_profile(
db: &Pool<Postgres>,
id: &str,
email: &str,
organization_id: Option<i32>,
roles: HashSet<String>,
) -> anyhow::Result<()> {
let macro_user_id = macro_uuid::string_to_uuid(id)?;
let user_profile_id = format!("macro|{}", email);
let mut transaction = db.begin().await?;
let user_id = sqlx::query!(
r#"
INSERT INTO "User" ("id", "email", "organizationId", "macro_user_id", "isAgent")
VALUES (
$1, $2, $3, $4,
COALESCE((SELECT BOOL_OR(u."isAgent") FROM "User" u WHERE u.macro_user_id = $4), FALSE)
)
RETURNING id
"#,
&user_profile_id,
email,
organization_id,
¯o_user_id
)
.map(|row| row.id)
.fetch_one(&mut *transaction)
.await?;
for role in roles {
sqlx::query!(
r#"
INSERT INTO "RolesOnUsers" ("userId", "roleId")
VALUES ($1, $2)
"#,
user_id,
role
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(())
}