Skip to content

Commit

Permalink
refactor: simplify chart-of-accounts schema types (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard authored Jul 10, 2024
1 parent 825d98a commit 45b5850
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 79 deletions.
29 changes: 13 additions & 16 deletions apps/admin-panel/lib/graphql/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,26 @@ export type ChartOfAccounts = {
name: Scalars['String']['output'];
};

export type ChartOfAccountsCategory = {
__typename?: 'ChartOfAccountsCategory';
accounts: Array<ChartOfAccountsCategoryAccount>;
id: Scalars['UUID']['output'];
name: Scalars['String']['output'];
};

export type ChartOfAccountsCategoryAccount = AccountDetails | AccountSetDetails;

export type ChartOfAccountsCategoryAccountWithSubAccounts = {
__typename?: 'ChartOfAccountsCategoryAccountWithSubAccounts';
export type ChartOfAccountsAccountSet = {
__typename?: 'ChartOfAccountsAccountSet';
id: Scalars['UUID']['output'];
name: Scalars['String']['output'];
subAccounts: Array<ChartOfAccountsCategorySubAccount>;
subAccounts: Array<ChartOfAccountsSubAccount>;
};


export type ChartOfAccountsCategoryAccountWithSubAccountsSubAccountsArgs = {
export type ChartOfAccountsAccountSetSubAccountsArgs = {
after?: InputMaybe<Scalars['String']['input']>;
first: Scalars['Int']['input'];
};

export type ChartOfAccountsCategorySubAccount = AccountDetails | AccountSetDetails;
export type ChartOfAccountsCategory = {
__typename?: 'ChartOfAccountsCategory';
accounts: Array<ChartOfAccountsSubAccount>;
name: Scalars['String']['output'];
};

export type ChartOfAccountsSubAccount = AccountDetails | AccountSetDetails;

export type Checking = {
__typename?: 'Checking';
Expand Down Expand Up @@ -277,15 +274,15 @@ export enum Period {
export type Query = {
__typename?: 'Query';
chartOfAccounts?: Maybe<ChartOfAccounts>;
chartOfAccountsCategoryAccountSet?: Maybe<ChartOfAccountsCategoryAccountWithSubAccounts>;
chartOfAccountsAccountSet?: Maybe<ChartOfAccountsAccountSet>;
loan?: Maybe<Loan>;
trialBalance?: Maybe<AccountSetAndMemberBalances>;
user?: Maybe<User>;
users: UserConnection;
};


export type QueryChartOfAccountsCategoryAccountSetArgs = {
export type QueryChartOfAccountsAccountSetArgs = {
accountSetId: Scalars['UUID']['input'];
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
query ChartOfAccountsCategoryAccountWithSubAccounts($accountSetId: UUID!) {
chartOfAccountsCategoryAccountSet(accountSetId: $accountSetId) {
query ChartOfAccountsAccountSet($accountSetId: UUID!) {
chartOfAccountsAccountSet(accountSetId: $accountSetId) {
name
subAccounts(first: 10) {
__typename
... on AccountDetails {
__typename
id
name
}
... on AccountSetDetails {
__typename
id
name
hasSubAccounts
Expand Down
4 changes: 1 addition & 3 deletions bats/admin-gql/chart-of-accounts.gql
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ query ChartOfAccount {
chartOfAccounts {
name
categories {
id
name
accounts {
__typename
... on AccountSetDetails {
__typename
queryableId: id
name
hasSubAccounts
}
... on AccountDetails {
__typename
id
name
}
Expand Down
4 changes: 2 additions & 2 deletions bats/chart-of-accounts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ teardown_file() {
accountSetId: $account_set_id
}'
)
exec_admin_graphql 'chart-of-accounts-category-account-set' "$variables"
sub_account_name=$(graphql_output '.data.chartOfAccountsCategoryAccountSet.subAccounts[0].name')
exec_admin_graphql 'chart-of-accounts-account-set' "$variables"
sub_account_name=$(graphql_output '.data.chartOfAccountsAccountSet.subAccounts[0].name')
[[ "$sub_account_name" =~ "Bfx" ]] || exit 1
}
2 changes: 1 addition & 1 deletion core/src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Ledger {
.map_err(|e| e.into())
}

pub async fn chart_of_accounts_category_account_set(
pub async fn chart_of_accounts_account_set(
&self,
account_set_id: LedgerAccountSetId,
first: i64,
Expand Down
70 changes: 30 additions & 40 deletions core/src/server/admin/graphql/account_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,83 @@ impl From<crate::ledger::account_set::LedgerChartOfAccountsAccountSet> for Accou
}

#[derive(Union)]
enum ChartOfAccountsCategorySubAccount {
enum ChartOfAccountsSubAccount {
Account(super::account::AccountDetails),
AccountSet(AccountSetDetails),
}

impl From<crate::ledger::account_set::LedgerChartOfAccountsCategorySubAccount>
for ChartOfAccountsCategorySubAccount
for ChartOfAccountsSubAccount
{
fn from(member: crate::ledger::account_set::LedgerChartOfAccountsCategorySubAccount) -> Self {
match member {
crate::ledger::account_set::LedgerChartOfAccountsCategorySubAccount::Account(val) => {
ChartOfAccountsCategorySubAccount::Account(super::account::AccountDetails::from(
val,
))
ChartOfAccountsSubAccount::Account(super::account::AccountDetails::from(val))
}
crate::ledger::account_set::LedgerChartOfAccountsCategorySubAccount::AccountSet(
val,
) => ChartOfAccountsCategorySubAccount::AccountSet(AccountSetDetails::from(val)),
) => ChartOfAccountsSubAccount::AccountSet(AccountSetDetails::from(val)),
}
}
}

impl From<crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount>
for ChartOfAccountsSubAccount
{
fn from(
category_account: crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount,
) -> Self {
match category_account {
crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount::Account(val) => {
ChartOfAccountsSubAccount::Account(val.into())
}
crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount::AccountSet(val) => {
ChartOfAccountsSubAccount::AccountSet(val.into())
}
}
}
}

#[derive(SimpleObject)]
#[graphql(complex)]
pub struct ChartOfAccountsCategoryAccountWithSubAccounts {
pub struct ChartOfAccountsAccountSet {
id: UUID,
name: String,
}

impl From<crate::ledger::account_set::LedgerChartOfAccountsCategoryAccountSet>
for ChartOfAccountsCategoryAccountWithSubAccounts
for ChartOfAccountsAccountSet
{
fn from(
account_set: crate::ledger::account_set::LedgerChartOfAccountsCategoryAccountSet,
) -> Self {
ChartOfAccountsCategoryAccountWithSubAccounts {
ChartOfAccountsAccountSet {
id: account_set.id.into(),
name: account_set.name,
}
}
}

#[ComplexObject]
impl ChartOfAccountsCategoryAccountWithSubAccounts {
impl ChartOfAccountsAccountSet {
async fn sub_accounts(
&self,
ctx: &Context<'_>,
first: i32,
after: Option<String>,
) -> async_graphql::Result<Vec<ChartOfAccountsCategorySubAccount>> {
) -> async_graphql::Result<Vec<ChartOfAccountsSubAccount>> {
let app = ctx.data_unchecked::<LavaApp>();
let account_set = app
.ledger()
.chart_of_accounts_category_account_set(self.id.clone().into(), first.into(), after)
.chart_of_accounts_account_set(self.id.clone().into(), first.into(), after)
.await?;

let sub_accounts = if let Some(account_set) = account_set {
account_set
.sub_accounts
.members
.into_iter()
.map(ChartOfAccountsCategorySubAccount::from)
.map(ChartOfAccountsSubAccount::from)
.collect()
} else {
Vec::new()
Expand All @@ -108,45 +123,20 @@ impl ChartOfAccountsCategoryAccountWithSubAccounts {
}
}

#[derive(Union)]
enum ChartOfAccountsCategoryAccount {
Account(super::account::AccountDetails),
AccountSet(AccountSetDetails),
}

impl From<crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount>
for ChartOfAccountsCategoryAccount
{
fn from(
category_account: crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount,
) -> Self {
match category_account {
crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount::Account(val) => {
ChartOfAccountsCategoryAccount::Account(val.into())
}
crate::ledger::account_set::LedgerChartOfAccountsCategoryAccount::AccountSet(val) => {
ChartOfAccountsCategoryAccount::AccountSet(val.into())
}
}
}
}

#[derive(SimpleObject)]
pub struct ChartOfAccountsCategory {
id: UUID,
name: String,
accounts: Vec<ChartOfAccountsCategoryAccount>,
accounts: Vec<ChartOfAccountsSubAccount>,
}

impl From<crate::ledger::account_set::LedgerChartOfAccountsCategory> for ChartOfAccountsCategory {
fn from(account_set: crate::ledger::account_set::LedgerChartOfAccountsCategory) -> Self {
ChartOfAccountsCategory {
id: account_set.id.into(),
name: account_set.name,
accounts: account_set
.category_accounts
.into_iter()
.map(ChartOfAccountsCategoryAccount::from)
.map(ChartOfAccountsSubAccount::from)
.collect(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/server/admin/graphql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ impl Query {
Ok(chart_of_accounts.map(ChartOfAccounts::from))
}

async fn chart_of_accounts_category_account_set(
async fn chart_of_accounts_account_set(
&self,
ctx: &Context<'_>,
account_set_id: UUID,
) -> async_graphql::Result<Option<ChartOfAccountsCategoryAccountWithSubAccounts>> {
) -> async_graphql::Result<Option<ChartOfAccountsAccountSet>> {
let app = ctx.data_unchecked::<LavaApp>();
let chart_of_accounts = app
.ledger()
.chart_of_accounts_category_account_set(account_set_id.into(), 0, None)
.chart_of_accounts_account_set(account_set_id.into(), 0, None)
.await?;
Ok(chart_of_accounts.map(ChartOfAccountsCategoryAccountWithSubAccounts::from))
Ok(chart_of_accounts.map(ChartOfAccountsAccountSet::from))
}
}

Expand Down
15 changes: 6 additions & 9 deletions core/src/server/admin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,18 @@ type ChartOfAccounts {
categories: [ChartOfAccountsCategory!]!
}

type ChartOfAccountsCategory {
type ChartOfAccountsAccountSet {
id: UUID!
name: String!
accounts: [ChartOfAccountsCategoryAccount!]!
subAccounts(first: Int!, after: String): [ChartOfAccountsSubAccount!]!
}

union ChartOfAccountsCategoryAccount = AccountDetails | AccountSetDetails

type ChartOfAccountsCategoryAccountWithSubAccounts {
id: UUID!
type ChartOfAccountsCategory {
name: String!
subAccounts(first: Int!, after: String): [ChartOfAccountsCategorySubAccount!]!
accounts: [ChartOfAccountsSubAccount!]!
}

union ChartOfAccountsCategorySubAccount = AccountDetails | AccountSetDetails
union ChartOfAccountsSubAccount = AccountDetails | AccountSetDetails

type Checking {
settled: UsdBalance!
Expand Down Expand Up @@ -213,7 +210,7 @@ type Query {
users(first: Int!, after: String): UserConnection!
trialBalance: AccountSetAndMemberBalances
chartOfAccounts: ChartOfAccounts
chartOfAccountsCategoryAccountSet(accountSetId: UUID!): ChartOfAccountsCategoryAccountWithSubAccounts
chartOfAccountsAccountSet(accountSetId: UUID!): ChartOfAccountsAccountSet
}

scalar Satoshis
Expand Down

0 comments on commit 45b5850

Please sign in to comment.