diff --git a/apps/mobile/packages/features/client/client_coverage/lib/src/data/repositories_impl/coverage_repository_impl.dart b/apps/mobile/packages/features/client/client_coverage/lib/src/data/repositories_impl/coverage_repository_impl.dart index 9ca753a8..fa29d536 100644 --- a/apps/mobile/packages/features/client/client_coverage/lib/src/data/repositories_impl/coverage_repository_impl.dart +++ b/apps/mobile/packages/features/client/client_coverage/lib/src/data/repositories_impl/coverage_repository_impl.dart @@ -37,9 +37,20 @@ class CoverageRepositoryImpl implements CoverageRepository { print( 'Coverage: request businessId=$businessId dayStart=${start.toIso8601String()} dayEnd=${end.toIso8601String()}', ); + final fdc.QueryResult< + dc.ListShiftRolesByBusinessAndDateRangeData, + dc.ListShiftRolesByBusinessAndDateRangeVariables> shiftRolesResult = + await _dataConnect + .listShiftRolesByBusinessAndDateRange( + businessId: businessId, + start: _toTimestamp(start), + end: _toTimestamp(end), + ) + .execute(); + final fdc.QueryResult< dc.ListStaffsApplicationsByBusinessForDayData, - dc.ListStaffsApplicationsByBusinessForDayVariables> result = + dc.ListStaffsApplicationsByBusinessForDayVariables> applicationsResult = await _dataConnect .listStaffsApplicationsByBusinessForDay( businessId: businessId, @@ -48,10 +59,14 @@ class CoverageRepositoryImpl implements CoverageRepository { ) .execute(); print( - 'Coverage: ${date.toIso8601String()} staffsApplications=${result.data.applications.length}', + 'Coverage: ${date.toIso8601String()} staffsApplications=${applicationsResult.data.applications.length}', ); - return _mapCoverageShifts(result.data.applications, date); + return _mapCoverageShifts( + shiftRolesResult.data.shiftRoles, + applicationsResult.data.applications, + date, + ); } /// Fetches coverage statistics for a specific date. @@ -91,16 +106,31 @@ class CoverageRepositoryImpl implements CoverageRepository { return fdc.Timestamp(nanoseconds, seconds); } - Future> _mapCoverageShifts( + List _mapCoverageShifts( + List shiftRoles, List applications, DateTime date, - ) async { - if (applications.isEmpty) { + ) { + if (shiftRoles.isEmpty && applications.isEmpty) { return []; } - final Map groups = - {}; + final Map groups = {}; + for (final dc.ListShiftRolesByBusinessAndDateRangeShiftRoles shiftRole + in shiftRoles) { + final String key = '${shiftRole.shiftId}:${shiftRole.roleId}'; + groups[key] = _CoverageGroup( + shiftId: shiftRole.shiftId, + roleId: shiftRole.roleId, + title: shiftRole.role.name, + location: shiftRole.shift.location ?? '', + startTime: _formatTime(shiftRole.startTime) ?? '00:00', + workersNeeded: shiftRole.count, + date: shiftRole.shift.date?.toDateTime() ?? date, + workers: [], + ); + } + for (final dc.ListStaffsApplicationsByBusinessForDayApplications app in applications) { final String key = '${app.shiftId}:${app.roleId}'; @@ -108,8 +138,11 @@ class CoverageRepositoryImpl implements CoverageRepository { _CoverageGroup( shiftId: app.shiftId, roleId: app.roleId, - roleName: app.shiftRole.role.name, + title: app.shiftRole.role.name, location: app.shiftRole.shift.location ?? '', + startTime: '00:00', + workersNeeded: 0, + date: date, workers: [], ); @@ -123,37 +156,19 @@ class CoverageRepositoryImpl implements CoverageRepository { groups[key] = existing; } - final List<_CoverageGroup> groupList = groups.values.toList(); - final List shifts = []; - for (final _CoverageGroup group in groupList) { - final fdc.QueryResult shiftRoleResult = - await _dataConnect - .getShiftRoleById( - shiftId: group.shiftId, - roleId: group.roleId, - ) - .execute(); - final dc.GetShiftRoleByIdShiftRole? shiftRole = - shiftRoleResult.data.shiftRole; - if (shiftRole == null) { - continue; - } - final String startTime = _formatTime(shiftRole.startTime) ?? '00:00'; - shifts.add( - CoverageShift( - id: shiftRole.id, - title: group.roleName, - location: group.location, - startTime: startTime, - workersNeeded: shiftRole.count, - date: date, - workers: group.workers, - ), - ); - } - - return shifts; + return groups.values + .map( + (_CoverageGroup group) => CoverageShift( + id: '${group.shiftId}:${group.roleId}', + title: group.title, + location: group.location, + startTime: group.startTime, + workersNeeded: group.workersNeeded, + date: group.date, + workers: group.workers, + ), + ) + .toList(); } String _mapWorkerStatus( @@ -191,14 +206,20 @@ class _CoverageGroup { _CoverageGroup({ required this.shiftId, required this.roleId, - required this.roleName, + required this.title, required this.location, + required this.startTime, + required this.workersNeeded, + required this.date, required this.workers, }); final String shiftId; final String roleId; - final String roleName; + final String title; final String location; + final String startTime; + final int workersNeeded; + final DateTime date; final List workers; }