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.

4.9 KB
Raw Blame
use chrono::Utc;
use document_sub_type::DocumentSubType;
use model_owner::Owner;
use models_properties::EntityType;
use uuid::Uuid;

/// Sub type of a document with associated properties encoded in each variant.
/// This ensures type-safety: task properties only exist when the document is a task.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[cfg_attr(feature = "mock", derive(PartialEq, Eq))]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SoupDocumentSubType {
    /// A task document with its associated properties
    Task {
        /// Whether the task is completed.
        /// True if the Status property is set to "Completed".
        is_completed: bool,
    },
    /// A snippet document — reusable markdown
    Snippet {},
    /// A skill document — markdown instructions for AI
    Skill {},
    /// The description document of an initiative
    InitiativeDescription {},
}

impl SoupDocumentSubType {
    /// Converts from DB representation (separate sub_type and is_completed columns)
    /// to the domain enum.
    pub fn from_db(sub_type: Option<DocumentSubType>, is_completed: Option<bool>) -> Option<Self> {
        match sub_type? {
            DocumentSubType::Task => Some(Self::Task {
                is_completed: is_completed.unwrap_or_default(),
            }),
            DocumentSubType::Snippet => Some(Self::Snippet {}),
            DocumentSubType::Skill => Some(Self::Skill {}),
            DocumentSubType::InitiativeDescription => Some(Self::InitiativeDescription {}),
        }
    }

    /// Returns whether this is a completed task
    pub fn is_task_completed(&self) -> Option<bool> {
        match self {
            Self::Task { is_completed } => Some(*is_completed),
            Self::Snippet {} | Self::Skill {} | Self::InitiativeDescription {} => None,
        }
    }
}

/// A document as displayed in Soup.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
pub struct SoupDocument<T = ()> {
    /// The document id
    pub id: Uuid,

    /// The version of the document
    /// This could be the document_instance_id or document_bom_id depending on the file type
    pub document_version_id: i64,

    /// The owner of the document
    #[cfg_attr(feature = "schema", schema(value_type = String))]
    pub owner_id: Owner,

    /// The name of the document
    pub name: String,

    /// The file type of the document (e.g. pdf, docx)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_type: Option<String>,

    /// If the document is a PDF, this is the SHA of the pdf
    /// If the document is a DOCX, this will not be present
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha: Option<String>,

    /// The id of the project that this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_id: Option<Uuid>,

    /// The id of the document this document branched from
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branched_from_id: Option<Uuid>,

    /// The id of the version this document branched from
    /// This could be either DocumentInstance or DocumentBom id depending on the file type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branched_from_version_id: Option<i64>,

    /// The id of the document family this document belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_family_id: Option<i64>,

    /// The time the document was created
    pub created_at: chrono::DateTime<Utc>,

    /// The time the document instance / document BOM was updated
    pub updated_at: chrono::DateTime<Utc>,

    /// The time the document was last viewed
    pub viewed_at: Option<chrono::DateTime<Utc>>,

    /// The sub type of the document if present.
    /// Task-related properties are encoded within the variant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_type: Option<SoupDocumentSubType>,

    /// The time the document was deleted
    pub deleted_at: Option<chrono::DateTime<Utc>>,

    /// Extra fields passed from above
    #[serde(flatten)]
    pub extra: T,
}

impl<T> SoupDocument<T> {
    /// Returns the entity type for this document.
    ///
    /// Documents with a `sub_type` of `Task` return `EntityType::Task`,
    /// otherwise they return `EntityType::Document`. Snippets and skills are
    /// documents as far as the entity system is concerned — their snippet-ness
    /// or skill-ness only lives in `sub_type`.
    pub fn entity_type(&self) -> EntityType {
        match &self.sub_type {
            Some(SoupDocumentSubType::Task { .. }) => EntityType::Task,
            Some(
                SoupDocumentSubType::Snippet {}
                | SoupDocumentSubType::Skill {}
                | SoupDocumentSubType::InitiativeDescription {},
            )
            | None => EntityType::Document,
        }
    }
}