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.

1.6 KB
Raw Blame
use model_notifications::UserUnsubscribe;

/// Gets the combined list of a users unsubscribe items and unsubscribe item events
#[tracing::instrument(skip(db))]
pub async fn get_user_unsubscribes(
    db: &sqlx::Pool<sqlx::Postgres>,
    user_id: &str,
) -> anyhow::Result<Vec<UserUnsubscribe>> {
    let result: Vec<UserUnsubscribe> = sqlx::query!(
        r#"
        SELECT
            'item' as unsubscribe_type,
            unsubscribe_item.item_id  as "item_id!",
            unsubscribe_item.item_type as "item_type!",
            unsubscribe_item.snoozed_until
        FROM
            user_notification_item_unsubscribe unsubscribe_item
        WHERE
            unsubscribe_item.user_id = $1
            AND (snoozed_until IS NULL OR snoozed_until > NOW())
    "#,
        user_id
    )
    .map(|row| UserUnsubscribe {
        item_id: row.item_id,
        item_type: row.item_type,
        snoozed_until: row.snoozed_until,
    })
    .fetch_all(db)
    .await?;

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use macro_db_migrator::MACRO_DB_MIGRATIONS;
    use sqlx::{Pool, Postgres};

    #[sqlx::test(
        migrator = "MACRO_DB_MIGRATIONS",
        fixtures(path = "../../fixtures", scripts("user_unsubscribes"))
    )]
    async fn test_get_user_unsubscribes(pool: Pool<Postgres>) {
        let result = get_user_unsubscribes(&pool, "no-user").await.unwrap();
        assert_eq!(result.len(), 0);

        let result = get_user_unsubscribes(&pool, "macro|[email protected]")
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
    }
}