1818class Import2Firefly :
1919 """Class to handle the import workflow."""
2020
21- def __init__ (self ) -> None :
21+ def __init__ (self , truelayer : TrueLayerClient , firefly : FireflyClient ) -> None :
2222 """Initialize the Import class."""
2323 self ._config : Config = Config ()
24- self ._truelayer_client : TrueLayerClient = TrueLayerClient ()
25- self ._firefly_client : FireflyClient = FireflyClient ()
24+ self ._truelayer_client = truelayer
25+ self ._firefly_client = firefly
2626
2727 self .start_time = datetime .now ()
2828 self .end_time = None
@@ -103,7 +103,6 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
103103 total_transactions = len (txns )
104104 for i , txn in enumerate (txns , start = 1 ):
105105 cp_iban = txn .get ("meta" , {}).get ("counter_party_iban" )
106- cp_name = txn .get ("meta" , {}).get ("counter_party_preferred_name" )
107106 transaction_type = (
108107 "debit" if txn ["transaction_type" ].lower () == "debit" else "credit"
109108 )
@@ -122,30 +121,14 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
122121 ):
123122 continue
124123
125- # Check if the IBAN matches
126124 if cp_iban == firefly_account ["attributes" ].get ("iban" ):
127125 yield f"Matching account found via IBAN: { txn ['description' ]} - { cp_iban } "
128126 linked_account = firefly_account
129127 matching += 1
130128 break
131129
132- # Check if the name matches, as a final fallback
133- # This is not prefered, but can be used if the IBAN is not available or when the account uses multiple IBANs
134- # Firefly doesn't allow to create multiple accounts with the same name, so this should be safe
135- if cp_name is not None and cp_name == firefly_account [
136- "attributes"
137- ].get ("name" ):
138- yield f"Matching account found via name: { txn ['description' ]} - { cp_name } "
139- linked_account = firefly_account
140- matching += 1
141- break
142-
143130 if linked_account is None :
144- account_type = (
145- "revenue" if transaction_type == "credit" else "Expense"
146- )
147-
148- yield f"No match, still a valid IBAN. Creating a new account: { txn } - { cp_iban } - { account_type } "
131+ yield f"No match, still a valid IBAN. Creating a new account: { txn } - { cp_iban } "
149132 response = await self ._firefly_client .create_account (
150133 {
151134 "name" : txn .get ("meta" , {}).get (
@@ -160,21 +143,15 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
160143 ),
161144 }
162145 )
163-
164146 if response .status_code != 200 :
165147 yield f"Error creating account in Firefly: { response .text } "
166148 continue
167149 yield f"New account created: { txn .get ('meta' , {}).get ('counter_party_preferred_name' )} - { cp_iban } "
168150 linked_account = response .json ()["data" ]
169151 newly_created += 1
170-
171- yield "Firefly: Enforcing refresh accounts from Firefly"
172- firefly_accounts = (
173- await self ._firefly_client .get_account_paginated ()
174- )
175- yield f"Firefly: A total of { len (firefly_accounts )} account(s) found"
176152 else :
177153 unmatching += 1
154+ # TODO: find the unknown destination account
178155 yield f"Transaction has no IBAN: { txn ['description' ]} "
179156
180157 # Ensure the amount is always positive
@@ -190,8 +167,8 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
190167 "date" : txn ["timestamp" ],
191168 "amount" : amount ,
192169 "type" : (
193- "revenue "
194- if transaction_type == "deposit "
170+ "deposit "
171+ if transaction_type == "credit "
195172 else "withdrawal"
196173 ),
197174 "destination_id" : (
@@ -200,7 +177,7 @@ async def start_import(self) -> AsyncGenerator[Any, Any]:
200177 "destination_name" : (
201178 "(unknown revenue account)"
202179 if linked_account is None
203- and transaction_type == "deposit "
180+ and transaction_type == "credit "
204181 else (
205182 "(unknown expense account)"
206183 if linked_account is None
0 commit comments