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