LCOV - code coverage report
Current view: top level - _classes/integration/wise - wise_importer.dart Coverage Total Hit
Test: lcov.info Lines: 100.0 % 46 46
Test Date: 2026-12-05 10:48:47 Functions: - 0 0

            Line data    Source code
       1              : // Copyright 2026 The terCAD team. All rights reserved.
       2              : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
       3              : 
       4              : import 'package:app_finance/_classes/integration/integration_record.dart';
       5              : import 'package:app_finance/_classes/integration/wise/wise_client.dart';
       6              : import 'package:app_finance/_classes/math/budget_prediction.dart';
       7              : import 'package:app_finance/_classes/structure/currency_app_data.dart';
       8              : import 'package:app_finance/_classes/structure/interface_app_data.dart';
       9              : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
      10              : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
      11              : import 'package:uuid/uuid.dart';
      12              : 
      13              : // Another balance of the same profile linked to an application's account
      14              : typedef WiseCounterpart = ({String balanceId, String account});
      15              : 
      16              : class WiseImporter {
      17              :   final bool Function(String uuid) exists;
      18              :   final void Function(InterfaceAppData item, String uuid) add;
      19              :   final void Function(CurrencyAppData rate) setRate;
      20              : 
      21            1 :   const WiseImporter({required this.exists, required this.add, required this.setRate});
      22              : 
      23            3 :   static String _uuid(String value) => const Uuid().v5(Namespace.url.value, value);
      24              : 
      25            1 :   static String recordUuid(String balanceId, String type, String reference) =>
      26            2 :       _uuid('wise:$balanceId:$type:$reference');
      27              : 
      28            3 :   static String conversionUuid(int profileId, String reference) => _uuid('wise:$profileId:conversion:$reference');
      29              : 
      30            1 :   IntegrationImportResult run({
      31              :     required int profileId,
      32              :     required WiseBalance balance,
      33              :     required List<WiseTransaction> transactions,
      34              :     required String account,
      35              :     required String category,
      36              :     BudgetPrediction? prediction,
      37              :     Map<String, WiseCounterpart> counterparts = const {},
      38              :   }) {
      39              :     var added = 0;
      40              :     var skipped = 0;
      41            2 :     for (final transaction in transactions) {
      42            5 :       final own = recordUuid('${balance.id}', transaction.type, transaction.referenceNumber);
      43            1 :       final counterpart = _counterpart(transaction, balance, counterparts);
      44              :       if (counterpart != null) {
      45            1 :         final other = recordUuid(
      46            3 :             counterpart.balanceId, transaction.type == 'DEBIT' ? 'CREDIT' : 'DEBIT', transaction.referenceNumber);
      47            2 :         final uuid = conversionUuid(profileId, transaction.referenceNumber);
      48            6 :         if (exists(uuid) || exists(own) || exists(other)) {
      49            1 :           skipped++;
      50              :           continue;
      51              :         }
      52            1 :         _addTransfer(transaction, balance, account, counterpart.account, uuid);
      53            1 :         added++;
      54              :         continue;
      55              :       }
      56            6 :       if (exists(own) || exists(conversionUuid(profileId, transaction.referenceNumber))) {
      57            1 :         skipped++;
      58              :         continue;
      59              :       }
      60            2 :       add(
      61            1 :         IntegrationRecord.toAppData(
      62            1 :           amount: transaction.amount.value,
      63            3 :           currency: transaction.amount.currency.isNotEmpty ? transaction.amount.currency : balance.currency,
      64            1 :           date: transaction.date,
      65            1 :           title: transaction.description,
      66              :           account: account,
      67              :           category: category,
      68              :           prediction: prediction,
      69              :         ),
      70              :         own,
      71              :       );
      72            1 :       added++;
      73              :     }
      74              :     return (added: added, skipped: skipped, pending: 0);
      75              :   }
      76              : 
      77            1 :   WiseCounterpart? _counterpart(
      78              :       WiseTransaction transaction, WiseBalance balance, Map<String, WiseCounterpart> counterparts) {
      79            1 :     if (!transaction.isConversion) {
      80              :       return null;
      81              :     }
      82            1 :     final source = transaction.sourceAmount!.currency;
      83            1 :     final target = transaction.targetAmount!.currency;
      84            5 :     if (source == target || (source != balance.currency && target != balance.currency)) {
      85              :       return null;
      86              :     }
      87            3 :     final result = counterparts[source == balance.currency ? target : source];
      88            3 :     return result != null && result.balanceId != '${balance.id}' ? result : null;
      89              :   }
      90              : 
      91            1 :   void _addTransfer(WiseTransaction transaction, WiseBalance balance, String account, String other, String uuid) {
      92            1 :     final source = transaction.sourceAmount!;
      93            1 :     final target = transaction.targetAmount!;
      94            2 :     final isOutgoing = source.currency == balance.currency;
      95            1 :     final from = CurrencyProvider.find(source.currency);
      96            1 :     final to = CurrencyProvider.find(target.currency);
      97            1 :     if (from != null && to != null && target.value != 0) {
      98              :       // A transfer is kept in the target currency, the source account is charged by the rate
      99            6 :       setRate(CurrencyAppData(currencyFrom: to, currency: from, details: source.value.abs() / target.value.abs()));
     100              :     }
     101            2 :     add(
     102            1 :       InvoiceAppData(
     103            1 :         title: transaction.description,
     104              :         account: isOutgoing ? other : account,
     105              :         accountFrom: isOutgoing ? account : other,
     106            1 :         details: target.value.abs(),
     107              :         currency: to,
     108            1 :         createdAt: transaction.date,
     109            1 :         updatedAt: transaction.date,
     110              :         hidden: false,
     111              :       ),
     112              :       uuid,
     113              :     );
     114              :   }
     115              : }
        

Generated by: LCOV version 2.0-1