Skip to content

Commit

Permalink
refactor: add children to chart category projections
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 16, 2025
1 parent 4e2a6e4 commit 5289e12
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
81 changes: 55 additions & 26 deletions core/chart-of-accounts/src/chart_of_accounts/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use crate::{path::*, ChartId};

use super::ChartEvent;

pub struct CategoryProjection {
pub name: String,
pub encoded_path: String,
pub children: Vec<ControlAccountProjection>,
}

struct ControlAccountAdded {
name: String,
path: ControlAccountPath,
Expand All @@ -23,11 +29,11 @@ pub struct ControlSubAccountProjection {
pub struct ChartOfAccountsProjection {
pub id: ChartId,
pub name: String,
pub assets: Vec<ControlAccountProjection>,
pub liabilities: Vec<ControlAccountProjection>,
pub equity: Vec<ControlAccountProjection>,
pub revenues: Vec<ControlAccountProjection>,
pub expenses: Vec<ControlAccountProjection>,
pub assets: CategoryProjection,
pub liabilities: CategoryProjection,
pub equity: CategoryProjection,
pub revenues: CategoryProjection,
pub expenses: CategoryProjection,
}

pub(super) fn project<'a>(
Expand Down Expand Up @@ -83,21 +89,41 @@ pub(super) fn project<'a>(
ChartOfAccountsProjection {
id: id.expect("Chart must be initialized"),
name: name.expect("Chart must be initialized"),
assets: control_accounts_by_category
.remove(&ChartCategory::Assets)
.unwrap_or_default(),
liabilities: control_accounts_by_category
.remove(&ChartCategory::Liabilities)
.unwrap_or_default(),
equity: control_accounts_by_category
.remove(&ChartCategory::Equity)
.unwrap_or_default(),
revenues: control_accounts_by_category
.remove(&ChartCategory::Revenues)
.unwrap_or_default(),
expenses: control_accounts_by_category
.remove(&ChartCategory::Expenses)
.unwrap_or_default(),
assets: CategoryProjection {
name: "Assets".to_string(),
encoded_path: ChartCategory::Assets.to_string(),
children: control_accounts_by_category
.remove(&ChartCategory::Assets)
.unwrap_or_default(),
},
liabilities: CategoryProjection {
name: "Liabilities".to_string(),
encoded_path: ChartCategory::Liabilities.to_string(),
children: control_accounts_by_category
.remove(&ChartCategory::Liabilities)
.unwrap_or_default(),
},
equity: CategoryProjection {
name: "Equity".to_string(),
encoded_path: ChartCategory::Equity.to_string(),
children: control_accounts_by_category
.remove(&ChartCategory::Equity)
.unwrap_or_default(),
},
revenues: CategoryProjection {
name: "Revenues".to_string(),
encoded_path: ChartCategory::Revenues.to_string(),
children: control_accounts_by_category
.remove(&ChartCategory::Revenues)
.unwrap_or_default(),
},
expenses: CategoryProjection {
name: "Expenses".to_string(),
encoded_path: ChartCategory::Expenses.to_string(),
children: control_accounts_by_category
.remove(&ChartCategory::Expenses)
.unwrap_or_default(),
},
}
}

Expand Down Expand Up @@ -157,7 +183,7 @@ mod tests {
.unwrap();
}
assert_eq!(
chart.chart().assets[0].children[0].encoded_path,
chart.chart().assets.children[0].children[0].encoded_path,
"10101000".to_string()
);

Expand All @@ -180,7 +206,7 @@ mod tests {
.unwrap();
}
assert_eq!(
chart.chart().liabilities[0].children[0].encoded_path,
chart.chart().liabilities.children[0].children[0].encoded_path,
"20101000".to_string()
);

Expand All @@ -202,7 +228,10 @@ mod tests {
)
.unwrap();
}
assert_eq!(chart.chart().equity[0].children[0].encoded_path, "30101000");
assert_eq!(
chart.chart().equity.children[0].children[0].encoded_path,
"30101000"
);

{
chart
Expand All @@ -214,9 +243,9 @@ mod tests {
)
.unwrap();
}
assert_eq!(chart.chart().revenues[0].encoded_path, "40100000");
assert_eq!(chart.chart().revenues[0].children.len(), 0);
assert_eq!(chart.chart().revenues.children[0].encoded_path, "40100000");
assert_eq!(chart.chart().revenues.children[0].children.len(), 0);

assert_eq!(chart.chart().expenses.len(), 0);
assert_eq!(chart.chart().expenses.children.len(), 0);
}
}
2 changes: 1 addition & 1 deletion core/chart-of-accounts/src/chart_of_accounts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod chart;
pub mod chart;
mod entity;
pub mod error;
mod repo;
Expand Down
1 change: 1 addition & 0 deletions core/chart-of-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tracing::instrument;
use audit::AuditSvc;
use authz::PermissionCheck;

pub use chart_of_accounts::chart;
use chart_of_accounts::*;
use error::*;
use path::ControlAccountPath;
Expand Down

0 comments on commit 5289e12

Please sign in to comment.