Pass Slip and Leave approval/rejection push notifications
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
@@ -74,7 +75,7 @@ class LeaveController {
|
||||
required bool autoApprove,
|
||||
}) async {
|
||||
final uid = _client.auth.currentUser!.id;
|
||||
await _client.from('leave_of_absence').insert({
|
||||
final payload = {
|
||||
'user_id': uid,
|
||||
'leave_type': leaveType,
|
||||
'justification': justification,
|
||||
@@ -82,23 +83,184 @@ class LeaveController {
|
||||
'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.
|
||||
@@ -108,4 +270,25 @@ class LeaveController {
|
||||
.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 [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user