use anyhow::Result;
use sqlx::PgPool;
use uuid::Uuid;
use backbone_orm::org_scope;
use crate::domain::entity::BankAccount;
pub const TABLE_NAME: &str = "banking.bank_accounts";
pub struct BankAccountRepository(
backbone_orm::GenericCrudRepository<BankAccount, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for BankAccountRepository {
type Target = backbone_orm::GenericCrudRepository<BankAccount, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl BankAccountRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewBankAccountRow<'a> {
pub id: Uuid,
pub branch_id: Option<Uuid>,
pub bank_id: Uuid,
pub account_name: &'a str,
pub account_number: &'a str,
pub gl_account_id: Uuid,
pub clearing_account_id: Uuid,
pub currency: &'a str,
pub account_type: &'a str,
}
impl BankAccountRepository {
pub async fn insert_bank_account(
&self,
pool: &PgPool,
a: &NewBankAccountRow<'_>,
) -> Result<(), sqlx::Error> {
org_scope::execute_scoped(
pool,
sqlx::query(
r#"INSERT INTO banking.bank_accounts
(id, branch_id, bank_id, account_name, account_number, gl_account_id,
clearing_account_id, currency, account_type, is_default, status)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9::bank_account_type,false,'active')"#,
)
.bind(a.id).bind(a.branch_id).bind(a.bank_id).bind(a.account_name)
.bind(a.account_number).bind(a.gl_account_id).bind(a.clearing_account_id)
.bind(a.currency).bind(a.account_type),
)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(BankAccountRepository, BankAccount, soft_delete);