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.

18.3 KB
Raw Blame
use crate::history::{upsert_item_last_accessed_timestamp, upsert_user_history_timestamp};
use crate::share_permission::create::create_document_permission;
use document_sub_type::DocumentSubType;
use macro_user_id::cowlike::CowLike;
use macro_user_id::user_id::MacroUserIdStr;
use model::document::DocumentMetadata;
use model::document::FileType;
use model::document::ID;
use model::document::VersionIDWithTimeStamps;
use model_entity::EntityType;
use models_permissions::share_permission::SharePermissionV2;
use models_permissions::share_permission::access_level::AccessLevel;
use sqlx::Pool;
use sqlx::Postgres;
use sqlx::Transaction;
use tracing::instrument;
use uuid::Uuid;

/// struct for creating a document
#[derive(Debug)]
pub struct CreateDocumentArgs<'a> {
    pub id: Option<&'a str>,
    pub sha: &'a str,
    pub document_name: &'a str,
    pub user_id: MacroUserIdStr<'static>,
    pub file_type: Option<FileType>,
    pub project_id: Option<&'a str>,
    pub project_name: Option<&'a str>,
    pub share_permission: &'a SharePermissionV2,
    pub skip_history: bool,
    /// Optional value. If set, creates entry in document_email
    pub email_attachment_id: Option<Uuid>,
    /// Optional value. defaults to now if not included
    pub created_at: Option<&'a chrono::DateTime<chrono::Utc>>,
    /// Whether the document is a task.
    /// This is only applicable for md documents.
    pub is_task: bool,
}

/// Creates a new document
#[instrument(skip(db))]
pub async fn create_document(
    db: &Pool<Postgres>,
    args: CreateDocumentArgs<'_>,
) -> anyhow::Result<DocumentMetadata> {
    let mut transaction = db.begin().await.inspect_err(|e| {
        tracing::error!(error=?e, "unable to create transaction");
    })?;

    let document_metadata = create_document_txn(&mut transaction, args).await?;

    transaction.commit().await.inspect_err(|e| {
        tracing::error!(error=?e, "unable to commit transaction");
    })?;

    Ok(document_metadata)
}

/// Creates a new document in a transaction without committing
#[instrument(skip(transaction))]
pub async fn create_document_txn(
    transaction: &mut Transaction<'_, Postgres>,
    args: CreateDocumentArgs<'_>,
) -> anyhow::Result<DocumentMetadata> {
    tracing::trace!("creating document");
    let CreateDocumentArgs {
        id,
        sha,
        document_name,
        user_id,
        file_type,
        project_id,
        project_name: provided_project_name,
        share_permission,
        skip_history,
        created_at: _, // overwritten below
        email_attachment_id,
        is_task,
    } = args;

    // default to now if created_at argument not included
    let now = chrono::Utc::now();
    let created_at = args.created_at.unwrap_or(&now);

    // Fetches project name for provided project_id if name is not provided
    let fetched_project_name: Option<String> = match (provided_project_name, project_id) {
        (None, Some(proj_id)) => {
            let name: String = sqlx::query_scalar!(
                r#"
                    SELECT name FROM "Project" WHERE id = $1
                "#,
                proj_id,
            )
            .fetch_one(transaction.as_mut())
            .await
            .map_err(|err| {
                tracing::error!(error=?err, project_id=%proj_id, "unable to get project");
                err
            })?;

            Some(name)
        }
        _ => None,
    };

    let project_name: Option<&str> = provided_project_name.or(fetched_project_name.as_deref());

    // insert document with id if included in request
    let document_id = if let Some(id) = id {
        insert_document_with_id(
            transaction,
            id,
            user_id.copied(),
            document_name,
            file_type,
            project_id,
            created_at,
        )
        .await?;
        id.to_string()
    } else {
        insert_document_no_id(
            transaction,
            user_id.copied(),
            document_name,
            file_type,
            project_id,
            created_at,
        )
        .await?
        .id
    };

    let sub_type: Option<DocumentSubType> = if is_task {
        if file_type != Some(FileType::Md) {
            anyhow::bail!("is_task is only applicable for md documents");
        }

        sqlx::query!(
            r#"
            INSERT INTO document_sub_type (document_id, sub_type) 
            VALUES ($1, $2)
            "#,
            document_id,
            DocumentSubType::Task as _
        )
        .execute(&mut **transaction)
        .await?;

        Some(DocumentSubType::Task)
    } else {
        None
    };

    // Docx documents have their versions associated with a DocumentBom
    // whereas all other file types use DocumentInstance
    let document_version = match file_type {
        Some(FileType::Docx) => {
            let document_bom = sqlx::query!(
            r#"
                INSERT INTO "DocumentBom" ("documentId", "createdAt", "updatedAt")
                VALUES ($1, $2, $2)
                RETURNING id, "createdAt"::timestamptz as created_at, "updatedAt"::timestamptz as updated_at;
            "#,
                &document_id,
                created_at.naive_utc(),
            )
            .fetch_one(transaction.as_mut())
            .await?;

            VersionIDWithTimeStamps {
                id: document_bom.id,
                created_at: document_bom.created_at,
                updated_at: document_bom.updated_at,
                sha: sha.to_string(),
            }
        }
        _ => {
            sqlx::query_as!(
                VersionIDWithTimeStamps,
                r#"
                    INSERT INTO "DocumentInstance" ("documentId", "sha", "createdAt", "updatedAt")
                    VALUES ($1, $2, $3, $3)
                    RETURNING id, sha, "createdAt"::timestamptz as created_at, "updatedAt"::timestamptz as updated_at;
                "#,
                &document_id,
                sha,
                created_at.naive_utc()
        )
        .fetch_one(transaction.as_mut())
        .await?
        }
    };

    // create document permission
    create_document_permission(transaction, &document_id, share_permission).await?;

    // Add item to user history for creator
    if !skip_history {
        upsert_user_history_timestamp(
            transaction,
            user_id.copied(),
            &document_id,
            "document",
            created_at,
        )
        .await?;
        upsert_item_last_accessed_timestamp(transaction, &document_id, "document", created_at)
            .await?;
    }

    entity_access_db_utils::insert_entity_access_row(
        transaction,
        &macro_uuid::string_to_uuid(&document_id).unwrap(),
        EntityType::Document,
        user_id.as_ref(),
        entity_access_db_utils::EntityAccessSourceType::User,
        AccessLevel::Owner,
    )
    .await?;
    entity_registry_db_utils::insert_entity(
        transaction,
        entity_registry_db_utils::NewEntityRecord::new(
            macro_uuid::string_to_uuid(&document_id)?,
            entity_registry_db_utils::RegisteredEntityType::Document,
            model_owner::Owner::User(user_id.clone()),
        ),
    )
    .await?;

    if let Some(attachment) = email_attachment_id {
        crate::document::document_email::create_document_email_record(
            transaction,
            &document_id,
            attachment,
        )
        .await?;
    }

    Ok(DocumentMetadata::new_document(
        &document_id,
        document_version.id,
        model_owner::Owner::User(user_id),
        document_name,
        file_type,
        document_version.sha.as_str(),
        None,
        None,
        None,
        project_id,
        project_name,
        document_version.created_at,
        document_version.updated_at,
        sub_type,
    ))
}

/// Inserts a new document record into the database
#[instrument(skip(transaction))]
async fn insert_document_no_id(
    transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
    user_id: MacroUserIdStr<'_>,
    document_name: &str,
    file_type: Option<FileType>,
    project_id: Option<&str>,
    created_at: &chrono::DateTime<chrono::Utc>,
) -> anyhow::Result<ID> {
    sqlx::query_as!(
        ID,
        r#"
            INSERT INTO "Document" (owner, name, "fileType", "projectId", "createdAt", "updatedAt") 
            VALUES ($1, $2, $3, $4, $5, $5)
            RETURNING id;
        "#,
        user_id.as_ref(),
        document_name,
        file_type.map(|file_type| file_type.as_str().to_string()),
        project_id,
        created_at.naive_utc()
    )
    .fetch_one(&mut **transaction)
    .await
    .map_err(|err| {
        tracing::error!(error=?err, "unable to create document");
        anyhow::anyhow!("unable to create document: {}", err)
    })
}

#[instrument(skip(transaction))]
async fn insert_document_with_id(
    transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
    id: &str,
    user_id: MacroUserIdStr<'_>,
    document_name: &str,
    file_type: Option<FileType>,
    project_id: Option<&str>,
    created_at: &chrono::DateTime<chrono::Utc>,
) -> anyhow::Result<()> {
    let result = sqlx::query!(
        r#"
            INSERT INTO "Document" (id, owner, name, "fileType", "projectId", "createdAt", "updatedAt")
            VALUES ($1, $2, $3, $4, $5, $6, $6)
        "#,
        id,
        user_id.as_ref(),
        document_name,
        file_type.map(|file_type| file_type.as_str().to_string()),
        project_id,
        created_at.naive_utc()
    )
    .execute(&mut **transaction)
    .await;

    match result {
        Ok(_) => Ok(()),
        Err(err) => {
            // Check for duplicate key violation
            if let sqlx::Error::Database(db_err) = &err
                && db_err.is_unique_violation()
            {
                tracing::warn!(error=?err, "document with ID already exists");
                anyhow::bail!("document with ID already exists: {}", id);
            }

            // All other errors
            tracing::error!(error=?err, "unable to create document with specified ID");
            anyhow::bail!("unable to create document with specified ID: {}", err)
        }
    }
}

#[cfg(test)]
mod test;

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;
    use chrono::TimeZone;
    use model_owner::Owner;
    use sqlx::{Pool, Postgres};

    #[sqlx::test(fixtures(path = "../../../fixtures", scripts("basic_user_with_documents")))]
    async fn test_create_document_no_id(pool: Pool<Postgres>) -> anyhow::Result<()> {
        sqlx::query!(
            r#"INSERT INTO "Project" ("id", "name", "userId") VALUES ($1, $2, $3)"#,
            "project-one",
            "name",
            "macro|[email protected]"
        )
        .execute(&pool)
        .await?;

        let ts = chrono::Utc.with_ymd_and_hms(1998, 1, 16, 0, 0, 0).unwrap();
        // document exists
        let document_metadata = create_document(
            &pool,
            CreateDocumentArgs {
                id: None,
                sha: "sha",
                document_name: "document-name",
                user_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
                file_type: Some(FileType::Pdf),
                project_id: Some("project-one"),
                project_name: None,
                share_permission: &SharePermissionV2::new_document_share_permission(
                    Some(FileType::Pdf),
                    None,
                ),
                skip_history: false,
                email_attachment_id: None,
                created_at: Some(&ts),
                is_task: false,
            },
        )
        .await?;

        assert!(!document_metadata.document_id.is_empty());
        assert_eq!(document_metadata.document_name, "document-name".to_string());
        assert_eq!(
            document_metadata.owner,
            Owner::from_principal_str("macro|[email protected]").unwrap()
        );
        assert_eq!(document_metadata.project_id.as_deref(), Some("project-one"));
        assert_eq!(document_metadata.project_name.as_deref(), Some("name"));
        assert_eq!(document_metadata.created_at, Some(ts));

        // document exists
        let document_metadata = create_document(
            &pool,
            CreateDocumentArgs {
                id: None,
                sha: "sha",
                document_name: "document-name",
                user_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
                file_type: Some(FileType::Docx),
                project_id: None,
                project_name: None,
                share_permission: &SharePermissionV2::new_document_share_permission(
                    Some(FileType::Docx),
                    None,
                ),
                skip_history: false,
                email_attachment_id: None,
                created_at: None,
                is_task: false,
            },
        )
        .await?;

        assert!(!document_metadata.document_id.is_empty());
        assert_eq!(document_metadata.document_name, "document-name".to_string());
        assert_eq!(
            document_metadata.owner,
            Owner::from_principal_str("macro|[email protected]").unwrap()
        );

        Ok(())
    }

    #[sqlx::test(fixtures(path = "../../../fixtures", scripts("basic_user_with_documents")))]
    async fn test_create_document_with_id(pool: Pool<Postgres>) -> anyhow::Result<()> {
        sqlx::query!(
            r#"INSERT INTO "Project" ("id", "name", "userId") VALUES ($1, $2, $3)"#,
            "project-one",
            "name",
            "macro|[email protected]"
        )
        .execute(&pool)
        .await?;

        let ts = chrono::Utc.with_ymd_and_hms(1998, 1, 16, 0, 0, 0).unwrap();
        // document exists
        let document_metadata = create_document(
            &pool,
            CreateDocumentArgs {
                id: Some("20f603c2-99db-aaaa-0000-1d8b9f95a52f"),
                sha: "sha",
                document_name: "document-name",
                user_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
                file_type: Some(FileType::Pdf),
                project_id: Some("project-one"),
                project_name: None,
                share_permission: &SharePermissionV2::new_document_share_permission(
                    Some(FileType::Pdf),
                    None,
                ),
                skip_history: false,
                email_attachment_id: None,
                created_at: Some(&ts),
                is_task: false,
            },
        )
        .await?;

        assert_eq!(
            document_metadata.document_id,
            "20f603c2-99db-aaaa-0000-1d8b9f95a52f"
        );
        assert_eq!(document_metadata.document_name, "document-name".to_string());
        assert_eq!(
            document_metadata.owner,
            Owner::from_principal_str("macro|[email protected]").unwrap()
        );
        assert_eq!(document_metadata.project_id.as_deref(), Some("project-one"));
        assert_eq!(document_metadata.project_name.as_deref(), Some("name"));
        assert_eq!(document_metadata.created_at, Some(ts));

        // document exists
        let document_metadata = create_document(
            &pool,
            CreateDocumentArgs {
                id: Some("20f603c2-99db-4f02-aaaa-1d8b9f95a52f"),
                sha: "sha",
                document_name: "document-name",
                user_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
                file_type: Some(FileType::Docx),
                project_id: None,
                project_name: None,
                share_permission: &SharePermissionV2::new_document_share_permission(
                    Some(FileType::Docx),
                    None,
                ),
                skip_history: false,
                email_attachment_id: None,
                created_at: None,
                is_task: false,
            },
        )
        .await?;

        assert_eq!(
            document_metadata.document_id,
            "20f603c2-99db-4f02-aaaa-1d8b9f95a52f"
        );
        assert_eq!(document_metadata.document_name, "document-name".to_string());
        assert_eq!(
            document_metadata.owner,
            Owner::from_principal_str("macro|[email protected]").unwrap()
        );

        Ok(())
    }

    // should return appropriate error if we try to insert a document with a duplicate ID
    #[sqlx::test(fixtures(path = "../../../fixtures", scripts("basic_user_with_documents")))]
    async fn test_insert_document_with_duplicate_id(pool: Pool<Postgres>) -> anyhow::Result<()> {
        let test_id = "duplicate-document-id";
        let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

        // First, create a document with the test ID
        let mut transaction = pool.begin().await?;

        let ts = chrono::Utc.with_ymd_and_hms(1998, 1, 16, 0, 0, 0).unwrap();

        // Insert the first document
        insert_document_with_id(
            &mut transaction,
            test_id,
            user_id.clone(),
            "First Document",
            Some(FileType::Pdf),
            None,
            &ts,
        )
        .await?;

        transaction.commit().await?;

        // Now try to insert another document with the same ID
        let mut transaction = pool.begin().await?;

        let result = insert_document_with_id(
            &mut transaction,
            test_id,
            user_id.clone(),
            "Second Document with Same ID",
            Some(FileType::Pdf),
            None,
            &ts,
        )
        .await;

        // Don't commit the transaction since we expect it to fail

        // Check that the function returned an error
        assert!(result.is_err());

        // Check that the error message contains our expected text about duplicate ID
        let error_message = result.unwrap_err().to_string();
        assert!(
            error_message.contains("document with ID already exists"),
            "Error message '{}' does not contain expected text about duplicate ID",
            error_message
        );

        // Verify that only one document with this ID exists in the database
        let count = sqlx::query!(
            r#"SELECT COUNT(*) as "count!" FROM "Document" WHERE id = $1"#,
            test_id
        )
        .fetch_one(&pool)
        .await?
        .count;

        assert_eq!(
            count, 1,
            "There should be exactly one document with ID '{}'",
            test_id
        );

        Ok(())
    }
}