chore: Implement code review suggestions and format code.

This commit is contained in:
Elisiário Couto
2025-09-03 21:11:19 +01:00
committed by Elisiário Couto
parent 47164e8546
commit de3da84dff
42 changed files with 1144 additions and 966 deletions

View File

@@ -30,7 +30,7 @@ class SyncService:
start_time = datetime.now()
self._sync_status.is_running = True
self._sync_status.errors = []
accounts_processed = 0
transactions_added = 0
transactions_updated = 0
@@ -39,22 +39,24 @@ class SyncService:
try:
logger.info("Starting sync of all accounts")
# Get all requisitions and accounts
requisitions = await self.gocardless.get_requisitions()
all_accounts = set()
for req in requisitions.get("results", []):
all_accounts.update(req.get("accounts", []))
self._sync_status.total_accounts = len(all_accounts)
# Process each account
for account_id in all_accounts:
try:
# Get account details
account_details = await self.gocardless.get_account_details(account_id)
account_details = await self.gocardless.get_account_details(
account_id
)
# Get and save balances
balances = await self.gocardless.get_account_balances(account_id)
if balances:
@@ -62,7 +64,9 @@ class SyncService:
balances_updated += len(balances.get("balances", []))
# Get and save transactions
transactions = await self.gocardless.get_account_transactions(account_id)
transactions = await self.gocardless.get_account_transactions(
account_id
)
if transactions:
processed_transactions = self.database.process_transactions(
account_id, account_details, transactions
@@ -71,16 +75,18 @@ class SyncService:
account_id, processed_transactions
)
transactions_added += len(new_transactions)
# Send notifications for new transactions
if new_transactions:
await self.notifications.send_transaction_notifications(new_transactions)
await self.notifications.send_transaction_notifications(
new_transactions
)
accounts_processed += 1
self._sync_status.accounts_synced = accounts_processed
logger.info(f"Synced account {account_id} successfully")
except Exception as e:
error_msg = f"Failed to sync account {account_id}: {str(e)}"
errors.append(error_msg)
@@ -88,9 +94,9 @@ class SyncService:
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
self._sync_status.last_sync = end_time
result = SyncResult(
success=len(errors) == 0,
accounts_processed=accounts_processed,
@@ -100,12 +106,14 @@ class SyncService:
duration_seconds=duration,
errors=errors,
started_at=start_time,
completed_at=end_time
completed_at=end_time,
)
logger.info(
f"Sync completed: {accounts_processed} accounts, {transactions_added} new transactions"
)
logger.info(f"Sync completed: {accounts_processed} accounts, {transactions_added} new transactions")
return result
except Exception as e:
error_msg = f"Sync failed: {str(e)}"
errors.append(error_msg)
@@ -114,7 +122,9 @@ class SyncService:
finally:
self._sync_status.is_running = False
async def sync_specific_accounts(self, account_ids: List[str], force: bool = False) -> SyncResult:
async def sync_specific_accounts(
self, account_ids: List[str], force: bool = False
) -> SyncResult:
"""Sync specific accounts"""
if self._sync_status.is_running and not force:
raise Exception("Sync is already running")
@@ -123,12 +133,12 @@ class SyncService:
# For brevity, implementing a simplified version
start_time = datetime.now()
self._sync_status.is_running = True
try:
# Process only specified accounts
# Implementation would be similar to sync_all_accounts
# but filtered to only the specified account_ids
end_time = datetime.now()
return SyncResult(
success=True,
@@ -139,7 +149,7 @@ class SyncService:
duration_seconds=(end_time - start_time).total_seconds(),
errors=[],
started_at=start_time,
completed_at=end_time
completed_at=end_time,
)
finally:
self._sync_status.is_running = False
self._sync_status.is_running = False