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:
2026-02-19 22:47:39 -05:00
parent 0a9fdd248b
commit b821713cc7
21 changed files with 1081 additions and 128 deletions

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Http\Resources\HolidayResource;
use App\Models\Holiday;
use App\Services\CapacityService;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -65,6 +66,7 @@ class HolidayController extends Controller
* "description": "Office closed"
* }
* }
* @response 422 {"message":"A holiday already exists for this date.","errors":{"date":["A holiday already exists for this date."]}}
*/
public function store(Request $request): JsonResponse
{
@@ -74,10 +76,19 @@ class HolidayController extends Controller
'description' => 'nullable|string',
]);
$holiday = Holiday::create($data);
$this->capacityService->forgetCapacityCacheForMonth($holiday->date->format('Y-m'));
try {
$holiday = Holiday::create($data);
$this->capacityService->forgetCapacityCacheForMonth($holiday->date->format('Y-m'));
return $this->wrapResource(new HolidayResource($holiday), 201);
return $this->wrapResource(new HolidayResource($holiday), 201);
} catch (UniqueConstraintViolationException $e) {
return response()->json([
'message' => 'A holiday already exists for this date.',
'errors' => [
'date' => ['A holiday already exists for this date.'],
],
], 422);
}
}
/**

View File

@@ -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();