Skip to content

Commit

Permalink
chore: integrate chart of accounts into credit facility (#1187)
Browse files Browse the repository at this point in the history
* refactor: add 'ChartOfAccountCreationDetails' type

* refactor: abstract create_control_sub_account function

* chore: add credit facility seed steps

* refactor: move app types to app-primitives modules

* chore: use chart of accounts create in initiate

* fix: fetch chart from in-progress db op
  • Loading branch information
vindard authored Jan 7, 2025
1 parent aba0720 commit 9ef3c70
Show file tree
Hide file tree
Showing 14 changed files with 518 additions and 174 deletions.
64 changes: 35 additions & 29 deletions core/chart-of-accounts/src/chart_of_accounts/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use es_entity::*;

use crate::{
code::*,
primitives::{ChartId, ChartOfAccountAccountDetails, LedgerAccountId},
primitives::{
ChartId, ChartOfAccountAccountDetails, ChartOfAccountCreationDetails, LedgerAccountId,
},
};

pub use super::error::*;
Expand Down Expand Up @@ -195,29 +197,25 @@ impl ChartOfAccount {

pub fn add_transaction_account(
&mut self,
account_id: impl Into<LedgerAccountId>,
control_sub_account: ChartOfAccountCode,
name: &str,
description: &str,
creation_details: ChartOfAccountCreationDetails,
audit_info: AuditInfo,
) -> Result<ChartOfAccountAccountDetails, ChartOfAccountError> {
let account_id = account_id.into();
let path = self.next_transaction_account(control_sub_account)?;
let path = self.next_transaction_account(creation_details.parent_path)?;
self.events
.push(ChartOfAccountEvent::TransactionAccountAdded {
id: account_id,
id: creation_details.account_id,
code: path,
name: name.to_string(),
description: description.to_string(),
name: creation_details.name.clone(),
description: creation_details.description.clone(),
audit_info,
});

Ok(ChartOfAccountAccountDetails {
account_id,
account_id: creation_details.account_id,
code: path.to_code(self.id),
path,
name: name.to_string(),
description: description.to_string(),
name: creation_details.name,
description: creation_details.description,
})
}

Expand Down Expand Up @@ -481,10 +479,12 @@ mod tests {

match chart
.add_transaction_account(
LedgerAccountId::new(),
control_sub_account,
"Cash",
"Cash account",
ChartOfAccountCreationDetails {
account_id: LedgerAccountId::new(),
parent_path: control_sub_account,
name: "Cash".to_string(),
description: "Cash account".to_string(),
},
dummy_audit_info(),
)
.unwrap()
Expand Down Expand Up @@ -603,20 +603,24 @@ mod tests {

chart
.add_transaction_account(
LedgerAccountId::new(),
sub_account,
"First",
"First transaction account",
ChartOfAccountCreationDetails {
account_id: LedgerAccountId::new(),
parent_path: sub_account,
name: "First".to_string(),
description: "First transaction account".to_string(),
},
dummy_audit_info(),
)
.unwrap();

match chart
.add_transaction_account(
LedgerAccountId::new(),
sub_account,
"Second",
"Second transaction account",
ChartOfAccountCreationDetails {
account_id: LedgerAccountId::new(),
parent_path: sub_account,
name: "Second".to_string(),
description: "Second transaction account".to_string(),
},
dummy_audit_info(),
)
.unwrap()
Expand Down Expand Up @@ -664,10 +668,12 @@ mod tests {
.unwrap();
let transaction_account = chart
.add_transaction_account(
LedgerAccountId::new(),
sub_account,
"Cash",
"Cash account",
ChartOfAccountCreationDetails {
account_id: LedgerAccountId::new(),
parent_path: sub_account,
name: "Cash".to_string(),
description: "Cash account".to_string(),
},
audit_info,
)
.unwrap();
Expand Down
8 changes: 8 additions & 0 deletions core/chart-of-accounts/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ pub struct ChartOfAccountAccountDetails {
pub name: String,
pub description: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChartOfAccountCreationDetails {
pub parent_path: ChartOfAccountCode,
pub account_id: LedgerAccountId,
pub name: String,
pub description: String,
}
34 changes: 17 additions & 17 deletions core/chart-of-accounts/src/transaction_account_factory.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use audit::AuditInfo;
use cala_ledger::{account::*, CalaLedger};
use cala_ledger::{account::*, CalaLedger, LedgerOperation};

use crate::{
chart_of_accounts::ChartOfAccountRepo,
code::ChartOfAccountCode,
error::CoreChartOfAccountError,
primitives::{ChartId, ChartOfAccountAccountDetails, LedgerAccountId},
primitives::{
ChartId, ChartOfAccountAccountDetails, ChartOfAccountCreationDetails, LedgerAccountId,
},
};

#[derive(Clone)]
Expand Down Expand Up @@ -33,25 +35,28 @@ impl TransactionAccountFactory {

pub async fn create_transaction_account_in_op(
&self,
mut op: es_entity::DbOp<'_>,
op: &mut LedgerOperation<'_>,
account_id: impl Into<LedgerAccountId>,
name: &str,
description: &str,
audit_info: AuditInfo,
) -> Result<ChartOfAccountAccountDetails, CoreChartOfAccountError> {
let mut chart = self.repo.find_by_id(self.chart_id).await?;
let mut chart = self
.repo
.find_by_id_in_tx(op.op().tx(), self.chart_id)
.await?;

let account_details = chart.add_transaction_account(
account_id,
self.control_sub_account,
name,
description,
ChartOfAccountCreationDetails {
account_id: account_id.into(),
parent_path: self.control_sub_account,
name: name.to_string(),
description: description.to_string(),
},
audit_info,
)?;

self.repo.update_in_op(&mut op, &mut chart).await?;

let mut op = self.cala.ledger_operation_from_db_op(op);
self.repo.update_in_op(op.op(), &mut chart).await?;

let new_account = NewAccount::builder()
.id(account_details.account_id)
Expand All @@ -62,12 +67,7 @@ impl TransactionAccountFactory {
.build()
.expect("Could not build new account");

self.cala
.accounts()
.create_in_op(&mut op, new_account)
.await?;

op.commit().await?;
self.cala.accounts().create_in_op(op, new_account).await?;

Ok(account_details)
}
Expand Down
9 changes: 8 additions & 1 deletion core/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ where
withdrawals: WithdrawalRepo,
approve_withdrawal: ApproveWithdrawal<Perms, E>,
ledger: DepositLedger,
cala: CalaLedger,
account_factory: TransactionAccountFactory,
authz: Perms,
governance: Governance<Perms, E>,
Expand All @@ -64,6 +65,7 @@ where
deposits: self.deposits.clone(),
withdrawals: self.withdrawals.clone(),
ledger: self.ledger.clone(),
cala: self.cala.clone(),
account_factory: self.account_factory.clone(),
authz: self.authz.clone(),
governance: self.governance.clone(),
Expand Down Expand Up @@ -122,6 +124,7 @@ where
authz: authz.clone(),
outbox: outbox.clone(),
governance: governance.clone(),
cala: cala.clone(),
approve_withdrawal,
ledger,
account_factory,
Expand Down Expand Up @@ -159,15 +162,19 @@ where
let mut op = self.accounts.begin_op().await?;
let account = self.accounts.create_in_op(&mut op, new_account).await?;

let mut op = self.cala.ledger_operation_from_db_op(op);
self.account_factory
.create_transaction_account_in_op(
op,
&mut op,
account_id,
&account.name,
&account.description,
audit_info,
)
.await?;

op.commit().await?;

Ok(account)
}

Expand Down
64 changes: 64 additions & 0 deletions lana/app/src/accounting_init/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
pub(super) const LANA_JOURNAL_CODE: &str = "LANA_BANK_JOURNAL";

pub(super) const CHART_REF: &str = "primary-chart";

pub(super) const DEPOSITS_CONTROL_ACCOUNT_REF: &str = "deposits";
pub(super) const DEPOSITS_CONTROL_ACCOUNT_NAME: &str = "Deposits";
pub(super) const DEPOSITS_CONTROL_SUB_ACCOUNT_REF: &str = "deposits-user";
pub(super) const DEPOSITS_CONTROL_SUB_ACCOUNT_NAME: &str = "User Deposits";

pub(super) const CREDIT_FACILITIES_DISBURSED_RECEIVABLE_CONTROL_ACCOUNT_REF: &str =
"credit-facilities-disbursed-receivable";
pub(super) const CREDIT_FACILITIES_DISBURSED_RECEIVABLE_CONTROL_ACCOUNT_NAME: &str =
"Credit Facilities Disbursed Receivable";
pub(super) const CREDIT_FACILITIES_DISBURSED_RECEIVABLE_CONTROL_SUB_ACCOUNT_REF: &str =
"fixed-term-credit-facilities-disbursed-receivable";
pub(super) const CREDIT_FACILITIES_DISBURSED_RECEIVABLE_CONTROL_SUB_ACCOUNT_NAME: &str =
"Fixed Term Credit Facilities Disbursed Receivable"; // Assets

pub(super) const CREDIT_FACILITIES_INTEREST_RECEIVABLE_CONTROL_ACCOUNT_REF: &str =
"credit-facilities-interest-receivable";
pub(super) const CREDIT_FACILITIES_INTEREST_RECEIVABLE_CONTROL_ACCOUNT_NAME: &str =
"Credit Facilities Interest Receivable";
pub(super) const CREDIT_FACILITIES_INTEREST_RECEIVABLE_CONTROL_SUB_ACCOUNT_REF: &str =
"fixed-term-credit-facilities-interest-receivable";
pub(super) const CREDIT_FACILITIES_INTEREST_RECEIVABLE_CONTROL_SUB_ACCOUNT_NAME: &str =
"Fixed Term Credit Facilities Interest Receivable"; // Assets

pub(super) const CREDIT_FACILITIES_INTEREST_INCOME_CONTROL_ACCOUNT_REF: &str =
"credit-facilities-interest-income";
pub(super) const CREDIT_FACILITIES_INTEREST_INCOME_CONTROL_ACCOUNT_NAME: &str =
"Credit Facilities Interest Income";
pub(super) const CREDIT_FACILITIES_INTEREST_INCOME_CONTROL_SUB_ACCOUNT_REF: &str =
"fixed-term-credit-facilities-interest-income";
pub(super) const CREDIT_FACILITIES_INTEREST_INCOME_CONTROL_SUB_ACCOUNT_NAME: &str =
"Fixed Term Credit Facilities Interest Income"; // Revenue

pub(super) const CREDIT_FACILITIES_FEE_INCOME_CONTROL_ACCOUNT_REF: &str =
"credit-facilities-fee-income";
pub(super) const CREDIT_FACILITIES_FEE_INCOME_CONTROL_ACCOUNT_NAME: &str =
"Credit Facilities Fee Income";
pub(super) const CREDIT_FACILITIES_FEE_INCOME_CONTROL_SUB_ACCOUNT_REF: &str =
"fixed-term-credit-facilities-fee-income";
pub(super) const CREDIT_FACILITIES_FEE_INCOME_CONTROL_SUB_ACCOUNT_NAME: &str =
"Fixed Term Credit Facilities Fee Income"; // Revenue

pub(super) const OBS_CHART_REF: &str = "off-balance-sheet-chart";

pub(super) const CREDIT_FACILITIES_COLLATERAL_CONTROL_ACCOUNT_REF: &str =
"credit-facilities-collateral";
pub(super) const CREDIT_FACILITIES_COLLATERAL_CONTROL_ACCOUNT_NAME: &str =
"Credit Facilities Collateral"; // Liabilities
pub(super) const CREDIT_FACILITIES_COLLATERAL_CONTROL_SUB_ACCOUNT_REF: &str =
"fixed-term-credit-facilities-collateral";
pub(super) const CREDIT_FACILITIES_COLLATERAL_CONTROL_SUB_ACCOUNT_NAME: &str =
"Fixed Term Credit Facilities Collateral"; // OBS Liabilities

pub(super) const CREDIT_FACILITIES_FACILITY_CONTROL_ACCOUNT_REF: &str =
"credit-facilities-facility";
pub(super) const CREDIT_FACILITIES_FACILITY_CONTROL_ACCOUNT_NAME: &str =
"Credit Facilities Facility Available";
pub(super) const CREDIT_FACILITIES_FACILITY_CONTROL_SUB_ACCOUNT_REF: &str =
"fixed-term-credit-facilities-facility";
pub(super) const CREDIT_FACILITIES_FACILITY_CONTROL_SUB_ACCOUNT_NAME: &str =
"Fixed Term Credit Facilities Facility Available"; // OBS Assets
8 changes: 5 additions & 3 deletions lana/app/src/accounting_init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod constants;
mod seed;

pub mod error;

use chart_of_accounts::{ChartId, ChartOfAccountCode};

use crate::chart_of_accounts::ChartOfAccounts;
use crate::{app::primitives::*, chart_of_accounts::ChartOfAccounts};

use cala_ledger::{CalaLedger, JournalId};

Expand All @@ -13,8 +14,9 @@ use error::*;
#[derive(Clone)]
pub struct AccountingInit {
pub journal_id: JournalId,
pub chart_id: ChartId,
pub deposits_control_sub_path: ChartOfAccountCode,
pub chart_ids: ChartIds,
pub deposits: DepositsAccountPaths,
pub credit_facilities: CreditFacilitiesAccountPaths,
}

impl AccountingInit {
Expand Down
Loading

0 comments on commit 9ef3c70

Please sign in to comment.