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/enablebanking/enablebanking_client.dart';
5 : import 'package:app_finance/_classes/integration/integration_record.dart';
6 : import 'package:app_finance/_classes/math/budget_prediction.dart';
7 : import 'package:app_finance/_classes/structure/interface_app_data.dart';
8 : import 'package:uuid/uuid.dart';
9 :
10 : class EnableBankingImporter {
11 : final bool Function(String uuid) exists;
12 : final void Function(InterfaceAppData item, String uuid) add;
13 :
14 1 : const EnableBankingImporter({required this.exists, required this.add});
15 :
16 1 : IntegrationImportResult run({
17 : required String accountKey,
18 : required List<EnableBankingTransaction> transactions,
19 : required String account,
20 : required String category,
21 : BudgetPrediction? prediction,
22 : }) {
23 : var added = 0;
24 : var skipped = 0;
25 : var pending = 0;
26 1 : final occurrences = <String, int>{};
27 2 : for (final transaction in transactions) {
28 1 : final date = transaction.date;
29 1 : if (!transaction.isBooked || date == null) {
30 1 : pending++;
31 : continue;
32 : }
33 1 : final reference = transaction.reference ??
34 6 : [date.toIso8601String().substring(0, 10), transaction.amount, transaction.currency, transaction.title]
35 1 : .join('|');
36 2 : final index = occurrences.update(reference, (value) => value + 1, ifAbsent: () => 0);
37 3 : final uuid = const Uuid().v5(Namespace.url.value, 'enablebanking:$accountKey:$reference#$index');
38 2 : if (exists(uuid)) {
39 1 : skipped++;
40 : continue;
41 : }
42 2 : add(
43 1 : IntegrationRecord.toAppData(
44 1 : amount: transaction.amount,
45 1 : currency: transaction.currency,
46 : date: date,
47 1 : title: transaction.title,
48 : account: account,
49 : category: category,
50 : prediction: prediction,
51 : ),
52 : uuid,
53 : );
54 1 : added++;
55 : }
56 : return (added: added, skipped: skipped, pending: pending);
57 : }
58 : }
|