feat(home-repository): Refactor HomeRepositoryImpl to utilize DataConnectService for data operations and simplify shift retrieval logic
This commit is contained in:
@@ -1,26 +1,13 @@
|
||||
import 'package:firebase_data_connect/firebase_data_connect.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:staff_home/src/domain/repositories/home_repository.dart';
|
||||
|
||||
extension TimestampExt on Timestamp {
|
||||
DateTime toDate() {
|
||||
return DateTimeUtils.toDeviceTime(toDateTime());
|
||||
}
|
||||
}
|
||||
|
||||
class HomeRepositoryImpl
|
||||
with DataErrorHandler
|
||||
implements HomeRepository {
|
||||
HomeRepositoryImpl();
|
||||
HomeRepositoryImpl() : _service = DataConnectService.instance;
|
||||
|
||||
String get _currentStaffId {
|
||||
final session = StaffSessionStore.instance.session;
|
||||
if (session?.staff?.id == null) throw Exception('User not logged in');
|
||||
return session!.staff!.id;
|
||||
}
|
||||
final DataConnectService _service;
|
||||
|
||||
@override
|
||||
Future<List<Shift>> getTodayShifts() async {
|
||||
@@ -33,59 +20,57 @@ class HomeRepositoryImpl
|
||||
}
|
||||
|
||||
Future<List<Shift>> _getShiftsForDate(DateTime date) async {
|
||||
final staffId = _currentStaffId;
|
||||
return _service.run(() async {
|
||||
final staffId = await _service.getStaffId();
|
||||
|
||||
// Create start and end timestamps for the target date
|
||||
final DateTime start = DateTime(date.year, date.month, date.day);
|
||||
final DateTime end =
|
||||
DateTime(date.year, date.month, date.day, 23, 59, 59, 999);
|
||||
// Create start and end timestamps for the target date
|
||||
final DateTime start = DateTime(date.year, date.month, date.day);
|
||||
final DateTime end =
|
||||
DateTime(date.year, date.month, date.day, 23, 59, 59, 999);
|
||||
|
||||
final response = await executeProtected(() => ExampleConnector.instance
|
||||
.getApplicationsByStaffId(staffId: staffId)
|
||||
.dayStart(_toTimestamp(start))
|
||||
.dayEnd(_toTimestamp(end))
|
||||
.execute());
|
||||
final response = await _service.run(() => _service.connector
|
||||
.getApplicationsByStaffId(staffId: staffId)
|
||||
.dayStart(_service.toTimestamp(start))
|
||||
.dayEnd(_service.toTimestamp(end))
|
||||
.execute());
|
||||
|
||||
// Filter for CONFIRMED applications (same logic as shifts_repository_impl)
|
||||
final apps = response.data.applications.where((app) =>
|
||||
(app.status is Known &&
|
||||
(app.status as Known).value == ApplicationStatus.CONFIRMED));
|
||||
// Filter for CONFIRMED applications (same logic as shifts_repository_impl)
|
||||
final apps = response.data.applications.where((app) =>
|
||||
(app.status is Known &&
|
||||
(app.status as Known).value == ApplicationStatus.CONFIRMED));
|
||||
|
||||
final List<Shift> shifts = [];
|
||||
for (final app in apps) {
|
||||
shifts.add(_mapApplicationToShift(app));
|
||||
}
|
||||
final List<Shift> shifts = [];
|
||||
for (final app in apps) {
|
||||
shifts.add(_mapApplicationToShift(app));
|
||||
}
|
||||
|
||||
return shifts;
|
||||
}
|
||||
|
||||
Timestamp _toTimestamp(DateTime dateTime) {
|
||||
final DateTime utc = dateTime.toUtc();
|
||||
final int seconds = utc.millisecondsSinceEpoch ~/ 1000;
|
||||
final int nanoseconds = (utc.microsecondsSinceEpoch % 1000000) * 1000;
|
||||
return Timestamp(nanoseconds, seconds);
|
||||
return shifts;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Shift>> getRecommendedShifts() async {
|
||||
// Logic: List ALL open shifts (simple recommendation engine)
|
||||
// Limitation: listShifts might return ALL shifts. We should ideally filter by status=PUBLISHED.
|
||||
final response = await executeProtected(() => ExampleConnector.instance.listShifts().execute());
|
||||
return _service.run(() async {
|
||||
final response =
|
||||
await _service.run(() => _service.connector.listShifts().execute());
|
||||
|
||||
return response.data.shifts
|
||||
.where((s) {
|
||||
final isOpen =
|
||||
s.status is Known && (s.status as Known).value == ShiftStatus.OPEN;
|
||||
if (!isOpen) return false;
|
||||
return response.data.shifts
|
||||
.where((s) {
|
||||
final isOpen = s.status is Known &&
|
||||
(s.status as Known).value == ShiftStatus.OPEN;
|
||||
if (!isOpen) return false;
|
||||
|
||||
final start = s.startTime?.toDate();
|
||||
if (start == null) return false;
|
||||
final start = _service.toDateTime(s.startTime);
|
||||
if (start == null) return false;
|
||||
|
||||
return start.isAfter(DateTime.now());
|
||||
})
|
||||
.take(10)
|
||||
.map((s) => _mapConnectorShiftToDomain(s))
|
||||
.toList();
|
||||
return start.isAfter(DateTime.now());
|
||||
})
|
||||
.take(10)
|
||||
.map((s) => _mapConnectorShiftToDomain(s))
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -100,7 +85,7 @@ class HomeRepositoryImpl
|
||||
Shift _mapApplicationToShift(GetApplicationsByStaffIdApplications app) {
|
||||
final s = app.shift;
|
||||
final r = app.shiftRole;
|
||||
|
||||
|
||||
return ShiftAdapter.fromApplicationData(
|
||||
shiftId: s.id,
|
||||
roleId: r.roleId,
|
||||
@@ -110,10 +95,10 @@ class HomeRepositoryImpl
|
||||
costPerHour: r.role.costPerHour,
|
||||
shiftLocation: s.location,
|
||||
teamHubName: s.order.teamHub.hubName,
|
||||
shiftDate: s.date?.toDate(),
|
||||
startTime: r.startTime?.toDate(),
|
||||
endTime: r.endTime?.toDate(),
|
||||
createdAt: app.createdAt?.toDate(),
|
||||
shiftDate: _service.toDateTime(s.date),
|
||||
startTime: _service.toDateTime(r.startTime),
|
||||
endTime: _service.toDateTime(r.endTime),
|
||||
createdAt: _service.toDateTime(app.createdAt),
|
||||
status: 'confirmed',
|
||||
description: s.description,
|
||||
durationDays: s.durationDays,
|
||||
@@ -132,10 +117,12 @@ class HomeRepositoryImpl
|
||||
hourlyRate: s.cost ?? 0.0,
|
||||
location: s.location ?? 'Unknown',
|
||||
locationAddress: s.locationAddress ?? '',
|
||||
date: s.date?.toDate().toIso8601String() ?? '',
|
||||
startTime: DateFormat('HH:mm').format(s.startTime?.toDate() ?? DateTime.now()),
|
||||
endTime: DateFormat('HH:mm').format(s.endTime?.toDate() ?? DateTime.now()),
|
||||
createdDate: s.createdAt?.toDate().toIso8601String() ?? '',
|
||||
date: _service.toDateTime(s.date)?.toIso8601String() ?? '',
|
||||
startTime: DateFormat('HH:mm')
|
||||
.format(_service.toDateTime(s.startTime) ?? DateTime.now()),
|
||||
endTime: DateFormat('HH:mm')
|
||||
.format(_service.toDateTime(s.endTime) ?? DateTime.now()),
|
||||
createdDate: _service.toDateTime(s.createdAt)?.toIso8601String() ?? '',
|
||||
tipsAvailable: false,
|
||||
mealProvided: false,
|
||||
managers: [],
|
||||
|
||||
Reference in New Issue
Block a user