Skip to content

Commit

Permalink
Remove duplicate account filters
Browse files Browse the repository at this point in the history
  • Loading branch information
DewaldV committed Apr 14, 2024
1 parent ef9ad63 commit 7500324
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
19 changes: 14 additions & 5 deletions src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,26 @@ fn print_balance_row(account_type: &str, account_no: &str, created: &str, balanc
);
}

pub async fn list(token: &str) -> monzo::Result<()> {
pub async fn get_supported_accounts(token: &str) -> monzo::Result<Vec<(Type, monzo::Account)>> {
let client = Client::new(token);

let accounts = client.accounts().await?;

let supported_accounts = accounts
.iter()
.filter_map(|acc| match Type::try_from(&acc.account_type).ok() {
Some(a) => Some((a, acc)),
None => None,
.filter_map(|acc| match Type::try_from(&acc.account_type) {
Ok(acc_type) => Some((acc_type, acc.clone())),
Err(_) => None,
})
.filter(|a| !a.1.account_number.is_empty());
.filter(|a| !a.1.account_number.is_empty())
.collect();

Ok(supported_accounts)
}

pub async fn list(token: &str) -> monzo::Result<()> {
let client = Client::new(token);
let supported_accounts = get_supported_accounts(token).await?;

print_balance_row("Account Type", "Account No", "Created", "Balance");
println!("-------------------------------------------------------------");
Expand Down
21 changes: 7 additions & 14 deletions src/pots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ fn print_pot_balance_row(account_type: &str, account_no: &str, pot_name: &str, b
pub async fn list(token: &str) -> monzo::Result<()> {
let client = Client::new(token);

let accounts = client.accounts().await?;
let supported_accounts = accounts.iter().filter(|acc| !acc.account_number.is_empty());
let supported_accounts = accounts::get_supported_accounts(token).await?;

print_pot_balance_row("Account Type", "Account Number", "Pot Name", "Balance");
println!("-------------------------------------------------------------------------------");
for account in supported_accounts {
let account_type = accounts::Type::try_from(&account.account_type)
.expect("already filtered for account types that converted successfully");

for (account_type, account) in supported_accounts {
let pots = client.pots(&account.id).await?;

for pot in pots.iter().filter(|p| !p.deleted) {
Expand All @@ -43,8 +39,7 @@ pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> monzo::Result

let client = Client::new(token);

let accounts = client.accounts().await?;
let found_pot = find_pot(token, pot_name, &accounts).await?;
let found_pot = find_pot(token, pot_name).await?;

match found_pot {
Some(pot) => {
Expand All @@ -66,14 +61,12 @@ pub async fn deposit(token: &str, pot_name: &str, amount: &str) -> monzo::Result
Ok(())
}

async fn find_pot(
token: &str,
name: &str,
accounts: &Vec<monzo::Account>,
) -> monzo::Result<Option<monzo::Pot>> {
async fn find_pot(token: &str, name: &str) -> monzo::Result<Option<monzo::Pot>> {
let client = Client::new(token);

for account in accounts.iter().filter(|acc| !acc.account_number.is_empty()) {
let supported_accounts = accounts::get_supported_accounts(token).await?;

for (_, account) in supported_accounts {
let pots = client.pots(&account.id).await?;
let found_pot = pots
.iter()
Expand Down

0 comments on commit 7500324

Please sign in to comment.