LCOV - code coverage report
Current view: top level - lib/_classes/integration/investec - investec_settings.dart Coverage Total Hit
Test: lcov.info Lines: 93.5 % 31 29
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 'dart:convert';
       5              : 
       6              : import 'package:app_finance/_classes/controller/encryption_handler.dart';
       7              : import 'package:app_finance/_classes/integration/investec/investec_client.dart';
       8              : import 'package:app_finance/_classes/storage/app_preferences.dart';
       9              : 
      10              : class InvestecSettings {
      11              :   // Test data of openapisandbox.investec.com instead of the real accounts
      12              :   bool sandbox;
      13              : 
      14              :   // Investec account id to the uuid of the application's account
      15              :   final Map<String, String> links;
      16              : 
      17              :   // Investec account id to the last day transactions were imported for
      18              :   final Map<String, DateTime> imported;
      19              : 
      20            1 :   InvestecSettings({this.sandbox = false, Map<String, String>? links, Map<String, DateTime>? imported})
      21            1 :       : links = links ?? {},
      22            1 :         imported = imported ?? {};
      23              : 
      24            2 :   Map<String, dynamic> toJson() => {
      25            1 :         'sandbox': sandbox,
      26            1 :         'links': links,
      27            5 :         'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
      28              :       };
      29              : 
      30            2 :   factory InvestecSettings.fromJson(Map<String, dynamic> json) => InvestecSettings(
      31            2 :         sandbox: json['sandbox'] == true,
      32            5 :         links: (json['links'] as Map<String, dynamic>? ?? {}).map((key, value) => MapEntry(key, value.toString())),
      33            2 :         imported: (json['imported'] as Map<String, dynamic>? ?? {}).map(
      34            4 :           (key, value) => MapEntry(key, DateTime.parse(value.toString())),
      35              :         ),
      36              :       );
      37              : 
      38            1 :   static InvestecSettings load() {
      39            1 :     final value = AppPreferences.get(AppPreferences.prefInvestec);
      40            1 :     if (value == null || value.isEmpty) {
      41            1 :       return InvestecSettings();
      42              :     }
      43              :     try {
      44            2 :       return InvestecSettings.fromJson(jsonDecode(value));
      45            0 :     } on FormatException {
      46            0 :       return InvestecSettings();
      47              :     }
      48              :   }
      49              : 
      50            4 :   Future<void> save() => AppPreferences.set(AppPreferences.prefInvestec, jsonEncode(toJson()));
      51              : 
      52            1 :   static InvestecCredentials? loadCredentials() {
      53            1 :     final value = AppPreferences.get(AppPreferences.prefInvestecCredentials);
      54            1 :     if (value == null || value.isEmpty) {
      55              :       return null;
      56              :     }
      57              :     try {
      58            2 :       final json = jsonDecode(EncryptionHandler.decrypt(value)) as Map<String, dynamic>;
      59              :       final result = (
      60            2 :         clientId: (json['clientId'] ?? '').toString(),
      61            2 :         secret: (json['secret'] ?? '').toString(),
      62            2 :         apiKey: (json['apiKey'] ?? '').toString(),
      63              :       );
      64            3 :       return result.clientId.isEmpty || result.secret.isEmpty || result.apiKey.isEmpty ? null : result;
      65              :     } catch (_) {
      66              :       return null;
      67              :     }
      68              :   }
      69              : 
      70            2 :   static Future<void> saveCredentials(InvestecCredentials credentials) => AppPreferences.set(
      71              :         AppPreferences.prefInvestecCredentials,
      72            3 :         EncryptionHandler.encrypt(jsonEncode({
      73              :           'clientId': credentials.clientId,
      74              :           'secret': credentials.secret,
      75              :           'apiKey': credentials.apiKey,
      76              :         })),
      77              :       );
      78              : 
      79            2 :   static Future<void> clearCredentials() => AppPreferences.clear(AppPreferences.prefInvestecCredentials);
      80              : }
        

Generated by: LCOV version 2.0-1