Skip to content

Commit d7fb8f1

Browse files
committed
feat: basic add note on deposit
1 parent 92b8948 commit d7fb8f1

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pub enum Error {
55
#[error("unsupported account type {account_type:?}")]
66
UnsupportedAccountType { account_type: monzo::accounts::Type },
77

8+
#[error("no pot found with name={pot_name}")]
9+
PotNotFound { pot_name: String },
10+
811
// External
912
#[error("Monzo API error: {0}")]
1013
MonzoAPI(#[from] monzo::Error),

src/pots.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use chrono::{Duration, Utc};
22
use monzo::Client;
33

4-
use crate::accounts;
54
use crate::currency::Amount;
6-
use crate::Result;
5+
use crate::{accounts, transactions};
6+
use crate::{Error, Result};
77

88
fn print_pot_balance_row(account_type: &str, account_no: &str, pot_name: &str, balance: &str) {
99
println!(
@@ -45,16 +45,15 @@ pub async fn deposit(
4545
let client = Client::new(token);
4646

4747
let found_pot = find_pot(token, pot_name).await?;
48+
let pot = found_pot.ok_or(Error::PotNotFound {
49+
pot_name: String::from(pot_name),
50+
})?;
4851

49-
if found_pot.is_none() {
50-
println!("No pot found with name: {}", pot_name);
51-
return Ok(());
52-
}
53-
54-
let pot = found_pot.expect("none checked above so this is safe");
55-
56-
let balance = Amount::from(pot.balance);
57-
println!("Found pot. Name: {}, Balance: {}", pot.name, balance);
52+
println!(
53+
"Found pot. Name: {}, Balance: {}",
54+
pot.name,
55+
Amount::from(pot.balance)
56+
);
5857

5958
let amount_i: u32 = amount.pence.try_into()?;
6059
client
@@ -63,7 +62,7 @@ pub async fn deposit(
6362
println!("Completed deposit. Name: {}, Amount: {}", pot.name, amount);
6463

6564
if let Some(description) = description {
66-
let since = Utc::now() - Duration::minutes(5);
65+
let since = Utc::now() - Duration::minutes(2);
6766
let limit = 10;
6867

6968
let transactions = client
@@ -73,10 +72,32 @@ pub async fn deposit(
7372
.send()
7473
.await?;
7574

76-
// transactions
77-
// .iter()
78-
// .filter(|tx| tx.metadata.contains_key("pot_id"))
79-
// .find(|tx| tx.metadata.get(k));
75+
// Look for the pot deposit.
76+
// If it's a transaction to the target pot with an exact matching amount
77+
// then it should be ours.
78+
let pot_tx: Vec<&monzo::Transaction> = transactions
79+
.iter()
80+
.filter(|tx| {
81+
tx.metadata
82+
.get("pot_id")
83+
.is_some_and(|pot_id| pot_id == &pot.id)
84+
})
85+
.filter(|tx| tx.amount.abs() == amount.pence)
86+
.collect();
87+
88+
if pot_tx.len() != 1 {
89+
println!(
90+
"Unable to find a matching transaction to annotate. len(tx)={}",
91+
pot_tx.len()
92+
);
93+
return Ok(());
94+
}
95+
96+
let tx = pot_tx
97+
.first()
98+
.expect("length is checked above so this is always safe");
99+
100+
transactions::annotate(token, &tx.id, description).await?;
80101

81102
// metadata: {
82103
// "ledger_committed_timestamp_earliest": "2024-04-26T21:17:37.867Z",
@@ -89,8 +110,6 @@ pub async fn deposit(
89110
// "ledger_committed_timestamp_latest": "2024-04-26T21:17:37.867Z",
90111
// "pot_id": "pot_0000AgbkY00Euhtjk7z0td",
91112
// },
92-
// let tx_list = transactions::list(token, account_type, since, limit).await?;
93-
// transactions::annotate(token, transaction_id, description).await?
94113
}
95114

96115
Ok(())

0 commit comments

Comments
 (0)