Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
cli: Add settle command to show how to settle an account
Browse files Browse the repository at this point in the history
  • Loading branch information
abustany committed Oct 13, 2019
1 parent 6785be4 commit f3228e8
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ enum Command {
account_name: String,
},

#[structopt(name = "settle")]
/// Display the list of transfers to do to settle the debts
Settle {
/// Name of the account
account_name: String,
},

#[structopt(name = "add-remote-account")]
/// Create a new account on a remote server
AddRemoteAccount {
Expand Down Expand Up @@ -459,6 +466,33 @@ fn run() -> Result<()> {

Ok(())
}
Command::Settle { account_name } => {
let store = SledRepository::new(&opt.file)?;
let account = find_account_by_label(&store, &account_name)?;

if account.is_none() {
bail!("No such account in the file");
}

let account = account.unwrap();
let persons: HashMap<&model::PersonId, &str> = account
.members
.iter()
.map(|p| (&p.uuid, p.name.as_str()))
.collect();
let balance = repository::get_balance(&store, &account)?;

for transfer in repository::get_transfers(&balance) {
let debitor = persons.get(&transfer.debitor).unwrap_or(&"??");
let creditor = persons.get(&transfer.creditor).unwrap_or(&"??");
println!(
"{} should transfer {} to {}",
debitor, transfer.amount, creditor
);
}

Ok(())
}
Command::AddRemoteAccount {
remote_address,
label,
Expand Down

0 comments on commit f3228e8

Please sign in to comment.