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/teller/teller_client.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 TellerImporter {
11 : final bool Function(String uuid) exists;
12 : final void Function(InterfaceAppData item, String uuid) add;
13 :
14 1 : const TellerImporter({required this.exists, required this.add});
15 :
16 1 : static String uuidOf(String accountId, String transactionId) =>
17 3 : const Uuid().v5(Namespace.url.value, 'teller:$accountId:$transactionId');
18 :
19 1 : IntegrationImportResult run({
20 : required TellerAccount source,
21 : required List<TellerTransaction> transactions,
22 : required String account,
23 : required String category,
24 : BudgetPrediction? prediction,
25 : }) {
26 : var added = 0;
27 : var skipped = 0;
28 : var pending = 0;
29 2 : for (final transaction in transactions) {
30 1 : final date = transaction.date;
31 : // Pending transactions may change (or get a new id) when posted
32 1 : if (transaction.pending || date == null) {
33 1 : pending++;
34 : continue;
35 : }
36 3 : final uuid = uuidOf(source.id, transaction.id);
37 2 : if (exists(uuid)) {
38 1 : skipped++;
39 : continue;
40 : }
41 2 : add(
42 1 : IntegrationRecord.toAppData(
43 1 : amount: transaction.amount,
44 3 : currency: source.currency.isEmpty ? null : source.currency,
45 : date: date,
46 1 : title: transaction.description,
47 : account: account,
48 : category: category,
49 : prediction: prediction,
50 : ),
51 : uuid,
52 : );
53 1 : added++;
54 : }
55 : return (added: added, skipped: skipped, pending: pending);
56 : }
57 : }
|