Fix stale Staff On Site count after sign-out.
Deploy Ladill Frontdesk / deploy (push) Successful in 52s
Deploy Ladill Frontdesk / deploy (push) Successful in 52s
Compute staff presence stats outside the dashboard cache so counts update immediately when employees sign out. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -4,8 +4,8 @@ namespace App\Http\Controllers\Frontdesk;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Frontdesk\Concerns\ScopesToAccount;
|
||||
use App\Models\Employee;
|
||||
use App\Models\EmployeePresence;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Visit;
|
||||
use App\Services\Frontdesk\OrganizationResolver;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -28,27 +28,15 @@ class DashboardController extends Controller
|
||||
$branchScope = app(OrganizationResolver::class)->branchScope($this->member($request));
|
||||
$cacheKey = "fd:dashboard:{$owner}:{$organization->id}:".($branchScope ?? 'all');
|
||||
|
||||
$stats = Cache::remember($cacheKey, 60, function () use ($visitQuery, $owner, $organization, $branchScope) {
|
||||
$stats = Cache::remember($cacheKey, 60, function () use ($visitQuery) {
|
||||
$today = (clone $visitQuery)->where(function ($q) {
|
||||
$q->whereDate('checked_in_at', today())
|
||||
->orWhereDate('scheduled_at', today());
|
||||
});
|
||||
|
||||
$employeeIds = Employee::owned($owner)
|
||||
->where('organization_id', $organization->id)
|
||||
->where('active', true);
|
||||
if ($branchScope !== null) {
|
||||
$employeeIds->where('branch_id', $branchScope);
|
||||
}
|
||||
$employeeIds = $employeeIds->pluck('id');
|
||||
|
||||
$presenceQuery = EmployeePresence::query()->whereIn('employee_id', $employeeIds);
|
||||
|
||||
return [
|
||||
'visitors_today' => (clone $today)->count(),
|
||||
'currently_inside' => (clone $visitQuery)->currentlyInside()->count(),
|
||||
'staff_on_site' => (clone $presenceQuery)->where('status', EmployeePresence::STATUS_ON_SITE)->count(),
|
||||
'staff_stepped_out' => (clone $presenceQuery)->where('status', EmployeePresence::STATUS_STEPPED_OUT)->count(),
|
||||
'expected_arrivals' => (clone $visitQuery)->whereIn('status', [
|
||||
Visit::STATUS_EXPECTED,
|
||||
Visit::STATUS_SCHEDULED,
|
||||
@@ -71,6 +59,8 @@ class DashboardController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
$stats = array_merge($stats, $this->staffPresenceStats($request, $owner, $organization));
|
||||
|
||||
$currentVisitors = (clone $visitQuery)->currentlyInside()
|
||||
->with(['visitor', 'host'])
|
||||
->latest('checked_in_at')
|
||||
@@ -112,4 +102,21 @@ class DashboardController extends Controller
|
||||
'stats', 'currentVisitors', 'expectedVisitors', 'pendingApprovals', 'staffSteppedOut', 'organization',
|
||||
));
|
||||
}
|
||||
|
||||
/** @return array{staff_on_site: int, staff_stepped_out: int} */
|
||||
protected function staffPresenceStats(Request $request, string $owner, Organization $organization): array
|
||||
{
|
||||
$query = EmployeePresence::query()
|
||||
->whereHas('employee', function ($q) use ($owner, $organization, $request) {
|
||||
$q->owned($owner)
|
||||
->where('organization_id', $organization->id)
|
||||
->where('active', true);
|
||||
$this->scopeToBranch($request, $q, 'branch_id');
|
||||
});
|
||||
|
||||
return [
|
||||
'staff_on_site' => (clone $query)->where('status', EmployeePresence::STATUS_ON_SITE)->count(),
|
||||
'staff_stepped_out' => (clone $query)->where('status', EmployeePresence::STATUS_STEPPED_OUT)->count(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user