refactor: extract staff presence helpers
- Extract activeLeaveByUser, activePassSlipByUser, and approvedLeaveDays to staff_presence.dart utility module - Replace inline indexing loops in dashboard_screen.dart with helper function calls - Behavior unchanged, tests added covering all functions Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
@@ -44,6 +44,7 @@ import '../../widgets/app_metric_card.dart';
|
||||
import '../../widgets/app_page_header.dart';
|
||||
import '../../widgets/app_section_header.dart';
|
||||
import '../../utils/app_time.dart';
|
||||
import '../../utils/staff_presence.dart' as staff_presence;
|
||||
|
||||
class DashboardMetrics {
|
||||
DashboardMetrics({
|
||||
@@ -369,22 +370,10 @@ final dashboardMetricsProvider = Provider<AsyncValue<DashboardMetrics>>((ref) {
|
||||
}
|
||||
|
||||
// Index today's leaves by user.
|
||||
final todayLeaveByUser = <String, LeaveOfAbsence>{};
|
||||
for (final l in allLeaves) {
|
||||
if (l.status == 'approved' &&
|
||||
!l.startTime.isAfter(now) &&
|
||||
l.endTime.isAfter(now)) {
|
||||
todayLeaveByUser[l.userId] = l;
|
||||
}
|
||||
}
|
||||
final todayLeaveByUser = staff_presence.activeLeaveByUser(allLeaves, now);
|
||||
|
||||
// Index active pass slips by user.
|
||||
final activePassSlipByUser = <String, PassSlip>{};
|
||||
for (final slip in allPassSlips) {
|
||||
if (slip.isActive) {
|
||||
activePassSlipByUser[slip.userId] = slip;
|
||||
}
|
||||
}
|
||||
final activePassSlipByUser = staff_presence.activePassSlipByUser(allPassSlips);
|
||||
|
||||
final noon = DateTime(now.year, now.month, now.day, 12, 0);
|
||||
final onePM = DateTime(now.year, now.month, now.day, 13, 0);
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import 'package:tasq/models/leave_of_absence.model.dart';
|
||||
import 'package:tasq/models/pass_slip.model.dart';
|
||||
import 'programmer_daily_time.dart';
|
||||
|
||||
/// Index approved leaves covering the given moment by user ID.
|
||||
///
|
||||
/// Filters to leaves where:
|
||||
/// - status == 'approved'
|
||||
/// - startTime <= now < endTime
|
||||
///
|
||||
/// Returns a map from userId to the leave record.
|
||||
Map<String, LeaveOfAbsence> activeLeaveByUser(
|
||||
List<LeaveOfAbsence> leaves,
|
||||
DateTime now,
|
||||
) {
|
||||
final result = <String, LeaveOfAbsence>{};
|
||||
for (final leave in leaves) {
|
||||
if (leave.status == 'approved' &&
|
||||
!leave.startTime.isAfter(now) &&
|
||||
leave.endTime.isAfter(now)) {
|
||||
result[leave.userId] = leave;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Index active pass slips by user ID.
|
||||
///
|
||||
/// Filters to slips where slip.isActive is true.
|
||||
///
|
||||
/// Returns a map from userId to the pass slip record.
|
||||
Map<String, PassSlip> activePassSlipByUser(List<PassSlip> slips) {
|
||||
final result = <String, PassSlip>{};
|
||||
for (final slip in slips) {
|
||||
if (slip.isActive) {
|
||||
result[slip.userId] = slip;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Get the set of Manila calendar days covered by approved leaves for a user.
|
||||
///
|
||||
/// Iterates through all approved leaves for the given user and collects the
|
||||
/// set of Manila day keys (as DateTime.utc(year, month, day)) from each
|
||||
/// leave's start date through end date, inclusive.
|
||||
Set<DateTime> approvedLeaveDays(
|
||||
List<LeaveOfAbsence> leaves,
|
||||
String userId,
|
||||
) {
|
||||
final result = <DateTime>{};
|
||||
for (final leave in leaves) {
|
||||
if (leave.userId == userId && leave.status == 'approved') {
|
||||
var current = manilaDay(leave.startTime);
|
||||
final endDay = manilaDay(leave.endTime);
|
||||
while (!current.isAfter(endDay)) {
|
||||
result.add(current);
|
||||
current = current.add(Duration(days: 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user