LCOV - code coverage report
Current view: top level - lib/_classes/integration/notification - notification_importer.dart Coverage Total Hit
Test: lcov.info Lines: 91.3 % 23 21
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/notification/notification_channel.dart';
       6              : import 'package:app_finance/_classes/integration/notification/notification_parser.dart';
       7              : import 'package:app_finance/_classes/math/budget_prediction.dart';
       8              : import 'package:app_finance/_classes/structure/interface_app_data.dart';
       9              : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
      10              : import 'package:uuid/uuid.dart';
      11              : 
      12              : typedef NotificationImportResult = ({int added, int skipped, int pending, List<String> processed});
      13              : 
      14              : class NotificationImporter {
      15              :   final bool Function(String uuid) exists;
      16              :   final void Function(InterfaceAppData item, String uuid) add;
      17              : 
      18            1 :   const NotificationImporter({required this.exists, required this.add});
      19              : 
      20            2 :   static String recordUuid(NotificationRecord record) => const Uuid().v5(
      21            1 :         Namespace.url.value,
      22            6 :         'notification:${record.package}:${record.time.millisecondsSinceEpoch}:${record.title}:${record.text}',
      23              :       );
      24              : 
      25              :   // Processed (imported or already present) notifications are returned to be removed from the buffer,
      26              :   // not recognized ones are kept as pending to be adjusted by keywords
      27            1 :   NotificationImportResult run({
      28              :     required List<NotificationRecord> records,
      29              :     required String account,
      30              :     required String category,
      31              :     required DateTime from,
      32              :     // Currency of the default account
      33              :     Currency? currency,
      34              :     // Default currency of the app: amounts without a currency are taken in it
      35              :     Currency? appCurrency,
      36              :     List<String> income = NotificationParser.incomeKeywords,
      37              :     List<String> balance = NotificationParser.balanceKeywords,
      38              :     List<String> merchant = NotificationParser.merchantLabels,
      39              :     // Keywords of accounts to be recognized instead of the default one, with their currencies
      40              :     Map<String, List<String>> accounts = const {},
      41              :     Currency? Function(String account)? currencyOf,
      42              :     BudgetPrediction? prediction,
      43              :   }) {
      44              :     var added = 0;
      45              :     var skipped = 0;
      46              :     var pending = 0;
      47            1 :     final processed = <String>[];
      48            2 :     for (final record in records) {
      49            2 :       if (record.time.isBefore(from)) {
      50              :         continue;
      51              :       }
      52            1 :       final uuid = recordUuid(record);
      53            2 :       if (exists(uuid)) {
      54            0 :         skipped++;
      55            0 :         processed.add(record.id);
      56              :         continue;
      57              :       }
      58            5 :       final target = NotificationParser.toAccount([record.title, record.text].join('\n'), accounts) ?? account;
      59              :       // A recognized account defines the currency the most precisely, then the app, then the default account
      60            1 :       final fallback = (target != account ? currencyOf?.call(target) : null) ?? appCurrency ?? currency;
      61            1 :       final parsed = NotificationParser.parse(
      62            1 :         record.title,
      63            1 :         record.text,
      64              :         fallback: fallback,
      65              :         isBare: appCurrency != null,
      66              :         income: income,
      67              :         balance: balance,
      68              :         merchant: merchant,
      69              :       );
      70              :       if (parsed == null) {
      71            1 :         pending++;
      72              :         continue;
      73              :       }
      74            2 :       add(
      75            1 :         IntegrationRecord.toAppData(
      76              :           amount: parsed.amount,
      77              :           currency: parsed.currency ?? fallback?.code,
      78            1 :           date: record.time,
      79              :           title: parsed.title,
      80              :           account: target,
      81              :           category: category,
      82              :           prediction: prediction,
      83              :         ),
      84              :         uuid,
      85              :       );
      86            1 :       added++;
      87            2 :       processed.add(record.id);
      88              :     }
      89              :     return (added: added, skipped: skipped, pending: pending, processed: processed);
      90              :   }
      91              : }
        

Generated by: LCOV version 2.0-1