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/integration/simplefin/simplefin_client.dart';
7 : import 'package:app_finance/_classes/structure/interface_app_data.dart';
8 : import 'package:uuid/uuid.dart';
9 :
10 : class SimpleFinImporter {
11 : final bool Function(String uuid) exists;
12 : final void Function(InterfaceAppData item, String uuid) add;
13 :
14 0 : const SimpleFinImporter({required this.exists, required this.add});
15 :
16 0 : IntegrationImportResult run({
17 : required SimpleFinAccount source,
18 : required String account,
19 : required String category,
20 : BudgetPrediction? prediction,
21 : }) {
22 : var added = 0;
23 : var skipped = 0;
24 : var pending = 0;
25 0 : for (final transaction in source.transactions) {
26 0 : final date = transaction.date;
27 0 : if (transaction.pending || date == null) {
28 0 : pending++;
29 : continue;
30 : }
31 0 : final uuid = const Uuid().v5(Namespace.url.value, 'simplefin:${source.id}:${transaction.id}');
32 0 : if (exists(uuid)) {
33 0 : skipped++;
34 : continue;
35 : }
36 0 : add(
37 0 : IntegrationRecord.toAppData(
38 0 : amount: transaction.amount,
39 0 : currency: source.currency,
40 : date: date,
41 0 : title: transaction.description,
42 : account: account,
43 : category: category,
44 : prediction: prediction,
45 : ),
46 : uuid,
47 : );
48 0 : added++;
49 : }
50 : return (added: added, skipped: skipped, pending: pending);
51 : }
52 : }
|