Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.1 KB
Raw Blame
use axum::{Json, extract::State};
use entity_access::domain::ports::EntityAccessService;
use macro_authorization::{MacroAuthorizationExtractor, MacroAuthorizationService, UserOrInternal};

use crate::domain::{
    model::{CreateTeamError, Team},
    team_repo::TeamService,
};

use super::TeamRouterState;

/// The request body to create a new team
#[derive(serde::Serialize, serde::Deserialize, utoipa::ToSchema)]
pub struct CreateTeamRequest {
    /// The name of the team
    pub name: String,
    /// Whether colleagues who sign up with the owner's work email domain join
    /// the team automatically. Ignored for generic email providers.
    #[serde(default = "default_allow_domain_join")]
    pub allow_domain_join: bool,
}

fn default_allow_domain_join() -> bool {
    true
}

/// Creates a new team.
#[utoipa::path(
    post,
    path = "/team",
    operation_id = "create_team",
    responses(
        (status = 200, body = Team),
        (status = 400, body = model_error_response::ErrorResponse),
        (status = 403, body = model_error_response::ErrorResponse),
        (status = 500, body = model_error_response::ErrorResponse),
    ),
)]
#[tracing::instrument(skip_all, err)]
pub async fn handler<T: TeamService, Eas: EntityAccessService, Auth: MacroAuthorizationService>(
    State(state): State<TeamRouterState<T, Eas, Auth>>,
    user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
    Json(req): Json<CreateTeamRequest>,
) -> Result<Json<Team>, CreateTeamError> {
    let user = &user.authorization.user;
    // Team creation and membership do not require a subscription. Link the
    // owner's existing subscription when present for paid billing/entitlements.
    let subscription_id = state
        .service
        .is_user_premium(&user.macro_user_id)
        .await
        .map_err(|e| CreateTeamError::StorageLayerError(e.into()))?;

    let team = state
        .service
        .create_team(
            &user.macro_user_id,
            &req.name,
            subscription_id.as_ref(),
            req.allow_domain_join,
        )
        .await?;

    Ok(Json(team))
}