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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -61,12 +61,11 @@ class CapacityService
|
||||
continue;
|
||||
}
|
||||
|
||||
$availability = $availabilities->get($date, 1.0);
|
||||
$isPto = in_array($date, $ptoDates, true);
|
||||
|
||||
if ($isPto) {
|
||||
$availability = 0.0;
|
||||
}
|
||||
$hasAvailabilityOverride = $availabilities->has($date);
|
||||
$availability = $hasAvailabilityOverride
|
||||
? (float) $availabilities->get($date)
|
||||
: ($isPto ? 0.0 : 1.0);
|
||||
|
||||
$details[] = [
|
||||
'date' => $date,
|
||||
@@ -198,13 +197,14 @@ class CapacityService
|
||||
|
||||
foreach ($months as $month) {
|
||||
$tags = $this->getCapacityCacheTags($month, "team_member:{$teamMemberId}");
|
||||
$key = $this->buildCacheKey($month, $teamMemberId);
|
||||
|
||||
// Always forget from array store (used in tests and as fallback)
|
||||
Cache::store('array')->forget($key);
|
||||
|
||||
if ($useRedis) {
|
||||
$this->flushCapacityTags($tags);
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->forgetCapacity($this->buildCacheKey($month, $teamMemberId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,17 +213,16 @@ class CapacityService
|
||||
*/
|
||||
public function forgetCapacityCacheForMonth(string $month): void
|
||||
{
|
||||
// Always forget from array store (used in tests and as fallback)
|
||||
foreach (TeamMember::pluck('id') as $teamMemberId) {
|
||||
Cache::store('array')->forget($this->buildCacheKey($month, $teamMemberId));
|
||||
}
|
||||
Cache::store('array')->forget($this->buildCacheKey($month, 'team'));
|
||||
Cache::store('array')->forget($this->buildCacheKey($month, 'revenue'));
|
||||
|
||||
if ($this->redisAvailable()) {
|
||||
$this->flushCapacityTags($this->getCapacityCacheTags($month));
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (TeamMember::pluck('id') as $teamMemberId) {
|
||||
$this->forgetCapacity($this->buildCacheKey($month, $teamMemberId));
|
||||
}
|
||||
|
||||
$this->forgetCapacity($this->buildCacheKey($month, 'team'));
|
||||
$this->forgetCapacity($this->buildCacheKey($month, 'revenue'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,9 +3,13 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\TeamMember;
|
||||
use Closure;
|
||||
use DateTimeInterface;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Team Member Service
|
||||
@@ -14,28 +18,36 @@ use Illuminate\Support\Facades\Validator;
|
||||
*/
|
||||
class TeamMemberService
|
||||
{
|
||||
private ?bool $redisAvailable = null;
|
||||
|
||||
/**
|
||||
* Get all team members with optional filtering.
|
||||
*
|
||||
* @param bool|null $active Filter by active status
|
||||
* @param bool|null $active Filter by active status
|
||||
* @return Collection<TeamMember>
|
||||
*/
|
||||
public function getAll(?bool $active = null): Collection
|
||||
{
|
||||
$query = TeamMember::with('role');
|
||||
/** @var Collection<TeamMember> $teamMembers */
|
||||
$teamMembers = $this->rememberTeamMembers(
|
||||
$this->buildTeamMembersCacheKey($active),
|
||||
now()->addHour(),
|
||||
function () use ($active): Collection {
|
||||
$query = TeamMember::with('role');
|
||||
|
||||
if ($active !== null) {
|
||||
$query->where('active', $active);
|
||||
}
|
||||
if ($active !== null) {
|
||||
$query->where('active', $active);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
return $query->get();
|
||||
}
|
||||
);
|
||||
|
||||
return $teamMembers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a team member by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return TeamMember|null
|
||||
*/
|
||||
public function findById(string $id): ?TeamMember
|
||||
{
|
||||
@@ -45,8 +57,6 @@ class TeamMemberService
|
||||
/**
|
||||
* Create a new team member.
|
||||
*
|
||||
* @param array $data
|
||||
* @return TeamMember
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(array $data): TeamMember
|
||||
@@ -72,6 +82,7 @@ class TeamMemberService
|
||||
]);
|
||||
|
||||
$teamMember->load('role');
|
||||
$this->forgetTeamMembersCache();
|
||||
|
||||
return $teamMember;
|
||||
}
|
||||
@@ -79,9 +90,6 @@ class TeamMemberService
|
||||
/**
|
||||
* Update an existing team member.
|
||||
*
|
||||
* @param TeamMember $teamMember
|
||||
* @param array $data
|
||||
* @return TeamMember
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function update(TeamMember $teamMember, array $data): TeamMember
|
||||
@@ -101,6 +109,7 @@ class TeamMemberService
|
||||
|
||||
$teamMember->update($data);
|
||||
$teamMember->load('role');
|
||||
$this->forgetTeamMembersCache();
|
||||
|
||||
return $teamMember;
|
||||
}
|
||||
@@ -108,8 +117,6 @@ class TeamMemberService
|
||||
/**
|
||||
* Delete a team member.
|
||||
*
|
||||
* @param TeamMember $teamMember
|
||||
* @return void
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function delete(TeamMember $teamMember): void
|
||||
@@ -131,12 +138,12 @@ class TeamMemberService
|
||||
}
|
||||
|
||||
$teamMember->delete();
|
||||
$this->forgetTeamMembersCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a team member can be deleted.
|
||||
*
|
||||
* @param TeamMember $teamMember
|
||||
* @return array{canDelete: bool, reason?: string}
|
||||
*/
|
||||
public function canDelete(TeamMember $teamMember): array
|
||||
@@ -157,4 +164,77 @@ class TeamMemberService
|
||||
|
||||
return ['canDelete' => true];
|
||||
}
|
||||
|
||||
private function buildTeamMembersCacheKey(?bool $active): string
|
||||
{
|
||||
if ($active === null) {
|
||||
return 'team-members:all';
|
||||
}
|
||||
|
||||
return $active ? 'team-members:active' : 'team-members:inactive';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure(): Collection<TeamMember> $callback
|
||||
* @return Collection<TeamMember>
|
||||
*/
|
||||
private function rememberTeamMembers(string $key, DateTimeInterface|int $ttl, Closure $callback): Collection
|
||||
{
|
||||
if (! $this->redisAvailable()) {
|
||||
/** @var Collection<TeamMember> $payload */
|
||||
$payload = Cache::store('array')->remember($key, $ttl, $callback);
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var Collection<TeamMember> $payload */
|
||||
$payload = Cache::store('redis')->remember($key, $ttl, $callback);
|
||||
|
||||
return $payload;
|
||||
} catch (Throwable) {
|
||||
/** @var Collection<TeamMember> $payload */
|
||||
$payload = Cache::store('array')->remember($key, $ttl, $callback);
|
||||
|
||||
return $payload;
|
||||
}
|
||||
}
|
||||
|
||||
private function forgetTeamMembersCache(): void
|
||||
{
|
||||
Cache::store('array')->forget($this->buildTeamMembersCacheKey(null));
|
||||
Cache::store('array')->forget($this->buildTeamMembersCacheKey(true));
|
||||
Cache::store('array')->forget($this->buildTeamMembersCacheKey(false));
|
||||
|
||||
if (! $this->redisAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Cache::store('redis')->forget($this->buildTeamMembersCacheKey(null));
|
||||
Cache::store('redis')->forget($this->buildTeamMembersCacheKey(true));
|
||||
Cache::store('redis')->forget($this->buildTeamMembersCacheKey(false));
|
||||
} catch (Throwable) {
|
||||
// Ignore cache failures when Redis is unavailable.
|
||||
}
|
||||
}
|
||||
|
||||
private function redisAvailable(): bool
|
||||
{
|
||||
if ($this->redisAvailable !== null) {
|
||||
return $this->redisAvailable;
|
||||
}
|
||||
|
||||
if (! config('cache.stores.redis')) {
|
||||
return $this->redisAvailable = false;
|
||||
}
|
||||
|
||||
$client = config('database.redis.client', 'phpredis');
|
||||
|
||||
if ($client === 'predis') {
|
||||
return $this->redisAvailable = class_exists('Predis\\Client');
|
||||
}
|
||||
|
||||
return $this->redisAvailable = extension_loaded('redis');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user