|
|
|
|
@@ -1,22 +1,15 @@
|
|
|
|
|
import 'package:krow_data_connect/krow_data_connect.dart';
|
|
|
|
|
import 'package:firebase_data_connect/firebase_data_connect.dart';
|
|
|
|
|
import 'package:krow_data_connect/krow_data_connect.dart' as dc;
|
|
|
|
|
import 'package:krow_data_connect/src/session/staff_session_store.dart';
|
|
|
|
|
import 'package:krow_domain/krow_domain.dart';
|
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
|
import '../../domain/repositories/shifts_repository_interface.dart';
|
|
|
|
|
|
|
|
|
|
extension TimestampExt on Timestamp {
|
|
|
|
|
DateTime toDate() {
|
|
|
|
|
return DateTime.fromMillisecondsSinceEpoch(seconds.toInt() * 1000 + nanoseconds ~/ 1000000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Implementation of [ShiftsRepositoryInterface] that delegates to [ShiftsRepositoryMock].
|
|
|
|
|
///
|
|
|
|
|
/// This class resides in the data layer and handles the communication with
|
|
|
|
|
/// the external data sources (currently mocks).
|
|
|
|
|
class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
|
|
|
|
|
ShiftsRepositoryImpl();
|
|
|
|
|
final dc.ExampleConnector _dataConnect;
|
|
|
|
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
|
|
|
|
|
|
|
|
|
ShiftsRepositoryImpl() : _dataConnect = dc.ExampleConnector.instance;
|
|
|
|
|
|
|
|
|
|
// Cache: ShiftID -> ApplicationID (For Accept/Decline)
|
|
|
|
|
final Map<String, String> _shiftToAppIdMap = {};
|
|
|
|
|
@@ -24,98 +17,140 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
|
|
|
|
|
final Map<String, String> _appToRoleIdMap = {};
|
|
|
|
|
|
|
|
|
|
String get _currentStaffId {
|
|
|
|
|
final session = StaffSessionStore.instance.session;
|
|
|
|
|
if (session?.staff?.id == null) throw Exception('User not logged in');
|
|
|
|
|
final StaffSession? session = StaffSessionStore.instance.session;
|
|
|
|
|
if (session?.staff?.id != null) {
|
|
|
|
|
return session!.staff!.id;
|
|
|
|
|
}
|
|
|
|
|
// Fallback? Or throw.
|
|
|
|
|
// If not logged in, we shouldn't be here.
|
|
|
|
|
return _auth.currentUser?.uid ?? 'STAFF_123';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Helper to convert Data Connect Timestamp to DateTime
|
|
|
|
|
DateTime? _toDateTime(dynamic t) {
|
|
|
|
|
if (t == null) return null;
|
|
|
|
|
try {
|
|
|
|
|
if (t is String) return DateTime.tryParse(t);
|
|
|
|
|
// If it accepts toJson
|
|
|
|
|
try {
|
|
|
|
|
return DateTime.tryParse(t.toJson() as String);
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
// If it's a Timestamp object (depends on SDK), usually .toDate() exists but 'dynamic' hides it.
|
|
|
|
|
// Assuming toString or toJson covers it, or using helper.
|
|
|
|
|
return DateTime.now(); // Placeholder if type unknown, but ideally fetch correct value
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<List<Shift>> getMyShifts() async {
|
|
|
|
|
return _fetchApplications(ApplicationStatus.ACCEPTED);
|
|
|
|
|
return _fetchApplications(dc.ApplicationStatus.ACCEPTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<List<Shift>> getPendingAssignments() async {
|
|
|
|
|
// Fetch both PENDING (User applied) and OFFERED (Business offered) if schema supports
|
|
|
|
|
// For now assuming PENDING covers invitations/offers.
|
|
|
|
|
return _fetchApplications(ApplicationStatus.PENDING);
|
|
|
|
|
return _fetchApplications(dc.ApplicationStatus.PENDING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<List<Shift>> _fetchApplications(ApplicationStatus status) async {
|
|
|
|
|
Future<List<Shift>> _fetchApplications(dc.ApplicationStatus status) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await ExampleConnector.instance
|
|
|
|
|
final response = await _dataConnect
|
|
|
|
|
.getApplicationsByStaffId(staffId: _currentStaffId)
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
return response.data.applications
|
|
|
|
|
.where((app) => app.status is Known && (app.status as Known).value == status)
|
|
|
|
|
.map((app) {
|
|
|
|
|
// Cache IDs for actions
|
|
|
|
|
_shiftToAppIdMap[app.shift.id] = app.id;
|
|
|
|
|
_appToRoleIdMap[app.id] = app.shiftRole.roleId;
|
|
|
|
|
final apps = response.data.applications.where((app) => app.status == status);
|
|
|
|
|
final List<Shift> shifts = [];
|
|
|
|
|
|
|
|
|
|
return _mapApplicationToShift(app);
|
|
|
|
|
})
|
|
|
|
|
.toList();
|
|
|
|
|
for (final app in apps) {
|
|
|
|
|
_shiftToAppIdMap[app.shift.id] = app.id;
|
|
|
|
|
_appToRoleIdMap[app.id] = app.shiftRole.id;
|
|
|
|
|
|
|
|
|
|
final shiftTuple = await _getShiftDetails(app.shift.id);
|
|
|
|
|
if (shiftTuple != null) {
|
|
|
|
|
shifts.add(shiftTuple);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return shifts;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return [];
|
|
|
|
|
return <Shift>[];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<List<Shift>> getAvailableShifts(String query, String type) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await ExampleConnector.instance.listShifts().execute();
|
|
|
|
|
final result = await _dataConnect.listShifts().execute();
|
|
|
|
|
final allShifts = result.data.shifts;
|
|
|
|
|
|
|
|
|
|
var shifts = response.data.shifts
|
|
|
|
|
.where((s) => s.status is Known && (s.status as Known).value == ShiftStatus.OPEN)
|
|
|
|
|
.map((s) => _mapConnectorShiftToDomain(s))
|
|
|
|
|
.toList();
|
|
|
|
|
final List<Shift> mappedShifts = [];
|
|
|
|
|
|
|
|
|
|
for (final s in allShifts) {
|
|
|
|
|
// For each shift, map to Domain Shift
|
|
|
|
|
// Note: date fields in generated code might be specific types
|
|
|
|
|
final startDt = _toDateTime(s.startTime);
|
|
|
|
|
final endDt = _toDateTime(s.endTime);
|
|
|
|
|
final createdDt = _toDateTime(s.createdAt);
|
|
|
|
|
|
|
|
|
|
mappedShifts.add(Shift(
|
|
|
|
|
id: s.id,
|
|
|
|
|
title: s.title,
|
|
|
|
|
clientName: s.order.business.businessName,
|
|
|
|
|
logoUrl: null,
|
|
|
|
|
hourlyRate: s.cost ?? 0.0,
|
|
|
|
|
location: s.location ?? '',
|
|
|
|
|
locationAddress: s.locationAddress ?? '',
|
|
|
|
|
date: startDt?.toIso8601String() ?? '',
|
|
|
|
|
startTime: startDt != null ? DateFormat('HH:mm').format(startDt) : '',
|
|
|
|
|
endTime: endDt != null ? DateFormat('HH:mm').format(endDt) : '',
|
|
|
|
|
createdDate: createdDt?.toIso8601String() ?? '',
|
|
|
|
|
status: s.status?.stringValue ?? 'OPEN',
|
|
|
|
|
description: s.description,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Client-side filtering
|
|
|
|
|
if (query.isNotEmpty) {
|
|
|
|
|
shifts = shifts.where((s) =>
|
|
|
|
|
return mappedShifts.where((s) =>
|
|
|
|
|
s.title.toLowerCase().contains(query.toLowerCase()) ||
|
|
|
|
|
s.clientName.toLowerCase().contains(query.toLowerCase())
|
|
|
|
|
).toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type != 'all') {
|
|
|
|
|
if (type == 'one-day') {
|
|
|
|
|
shifts = shifts.where((s) => !s.title.contains('Multi-Day')).toList();
|
|
|
|
|
} else if (type == 'multi-day') {
|
|
|
|
|
shifts = shifts.where((s) => s.title.contains('Multi-Day')).toList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return shifts;
|
|
|
|
|
return mappedShifts;
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return [];
|
|
|
|
|
return <Shift>[];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Shift?> getShiftDetails(String shiftId) async {
|
|
|
|
|
return _getShiftDetails(shiftId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Shift?> _getShiftDetails(String shiftId) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await ExampleConnector.instance.getShiftById(id: shiftId).execute();
|
|
|
|
|
final s = response.data.shift;
|
|
|
|
|
final result = await _dataConnect.getShiftById(id: shiftId).execute();
|
|
|
|
|
final s = result.data.shift;
|
|
|
|
|
if (s == null) return null;
|
|
|
|
|
|
|
|
|
|
// Map to domain Shift
|
|
|
|
|
final startDt = _toDateTime(s.startTime);
|
|
|
|
|
final endDt = _toDateTime(s.endTime);
|
|
|
|
|
final createdDt = _toDateTime(s.createdAt);
|
|
|
|
|
|
|
|
|
|
return Shift(
|
|
|
|
|
id: s.id,
|
|
|
|
|
title: s.title,
|
|
|
|
|
clientName: s.order.business.businessName,
|
|
|
|
|
logoUrl: null,
|
|
|
|
|
hourlyRate: s.cost ?? 0.0,
|
|
|
|
|
location: s.location ?? 'Unknown',
|
|
|
|
|
location: s.location ?? '',
|
|
|
|
|
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() ?? '',
|
|
|
|
|
tipsAvailable: false,
|
|
|
|
|
mealProvided: false,
|
|
|
|
|
managers: [],
|
|
|
|
|
date: startDt?.toIso8601String() ?? '',
|
|
|
|
|
startTime: startDt != null ? DateFormat('HH:mm').format(startDt) : '',
|
|
|
|
|
endTime: endDt != null ? DateFormat('HH:mm').format(endDt) : '',
|
|
|
|
|
createdDate: createdDt?.toIso8601String() ?? '',
|
|
|
|
|
status: s.status?.stringValue ?? 'OPEN',
|
|
|
|
|
description: s.description,
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
@@ -125,92 +160,62 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> applyForShift(String shiftId) async {
|
|
|
|
|
// API LIMITATION: 'createApplication' requires roleId.
|
|
|
|
|
// 'listShifts' / 'getShiftById' does not currently return the Shift's available Roles.
|
|
|
|
|
// We cannot reliably apply for a shift without knowing the Role ID.
|
|
|
|
|
// Falling back to Mock delay for now.
|
|
|
|
|
await Future.delayed(const Duration(milliseconds: 500));
|
|
|
|
|
final rolesResult = await _dataConnect.listShiftRolesByShiftId(shiftId: shiftId).execute();
|
|
|
|
|
if (rolesResult.data.shiftRoles.isEmpty) throw Exception('No open roles for this shift');
|
|
|
|
|
|
|
|
|
|
// In future:
|
|
|
|
|
// 1. Fetch Shift Roles
|
|
|
|
|
// 2. Select Role
|
|
|
|
|
// 3. createApplication(shiftId, roleId, staffId, status: PENDING, origin: MOBILE)
|
|
|
|
|
final role = rolesResult.data.shiftRoles.first;
|
|
|
|
|
|
|
|
|
|
await _dataConnect.createApplication(
|
|
|
|
|
shiftId: shiftId,
|
|
|
|
|
staffId: _currentStaffId,
|
|
|
|
|
roleId: role.id,
|
|
|
|
|
status: dc.ApplicationStatus.PENDING,
|
|
|
|
|
origin: dc.ApplicationOrigin.STAFF,
|
|
|
|
|
).execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> acceptShift(String shiftId) async {
|
|
|
|
|
await _updateApplicationStatus(shiftId, ApplicationStatus.ACCEPTED);
|
|
|
|
|
await _updateApplicationStatus(shiftId, dc.ApplicationStatus.ACCEPTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<void> declineShift(String shiftId) async {
|
|
|
|
|
await _updateApplicationStatus(shiftId, ApplicationStatus.REJECTED);
|
|
|
|
|
await _updateApplicationStatus(shiftId, dc.ApplicationStatus.REJECTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _updateApplicationStatus(String shiftId, ApplicationStatus newStatus) async {
|
|
|
|
|
Future<void> _updateApplicationStatus(String shiftId, dc.ApplicationStatus newStatus) async {
|
|
|
|
|
String? appId = _shiftToAppIdMap[shiftId];
|
|
|
|
|
String? roleId;
|
|
|
|
|
|
|
|
|
|
// Refresh if missing from cache
|
|
|
|
|
if (appId == null) {
|
|
|
|
|
// Try to find it in pending
|
|
|
|
|
await getPendingAssignments();
|
|
|
|
|
appId = _shiftToAppIdMap[shiftId];
|
|
|
|
|
}
|
|
|
|
|
// Re-check map
|
|
|
|
|
appId = _shiftToAppIdMap[shiftId];
|
|
|
|
|
if (appId != null) {
|
|
|
|
|
roleId = _appToRoleIdMap[appId];
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback fetch
|
|
|
|
|
final apps = await _dataConnect.getApplicationsByStaffId(staffId: _currentStaffId).execute();
|
|
|
|
|
final app = apps.data.applications.where((a) => a.shiftId == shiftId).firstOrNull;
|
|
|
|
|
if (app != null) {
|
|
|
|
|
appId = app.id;
|
|
|
|
|
roleId = app.shiftRole.id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (appId == null || roleId == null) {
|
|
|
|
|
throw Exception("Application not found for shift $shiftId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ExampleConnector.instance.updateApplicationStatus(
|
|
|
|
|
await _dataConnect.updateApplicationStatus(
|
|
|
|
|
id: appId,
|
|
|
|
|
roleId: roleId,
|
|
|
|
|
)
|
|
|
|
|
.status(newStatus)
|
|
|
|
|
.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mappers
|
|
|
|
|
|
|
|
|
|
Shift _mapApplicationToShift(GetApplicationsByStaffIdApplications app) {
|
|
|
|
|
final s = app.shift;
|
|
|
|
|
final r = app.shiftRole;
|
|
|
|
|
final statusVal = app.status is Known
|
|
|
|
|
? (app.status as Known).value.name.toLowerCase() : 'pending';
|
|
|
|
|
|
|
|
|
|
return Shift(
|
|
|
|
|
id: s.id,
|
|
|
|
|
title: r.role.name,
|
|
|
|
|
clientName: s.order.business.businessName,
|
|
|
|
|
hourlyRate: r.role.costPerHour,
|
|
|
|
|
location: s.location ?? 'Unknown',
|
|
|
|
|
locationAddress: s.location ?? '',
|
|
|
|
|
date: s.date?.toDate().toIso8601String() ?? '',
|
|
|
|
|
startTime: DateFormat('HH:mm').format(r.startTime?.toDate() ?? DateTime.now()),
|
|
|
|
|
endTime: DateFormat('HH:mm').format(r.endTime?.toDate() ?? DateTime.now()),
|
|
|
|
|
createdDate: app.createdAt?.toDate().toIso8601String() ?? '',
|
|
|
|
|
status: statusVal,
|
|
|
|
|
description: null,
|
|
|
|
|
managers: [],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Shift _mapConnectorShiftToDomain(ListShiftsShifts s) {
|
|
|
|
|
return Shift(
|
|
|
|
|
id: s.id,
|
|
|
|
|
title: s.title,
|
|
|
|
|
clientName: s.order.business.businessName,
|
|
|
|
|
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() ?? '',
|
|
|
|
|
description: s.description,
|
|
|
|
|
managers: [],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|