LCOV - code coverage report
Current view: top level - _classes/integration/fints - fints_importer.dart Coverage Total Hit
Test: lcov.info Lines: 100.0 % 36 36
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/math/budget_prediction.dart';
       5              : import 'package:app_finance/_classes/integration/integration_record.dart';
       6              : import 'package:app_finance/_classes/structure/interface_app_data.dart';
       7              : import 'package:fints/fints.dart';
       8              : import 'package:uuid/uuid.dart';
       9              : 
      10              : typedef FinTsImportResult = IntegrationImportResult;
      11              : 
      12              : class FinTsImporter {
      13              :   final bool Function(String uuid) exists;
      14              :   final void Function(InterfaceAppData item, String uuid) add;
      15              : 
      16            1 :   const FinTsImporter({required this.exists, required this.add});
      17              : 
      18            1 :   FinTsImportResult run({
      19              :     required String accountKey,
      20              :     required List<Transaction> transactions,
      21              :     required String account,
      22              :     required String category,
      23              :     BudgetPrediction? prediction,
      24              :   }) {
      25              :     var added = 0;
      26              :     var skipped = 0;
      27              :     var pending = 0;
      28            1 :     final occurrences = <String, int>{};
      29            2 :     for (final transaction in transactions) {
      30            1 :       if (transaction.isPending) {
      31            1 :         pending++;
      32              :         continue;
      33              :       }
      34            1 :       final fingerprint = _fingerprint(accountKey, transaction);
      35            4 :       final index = occurrences.update(fingerprint, (value) => value + 1, ifAbsent: () => 0);
      36            3 :       final uuid = const Uuid().v5(Namespace.url.value, 'fints:$fingerprint#$index');
      37            2 :       if (exists(uuid)) {
      38            1 :         skipped++;
      39              :         continue;
      40              :       }
      41            3 :       add(convert(transaction, account: account, category: category, prediction: prediction), uuid);
      42            1 :       added++;
      43              :     }
      44              :     return (added: added, skipped: skipped, pending: pending);
      45              :   }
      46              : 
      47            1 :   static InterfaceAppData convert(Transaction transaction,
      48              :           {required String account, required String category, BudgetPrediction? prediction}) =>
      49            1 :       IntegrationRecord.toAppData(
      50            2 :         amount: transaction.amount.value,
      51            2 :         currency: transaction.amount.currency,
      52            2 :         date: transaction.bookingDate ?? transaction.valueDate,
      53            1 :         title: getTitle(transaction),
      54              :         account: account,
      55              :         category: category,
      56              :         prediction: prediction,
      57              :       );
      58              : 
      59            2 :   static String getTitle(Transaction transaction) {
      60            6 :     final parts = [transaction.counterpartyName, transaction.purpose]
      61            6 :         .map((e) => e?.trim() ?? '')
      62            6 :         .where((e) => e.isNotEmpty)
      63            2 :         .toList();
      64            6 :     return parts.isNotEmpty ? parts.join(' — ') : (transaction.bookingText?.trim() ?? '');
      65              :   }
      66              : 
      67            1 :   static String _fingerprint(String accountKey, Transaction transaction) {
      68            3 :     final reference = transaction.endToEndId ?? transaction.bankReference ?? transaction.customerReference ?? '';
      69            1 :     return [
      70              :       accountKey,
      71            3 :       transaction.valueDate.toIso8601String().substring(0, 10),
      72            2 :       transaction.amount.minorUnits,
      73            2 :       transaction.amount.scale,
      74            2 :       transaction.amount.currency,
      75            1 :       reference == 'NOTPROVIDED' ? '' : reference,
      76            1 :       transaction.counterpartyIban ?? '',
      77            1 :       transaction.purpose ?? '',
      78            1 :     ].join('|');
      79              :   }
      80              : }
        

Generated by: LCOV version 2.0-1