use anyhow::Result;
use rust_decimal::Decimal;
use sqlx::{PgConnection, PgPool};
use uuid::Uuid;
use crate::domain::entity::FxGainLoss;
pub const TABLE_NAME: &str = "banking.fx_gain_losses";
pub struct FxGainLossRepository(
backbone_orm::GenericCrudRepository<FxGainLoss, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for FxGainLossRepository {
type Target = backbone_orm::GenericCrudRepository<FxGainLoss, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl FxGainLossRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewFxGainLossRow<'a> {
pub id: Uuid,
pub bank_clearance_id: Option<Uuid>,
pub matched_source_id: Uuid,
pub currency: &'a str,
pub original_rate: Decimal,
pub realised_rate: Decimal,
pub base_amount_delta: Decimal,
pub direction: &'a str,
pub fx_account_id: Uuid,
}
impl FxGainLossRepository {
pub async fn insert_gain_loss(
&self,
conn: &mut PgConnection,
r: &NewFxGainLossRow<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO banking.fx_gain_losses
(id, bank_clearance_id, matched_source_id, currency,
original_rate, realised_rate, base_amount_delta, direction, fx_account_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8::fx_direction, $9)"#,
)
.bind(r.id)
.bind(r.bank_clearance_id)
.bind(r.matched_source_id)
.bind(r.currency)
.bind(r.original_rate)
.bind(r.realised_rate)
.bind(r.base_amount_delta)
.bind(r.direction)
.bind(r.fx_account_id)
.execute(conn)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(FxGainLossRepository, FxGainLoss, soft_delete);