From 8ae8ce849fae3d52fa1b1ae908f5dc38f6c82540 Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Mon, 6 Jan 2025 10:01:24 -0300 Subject: [PATCH] wip --- .../features/module/module_test.dart | 0 .../backend/api_requests/api_calls.dart | 8 ++- .../repositories/menu_repository_impl.dart | 3 +- .../menu/presentation/blocs/menu_bloc.dart | 3 +- .../license_local_data_source.dart | 57 ++++++++++++++++--- .../license_remote_data_source.dart | 49 +++------------- .../repositories/license_repository_impl.dart | 27 +++++++-- 7 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 integration_test/features/module/module_test.dart diff --git a/integration_test/features/module/module_test.dart b/integration_test/features/module/module_test.dart new file mode 100644 index 00000000..e69de29b diff --git a/lib/features/backend/api_requests/api_calls.dart b/lib/features/backend/api_requests/api_calls.dart index b5665bea..72d1a8a2 100644 --- a/lib/features/backend/api_requests/api_calls.dart +++ b/lib/features/backend/api_requests/api_calls.dart @@ -18,7 +18,11 @@ const _kPrivateApiFunctionName = 'ffPrivateApiCall'; /// Start PHP Group Code -class PhpGroup { +abstract class Api { + GetLicense getLicense = GetLicense(); +} + +class PhpGroup extends Api { static String getBaseUrl() => 'https://freaccess.com.br/freaccess'; static Map headers = {}; static LoginCall loginCall = LoginCall(); @@ -62,7 +66,7 @@ class PhpGroup { static GetResidentsByProperty getResidentsByProperty = GetResidentsByProperty(); static GetOpenedVisits getOpenedVisits = GetOpenedVisits(); - static GetLicense getLicense = GetLicense(); + GetLicense getLicense = GetLicense(); static GetProvSchedules getProvSchedules = GetProvSchedules(); } diff --git a/lib/features/menu/data/repositories/menu_repository_impl.dart b/lib/features/menu/data/repositories/menu_repository_impl.dart index c5642310..af3a9df4 100644 --- a/lib/features/menu/data/repositories/menu_repository_impl.dart +++ b/lib/features/menu/data/repositories/menu_repository_impl.dart @@ -2,6 +2,7 @@ import 'dart:developer'; import 'package:hub/features/menu/index.dart'; import 'package:hub/features/module/index.dart'; +import 'package:hub/features/storage/index.dart'; import 'package:hub/flutter_flow/custom_functions.dart'; class MenuRepositoryImpl implements MenuRepository { @@ -55,7 +56,7 @@ class MenuRepositoryImpl implements MenuRepository { if (await _shouldUpdateDisplay(module, isNewVersion)) { final displayValue = module['display'] == EnumDisplay.active ? 'VISIVEL' : 'INVISIVEL'; - await LicenseLocalDataSourceImpl() + await LicenseLocalDataSourceImpl(DatabaseService.database) .setDisplayByKey(['FRE-HUB-ABOUT-PROPERTY'], displayValue); return EnumDisplay.fromString(displayValue); } diff --git a/lib/features/menu/presentation/blocs/menu_bloc.dart b/lib/features/menu/presentation/blocs/menu_bloc.dart index 4d4bb797..65bdd22a 100644 --- a/lib/features/menu/presentation/blocs/menu_bloc.dart +++ b/lib/features/menu/presentation/blocs/menu_bloc.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:hub/features/backend/index.dart'; import 'package:hub/features/menu/index.dart'; import 'package:hub/features/module/index.dart'; import 'package:hub/features/local/index.dart'; @@ -36,7 +37,7 @@ class MenuBloc extends Bloc { } Future _onMenuEvent(MenuEvent event, Emitter emit) async { - await LicenseRemoteDataSourceImpl().waitForSaveCompletion(); + await LicenseRemoteDataSourceImpl(PhpGroup()).waitForSaveCompletion(); final List newEntries = await MenuRepositoryImpl().generateMenuEntries(entries, item); diff --git a/lib/features/module/data/data_sources/license_local_data_source.dart b/lib/features/module/data/data_sources/license_local_data_source.dart index 25251b41..db6d58d9 100644 --- a/lib/features/module/data/data_sources/license_local_data_source.dart +++ b/lib/features/module/data/data_sources/license_local_data_source.dart @@ -1,6 +1,9 @@ +import 'dart:developer'; + import 'package:hub/features/module/index.dart'; import 'package:hub/features/storage/index.dart'; import 'package:hub/shared/extensions/string_extensions.dart'; +import 'package:sqflite/sqflite.dart'; abstract class LicenseLocalDataSource { Future init(); @@ -9,13 +12,22 @@ abstract class LicenseLocalDataSource { Future get(String key); Future set(String key, T value); Future del(String key); + Future setupLicense(bool isNewVersion); + Future clear(); } class LicenseLocalDataSourceImpl implements LicenseLocalDataSource { + late final Database database; + static final LicenseLocalDataSourceImpl _instance = LicenseLocalDataSourceImpl._internal(); - factory LicenseLocalDataSourceImpl() => _instance; + + factory LicenseLocalDataSourceImpl(Database database) { + _instance.database = database; + return _instance; + } + LicenseLocalDataSourceImpl._internal(); @override @@ -23,6 +35,38 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource { await DatabaseService.instance.init(); } + @override + Future setupLicense(bool isNewVersion) async { + log('Setting up license...'); + try { + final License license = await License.getLicense(isNewVersion); + + final List inactiveModuleKey = license.modules + .where((module) => module.display == ModuleStatus.inactive.key) + .map((module) => module.key) + .toList(); + + final List activeModuleKey = license.modules + .where((module) => module.display == ModuleStatus.active.key) + .map((module) => module.key) + .toList(); + + final List disabledModuleKey = license.modules + .where((module) => module.display == ModuleStatus.disabled.key) + .map((module) => module.key) + .toList(); + + await setDisplayByKey(inactiveModuleKey, 'INVISIVEL'); + await setDisplayByKey(activeModuleKey, 'VISIVEL'); + await setDisplayByKey(disabledModuleKey, 'DESABILITADO'); + + return true; + } catch (e, s) { + log('Erro ao configurar licenças: $e', stackTrace: s); + return false; + } + } + @override Future setDisplayByKey( final List keys, final String display) async { @@ -48,8 +92,8 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource { @override Future get(String key) async { - var response = await DatabaseService.database - .query(tableLicense, where: 'key = ?', whereArgs: [key]); + var response = + await database.query(tableLicense, where: 'key = ?', whereArgs: [key]); if (response.isEmpty) { return null; } @@ -63,7 +107,7 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource { if (value.isEmpty) return; if (key == 'null') return; - await DatabaseService.database.rawInsert( + await database.rawInsert( 'INSERT OR REPLACE INTO $tableLicense (key, display, expirationDate, startDate, quantity) VALUES (?, ?, ?, ?, ?)', [ key, @@ -78,12 +122,11 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource { @override Future del(String key) async { - await DatabaseService.database - .delete(tableLicense, where: 'key = ?', whereArgs: [key]); + await database.delete(tableLicense, where: 'key = ?', whereArgs: [key]); } @override Future clear() async { - await DatabaseService.database.delete(tableLicense); + await database.delete(tableLicense); } } diff --git a/lib/features/module/data/data_sources/license_remote_data_source.dart b/lib/features/module/data/data_sources/license_remote_data_source.dart index 124f432d..f88ae100 100644 --- a/lib/features/module/data/data_sources/license_remote_data_source.dart +++ b/lib/features/module/data/data_sources/license_remote_data_source.dart @@ -7,19 +7,23 @@ import 'package:hub/features/backend/index.dart'; import 'package:hub/features/local/data/repositories/locals_repository_impl.dart'; import 'package:hub/features/module/index.dart'; -import 'package:sqflite/sqflite.dart'; - abstract class LicenseRemoteDataSource { Future fetchLicenses(bool isNewVersion); Future cleanLicense(); Future processLicense(); - Future setupLicense(Database database, bool isNewVersion); } class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource { + late final Api api; + static final LicenseRemoteDataSourceImpl _instance = LicenseRemoteDataSourceImpl._internal(); - factory LicenseRemoteDataSourceImpl() => _instance; + + factory LicenseRemoteDataSourceImpl(Api api) { + _instance.api = api; + return _instance; + } + LicenseRemoteDataSourceImpl._internal(); @override @@ -30,46 +34,11 @@ class LicenseRemoteDataSourceImpl implements LicenseRemoteDataSource { LocalsRepositoryImpl.license.add(false); } - @override - Future setupLicense(Database database, bool isNewVersion) async { - log('Setting up license...'); - try { - final License license = await License.getLicense(isNewVersion); - - final List inactiveModuleKey = license.modules - .where((module) => module.display == ModuleStatus.inactive.key) - .map((module) => module.key) - .toList(); - - final List activeModuleKey = license.modules - .where((module) => module.display == ModuleStatus.active.key) - .map((module) => module.key) - .toList(); - - final List disabledModuleKey = license.modules - .where((module) => module.display == ModuleStatus.disabled.key) - .map((module) => module.key) - .toList(); - - await LicenseLocalDataSourceImpl() - .setDisplayByKey(inactiveModuleKey, 'INVISIVEL'); - await LicenseLocalDataSourceImpl() - .setDisplayByKey(activeModuleKey, 'VISIVEL'); - await LicenseLocalDataSourceImpl() - .setDisplayByKey(disabledModuleKey, 'DESABILITADO'); - - return true; - } catch (e, s) { - log('Erro ao configurar licenças: $e', stackTrace: s); - return false; - } - } - @override Future fetchLicenses(bool isNewVersion) async { log('Fetching licenses...'); try { - final response = await PhpGroup.getLicense(); + final response = await _instance.api.getLicense(); final dynamic responseBody = response.jsonBody; if (responseBody == []) { diff --git a/lib/features/module/data/repositories/license_repository_impl.dart b/lib/features/module/data/repositories/license_repository_impl.dart index a201b543..388ae1b6 100644 --- a/lib/features/module/data/repositories/license_repository_impl.dart +++ b/lib/features/module/data/repositories/license_repository_impl.dart @@ -1,5 +1,6 @@ import 'dart:developer'; +import 'package:hub/features/backend/index.dart'; import 'package:hub/features/module/data/data_sources/index.dart'; import 'package:hub/features/module/domain/index.dart'; import 'package:hub/features/storage/index.dart'; @@ -8,19 +9,33 @@ import 'package:sqflite/sqflite.dart'; typedef LicenseStatus = bool; class LicenseRepositoryImpl implements LicenseRepository { - final LicenseLocalDataSource localDataSource = LicenseLocalDataSourceImpl(); - final LicenseRemoteDataSource remoteDataSource = - LicenseRemoteDataSourceImpl(); - final Database database = DatabaseService.database; + late final Database database; + late final PhpGroup api; + late final LicenseLocalDataSource localDataSource; + late final LicenseRemoteDataSource remoteDataSource; - LicenseRepositoryImpl(); + LicenseRepositoryImpl([Database? database, PhpGroup? api]) { + if (database != null) { + this.database = database; + } else { + this.database = DatabaseService.database; + } + if (api != null) { + this.api = api; + } else { + this.api = PhpGroup(); + } + + localDataSource = LicenseLocalDataSourceImpl(this.database); + remoteDataSource = LicenseRemoteDataSourceImpl(this.api); + } @override Future updateLicense() async { log('updateLicense'); bool result = false; final bool isNewVersion = await localDataSource.isNewVersion(); - result = await remoteDataSource.setupLicense(database, isNewVersion); + result = await localDataSource.setupLicense(isNewVersion); if (isNewVersion) { result = await remoteDataSource.fetchLicenses(isNewVersion); }