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