fix(capacity): stabilize PTO flows and calendar consistency
Make PTO creation immediately approved, add PTO deletion, and ensure cache invalidation updates individual/team/revenue capacity consistently. Harden holiday duplicate handling (422), support PTO-day availability overrides without disabling edits, and align tests plus OpenSpec artifacts with the new behavior.
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Http\Resources\PtoResource;
|
||||
use App\Models\Pto;
|
||||
use App\Services\CapacityService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\UniqueConstraintViolationException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@@ -68,7 +69,7 @@ class PtoController extends Controller
|
||||
/**
|
||||
* Request PTO
|
||||
*
|
||||
* Create a PTO request for a team member and keep it in pending status.
|
||||
* Create a PTO request for a team member and approve it immediately.
|
||||
*
|
||||
* @group Capacity Planning
|
||||
*
|
||||
@@ -83,7 +84,7 @@ class PtoController extends Controller
|
||||
* "team_member_id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
* "start_date": "2026-02-10",
|
||||
* "end_date": "2026-02-12",
|
||||
* "status": "pending",
|
||||
* "status": "approved",
|
||||
* "reason": "Family travel"
|
||||
* }
|
||||
* }
|
||||
@@ -97,10 +98,26 @@ class PtoController extends Controller
|
||||
'reason' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$pto = Pto::create(array_merge($data, ['status' => 'pending']));
|
||||
$pto->load('teamMember');
|
||||
try {
|
||||
$pto = Pto::create(array_merge($data, ['status' => 'approved']));
|
||||
$months = $this->monthsBetween($pto->start_date, $pto->end_date);
|
||||
$this->capacityService->forgetCapacityCacheForTeamMember($pto->team_member_id, $months);
|
||||
|
||||
return $this->wrapResource(new PtoResource($pto), 201);
|
||||
foreach ($months as $month) {
|
||||
$this->capacityService->forgetCapacityCacheForMonth($month);
|
||||
}
|
||||
|
||||
$pto->load('teamMember');
|
||||
|
||||
return $this->wrapResource(new PtoResource($pto), 201);
|
||||
} catch (UniqueConstraintViolationException $e) {
|
||||
return response()->json([
|
||||
'message' => 'A PTO request with these details already exists.',
|
||||
'errors' => [
|
||||
'general' => ['A PTO request with these details already exists.'],
|
||||
],
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,6 +145,10 @@ class PtoController extends Controller
|
||||
$pto->save();
|
||||
$months = $this->monthsBetween($pto->start_date, $pto->end_date);
|
||||
$this->capacityService->forgetCapacityCacheForTeamMember($pto->team_member_id, $months);
|
||||
|
||||
foreach ($months as $month) {
|
||||
$this->capacityService->forgetCapacityCacheForMonth($month);
|
||||
}
|
||||
}
|
||||
|
||||
$pto->load('teamMember');
|
||||
@@ -135,6 +156,27 @@ class PtoController extends Controller
|
||||
return $this->wrapResource(new PtoResource($pto));
|
||||
}
|
||||
|
||||
public function destroy(string $id): JsonResponse
|
||||
{
|
||||
$pto = Pto::find($id);
|
||||
|
||||
if (! $pto) {
|
||||
return response()->json(['message' => 'PTO not found'], 404);
|
||||
}
|
||||
|
||||
$months = $this->monthsBetween($pto->start_date, $pto->end_date);
|
||||
$teamMemberId = $pto->team_member_id;
|
||||
$pto->delete();
|
||||
|
||||
$this->capacityService->forgetCapacityCacheForTeamMember($teamMemberId, $months);
|
||||
|
||||
foreach ($months as $month) {
|
||||
$this->capacityService->forgetCapacityCacheForMonth($month);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'PTO deleted']);
|
||||
}
|
||||
|
||||
private function monthsBetween(Carbon|string $start, Carbon|string $end): array
|
||||
{
|
||||
$startMonth = Carbon::create($start)->copy()->startOfMonth();
|
||||
|
||||
Reference in New Issue
Block a user