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/monzo/monzo_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 MonzoImporter {
11 : final bool Function(String uuid) exists;
12 : final void Function(InterfaceAppData item, String uuid) add;
13 :
14 0 : const MonzoImporter({required this.exists, required this.add});
15 :
16 0 : static String uuidOf(String accountId, String transactionId) =>
17 0 : const Uuid().v5(Namespace.url.value, 'monzo:$accountId:$transactionId');
18 :
19 0 : IntegrationImportResult run({
20 : required String accountId,
21 : required List<MonzoTransaction> 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 0 : for (final transaction in transactions) {
30 : // Declined transactions moved no money
31 0 : if (transaction.declined) {
32 : continue;
33 : }
34 0 : if (transaction.pending) {
35 0 : pending++;
36 : continue;
37 : }
38 0 : final uuid = uuidOf(accountId, transaction.id);
39 0 : if (exists(uuid)) {
40 0 : skipped++;
41 : continue;
42 : }
43 0 : add(
44 0 : IntegrationRecord.toAppData(
45 0 : amount: transaction.amount,
46 0 : currency: transaction.currency,
47 0 : date: transaction.created,
48 0 : title: transaction.description,
49 : account: account,
50 : category: category,
51 : prediction: prediction,
52 : ),
53 : uuid,
54 : );
55 0 : added++;
56 : }
57 : return (added: added, skipped: skipped, pending: pending);
58 : }
59 : }
|