Attempt to properly identify wise accounts with wrong primary currency
authorMagnus Hagander <magnus@hagander.net>
Wed, 20 Mar 2024 16:39:48 +0000 (17:39 +0100)
committerMagnus Hagander <magnus@hagander.net>
Wed, 20 Mar 2024 16:39:48 +0000 (17:39 +0100)
Our check only worked properly if the primary currency was the one we
used, and it seems wise can open a new balance and just chance the
primary currency on us, so update the check.

postgresqleu/transferwise/api.py

index 545d3f693537528259f13554f0bc0736c9bc5d77..fa81ee2213570bb3e9639e78d650a98b175251c9 100644 (file)
@@ -103,10 +103,20 @@ class TransferwiseApi(object):
 
     def get_account(self):
         if not self.account:
-            try:
-                self.account = next((a for a in self.get('borderless-accounts', {'profileId': self.get_profile()}) if a['balances'][0]['currency'] == settings.CURRENCY_ABBREV))
-            except Exception as e:
-                raise Exception("Failed to get account: {}".format(e))
+            for a in self.get('borderless-accounts', {'profileId': self.get_profile()}):
+                # Each account has multiple currencies, so we look for the first one that
+                # has our currency somewhere.
+                for b in a['balances']:
+                    if b['currency'] == settings.CURRENCY_ABBREV:
+                        self.account = a
+                        break
+
+                if self.account:
+                    # If we found our currency on this account, use it
+                    break
+
+            if not self.account:
+                raise Exception("Failed to identify account based on currency")
         return self.account
 
     def get_balance(self):