use anyhow::Result;
use rust_decimal::Decimal;
use sqlx::PgPool;
use uuid::Uuid;
use crate::domain::entity::BankStatementImport;
pub const TABLE_NAME: &str = "banking.bank_statement_imports";
pub struct BankStatementImportRepository(
backbone_orm::GenericCrudRepository<BankStatementImport, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for BankStatementImportRepository {
type Target =
backbone_orm::GenericCrudRepository<BankStatementImport, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl BankStatementImportRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewStatementImportRow<'a> {
pub id: Uuid,
pub bank_account_id: Uuid,
pub source_format: &'a str,
pub statement_period_start: chrono::NaiveDate,
pub statement_period_end: chrono::NaiveDate,
pub opening_balance: Decimal,
pub closing_balance: Decimal,
pub file_ref: Option<&'a str>,
pub row_count: i32,
}
impl BankStatementImportRepository {
pub async fn insert_import(
&self,
conn: &mut sqlx::PgConnection,
i: &NewStatementImportRow<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO banking.bank_statement_imports
(id, bank_account_id, source_format, statement_period_start, statement_period_end,
opening_balance, closing_balance, file_ref, status, row_count)
VALUES ($1,$2,$3::source_format,$4,$5,$6,$7,$8,'imported'::import_status,$9)"#,
)
.bind(i.id).bind(i.bank_account_id).bind(i.source_format)
.bind(i.statement_period_start).bind(i.statement_period_end)
.bind(i.opening_balance).bind(i.closing_balance).bind(i.file_ref).bind(i.row_count)
.execute(conn)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(
BankStatementImportRepository,
BankStatementImport,
soft_delete
);