Skip to content

Commit

Permalink
refactor: add credit factories wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 21, 2025
1 parent 2b00ed1 commit 99955fb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 67 deletions.
3 changes: 2 additions & 1 deletion lana/app/src/accounting_init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::chart_of_accounts::ChartOfAccounts;
use cala_ledger::CalaLedger;

use error::*;
use primitives::*;
pub use primitives::CreditFacilitiesAccountPaths;
use primitives::{ChartIds, DepositsAccountPaths, LedgerJournalId};

#[derive(Clone)]
pub struct JournalInit {
Expand Down
23 changes: 4 additions & 19 deletions lana/app/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
audit::{Audit, AuditCursor, AuditEntry},
authorization::{init as init_authz, AppAction, AppObject, AuditAction, Authorization},
chart_of_accounts::ChartOfAccounts,
credit_facility::CreditFacilities,
credit_facility::{CreditFacilities, CreditFacilityAccountFactories},
customer::Customers,
dashboard::Dashboard,
deposit::Deposits,
Expand Down Expand Up @@ -96,18 +96,8 @@ impl LanaApp {
let customers = Customers::new(&pool, &config.customer, &deposits, &authz);
let applicants = Applicants::new(&pool, &config.sumsub, &customers, &jobs);

let collateral_factory =
chart_of_accounts.transaction_account_factory(charts_init.credit_facilities.collateral);
let facility_factory =
chart_of_accounts.transaction_account_factory(charts_init.credit_facilities.facility);
let disbursed_receivable_factory = chart_of_accounts
.transaction_account_factory(charts_init.credit_facilities.disbursed_receivable);
let interest_receivable_factory = chart_of_accounts
.transaction_account_factory(charts_init.credit_facilities.interest_receivable);
let interest_income_factory = chart_of_accounts
.transaction_account_factory(charts_init.credit_facilities.interest_income);
let fee_income_factory =
chart_of_accounts.transaction_account_factory(charts_init.credit_facilities.fee_income);
let credit_account_factories =
CreditFacilityAccountFactories::new(&chart_of_accounts, charts_init.credit_facilities);
let credit_facilities = CreditFacilities::init(
&pool,
config.credit_facility,
Expand All @@ -117,12 +107,7 @@ impl LanaApp {
&deposits,
&price,
&outbox,
collateral_factory,
facility_factory,
disbursed_receivable_factory,
interest_receivable_factory,
interest_income_factory,
fee_income_factory,
credit_account_factories,
&cala,
journal_init.journal_id,
)
Expand Down
38 changes: 19 additions & 19 deletions lana/app/src/credit_facility/ledger/credit_facility_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use cala_ledger::AccountId as LedgerAccountId;
use chart_of_accounts::TransactionAccountFactory;

use crate::{
accounting_init::CreditFacilitiesAccountPaths,
chart_of_accounts::ChartOfAccounts,
primitives::{LedgerTxId, Satoshis, UsdCents},
terms::InterestPeriod,
};
Expand Down Expand Up @@ -35,34 +37,32 @@ impl CreditFacilityAccountIds {

#[derive(Clone)]
pub struct CreditFacilityAccountFactories {
pub facility_factory: TransactionAccountFactory,
pub disbursed_receivable_factory: TransactionAccountFactory,
pub collateral_factory: TransactionAccountFactory,
pub interest_receivable_factory: TransactionAccountFactory,
pub interest_income_factory: TransactionAccountFactory,
pub fee_income_factory: TransactionAccountFactory,
pub facility: TransactionAccountFactory,
pub disbursed_receivable: TransactionAccountFactory,
pub collateral: TransactionAccountFactory,
pub interest_receivable: TransactionAccountFactory,
pub interest_income: TransactionAccountFactory,
pub fee_income: TransactionAccountFactory,
}

impl CreditFacilityAccountFactories {
pub fn new(
facility_factory: TransactionAccountFactory,
disbursed_receivable_factory: TransactionAccountFactory,
collateral_factory: TransactionAccountFactory,
interest_receivable_factory: TransactionAccountFactory,
interest_income_factory: TransactionAccountFactory,
fee_income_factory: TransactionAccountFactory,
chart_of_accounts: &ChartOfAccounts,
credit_facilities: CreditFacilitiesAccountPaths,
) -> Self {
Self {
facility_factory,
disbursed_receivable_factory,
collateral_factory,
interest_receivable_factory,
interest_income_factory,
fee_income_factory,
facility: chart_of_accounts.transaction_account_factory(credit_facilities.facility),
disbursed_receivable: chart_of_accounts
.transaction_account_factory(credit_facilities.disbursed_receivable),
collateral: chart_of_accounts.transaction_account_factory(credit_facilities.collateral),
interest_receivable: chart_of_accounts
.transaction_account_factory(credit_facilities.interest_receivable),
interest_income: chart_of_accounts
.transaction_account_factory(credit_facilities.interest_income),
fee_income: chart_of_accounts.transaction_account_factory(credit_facilities.fee_income),
}
}
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct CreditFacilityLedgerBalance {
pub facility: UsdCents,
Expand Down
12 changes: 6 additions & 6 deletions lana/app/src/credit_facility/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl CreditLedger {
credit_facility_id
);
self.account_factories
.collateral_factory
.collateral
.create_transaction_account_in_op(
&mut op,
account_ids.collateral_account_id,
Expand All @@ -552,7 +552,7 @@ impl CreditLedger {
credit_facility_id
);
self.account_factories
.facility_factory
.facility
.create_transaction_account_in_op(
&mut op,
account_ids.facility_account_id,
Expand All @@ -566,7 +566,7 @@ impl CreditLedger {
credit_facility_id
);
self.account_factories
.disbursed_receivable_factory
.disbursed_receivable
.create_transaction_account_in_op(
&mut op,
account_ids.disbursed_receivable_account_id,
Expand All @@ -580,7 +580,7 @@ impl CreditLedger {
credit_facility_id
);
self.account_factories
.interest_receivable_factory
.interest_receivable
.create_transaction_account_in_op(
&mut op,
account_ids.interest_receivable_account_id,
Expand All @@ -594,7 +594,7 @@ impl CreditLedger {
credit_facility_id
);
self.account_factories
.interest_income_factory
.interest_income
.create_transaction_account_in_op(
&mut op,
account_ids.interest_account_id,
Expand All @@ -608,7 +608,7 @@ impl CreditLedger {
credit_facility_id
);
self.account_factories
.fee_income_factory
.fee_income
.create_transaction_account_in_op(
&mut op,
account_ids.fee_income_account_id,
Expand Down
25 changes: 3 additions & 22 deletions lana/app/src/credit_facility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ mod repo;

use std::collections::HashMap;

use chart_of_accounts::TransactionAccountFactory;

use authz::PermissionCheck;
use cala_ledger::CalaLedger;
use deposit::{DepositAccount, DepositAccountHolderId};
Expand Down Expand Up @@ -41,7 +39,7 @@ use error::*;
pub use history::*;
pub use interest_accrual::*;
use jobs::*;
use ledger::*;
pub use ledger::*;
use processes::activate_credit_facility::*;
pub use processes::approve_credit_facility::*;
pub use processes::approve_disbursal::*;
Expand Down Expand Up @@ -78,31 +76,14 @@ impl CreditFacilities {
deposits: &Deposits,
price: &Price,
outbox: &Outbox,
collateral_factory: TransactionAccountFactory,
facility_factory: TransactionAccountFactory,
disbursed_receivable_factory: TransactionAccountFactory,
interest_receivable_factory: TransactionAccountFactory,
interest_income_factory: TransactionAccountFactory,
fee_income_factory: TransactionAccountFactory,
account_factories: CreditFacilityAccountFactories,
cala: &CalaLedger,
journal_id: cala_ledger::JournalId,
) -> Result<Self, CreditFacilityError> {
let publisher = CreditFacilityPublisher::new(outbox);
let credit_facility_repo = CreditFacilityRepo::new(pool, &publisher);
let disbursal_repo = DisbursalRepo::new(pool);
let ledger = CreditLedger::init(
cala,
journal_id,
CreditFacilityAccountFactories::new(
facility_factory,
disbursed_receivable_factory,
collateral_factory,
interest_receivable_factory,
interest_income_factory,
fee_income_factory,
),
)
.await?;
let ledger = CreditLedger::init(cala, journal_id, account_factories).await?;
let approve_disbursal = ApproveDisbursal::new(
&disbursal_repo,
&credit_facility_repo,
Expand Down

0 comments on commit 99955fb

Please sign in to comment.