Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

6.0 KB
Raw Blame
use common_enums::BatchBlocklistJobType;
use common_utils::id_type;
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};

use super::generics;
use crate::{
    batch_blocklist_job::{BatchBlocklistJob, BatchBlocklistJobNew, BatchBlocklistJobUpdate},
    schema::batch_blocklist_jobs::dsl,
    DatabaseConnectionWithContext, StorageResult,
};

impl BatchBlocklistJobNew {
    pub async fn insert(
        self,
        conn: &DatabaseConnectionWithContext<'_>,
    ) -> StorageResult<BatchBlocklistJob> {
        generics::generic_insert(conn, self).await
    }
}

impl BatchBlocklistJob {
    pub async fn find_by_id_merchant_id(
        conn: &DatabaseConnectionWithContext<'_>,
        id: &str,
        merchant_id: &str,
    ) -> StorageResult<Self> {
        generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
            conn,
            dsl::id
                .eq(id.to_owned())
                .and(dsl::merchant_id.eq(merchant_id.to_owned())),
        )
        .await
    }

    // The listing and the count must be given the same filter, or the page and the total disagree.
    pub async fn list_by_merchant_id(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        limit: i64,
        offset: i64,
    ) -> StorageResult<Vec<Self>> {
        generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
            conn,
            dsl::merchant_id.eq(merchant_id.to_owned()),
            Some(limit),
            Some(offset),
            Some(dsl::created_at.desc()),
        )
        .await
    }

    pub async fn count_by_merchant_id(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
    ) -> StorageResult<usize> {
        generics::generic_count::<<Self as HasTable>::Table, _>(
            conn,
            dsl::merchant_id.eq(merchant_id.to_owned()),
        )
        .await
    }

    pub async fn list_by_merchant_id_job_type(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        job_type: BatchBlocklistJobType,
        limit: i64,
        offset: i64,
    ) -> StorageResult<Vec<Self>> {
        generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
            conn,
            dsl::merchant_id
                .eq(merchant_id.to_owned())
                .and(dsl::job_type.eq(job_type)),
            Some(limit),
            Some(offset),
            Some(dsl::created_at.desc()),
        )
        .await
    }

    pub async fn count_by_merchant_id_job_type(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        job_type: BatchBlocklistJobType,
    ) -> StorageResult<usize> {
        generics::generic_count::<<Self as HasTable>::Table, _>(
            conn,
            dsl::merchant_id
                .eq(merchant_id.to_owned())
                .and(dsl::job_type.eq(job_type)),
        )
        .await
    }

    pub async fn list_by_merchant_id_profile_id(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        profile_id: &id_type::ProfileId,
        limit: i64,
        offset: i64,
    ) -> StorageResult<Vec<Self>> {
        generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
            conn,
            dsl::merchant_id.eq(merchant_id.to_owned()).and(
                dsl::profile_id
                    .eq(profile_id.to_owned())
                    .or(dsl::profile_id.is_null()),
            ),
            Some(limit),
            Some(offset),
            Some(dsl::created_at.desc()),
        )
        .await
    }

    pub async fn count_by_merchant_id_profile_id(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        profile_id: &id_type::ProfileId,
    ) -> StorageResult<usize> {
        generics::generic_count::<<Self as HasTable>::Table, _>(
            conn,
            dsl::merchant_id.eq(merchant_id.to_owned()).and(
                dsl::profile_id
                    .eq(profile_id.to_owned())
                    .or(dsl::profile_id.is_null()),
            ),
        )
        .await
    }

    pub async fn list_by_merchant_id_profile_id_job_type(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        profile_id: &id_type::ProfileId,
        job_type: BatchBlocklistJobType,
        limit: i64,
        offset: i64,
    ) -> StorageResult<Vec<Self>> {
        generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(
            conn,
            dsl::merchant_id
                .eq(merchant_id.to_owned())
                .and(dsl::job_type.eq(job_type))
                .and(
                    dsl::profile_id
                        .eq(profile_id.to_owned())
                        .or(dsl::profile_id.is_null()),
                ),
            Some(limit),
            Some(offset),
            Some(dsl::created_at.desc()),
        )
        .await
    }

    pub async fn count_by_merchant_id_profile_id_job_type(
        conn: &DatabaseConnectionWithContext<'_>,
        merchant_id: &str,
        profile_id: &id_type::ProfileId,
        job_type: BatchBlocklistJobType,
    ) -> StorageResult<usize> {
        generics::generic_count::<<Self as HasTable>::Table, _>(
            conn,
            dsl::merchant_id
                .eq(merchant_id.to_owned())
                .and(dsl::job_type.eq(job_type))
                .and(
                    dsl::profile_id
                        .eq(profile_id.to_owned())
                        .or(dsl::profile_id.is_null()),
                ),
        )
        .await
    }

    pub async fn update_by_id_merchant_id(
        conn: &DatabaseConnectionWithContext<'_>,
        id: &str,
        merchant_id: &str,
        update: BatchBlocklistJobUpdate,
    ) -> StorageResult<Self> {
        generics::generic_update_with_unique_predicate_get_result::<
            <Self as HasTable>::Table,
            _,
            _,
            _,
        >(
            conn,
            dsl::id
                .eq(id.to_owned())
                .and(dsl::merchant_id.eq(merchant_id.to_owned())),
            update,
        )
        .await
    }
}