LCOV - code coverage report
Current view: top level - lib/_classes/integration/monobank - monobank_importer.dart Coverage Total Hit
Test: lcov.info Lines: 71.0 % 31 22
Test Date: 2026-12-05 10:50:38 Functions: - 0 0

            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/monobank/monobank_client.dart';
       6              : import 'package:app_finance/_classes/math/budget_prediction.dart';
       7              : import 'package:app_finance/_classes/structure/bill_app_data.dart';
       8              : import 'package:app_finance/_classes/structure/interface_app_data.dart';
       9              : import 'package:uuid/uuid.dart';
      10              : 
      11              : class MonobankImporter {
      12              :   final bool Function(String uuid) exists;
      13              :   final void Function(InterfaceAppData item, String uuid) add;
      14              :   final Map<int, String> categories;
      15              :   final Map<String, int> records = {};
      16              :   DateTime? pendingSince;
      17              : 
      18            1 :   MonobankImporter({required this.exists, required this.add, Map<int, String>? categories})
      19            0 :       : categories = categories ?? {};
      20              : 
      21            4 :   static String recordUuid(String account, String id) => const Uuid().v5(Namespace.url.value, 'monobank:$account:$id');
      22              : 
      23            1 :   static Map<int, String> learn(Map<String, int> records, String? Function(String uuid) categoryOf) {
      24            1 :     final votes = <int, Map<String, int>>{};
      25            1 :     records.forEach((uuid, mcc) {
      26            0 :       final category = categoryOf(uuid);
      27              :       if (category != null) {
      28            0 :         final scope = votes.putIfAbsent(mcc, () => {});
      29            0 :         scope[category] = (scope[category] ?? 0) + 1;
      30              :       }
      31              :     });
      32            1 :     return votes.map((mcc, scope) => MapEntry(mcc, scope.entries.reduce((a, b) => b.value > a.value ? b : a).key));
      33              :   }
      34              : 
      35            1 :   IntegrationImportResult run({
      36              :     required String id,
      37              :     required List<MonobankTransaction> transactions,
      38              :     required String account,
      39              :     required String category,
      40              :     required String currency,
      41              :     BudgetPrediction? prediction,
      42              :   }) {
      43              :     var added = 0;
      44              :     var skipped = 0;
      45              :     var pending = 0;
      46            2 :     for (final transaction in transactions) {
      47            2 :       if (transaction.amount == 0) {
      48              :         continue;
      49              :       }
      50            1 :       if (transaction.isHold) {
      51            0 :         pending++;
      52            0 :         if (pendingSince == null || transaction.date.isBefore(pendingSince!)) {
      53            0 :           pendingSince = transaction.date;
      54              :         }
      55              :         continue;
      56              :       }
      57            2 :       final uuid = recordUuid(id, transaction.id);
      58            2 :       if (exists(uuid)) {
      59            0 :         skipped++;
      60              :         continue;
      61              :       }
      62            1 :       final item = IntegrationRecord.toAppData(
      63            2 :         amount: MonobankClient.fromMinor(transaction.amount, currency),
      64              :         currency: currency,
      65            1 :         date: transaction.date,
      66            1 :         title: transaction.title,
      67              :         account: account,
      68              :         category: category,
      69              :         prediction: prediction,
      70              :       );
      71            1 :       final mcc = transaction.mcc;
      72            1 :       if (item is BillAppData && mcc != null) {
      73            2 :         final known = categories[mcc];
      74              :         if (known != null) {
      75            0 :           item.category = known;
      76              :         }
      77            2 :         records[uuid] = mcc;
      78              :       }
      79            2 :       add(item, uuid);
      80            1 :       added++;
      81              :     }
      82              :     return (added: added, skipped: skipped, pending: pending);
      83              :   }
      84              : }
        

Generated by: LCOV version 2.0-1