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