296 lines
9.0 KiB
Dart
296 lines
9.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
import '../models/leave_of_absence.dart';
|
|
import 'profile_provider.dart';
|
|
import 'supabase_provider.dart';
|
|
import 'stream_recovery.dart';
|
|
import 'realtime_controller.dart';
|
|
|
|
/// All visible leaves (own for standard, all for admin/dispatcher/it_staff).
|
|
///
|
|
/// Consumers should **not** treat every record as an active absence; the UI
|
|
/// layers (dashboard, logbook) explicitly filter to `status == 'approved'` and
|
|
/// verify the leave overlaps the current time. This prevents rejected or
|
|
/// pending applications from inadvertently influencing schedules or status
|
|
/// computations.
|
|
final leavesProvider = StreamProvider<List<LeaveOfAbsence>>((ref) {
|
|
final client = ref.watch(supabaseClientProvider);
|
|
final profileAsync = ref.watch(currentProfileProvider);
|
|
final profile = profileAsync.valueOrNull;
|
|
if (profile == null) return Stream.value(const <LeaveOfAbsence>[]);
|
|
|
|
final hasFullAccess =
|
|
profile.role == 'admin' ||
|
|
profile.role == 'programmer' ||
|
|
profile.role == 'dispatcher' ||
|
|
profile.role == 'it_staff';
|
|
|
|
final wrapper = StreamRecoveryWrapper<LeaveOfAbsence>(
|
|
stream: hasFullAccess
|
|
? client
|
|
.from('leave_of_absence')
|
|
.stream(primaryKey: ['id'])
|
|
.order('start_time', ascending: false)
|
|
: client
|
|
.from('leave_of_absence')
|
|
.stream(primaryKey: ['id'])
|
|
.eq('user_id', profile.id)
|
|
.order('start_time', ascending: false),
|
|
onPollData: () async {
|
|
final query = client.from('leave_of_absence').select();
|
|
final data = hasFullAccess
|
|
? await query.order('start_time', ascending: false)
|
|
: await query
|
|
.eq('user_id', profile.id)
|
|
.order('start_time', ascending: false);
|
|
return data.map(LeaveOfAbsence.fromMap).toList();
|
|
},
|
|
fromMap: LeaveOfAbsence.fromMap,
|
|
channelName: 'leave_of_absence',
|
|
onStatusChanged: ref.read(realtimeControllerProvider).handleChannelStatus,
|
|
);
|
|
|
|
ref.onDispose(wrapper.dispose);
|
|
return wrapper.stream.map((result) => result.data);
|
|
});
|
|
|
|
final leaveControllerProvider = Provider<LeaveController>((ref) {
|
|
final client = ref.watch(supabaseClientProvider);
|
|
return LeaveController(client);
|
|
});
|
|
|
|
class LeaveController {
|
|
LeaveController(this._client);
|
|
|
|
final SupabaseClient _client;
|
|
|
|
/// File a leave of absence for the current user.
|
|
/// Caller controls auto-approval based on role policy.
|
|
Future<void> fileLeave({
|
|
required String leaveType,
|
|
required String justification,
|
|
required DateTime startTime,
|
|
required DateTime endTime,
|
|
required bool autoApprove,
|
|
}) async {
|
|
final uid = _client.auth.currentUser!.id;
|
|
final payload = {
|
|
'user_id': uid,
|
|
'leave_type': leaveType,
|
|
'justification': justification,
|
|
'start_time': startTime.toIso8601String(),
|
|
'end_time': endTime.toIso8601String(),
|
|
'status': autoApprove ? 'approved' : 'pending',
|
|
'filed_by': uid,
|
|
};
|
|
|
|
final insertedRaw = await _client
|
|
.from('leave_of_absence')
|
|
.insert(payload)
|
|
.select()
|
|
.maybeSingle();
|
|
final Map<String, dynamic>? inserted = insertedRaw is Map<String, dynamic>
|
|
? insertedRaw
|
|
: null;
|
|
|
|
// If this was filed as pending, notify admins for approval
|
|
final status = payload['status'] as String;
|
|
if (status != 'pending') return;
|
|
|
|
try {
|
|
final adminIds = await _fetchRoleUserIds(
|
|
roles: const ['admin'],
|
|
excludeUserId: uid,
|
|
);
|
|
if (adminIds.isEmpty) return;
|
|
|
|
// Resolve actor display name for nicer push text
|
|
String actorName = 'Someone';
|
|
try {
|
|
final p = await _client
|
|
.from('profiles')
|
|
.select('full_name,display_name,name')
|
|
.eq('id', uid)
|
|
.maybeSingle();
|
|
if (p != null) {
|
|
if (p['full_name'] != null) {
|
|
actorName = p['full_name'].toString();
|
|
} else if (p['display_name'] != null) {
|
|
actorName = p['display_name'].toString();
|
|
} else if (p['name'] != null) {
|
|
actorName = p['name'].toString();
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
|
|
final leaveId = (inserted ?? <String, dynamic>{})['id']?.toString() ?? '';
|
|
final title = 'Leave Filed for Approval';
|
|
final body = '$actorName filed a leave request that requires approval.';
|
|
final notificationId = (inserted ?? <String, dynamic>{})['id']
|
|
?.toString();
|
|
|
|
final dataPayload = <String, dynamic>{
|
|
'type': 'leave_filed',
|
|
'leave_id': leaveId,
|
|
...?(notificationId != null
|
|
? {'notification_id': notificationId}
|
|
: null),
|
|
};
|
|
|
|
await _client
|
|
.from('notifications')
|
|
.insert(
|
|
adminIds
|
|
.map(
|
|
(userId) => {
|
|
'user_id': userId,
|
|
'actor_id': uid,
|
|
'type': 'leave_filed',
|
|
'leave_id': leaveId,
|
|
},
|
|
)
|
|
.toList(),
|
|
);
|
|
|
|
final res = await _client.functions.invoke(
|
|
'send_fcm',
|
|
body: {
|
|
'user_ids': adminIds,
|
|
'title': title,
|
|
'body': body,
|
|
'data': dataPayload,
|
|
},
|
|
);
|
|
debugPrint('leave filing send_fcm result: $res');
|
|
} catch (e) {
|
|
debugPrint('leave filing send_fcm error: $e');
|
|
// Non-fatal: keep leave filing working even if send_fcm fails
|
|
}
|
|
}
|
|
|
|
/// Approve a leave request.
|
|
Future<void> approveLeave(String leaveId) async {
|
|
final userId = _client.auth.currentUser?.id;
|
|
if (userId == null) throw Exception('Not authenticated');
|
|
|
|
// Update status first; then notify the requester.
|
|
await _client
|
|
.from('leave_of_absence')
|
|
.update({'status': 'approved'})
|
|
.eq('id', leaveId);
|
|
|
|
// Notify requestor
|
|
await _notifyRequester(leaveId: leaveId, actorId: userId, approved: true);
|
|
}
|
|
|
|
/// Reject a leave request.
|
|
Future<void> rejectLeave(String leaveId) async {
|
|
final userId = _client.auth.currentUser?.id;
|
|
if (userId == null) throw Exception('Not authenticated');
|
|
|
|
await _client
|
|
.from('leave_of_absence')
|
|
.update({'status': 'rejected'})
|
|
.eq('id', leaveId);
|
|
|
|
// Notify requestor
|
|
await _notifyRequester(leaveId: leaveId, actorId: userId, approved: false);
|
|
}
|
|
|
|
Future<void> _notifyRequester({
|
|
required String leaveId,
|
|
required String actorId,
|
|
required bool approved,
|
|
}) async {
|
|
try {
|
|
final row = await _client
|
|
.from('leave_of_absence')
|
|
.select('user_id')
|
|
.eq('id', leaveId)
|
|
.maybeSingle();
|
|
// ignore: unnecessary_cast
|
|
final rowMap = row as Map<String, dynamic>?;
|
|
final userId = rowMap?['user_id']?.toString();
|
|
if (userId == null || userId.isEmpty) return;
|
|
|
|
String actorName = 'Someone';
|
|
try {
|
|
final p = await _client
|
|
.from('profiles')
|
|
.select('full_name,display_name,name')
|
|
.eq('id', actorId)
|
|
.maybeSingle();
|
|
if (p != null) {
|
|
if (p['full_name'] != null) {
|
|
actorName = p['full_name'].toString();
|
|
} else if (p['display_name'] != null) {
|
|
actorName = p['display_name'].toString();
|
|
} else if (p['name'] != null) {
|
|
actorName = p['name'].toString();
|
|
}
|
|
}
|
|
} catch (_) {}
|
|
|
|
final title = approved ? 'Leave Approved' : 'Leave Rejected';
|
|
final body = approved
|
|
? '$actorName approved your leave request.'
|
|
: '$actorName rejected your leave request.';
|
|
|
|
final dataPayload = <String, dynamic>{
|
|
'type': approved ? 'leave_approved' : 'leave_rejected',
|
|
'leave_id': leaveId,
|
|
};
|
|
|
|
await _client.from('notifications').insert({
|
|
'user_id': userId,
|
|
'actor_id': actorId,
|
|
'type': approved ? 'leave_approved' : 'leave_rejected',
|
|
'leave_id': leaveId,
|
|
});
|
|
|
|
await _client.functions.invoke(
|
|
'send_fcm',
|
|
body: {
|
|
'user_ids': [userId],
|
|
'title': title,
|
|
'body': body,
|
|
'data': dataPayload,
|
|
},
|
|
);
|
|
} catch (_) {
|
|
// non-fatal
|
|
}
|
|
}
|
|
|
|
/// Cancel an approved leave.
|
|
Future<void> cancelLeave(String leaveId) async {
|
|
await _client
|
|
.from('leave_of_absence')
|
|
.update({'status': 'cancelled'})
|
|
.eq('id', leaveId);
|
|
}
|
|
|
|
Future<List<String>> _fetchRoleUserIds({
|
|
required List<String> roles,
|
|
required String? excludeUserId,
|
|
}) async {
|
|
try {
|
|
final data = await _client
|
|
.from('profiles')
|
|
.select('id, role')
|
|
.inFilter('role', roles);
|
|
final rows = data as List<dynamic>;
|
|
final ids = rows
|
|
.map((row) => row['id'] as String?)
|
|
.whereType<String>()
|
|
.where((id) => id.isNotEmpty && id != excludeUserId)
|
|
.toList();
|
|
return ids;
|
|
} catch (_) {
|
|
return [];
|
|
}
|
|
}
|
|
}
|