Skip to content

Commit

Permalink
feat: add 'create_trial_balance_statement' use-case
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 21, 2025
1 parent d717779 commit 653049b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
8 changes: 8 additions & 0 deletions core/statements/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ use thiserror::Error;
pub enum CoreStatementsError {
#[error("CoreStatementsError - Sqlx: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("CoreStatementsError - AuditError: {0}")]
AuditError(#[from] audit::error::AuditError),
#[error("CoreStatementsError - TrialBalanceStatementError: {0}")]
TrialBalanceStatementError(#[from] crate::trial_balance::error::TrialBalanceStatementError),
#[error("CoreStatementsError - TrialBalanceStatementLedgerError: {0}")]
TrialBalanceStatementLedgerError(
#[from] crate::trial_balance::ledger::error::TrialBalanceStatementLedgerError,
),
}
55 changes: 52 additions & 3 deletions core/statements/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use authz::PermissionCheck;
pub use auth::*;
use error::*;
pub use primitives::*;
use trial_balance::*;

pub struct CoreStatements<Perms>
where
Perms: PermissionCheck,
{
cala: CalaLedger,
trial_balance_repo: TrialBalanceStatementRepo,
trial_balance_ledger: TrialBalanceStatementLedger,
authz: Perms,
journal_id: LedgerJournalId,
}
Expand All @@ -30,7 +32,8 @@ where
{
fn clone(&self) -> Self {
Self {
cala: self.cala.clone(),
trial_balance_repo: self.trial_balance_repo.clone(),
trial_balance_ledger: self.trial_balance_ledger.clone(),
authz: self.authz.clone(),
journal_id: self.journal_id,
}
Expand All @@ -44,15 +47,61 @@ where
<<Perms as PermissionCheck>::Audit as AuditSvc>::Object: From<CoreStatementsObject>,
{
pub async fn init(
pool: &sqlx::PgPool,
authz: &Perms,
cala: &CalaLedger,
journal_id: LedgerJournalId,
) -> Result<Self, CoreStatementsError> {
let trial_balance_repo = TrialBalanceStatementRepo::new(pool);
let trial_balance_ledger = TrialBalanceStatementLedger::new(cala, journal_id);

let res = Self {
cala: cala.clone(),
trial_balance_repo,
trial_balance_ledger,
authz: authz.clone(),
journal_id,
};
Ok(res)
}

pub async fn create_trial_balance_statement(
&self,
id: impl Into<TrialBalanceStatementId>,
name: String,
reference: String,
) -> Result<(), CoreStatementsError> {
let id = id.into();
let statement_id: StatementId = id.into();
let account_set_id: LedgerAccountSetId = id.into();

let mut op = self.trial_balance_repo.begin_op().await?;

let audit_info = self
.authz
.audit()
.record_system_entry_in_tx(
op.tx(),
CoreStatementsObject::statement(statement_id),
CoreStatementsAction::STATEMENT_CREATE,
)
.await?;

let new_trial_balance = NewTrialBalanceStatement::builder()
.id(id)
.name(name.to_string())
.reference(reference)
.audit_info(audit_info)
.build()
.expect("Could not build new chart of accounts");

self.trial_balance_repo
.create_in_op(&mut op, new_trial_balance)
.await?;

self.trial_balance_ledger
.create(op, account_set_id, &name)
.await?;

Ok(())
}
}
1 change: 1 addition & 0 deletions core/statements/src/trial_balance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod ledger;
mod repo;

pub(super) use entity::*;
pub use ledger::*;
pub(super) use repo::*;

0 comments on commit 653049b

Please sign in to comment.