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 1 : const EnableBankingImporter({required this.exists, required this.add});
14 :
15 1 : 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 1 : final occurrences = <String, int>{};
25 2 : for (final transaction in transactions) {
26 1 : final date = transaction.date;
27 1 : if (!transaction.isBooked || date == null) {
28 1 : pending++;
29 : continue;
30 : }
31 1 : final reference = transaction.reference ??
32 6 : [date.toIso8601String().substring(0, 10), transaction.amount, transaction.currency, transaction.title]
33 1 : .join('|');
34 2 : final index = occurrences.update(reference, (value) => value + 1, ifAbsent: () => 0);
35 3 : final uuid = const Uuid().v5(Namespace.url.value, 'enablebanking:$accountKey:$reference#$index');
36 2 : if (exists(uuid)) {
37 1 : skipped++;
38 : continue;
39 : }
40 2 : add(
41 1 : IntegrationRecord.toAppData(
42 1 : amount: transaction.amount,
43 1 : currency: transaction.currency,
44 : date: date,
45 1 : title: transaction.title,
46 : account: account,
47 : category: category,
48 : ),
49 : uuid,
50 : );
51 1 : added++;
52 : }
53 : return (added: added, skipped: skipped, pending: pending);
54 : }
55 : }
|