feat: Refactor CoverageModule and CoverageRepositoryImpl to utilize DataConnectService
This commit is contained in:
@@ -10,24 +10,20 @@ import 'presentation/pages/coverage_page.dart';
|
|||||||
|
|
||||||
/// Modular module for the coverage feature.
|
/// Modular module for the coverage feature.
|
||||||
class CoverageModule extends Module {
|
class CoverageModule extends Module {
|
||||||
|
@override
|
||||||
|
List<Module> get imports => <Module>[DataConnectModule()];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void binds(Injector i) {
|
void binds(Injector i) {
|
||||||
// Repositories
|
// Repositories
|
||||||
i.addSingleton<CoverageRepository>(
|
i.addSingleton<CoverageRepository>(CoverageRepositoryImpl.new);
|
||||||
() => CoverageRepositoryImpl(dataConnect: ExampleConnector.instance),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Use Cases
|
// Use Cases
|
||||||
i.addSingleton(GetShiftsForDateUseCase.new);
|
i.addSingleton(GetShiftsForDateUseCase.new);
|
||||||
i.addSingleton(GetCoverageStatsUseCase.new);
|
i.addSingleton(GetCoverageStatsUseCase.new);
|
||||||
|
|
||||||
// BLoCs
|
// BLoCs
|
||||||
i.addSingleton<CoverageBloc>(
|
i.addSingleton<CoverageBloc>(CoverageBloc.new);
|
||||||
() => CoverageBloc(
|
|
||||||
getShiftsForDate: i.get<GetShiftsForDateUseCase>(),
|
|
||||||
getCoverageStats: i.get<GetCoverageStatsUseCase>(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -15,44 +15,36 @@ import '../../domain/repositories/coverage_repository.dart';
|
|||||||
/// - Returns domain entities from `domain/ui_entities`.
|
/// - Returns domain entities from `domain/ui_entities`.
|
||||||
class CoverageRepositoryImpl implements CoverageRepository {
|
class CoverageRepositoryImpl implements CoverageRepository {
|
||||||
/// Creates a [CoverageRepositoryImpl].
|
/// Creates a [CoverageRepositoryImpl].
|
||||||
CoverageRepositoryImpl({required dc.ExampleConnector dataConnect})
|
CoverageRepositoryImpl({required dc.DataConnectService service}) : _service = service;
|
||||||
: _dataConnect = dataConnect;
|
|
||||||
|
|
||||||
final dc.ExampleConnector _dataConnect;
|
final dc.DataConnectService _service;
|
||||||
|
|
||||||
/// Fetches shifts for a specific date.
|
/// Fetches shifts for a specific date.
|
||||||
@override
|
@override
|
||||||
Future<List<CoverageShift>> getShiftsForDate({required DateTime date}) async {
|
Future<List<CoverageShift>> getShiftsForDate({required DateTime date}) async {
|
||||||
try {
|
return _service.run(() async {
|
||||||
final String? businessId =
|
final String businessId = await _service.getBusinessId();
|
||||||
dc.ClientSessionStore.instance.session?.business?.id;
|
|
||||||
if (businessId == null || businessId.isEmpty) {
|
|
||||||
return <CoverageShift>[];
|
|
||||||
}
|
|
||||||
|
|
||||||
final DateTime start = DateTime(date.year, date.month, date.day);
|
final DateTime start = DateTime(date.year, date.month, date.day);
|
||||||
final DateTime end =
|
final DateTime end = DateTime(date.year, date.month, date.day, 23, 59, 59, 999);
|
||||||
DateTime(date.year, date.month, date.day, 23, 59, 59, 999);
|
|
||||||
|
|
||||||
final fdc.QueryResult<
|
final fdc.QueryResult<dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||||
dc.ListShiftRolesByBusinessAndDateRangeData,
|
dc.ListShiftRolesByBusinessAndDateRangeVariables> shiftRolesResult =
|
||||||
dc.ListShiftRolesByBusinessAndDateRangeVariables> shiftRolesResult =
|
await _service.connector
|
||||||
await _dataConnect
|
|
||||||
.listShiftRolesByBusinessAndDateRange(
|
.listShiftRolesByBusinessAndDateRange(
|
||||||
businessId: businessId,
|
businessId: businessId,
|
||||||
start: _toTimestamp(start),
|
start: _service.toTimestamp(start),
|
||||||
end: _toTimestamp(end),
|
end: _service.toTimestamp(end),
|
||||||
)
|
)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
final fdc.QueryResult<
|
final fdc.QueryResult<dc.ListStaffsApplicationsByBusinessForDayData,
|
||||||
dc.ListStaffsApplicationsByBusinessForDayData,
|
dc.ListStaffsApplicationsByBusinessForDayVariables> applicationsResult =
|
||||||
dc.ListStaffsApplicationsByBusinessForDayVariables> applicationsResult =
|
await _service.connector
|
||||||
await _dataConnect
|
|
||||||
.listStaffsApplicationsByBusinessForDay(
|
.listStaffsApplicationsByBusinessForDay(
|
||||||
businessId: businessId,
|
businessId: businessId,
|
||||||
dayStart: _toTimestamp(start),
|
dayStart: _service.toTimestamp(start),
|
||||||
dayEnd: _toTimestamp(end),
|
dayEnd: _service.toTimestamp(end),
|
||||||
)
|
)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
@@ -61,18 +53,7 @@ class CoverageRepositoryImpl implements CoverageRepository {
|
|||||||
applicationsResult.data.applications,
|
applicationsResult.data.applications,
|
||||||
date,
|
date,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
});
|
||||||
final String error = e.toString().toLowerCase();
|
|
||||||
if (error.contains('network') ||
|
|
||||||
error.contains('connection') ||
|
|
||||||
error.contains('unavailable') ||
|
|
||||||
error.contains('offline') ||
|
|
||||||
error.contains('socket') ||
|
|
||||||
error.contains('failed host lookup')) {
|
|
||||||
throw NetworkException(technicalMessage: 'Coverage fetch failed: $e');
|
|
||||||
}
|
|
||||||
throw ServerException(technicalMessage: 'Coverage fetch failed: $e');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetches coverage statistics for a specific date.
|
/// Fetches coverage statistics for a specific date.
|
||||||
@@ -110,14 +91,6 @@ class CoverageRepositoryImpl implements CoverageRepository {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fdc.Timestamp _toTimestamp(DateTime dateTime) {
|
|
||||||
final DateTime utc = dateTime.toUtc();
|
|
||||||
final int seconds = utc.millisecondsSinceEpoch ~/ 1000;
|
|
||||||
final int nanoseconds =
|
|
||||||
(utc.millisecondsSinceEpoch % 1000) * 1000000;
|
|
||||||
return fdc.Timestamp(nanoseconds, seconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<CoverageShift> _mapCoverageShifts(
|
List<CoverageShift> _mapCoverageShifts(
|
||||||
List<dc.ListShiftRolesByBusinessAndDateRangeShiftRoles> shiftRoles,
|
List<dc.ListShiftRolesByBusinessAndDateRangeShiftRoles> shiftRoles,
|
||||||
List<dc.ListStaffsApplicationsByBusinessForDayApplications> applications,
|
List<dc.ListStaffsApplicationsByBusinessForDayApplications> applications,
|
||||||
|
|||||||
Reference in New Issue
Block a user