From 0688e3d2fdd0b372d88d44239a543a8f5946d85c Mon Sep 17 00:00:00 2001 From: vindard <17693119+vindard@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:29:26 -0400 Subject: [PATCH] refactor: split ChartPath enum into structs --- .../src/chart_of_accounts/entity.rs | 202 +++---- core/chart-of-accounts/src/lib.rs | 22 +- core/chart-of-accounts/src/path/mod.rs | 504 +++++++----------- core/chart-of-accounts/src/primitives.rs | 7 +- .../src/transaction_account_factory.rs | 8 +- lana/app/src/accounting_init/mod.rs | 2 +- lana/app/src/accounting_init/primitives.rs | 16 +- lana/app/src/accounting_init/seed.rs | 2 +- 8 files changed, 295 insertions(+), 468 deletions(-) diff --git a/core/chart-of-accounts/src/chart_of_accounts/entity.rs b/core/chart-of-accounts/src/chart_of_accounts/entity.rs index 5d57d87d0..2a55a50c0 100644 --- a/core/chart-of-accounts/src/chart_of_accounts/entity.rs +++ b/core/chart-of-accounts/src/chart_of_accounts/entity.rs @@ -23,14 +23,14 @@ pub enum ChartEvent { }, ControlAccountAdded { encoded_path: String, - path: ChartPath, + path: ControlAccountPath, name: String, reference: String, audit_info: AuditInfo, }, ControlSubAccountAdded { encoded_path: String, - path: ChartPath, + path: ControlSubAccountPath, name: String, reference: String, audit_info: AuditInfo, @@ -38,7 +38,7 @@ pub enum ChartEvent { TransactionAccountAdded { id: LedgerAccountId, encoded_path: String, - path: ChartPath, + path: TransactionAccountPath, name: String, description: String, audit_info: AuditInfo, @@ -54,24 +54,27 @@ pub struct Chart { } impl Chart { - fn next_control_account(&self, category: ChartCategory) -> Result { + fn next_control_account( + &self, + category: ChartCategory, + ) -> Result { Ok(self .events .iter_all() .rev() .find_map(|event| match event { - ChartEvent::ControlAccountAdded { path, .. } if path.category() == category => { + ChartEvent::ControlAccountAdded { path, .. } if path.category == category => { Some(path.next()) } _ => None, }) - .unwrap_or_else(|| Ok(ChartPath::first_control_account(category)))?) + .unwrap_or_else(|| Ok(category.first_control_account()))?) } pub fn find_control_account_by_reference( &self, reference_to_check: String, - ) -> Option { + ) -> Option { self.events.iter_all().rev().find_map(|event| match event { ChartEvent::ControlAccountAdded { path, reference, .. @@ -86,7 +89,7 @@ impl Chart { name: String, reference: String, audit_info: AuditInfo, - ) -> Result { + ) -> Result { if self .find_control_account_by_reference(reference.to_string()) .is_some() @@ -108,28 +111,28 @@ impl Chart { fn next_control_sub_account( &self, - control_account: ChartPath, - ) -> Result { + control_account: ControlAccountPath, + ) -> Result { Ok(self .events .iter_all() .rev() .find_map(|event| match event { ChartEvent::ControlSubAccountAdded { path, .. } - if path.category() == control_account.category() - && path.control_account() == control_account.control_account() => + if path.category == control_account.category + && path.control_account() == control_account => { Some(path.next()) } _ => None, }) - .unwrap_or_else(|| ChartPath::first_control_sub_account(&control_account))?) + .unwrap_or(Ok(control_account.first_control_sub_account()))?) } pub fn find_control_sub_account_by_reference( &self, reference_to_check: String, - ) -> Option { + ) -> Option { self.events.iter_all().rev().find_map(|event| match event { ChartEvent::ControlSubAccountAdded { path, reference, .. @@ -140,11 +143,11 @@ impl Chart { pub fn create_control_sub_account( &mut self, - control_account: ChartPath, + control_account: ControlAccountPath, name: String, reference: String, audit_info: AuditInfo, - ) -> Result { + ) -> Result { if self .find_control_sub_account_by_reference(reference.to_string()) .is_some() @@ -166,24 +169,23 @@ impl Chart { fn next_transaction_account( &self, - control_sub_account: ChartPath, - ) -> Result { + control_sub_account: ControlSubAccountPath, + ) -> Result { Ok(self .events .iter_all() .rev() .find_map(|event| match event { ChartEvent::TransactionAccountAdded { path, .. } - if path.category() == control_sub_account.category() + if path.category == control_sub_account.category && path.control_account() == control_sub_account.control_account() - && path.control_sub_account() - == control_sub_account.control_sub_account() => + && path.control_sub_account() == control_sub_account => { Some(path.next()) } _ => None, }) - .unwrap_or_else(|| ChartPath::first_transaction_account(&control_sub_account))?) + .unwrap_or(Ok(control_sub_account.first_transaction_account()))?) } pub fn add_transaction_account( @@ -191,7 +193,7 @@ impl Chart { creation_details: ChartCreationDetails, audit_info: AuditInfo, ) -> Result { - let path = self.next_transaction_account(creation_details.parent_path)?; + let path = self.next_transaction_account(creation_details.control_sub_account)?; self.events.push(ChartEvent::TransactionAccountAdded { id: creation_details.account_id, encoded_path: path.path_encode(self.id), @@ -330,21 +332,16 @@ mod tests { #[test] fn test_create_control_account() { let mut chart = init_chart_of_events(); - match chart + let ControlAccountPath { category, index } = chart .create_control_account( ChartCategory::Assets, "Assets".to_string(), "assets".to_string(), dummy_audit_info(), ) - .unwrap() - { - ChartPath::ControlAccount { category, index } => { - assert_eq!(category, ChartCategory::Assets); - assert_eq!(index, AccountIdx::FIRST); - } - other => panic!("Expected FIRST control account, got {:?}", other), - } + .unwrap(); + assert_eq!(category, ChartCategory::Assets); + assert_eq!(index, AccountIdx::FIRST); } #[test] @@ -386,26 +383,21 @@ mod tests { ) .unwrap(); - match chart + let ControlSubAccountPath { + category, + control_index, + index, + } = chart .create_control_sub_account( control_account, "Current Assets".to_string(), "current-assets".to_string(), dummy_audit_info(), ) - .unwrap() - { - ChartPath::ControlSubAccount { - category, - control_index, - index, - } => { - assert_eq!(category, ChartCategory::Assets); - assert_eq!(control_index, AccountIdx::FIRST); - assert_eq!(index, AccountIdx::FIRST); - } - other => panic!("Expected FIRST control sub account, got {:?}", other), - } + .unwrap(); + assert_eq!(category, ChartCategory::Assets); + assert_eq!(control_index, AccountIdx::FIRST); + assert_eq!(index, AccountIdx::FIRST); } #[test] @@ -466,35 +458,30 @@ mod tests { ) .unwrap(); - match chart + let ChartAccountDetails { + path: + TransactionAccountPath { + category, + control_index, + control_sub_index, + index, + }, + .. + } = chart .add_transaction_account( ChartCreationDetails { account_id: LedgerAccountId::new(), - parent_path: control_sub_account, + control_sub_account, name: "Cash".to_string(), description: "Cash account".to_string(), }, dummy_audit_info(), ) - .unwrap() - { - ChartAccountDetails { - path: - ChartPath::TransactionAccount { - category, - control_index, - control_sub_index, - index, - }, - .. - } => { - assert_eq!(category, ChartCategory::Assets); - assert_eq!(control_index, AccountIdx::FIRST); - assert_eq!(control_sub_index, AccountIdx::FIRST); - assert_eq!(index, AccountIdx::FIRST); - } - other => panic!("Expected FIRST transaction account, got {:?}", other), - } + .unwrap(); + assert_eq!(category, ChartCategory::Assets); + assert_eq!(control_index, AccountIdx::FIRST); + assert_eq!(control_sub_index, AccountIdx::FIRST); + assert_eq!(index, AccountIdx::FIRST); } #[test] @@ -510,21 +497,16 @@ mod tests { ) .unwrap(); - match chart + let ControlAccountPath { category, index } = chart .create_control_account( ChartCategory::Assets, "Second".to_string(), "assets-02".to_string(), dummy_audit_info(), ) - .unwrap() - { - ChartPath::ControlAccount { category, index } => { - assert_eq!(category, ChartCategory::Assets); - assert_eq!(index, AccountIdx::FIRST.next()); - } - other => panic!("Expected SECOND control account, got {:?}", other), - } + .unwrap(); + assert_eq!(category, ChartCategory::Assets); + assert_eq!(index, AccountIdx::FIRST.next()); } #[test] @@ -548,26 +530,21 @@ mod tests { ) .unwrap(); - match chart + let ControlSubAccountPath { + category, + control_index, + index, + } = chart .create_control_sub_account( control_account, "Second".to_string(), "second-asset".to_string(), dummy_audit_info(), ) - .unwrap() - { - ChartPath::ControlSubAccount { - category, - control_index, - index, - } => { - assert_eq!(category, ChartCategory::Assets); - assert_eq!(control_index, AccountIdx::FIRST); - assert_eq!(index, AccountIdx::FIRST.next()); - } - other => panic!("Expected SECOND control sub account, got {:?}", other), - } + .unwrap(); + assert_eq!(category, ChartCategory::Assets); + assert_eq!(control_index, AccountIdx::FIRST); + assert_eq!(index, AccountIdx::FIRST.next()); } #[test] @@ -581,7 +558,7 @@ mod tests { dummy_audit_info(), ) .unwrap(); - let sub_account = chart + let control_sub_account = chart .create_control_sub_account( control_account, "Current Assets".to_string(), @@ -594,7 +571,7 @@ mod tests { .add_transaction_account( ChartCreationDetails { account_id: LedgerAccountId::new(), - parent_path: sub_account, + control_sub_account, name: "First".to_string(), description: "First transaction account".to_string(), }, @@ -602,35 +579,30 @@ mod tests { ) .unwrap(); - match chart + let ChartAccountDetails { + path: + TransactionAccountPath { + category, + control_index, + control_sub_index, + index, + }, + .. + } = chart .add_transaction_account( ChartCreationDetails { account_id: LedgerAccountId::new(), - parent_path: sub_account, + control_sub_account, name: "Second".to_string(), description: "Second transaction account".to_string(), }, dummy_audit_info(), ) - .unwrap() - { - ChartAccountDetails { - path: - ChartPath::TransactionAccount { - category, - control_index, - control_sub_index, - index, - }, - .. - } => { - assert_eq!(category, ChartCategory::Assets); - assert_eq!(control_index, AccountIdx::FIRST); - assert_eq!(control_sub_index, AccountIdx::FIRST); - assert_eq!(index, AccountIdx::FIRST.next()); - } - other => panic!("Expected SECOND transaction account, got {:?}", other), - } + .unwrap(); + assert_eq!(category, ChartCategory::Assets); + assert_eq!(control_index, AccountIdx::FIRST); + assert_eq!(control_sub_index, AccountIdx::FIRST); + assert_eq!(index, AccountIdx::FIRST.next()); } #[test] @@ -647,7 +619,7 @@ mod tests { audit_info.clone(), ) .unwrap(); - let sub_account = chart + let control_sub_account = chart .create_control_sub_account( control_account, "Current Assets".to_string(), @@ -659,7 +631,7 @@ mod tests { .add_transaction_account( ChartCreationDetails { account_id: LedgerAccountId::new(), - parent_path: sub_account, + control_sub_account, name: "Cash".to_string(), description: "Cash account".to_string(), }, diff --git a/core/chart-of-accounts/src/lib.rs b/core/chart-of-accounts/src/lib.rs index ba1619cea..966c8ac25 100644 --- a/core/chart-of-accounts/src/lib.rs +++ b/core/chart-of-accounts/src/lib.rs @@ -15,6 +15,8 @@ use authz::PermissionCheck; use chart_of_accounts::*; use error::*; +use path::ControlAccountPath; +pub use path::ControlSubAccountPath; pub use primitives::*; pub use transaction_account_factory::*; @@ -63,7 +65,7 @@ where pub fn transaction_account_factory( &self, chart_id: ChartId, - control_sub_account: ChartPath, + control_sub_account: ControlSubAccountPath, ) -> TransactionAccountFactory { TransactionAccountFactory::new(&self.repo, &self.cala, chart_id, control_sub_account) } @@ -152,7 +154,7 @@ where &self, chart_id: impl Into, reference: String, - ) -> Result, CoreChartOfAccountsError> { + ) -> Result, CoreChartOfAccountsError> { let chart_id = chart_id.into(); let mut op = self.repo.begin_op().await?; @@ -177,7 +179,7 @@ where category: ChartCategory, name: String, reference: String, - ) -> Result { + ) -> Result { let chart_id = chart_id.into(); let mut op = self.repo.begin_op().await?; @@ -194,20 +196,20 @@ where let mut chart = self.repo.find_by_id(chart_id).await?; - let code = chart.create_control_account(category, name, reference, audit_info)?; + let path = chart.create_control_account(category, name, reference, audit_info)?; self.repo.update_in_op(&mut op, &mut chart).await?; op.commit().await?; - Ok(code) + Ok(path) } pub async fn find_control_sub_account_by_reference( &self, chart_id: impl Into, reference: String, - ) -> Result, CoreChartOfAccountsError> { + ) -> Result, CoreChartOfAccountsError> { let chart_id = chart_id.into(); let mut op = self.repo.begin_op().await?; @@ -229,10 +231,10 @@ where pub async fn create_control_sub_account( &self, chart_id: impl Into + std::fmt::Debug, - control_account: ChartPath, + control_account: ControlAccountPath, name: String, reference: String, - ) -> Result { + ) -> Result { let chart_id = chart_id.into(); let mut op = self.repo.begin_op().await?; @@ -249,7 +251,7 @@ where let mut chart = self.repo.find_by_id(chart_id).await?; - let code = + let path = chart.create_control_sub_account(control_account, name, reference, audit_info)?; let mut op = self.repo.begin_op().await?; @@ -257,7 +259,7 @@ where op.commit().await?; - Ok(code) + Ok(path) } #[instrument(name = "chart_of_accounts.find_account_in_chart", skip(self))] diff --git a/core/chart-of-accounts/src/path/mod.rs b/core/chart-of-accounts/src/path/mod.rs index 8c5581a13..15ad8c411 100644 --- a/core/chart-of-accounts/src/path/mod.rs +++ b/core/chart-of-accounts/src/path/mod.rs @@ -57,75 +57,43 @@ impl ChartCategory { Self::Expenses => AccountIdx::from(5), } } + + pub const fn first_control_account(&self) -> ControlAccountPath { + ControlAccountPath { + category: *self, + index: AccountIdx::FIRST, + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] -pub enum ChartPath { - ControlAccount { - category: ChartCategory, - index: AccountIdx, - }, - ControlSubAccount { - category: ChartCategory, - control_index: AccountIdx, - index: AccountIdx, - }, - TransactionAccount { - category: ChartCategory, - control_index: AccountIdx, - control_sub_index: AccountIdx, - index: AccountIdx, - }, +pub struct ControlAccountPath { + pub category: ChartCategory, + pub index: AccountIdx, } -impl std::fmt::Display for ChartPath { +impl std::fmt::Display for ControlAccountPath { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::ControlAccount { category, index } => { - write!( - f, - "{:0 { - write!( - f, - "{:0 { - write!( - f, - "{:0 DebitOrCredit { - match self.category() { - ChartCategory::Assets | ChartCategory::Expenses => DebitOrCredit::Debit, - _ => DebitOrCredit::Credit, +impl ControlAccountPath { + pub fn next(&self) -> Result { + let next_index = self.index.next(); + if next_index > AccountIdx::MAX_TWO_DIGIT { + Err(ChartPathError::ControlIndexOverflowForCategory( + self.category, + )) + } else { + Ok(Self { + category: self.category, + index: next_index, + }) } } @@ -133,151 +101,143 @@ impl ChartPath { format!("{}::{}", chart_id, self) } - pub fn category(&self) -> ChartCategory { - match *self { - Self::ControlAccount { category, .. } => category, - Self::ControlSubAccount { category, .. } => category, - Self::TransactionAccount { category, .. } => category, + pub const fn first_control_sub_account(&self) -> ControlSubAccountPath { + ControlSubAccountPath { + category: self.category, + control_index: self.index, + index: AccountIdx::FIRST, } } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct ControlSubAccountPath { + pub category: ChartCategory, + pub control_index: AccountIdx, + pub index: AccountIdx, +} + +impl std::fmt::Display for ControlSubAccountPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{:0 ChartPath { - match *self { - Self::ControlAccount { category, index } => Self::ControlAccount { category, index }, - Self::ControlSubAccount { - category, - control_index, - .. - } => Self::ControlAccount { - category, - index: control_index, - }, - Self::TransactionAccount { - category, - control_index, - .. - } => Self::ControlAccount { - category, - index: control_index, - }, +impl ControlSubAccountPath { + pub fn next(&self) -> Result { + let next_index = self.index.next(); + if next_index > AccountIdx::MAX_TWO_DIGIT { + Err(ChartPathError::ControlSubIndexOverflowForControlAccount( + self.category, + self.control_index, + )) + } else { + Ok(Self { + category: self.category, + control_index: self.control_index, + index: next_index, + }) } } - pub fn control_sub_account(&self) -> Option { - match *self { - Self::TransactionAccount { - category, - control_index, - control_sub_index, - .. - } => Some(Self::ControlSubAccount { - category, - control_index, - index: control_sub_index, - }), - Self::ControlSubAccount { - category, - control_index, - index, - } => Some(Self::ControlSubAccount { - category, - control_index, - index, - }), - _ => None, + pub fn path_encode(&self, chart_id: ChartId) -> String { + format!("{}::{}", chart_id, self) + } + + pub fn control_account(&self) -> ControlAccountPath { + ControlAccountPath { + category: self.category, + index: self.control_index, } } - pub const fn first_control_account(category: ChartCategory) -> Self { - Self::ControlAccount { - category, + pub fn first_transaction_account(&self) -> TransactionAccountPath { + TransactionAccountPath { + category: self.category, + control_index: self.control_index, + control_sub_index: self.index, index: AccountIdx::FIRST, } } +} - pub fn first_control_sub_account(control_account: &Self) -> Result { - match control_account { - Self::ControlAccount { category, index } => Ok(Self::ControlSubAccount { - category: *category, - control_index: *index, - index: AccountIdx::FIRST, - }), - _ => Err(ChartPathError::InvalidControlAccountPathForNewControlSubAccount), +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct TransactionAccountPath { + pub category: ChartCategory, + pub control_index: AccountIdx, + pub control_sub_index: AccountIdx, + pub index: AccountIdx, +} + +impl std::fmt::Display for TransactionAccountPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{:0 Result { + let next_index = self.index.next(); + if next_index > AccountIdx::MAX_THREE_DIGIT { + Err( + ChartPathError::TransactionIndexOverflowForControlSubAccount( + self.category, + self.control_index, + self.control_sub_index, + ), + ) + } else { + Ok(Self { + category: self.category, + control_index: self.control_index, + control_sub_index: self.control_sub_index, + index: next_index, + }) } } - pub fn first_transaction_account(control_sub_account: &Self) -> Result { - match control_sub_account { - Self::ControlSubAccount { - category, - control_index, - index, - } => Ok(Self::TransactionAccount { - category: *category, - control_index: *control_index, - control_sub_index: *index, - index: AccountIdx::FIRST, - }), - _ => Err(ChartPathError::InvalidSubControlAccountPathForNewTransactionAccount), + pub fn path_encode(&self, chart_id: ChartId) -> String { + format!("{}::{}", chart_id, self) + } + + pub fn normal_balance_type(&self) -> DebitOrCredit { + match self.category { + ChartCategory::Assets | ChartCategory::Expenses => DebitOrCredit::Debit, + _ => DebitOrCredit::Credit, } } - pub fn next(&self) -> Result { - match *self { - Self::ControlAccount { category, index } => { - let next_index = index.next(); - if next_index > AccountIdx::MAX_TWO_DIGIT { - Err(ChartPathError::ControlIndexOverflowForCategory(category)) - } else { - Ok(Self::ControlAccount { - category, - index: next_index, - }) - } - } - Self::ControlSubAccount { - category, - control_index, - index, - } => { - let next_index = index.next(); - if next_index > AccountIdx::MAX_TWO_DIGIT { - Err(ChartPathError::ControlSubIndexOverflowForControlAccount( - category, - control_index, - )) - } else { - Ok(Self::ControlSubAccount { - category, - control_index, - index: next_index, - }) - } - } - Self::TransactionAccount { - category, - control_index, - control_sub_index, - index, - } => { - let next_index = index.next(); - if next_index > AccountIdx::MAX_THREE_DIGIT { - Err( - ChartPathError::TransactionIndexOverflowForControlSubAccount( - category, - control_index, - control_sub_index, - ), - ) - } else { - Ok(Self::TransactionAccount { - category, - control_index, - control_sub_index, - index: next_index, - }) - } - } + pub fn control_account(&self) -> ControlAccountPath { + ControlAccountPath { + category: self.category, + index: self.control_index, + } + } + + pub fn control_sub_account(&self) -> ControlSubAccountPath { + ControlSubAccountPath { + category: self.category, + control_index: self.control_index, + index: self.control_index, } } } @@ -291,96 +251,38 @@ mod tests { #[test] fn test_category_formatting() { - let code = ChartCategory::Assets; - assert_eq!(code.to_string(), "10000000"); + let path = ChartCategory::Assets; + assert_eq!(path.to_string(), "10000000"); } #[test] fn test_control_account_formatting() { - let code = ChartPath::ControlAccount { + let path = ControlAccountPath { category: ChartCategory::Liabilities, index: 1.into(), }; - assert_eq!(code.to_string(), "20100000"); + assert_eq!(path.to_string(), "20100000"); } #[test] fn test_control_sub_account_formatting() { - let code = ChartPath::ControlSubAccount { + let path = ControlSubAccountPath { category: ChartCategory::Equity, control_index: 1.into(), index: 2.into(), }; - assert_eq!(code.to_string(), "30102000"); + assert_eq!(path.to_string(), "30102000"); } #[test] fn test_transaction_account_formatting() { - let code = ChartPath::TransactionAccount { + let path = TransactionAccountPath { category: ChartCategory::Revenues, control_index: 1.into(), control_sub_index: 2.into(), index: 3.into(), }; - assert_eq!(code.to_string(), "40102003"); - } - } - - mod category_extraction_tests { - use super::*; - - #[test] - fn test_category_from_control_account() { - for category in [ - ChartCategory::Assets, - ChartCategory::Liabilities, - ChartCategory::Equity, - ChartCategory::Revenues, - ChartCategory::Expenses, - ] { - let code = ChartPath::ControlAccount { - category, - index: 1.into(), - }; - assert_eq!(code.category(), category); - } - } - - #[test] - fn test_category_from_control_sub_account() { - for category in [ - ChartCategory::Assets, - ChartCategory::Liabilities, - ChartCategory::Equity, - ChartCategory::Revenues, - ChartCategory::Expenses, - ] { - let code = ChartPath::ControlSubAccount { - category, - control_index: 1.into(), - index: 2.into(), - }; - assert_eq!(code.category(), category); - } - } - - #[test] - fn test_category_from_transaction_account() { - for category in [ - ChartCategory::Assets, - ChartCategory::Liabilities, - ChartCategory::Equity, - ChartCategory::Revenues, - ChartCategory::Expenses, - ] { - let code = ChartPath::TransactionAccount { - category, - control_index: 1.into(), - control_sub_index: 2.into(), - index: 3.into(), - }; - assert_eq!(code.category(), category); - } + assert_eq!(path.to_string(), "40102003"); } } @@ -389,14 +291,14 @@ mod tests { const CATEGORY: ChartCategory = ChartCategory::Assets; const CONTROL_INDEX: AccountIdx = AccountIdx::FIRST; - const EXPECTED: ChartPath = ChartPath::ControlAccount { + const EXPECTED: ControlAccountPath = ControlAccountPath { category: CATEGORY, index: CONTROL_INDEX, }; #[test] fn test_control_account_from_transaction_account() { - let transaction = ChartPath::TransactionAccount { + let transaction = TransactionAccountPath { category: CATEGORY, control_index: CONTROL_INDEX, control_sub_index: 2.into(), @@ -408,7 +310,7 @@ mod tests { #[test] fn test_control_account_from_control_sub_account() { - let sub_account = ChartPath::ControlSubAccount { + let sub_account = ControlSubAccountPath { category: CATEGORY, control_index: CONTROL_INDEX, index: 2.into(), @@ -416,16 +318,6 @@ mod tests { assert_eq!(sub_account.control_account(), EXPECTED); } - - #[test] - fn test_control_account_from_control_account() { - let control_account = ChartPath::ControlAccount { - category: CATEGORY, - index: CONTROL_INDEX, - }; - - assert_eq!(control_account.control_account(), EXPECTED); - } } mod control_sub_account_extraction_tests { @@ -434,7 +326,7 @@ mod tests { const CATEGORY: ChartCategory = ChartCategory::Assets; const CONTROL_INDEX: AccountIdx = AccountIdx::FIRST; const SUB_INDEX: AccountIdx = AccountIdx::FIRST; - const EXPECTED: ChartPath = ChartPath::ControlSubAccount { + const EXPECTED: ControlSubAccountPath = ControlSubAccountPath { category: CATEGORY, control_index: CONTROL_INDEX, index: SUB_INDEX, @@ -442,35 +334,14 @@ mod tests { #[test] fn test_control_sub_account_from_transaction_account() { - let transaction = ChartPath::TransactionAccount { + let transaction = TransactionAccountPath { category: CATEGORY, control_index: CONTROL_INDEX, control_sub_index: SUB_INDEX, index: 3.into(), }; - assert_eq!(transaction.control_sub_account(), Some(EXPECTED)); - } - - #[test] - fn test_control_sub_account_from_control_sub_account() { - let sub_account = ChartPath::ControlSubAccount { - category: CATEGORY, - control_index: CONTROL_INDEX, - index: SUB_INDEX, - }; - - assert_eq!(sub_account.control_sub_account(), Some(EXPECTED)); - } - - #[test] - fn test_control_sub_account_from_control_account_returns_none() { - let control_account = ChartPath::ControlAccount { - category: CATEGORY, - index: CONTROL_INDEX, - }; - - assert_eq!(control_account.control_sub_account(), None); + assert_eq!(transaction.control_sub_account(), EXPECTED); } } @@ -480,11 +351,11 @@ mod tests { #[test] fn test_first_control_account_creation() { let category = ChartCategory::Assets; - let control = ChartPath::first_control_account(category); + let control = category.first_control_account(); assert_eq!( control, - ChartPath::ControlAccount { + ControlAccountPath { category: ChartCategory::Assets, index: AccountIdx::FIRST, } @@ -493,15 +364,15 @@ mod tests { #[test] fn test_first_control_sub_account_creation() { - let control = ChartPath::ControlAccount { + let control_account = ControlAccountPath { category: ChartCategory::Assets, index: AccountIdx::FIRST, }; - let sub = ChartPath::first_control_sub_account(&control).unwrap(); + let sub = control_account.first_control_sub_account(); assert_eq!( sub, - ChartPath::ControlSubAccount { + ControlSubAccountPath { category: ChartCategory::Assets, control_index: AccountIdx::FIRST, index: AccountIdx::FIRST, @@ -509,28 +380,18 @@ mod tests { ); } - #[test] - fn test_first_control_sub_account_invalid_input() { - let invalid_input = ChartPath::ControlSubAccount { - category: ChartCategory::Assets, - control_index: 1.into(), - index: 1.into(), - }; - assert!(ChartPath::first_control_sub_account(&invalid_input).is_err()); - } - #[test] fn test_first_transaction_account_creation() { - let sub = ChartPath::ControlSubAccount { + let sub_account = ControlSubAccountPath { category: ChartCategory::Assets, control_index: AccountIdx::FIRST, index: AccountIdx::FIRST, }; - let transaction = ChartPath::first_transaction_account(&sub).unwrap(); + let transaction = sub_account.first_transaction_account(); assert_eq!( transaction, - ChartPath::TransactionAccount { + TransactionAccountPath { category: ChartCategory::Assets, control_index: AccountIdx::FIRST, control_sub_index: AccountIdx::FIRST, @@ -538,15 +399,6 @@ mod tests { } ); } - - #[test] - fn test_first_transaction_account_invalid_input() { - let invalid_input = ChartPath::ControlAccount { - category: ChartCategory::Assets, - index: 1.into(), - }; - assert!(ChartPath::first_transaction_account(&invalid_input).is_err()); - } } mod next_account_create { @@ -554,15 +406,15 @@ mod tests { #[test] fn test_next_control_account_success() { - let control = ChartPath::ControlAccount { + let control_account = ControlAccountPath { category: ChartCategory::Assets, index: 1.into(), }; - let next_control = control.next().unwrap(); + let next_control = control_account.next().unwrap(); assert_eq!( next_control, - ChartPath::ControlAccount { + ControlAccountPath { category: ChartCategory::Assets, index: 2.into(), } @@ -571,7 +423,7 @@ mod tests { #[test] fn test_next_control_account_overflow() { - let max_control = ChartPath::ControlAccount { + let max_control = ControlAccountPath { category: ChartCategory::Assets, index: AccountIdx::MAX_TWO_DIGIT, }; @@ -580,7 +432,7 @@ mod tests { #[test] fn test_next_control_sub_account_success() { - let sub = ChartPath::ControlSubAccount { + let sub = ControlSubAccountPath { category: ChartCategory::Assets, control_index: 1.into(), index: 1.into(), @@ -589,7 +441,7 @@ mod tests { let next_sub = sub.next().unwrap(); assert_eq!( next_sub, - ChartPath::ControlSubAccount { + ControlSubAccountPath { category: ChartCategory::Assets, control_index: 1.into(), index: 2.into(), @@ -599,7 +451,7 @@ mod tests { #[test] fn test_next_control_sub_account_overflow() { - let max_sub = ChartPath::ControlSubAccount { + let max_sub = ControlSubAccountPath { category: ChartCategory::Assets, control_index: 1.into(), index: AccountIdx::MAX_TWO_DIGIT, @@ -609,7 +461,7 @@ mod tests { #[test] fn test_next_transaction_account_success() { - let transaction = ChartPath::TransactionAccount { + let transaction = TransactionAccountPath { category: ChartCategory::Assets, control_index: 1.into(), control_sub_index: 1.into(), @@ -619,7 +471,7 @@ mod tests { let next_transaction = transaction.next().unwrap(); assert_eq!( next_transaction, - ChartPath::TransactionAccount { + TransactionAccountPath { category: ChartCategory::Assets, control_index: 1.into(), control_sub_index: 1.into(), @@ -630,7 +482,7 @@ mod tests { #[test] fn test_next_transaction_account_overflow() { - let max_transaction = ChartPath::TransactionAccount { + let max_transaction = TransactionAccountPath { category: ChartCategory::Assets, control_index: 1.into(), control_sub_index: 1.into(), diff --git a/core/chart-of-accounts/src/primitives.rs b/core/chart-of-accounts/src/primitives.rs index 85d663016..f4ec825c7 100644 --- a/core/chart-of-accounts/src/primitives.rs +++ b/core/chart-of-accounts/src/primitives.rs @@ -5,7 +5,8 @@ use serde::{Deserialize, Serialize}; pub use cala_ledger::{primitives::AccountId as LedgerAccountId, DebitOrCredit}; -pub use crate::path::{ChartCategory, ChartPath}; +pub use crate::path::ChartCategory; +use crate::path::{ControlSubAccountPath, TransactionAccountPath}; es_entity::entity_id! { ChartId, @@ -123,7 +124,7 @@ impl From for CoreChartOfAccountsAction { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ChartAccountDetails { pub account_id: LedgerAccountId, - pub path: ChartPath, + pub path: TransactionAccountPath, pub encoded_path: String, pub name: String, pub description: String, @@ -131,7 +132,7 @@ pub struct ChartAccountDetails { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ChartCreationDetails { - pub parent_path: ChartPath, + pub control_sub_account: ControlSubAccountPath, pub account_id: LedgerAccountId, pub name: String, pub description: String, diff --git a/core/chart-of-accounts/src/transaction_account_factory.rs b/core/chart-of-accounts/src/transaction_account_factory.rs index ee5be5557..726a5b2ed 100644 --- a/core/chart-of-accounts/src/transaction_account_factory.rs +++ b/core/chart-of-accounts/src/transaction_account_factory.rs @@ -4,7 +4,7 @@ use cala_ledger::{account::*, CalaLedger, LedgerOperation}; use crate::{ chart_of_accounts::ChartRepo, error::CoreChartOfAccountsError, - path::ChartPath, + path::ControlSubAccountPath, primitives::{ChartAccountDetails, ChartCreationDetails, ChartId, LedgerAccountId}, }; @@ -13,7 +13,7 @@ pub struct TransactionAccountFactory { repo: ChartRepo, cala: CalaLedger, chart_id: ChartId, - control_sub_account: ChartPath, + control_sub_account: ControlSubAccountPath, } impl TransactionAccountFactory { @@ -21,7 +21,7 @@ impl TransactionAccountFactory { repo: &ChartRepo, cala: &CalaLedger, chart_id: ChartId, - control_sub_account: ChartPath, + control_sub_account: ControlSubAccountPath, ) -> Self { Self { repo: repo.clone(), @@ -47,7 +47,7 @@ impl TransactionAccountFactory { let account_details = chart.add_transaction_account( ChartCreationDetails { account_id: account_id.into(), - parent_path: self.control_sub_account, + control_sub_account: self.control_sub_account, name: name.to_string(), description: description.to_string(), }, diff --git a/lana/app/src/accounting_init/mod.rs b/lana/app/src/accounting_init/mod.rs index 750792402..7498a82a3 100644 --- a/lana/app/src/accounting_init/mod.rs +++ b/lana/app/src/accounting_init/mod.rs @@ -4,7 +4,7 @@ mod seed; pub mod error; -use chart_of_accounts::{ChartId, ChartPath}; +use chart_of_accounts::{ChartId, ControlSubAccountPath}; use crate::chart_of_accounts::ChartOfAccounts; diff --git a/lana/app/src/accounting_init/primitives.rs b/lana/app/src/accounting_init/primitives.rs index 55602d631..75c3db996 100644 --- a/lana/app/src/accounting_init/primitives.rs +++ b/lana/app/src/accounting_init/primitives.rs @@ -1,4 +1,4 @@ -use chart_of_accounts::{ChartId, ChartPath}; +use chart_of_accounts::{ChartId, ControlSubAccountPath}; #[derive(Clone, Copy)] pub struct ChartIds { @@ -8,15 +8,15 @@ pub struct ChartIds { #[derive(Clone)] pub struct DepositsAccountPaths { - pub deposits: ChartPath, + pub deposits: ControlSubAccountPath, } #[derive(Clone)] pub struct CreditFacilitiesAccountPaths { - pub collateral: ChartPath, - pub facility: ChartPath, - pub disbursed_receivable: ChartPath, - pub interest_receivable: ChartPath, - pub interest_income: ChartPath, - pub fee_income: ChartPath, + pub collateral: ControlSubAccountPath, + pub facility: ControlSubAccountPath, + pub disbursed_receivable: ControlSubAccountPath, + pub interest_receivable: ControlSubAccountPath, + pub interest_income: ControlSubAccountPath, + pub fee_income: ControlSubAccountPath, } diff --git a/lana/app/src/accounting_init/seed.rs b/lana/app/src/accounting_init/seed.rs index 66c41dc9e..aa5b20621 100644 --- a/lana/app/src/accounting_init/seed.rs +++ b/lana/app/src/accounting_init/seed.rs @@ -87,7 +87,7 @@ async fn create_control_sub_account( control_reference: String, sub_name: String, sub_reference: String, -) -> Result { +) -> Result { let control_path = match chart_of_accounts .find_control_account_by_reference(chart_id, control_reference.clone()) .await?