feat: enhance FastRequest functionality with shift scheduling and improve payment callback handling
This commit is contained in:
@@ -156,12 +156,13 @@ class CaseService
|
||||
->where('invoices.paid_at', '>=', $monthStart);
|
||||
|
||||
$nextShift = $isDoctor ? $this->shiftScheduleService->nextShiftStart($user, now()) : null;
|
||||
$onShift = $isDoctor && $this->shiftScheduleService->isOnShift($user, now());
|
||||
|
||||
return [
|
||||
'pending_cases' => $this->pendingForUser($user)->count(),
|
||||
'monthly_reviewed' => $reviewedQuery->count(),
|
||||
'monthly_amount' => (int) $amountQuery->sum('invoices.amount'),
|
||||
'next_shift_hours' => $nextShift ? (int) ceil(now()->diffInMinutes($nextShift, true) / 60) : null,
|
||||
'next_shift_hours' => $onShift ? 0 : ($nextShift ? (int) ceil(now()->diffInMinutes($nextShift, true) / 60) : null),
|
||||
'is_doctor' => $isDoctor,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ class FastRequestService
|
||||
protected function resolvePet(User $requester, array $data): Pet
|
||||
{
|
||||
if (!empty($data['pet_id'])) {
|
||||
return Pet::findOrFail($data['pet_id']);
|
||||
return $requester->pets()->findOrFail($data['pet_id']);
|
||||
}
|
||||
|
||||
$pet = $data['pet'];
|
||||
|
||||
@@ -60,12 +60,17 @@ class ShiftScheduleService
|
||||
->get();
|
||||
}
|
||||
|
||||
public function isOnShift(User $doctor, Carbon $moment): bool
|
||||
{
|
||||
return $this->shiftsCoveringMoment($moment)->contains('user_id', $doctor->id);
|
||||
}
|
||||
|
||||
public function autoSelectDoctor(Carbon $moment): ?User
|
||||
{
|
||||
$doctorIds = $this->shiftsCoveringMoment($moment)->pluck('user_id')->filter()->unique();
|
||||
|
||||
if ($doctorIds->isEmpty()) {
|
||||
return null;
|
||||
return $this->doctorWithNearestShift($moment);
|
||||
}
|
||||
|
||||
return User::where('level', UserEnum::Doctor->value)
|
||||
@@ -81,6 +86,20 @@ class ShiftScheduleService
|
||||
->first();
|
||||
}
|
||||
|
||||
// fallback when nobody is on shift: the doctor whose next shift starts soonest
|
||||
public function doctorWithNearestShift(Carbon $from): ?User
|
||||
{
|
||||
$doctorIds = Shift::active()->whereNotNull('user_id')->distinct()->pluck('user_id');
|
||||
|
||||
return User::where('level', UserEnum::Doctor->value)
|
||||
->whereIn('id', $doctorIds)
|
||||
->get()
|
||||
->map(fn (User $doctor) => ['doctor' => $doctor, 'start' => $this->nextShiftStart($doctor, $from)])
|
||||
->filter(fn (array $item) => $item['start'] !== null)
|
||||
->sortBy('start')
|
||||
->first()['doctor'] ?? null;
|
||||
}
|
||||
|
||||
public function nextShiftStart(User $doctor, Carbon $from): ?Carbon
|
||||
{
|
||||
$shifts = Shift::active()->where('user_id', $doctor->id)->get();
|
||||
|
||||
Reference in New Issue
Block a user