1
1
use chrono:: { Duration , Utc } ;
2
2
use monzo:: Client ;
3
3
4
- use crate :: accounts;
5
4
use crate :: currency:: Amount ;
6
- use crate :: Result ;
5
+ use crate :: { accounts, transactions} ;
6
+ use crate :: { Error , Result } ;
7
7
8
8
fn print_pot_balance_row ( account_type : & str , account_no : & str , pot_name : & str , balance : & str ) {
9
9
println ! (
@@ -45,16 +45,15 @@ pub async fn deposit(
45
45
let client = Client :: new ( token) ;
46
46
47
47
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
+ } ) ?;
48
51
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
+ ) ;
58
57
59
58
let amount_i: u32 = amount. pence . try_into ( ) ?;
60
59
client
@@ -63,7 +62,7 @@ pub async fn deposit(
63
62
println ! ( "Completed deposit. Name: {}, Amount: {}" , pot. name, amount) ;
64
63
65
64
if let Some ( description) = description {
66
- let since = Utc :: now ( ) - Duration :: minutes ( 5 ) ;
65
+ let since = Utc :: now ( ) - Duration :: minutes ( 2 ) ;
67
66
let limit = 10 ;
68
67
69
68
let transactions = client
@@ -73,10 +72,32 @@ pub async fn deposit(
73
72
. send ( )
74
73
. await ?;
75
74
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 ?;
80
101
81
102
// metadata: {
82
103
// "ledger_committed_timestamp_earliest": "2024-04-26T21:17:37.867Z",
@@ -89,8 +110,6 @@ pub async fn deposit(
89
110
// "ledger_committed_timestamp_latest": "2024-04-26T21:17:37.867Z",
90
111
// "pot_id": "pot_0000AgbkY00Euhtjk7z0td",
91
112
// },
92
- // let tx_list = transactions::list(token, account_type, since, limit).await?;
93
- // transactions::annotate(token, transaction_id, description).await?
94
113
}
95
114
96
115
Ok ( ( ) )
0 commit comments