mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Fix: Transaction Sync Issues & Enhanced Debugging (#632)
* Fix mobile app to fetch all transactions with pagination
The mobile app was only fetching 25 transactions per account because:
1. TransactionsService didn't pass pagination parameters to the API
2. The backend defaults to 25 records per page when no per_page is specified
3. SyncService didn't implement pagination to fetch all pages
Changes:
- Updated TransactionsService.getTransactions() to accept page and perPage parameters
- Modified the method to extract and return pagination metadata from API response
- Updated SyncService.syncFromServer() to fetch all pages (up to 100 per page)
- Added pagination loop to continue fetching until all pages are retrieved
- Enhanced logging to show pagination progress
This ensures users see all their transactions in the mobile app, not just the first 25.
* Add clear local data feature and enhanced sync logging
Added features:
1. Clear Local Data button in Settings
- Allows users to clear all cached transactions and accounts
- Shows confirmation dialog before clearing
- Displays success/error feedback
2. Enhanced sync logging for debugging
- Added detailed logs in syncFromServer to track pagination
- Shows page-by-page progress with transaction counts
- Logs pagination metadata (total pages, total count, etc.)
- Tracks upsert progress every 50 transactions
- Added clear section markers for easier log reading
3. Simplified upsertTransactionFromServer logging
- Removed verbose debug logs to reduce noise
- Keeps only essential error/warning logs
This will help users troubleshoot sync issues by:
- Clearing stale data and forcing a fresh sync
- Providing detailed logs to identify where sync might fail
* Fix transaction accountId parsing from API response
The mobile app was only showing 25 transactions per account because:
- The backend API returns account info in nested format: {"account": {"id": "xxx"}}
- The mobile Transaction model expected flat format: {"account_id": "xxx"}
- When parsing, accountId was always empty, so database queries by account_id returned incomplete results
Changes:
1. Updated Transaction.fromJson to handle both formats:
- New format: {"account": {"id": "xxx", "name": "..."}}
- Old format: {"account_id": "xxx"} (for backward compatibility)
2. Fixed classification/nature field parsing:
- Backend sends "classification" field (income/expense)
- Mobile uses "nature" field
- Now handles both fields correctly
3. Added debug logging to identify empty accountId issues:
- Logs first transaction's accountId when syncing
- Counts and warns about transactions with empty accountId
- Shows critical errors when trying to save with empty accountId
This ensures all transactions from the server are correctly associated with their accounts in the local database.
---------
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -217,49 +217,100 @@ class SyncService with ChangeNotifier {
|
||||
String? accountId,
|
||||
}) async {
|
||||
try {
|
||||
_log.debug('SyncService', 'Fetching transactions from server (accountId: $accountId)');
|
||||
final result = await _transactionsService.getTransactions(
|
||||
accessToken: accessToken,
|
||||
accountId: accountId,
|
||||
);
|
||||
_log.info('SyncService', '========== SYNC FROM SERVER START ==========');
|
||||
_log.info('SyncService', 'Fetching transactions from server (accountId: ${accountId ?? "ALL"})');
|
||||
|
||||
if (result['success'] == true) {
|
||||
final transactions = (result['transactions'] as List<dynamic>?)
|
||||
?.cast<Transaction>() ?? [];
|
||||
List<Transaction> allTransactions = [];
|
||||
int currentPage = 1;
|
||||
int totalPages = 1;
|
||||
const int perPage = 100; // Use maximum allowed by backend
|
||||
|
||||
_log.info('SyncService', 'Received ${transactions.length} transactions from server');
|
||||
// Fetch all pages
|
||||
while (currentPage <= totalPages) {
|
||||
_log.info('SyncService', '>>> Fetching page $currentPage of $totalPages (perPage: $perPage)');
|
||||
|
||||
// Update local cache with server data
|
||||
if (accountId == null) {
|
||||
_log.debug('SyncService', 'Full sync - clearing and replacing all transactions');
|
||||
// Full sync - replace all transactions
|
||||
await _offlineStorage.syncTransactionsFromServer(transactions);
|
||||
final result = await _transactionsService.getTransactions(
|
||||
accessToken: accessToken,
|
||||
accountId: accountId,
|
||||
page: currentPage,
|
||||
perPage: perPage,
|
||||
);
|
||||
|
||||
_log.debug('SyncService', 'API call completed for page $currentPage, success: ${result['success']}');
|
||||
|
||||
if (result['success'] == true) {
|
||||
final pageTransactions = (result['transactions'] as List<dynamic>?)
|
||||
?.cast<Transaction>() ?? [];
|
||||
|
||||
_log.info('SyncService', 'Page $currentPage returned ${pageTransactions.length} transactions');
|
||||
allTransactions.addAll(pageTransactions);
|
||||
_log.info('SyncService', 'Total transactions accumulated: ${allTransactions.length}');
|
||||
|
||||
// Extract pagination info if available
|
||||
final pagination = result['pagination'] as Map<String, dynamic>?;
|
||||
if (pagination != null) {
|
||||
final prevTotalPages = totalPages;
|
||||
totalPages = pagination['total_pages'] as int? ?? 1;
|
||||
final totalCount = pagination['total_count'] as int? ?? 0;
|
||||
final currentPageFromApi = pagination['page'] as int? ?? currentPage;
|
||||
final perPageFromApi = pagination['per_page'] as int? ?? perPage;
|
||||
|
||||
_log.info('SyncService', 'Pagination info: page=$currentPageFromApi/$totalPages, per_page=$perPageFromApi, total_count=$totalCount');
|
||||
|
||||
if (prevTotalPages != totalPages) {
|
||||
_log.info('SyncService', 'Total pages updated from $prevTotalPages to $totalPages');
|
||||
}
|
||||
} else {
|
||||
// No pagination info means this is the only page
|
||||
_log.warning('SyncService', 'No pagination info in response - assuming single page');
|
||||
totalPages = currentPage;
|
||||
}
|
||||
|
||||
_log.info('SyncService', 'Moving to next page (current: $currentPage, total: $totalPages)');
|
||||
currentPage++;
|
||||
} else {
|
||||
_log.debug('SyncService', 'Partial sync - upserting ${transactions.length} transactions for account $accountId');
|
||||
// Partial sync - upsert transactions
|
||||
for (final transaction in transactions) {
|
||||
_log.debug('SyncService', 'Upserting transaction ${transaction.id} (accountId from server: "${transaction.accountId}", provided: "$accountId")');
|
||||
await _offlineStorage.upsertTransactionFromServer(
|
||||
transaction,
|
||||
accountId: accountId,
|
||||
);
|
||||
_log.error('SyncService', 'Server returned error on page $currentPage: ${result['error']}');
|
||||
return SyncResult(
|
||||
success: false,
|
||||
error: result['error'] as String? ?? 'Failed to sync from server',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_log.info('SyncService', '>>> Pagination loop completed. Fetched ${currentPage - 1} pages');
|
||||
_log.info('SyncService', '>>> Received total of ${allTransactions.length} transactions from server');
|
||||
|
||||
// Update local cache with server data
|
||||
_log.info('SyncService', '========== UPDATING LOCAL CACHE ==========');
|
||||
if (accountId == null) {
|
||||
_log.info('SyncService', 'Full sync - clearing and replacing all transactions');
|
||||
// Full sync - replace all transactions
|
||||
await _offlineStorage.syncTransactionsFromServer(allTransactions);
|
||||
} else {
|
||||
_log.info('SyncService', 'Partial sync - upserting ${allTransactions.length} transactions for account $accountId');
|
||||
// Partial sync - upsert transactions
|
||||
int upsertCount = 0;
|
||||
for (final transaction in allTransactions) {
|
||||
await _offlineStorage.upsertTransactionFromServer(
|
||||
transaction,
|
||||
accountId: accountId,
|
||||
);
|
||||
upsertCount++;
|
||||
if (upsertCount % 50 == 0) {
|
||||
_log.info('SyncService', 'Upserted $upsertCount/${allTransactions.length} transactions');
|
||||
}
|
||||
}
|
||||
|
||||
_lastSyncTime = DateTime.now();
|
||||
notifyListeners();
|
||||
|
||||
return SyncResult(
|
||||
success: true,
|
||||
syncedCount: transactions.length,
|
||||
);
|
||||
} else {
|
||||
_log.error('SyncService', 'Server returned error: ${result['error']}');
|
||||
return SyncResult(
|
||||
success: false,
|
||||
error: result['error'] as String? ?? 'Failed to sync from server',
|
||||
);
|
||||
_log.info('SyncService', 'Completed upserting $upsertCount transactions');
|
||||
}
|
||||
|
||||
_log.info('SyncService', '========== SYNC FROM SERVER COMPLETE ==========');
|
||||
_lastSyncTime = DateTime.now();
|
||||
notifyListeners();
|
||||
|
||||
return SyncResult(
|
||||
success: true,
|
||||
syncedCount: allTransactions.length,
|
||||
);
|
||||
} catch (e) {
|
||||
_log.error('SyncService', 'Exception in syncFromServer: $e');
|
||||
return SyncResult(
|
||||
|
||||
Reference in New Issue
Block a user