Files
sure/mobile/lib/services/transactions_service.dart
Lazy Bone 62dabb6971 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>
2026-01-13 09:27:39 +01:00

236 lines
6.3 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/transaction.dart';
import 'api_config.dart';
class TransactionsService {
Future<Map<String, dynamic>> createTransaction({
required String accessToken,
required String accountId,
required String name,
required String date,
required String amount,
required String currency,
required String nature,
String? notes,
}) async {
final url = Uri.parse('${ApiConfig.baseUrl}/api/v1/transactions');
final body = {
'transaction': {
'account_id': accountId,
'name': name,
'date': date,
'amount': amount,
'currency': currency,
'nature': nature,
if (notes != null) 'notes': notes,
}
};
try {
final response = await http.post(
url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $accessToken',
},
body: jsonEncode(body),
).timeout(const Duration(seconds: 30));
if (response.statusCode == 200 || response.statusCode == 201) {
final responseData = jsonDecode(response.body);
return {
'success': true,
'transaction': Transaction.fromJson(responseData),
};
} else if (response.statusCode == 401) {
return {
'success': false,
'error': 'unauthorized',
};
} else {
try {
final responseData = jsonDecode(response.body);
return {
'success': false,
'error': responseData['error'] ?? 'Failed to create transaction',
};
} catch (e) {
return {
'success': false,
'error': 'Failed to create transaction: ${response.body}',
};
}
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
Future<Map<String, dynamic>> getTransactions({
required String accessToken,
String? accountId,
int? page,
int? perPage,
}) async {
final Map<String, String> queryParams = {};
if (accountId != null) {
queryParams['account_id'] = accountId;
}
if (page != null) {
queryParams['page'] = page.toString();
}
if (perPage != null) {
queryParams['per_page'] = perPage.toString();
}
final baseUri = Uri.parse('${ApiConfig.baseUrl}/api/v1/transactions');
final url = queryParams.isNotEmpty
? baseUri.replace(queryParameters: queryParams)
: baseUri;
try {
final response = await http.get(
url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $accessToken',
},
).timeout(const Duration(seconds: 30));
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
// Handle both array and object responses
List<dynamic> transactionsJson;
Map<String, dynamic>? pagination;
if (responseData is List) {
transactionsJson = responseData;
} else if (responseData is Map && responseData.containsKey('transactions')) {
transactionsJson = responseData['transactions'];
// Extract pagination metadata if present
if (responseData.containsKey('pagination')) {
pagination = responseData['pagination'];
}
} else {
transactionsJson = [];
}
final transactions = transactionsJson
.map((json) => Transaction.fromJson(json))
.toList();
return {
'success': true,
'transactions': transactions,
if (pagination != null) 'pagination': pagination,
};
} else if (response.statusCode == 401) {
return {
'success': false,
'error': 'unauthorized',
};
} else {
return {
'success': false,
'error': 'Failed to fetch transactions',
};
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
Future<Map<String, dynamic>> deleteTransaction({
required String accessToken,
required String transactionId,
}) async {
final url = Uri.parse('${ApiConfig.baseUrl}/api/v1/transactions/$transactionId');
try {
final response = await http.delete(
url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $accessToken',
},
).timeout(const Duration(seconds: 30));
if (response.statusCode == 200 || response.statusCode == 204) {
return {
'success': true,
};
} else if (response.statusCode == 401) {
return {
'success': false,
'error': 'unauthorized',
};
} else {
try {
final responseData = jsonDecode(response.body);
return {
'success': false,
'error': responseData['error'] ?? 'Failed to delete transaction',
};
} catch (e) {
return {
'success': false,
'error': 'Failed to delete transaction: ${response.body}',
};
}
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
Future<Map<String, dynamic>> deleteMultipleTransactions({
required String accessToken,
required List<String> transactionIds,
}) async {
try {
final results = await Future.wait(
transactionIds.map((id) => deleteTransaction(
accessToken: accessToken,
transactionId: id,
)),
);
final allSuccess = results.every((result) => result['success'] == true);
if (allSuccess) {
return {
'success': true,
'deleted_count': transactionIds.length,
};
} else {
final failedCount = results.where((r) => r['success'] != true).length;
return {
'success': false,
'error': 'Failed to delete $failedCount transactions',
};
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
}