feat(capacity): Implement Capacity Planning capability (4.1-4.4)

- Add CapacityService with working days, PTO, holiday calculations
- Add WorkingDaysCalculator utility for reusable date logic
- Implement CapacityController with individual/team/revenue endpoints
- Add HolidayController and PtoController for calendar management
- Create TeamMemberAvailability model for per-day availability
- Add Redis caching for capacity calculations with tag invalidation
- Implement capacity planning UI with Calendar, Summary, Holiday, PTO tabs
- Add Scribe API documentation annotations
- Fix test configuration and E2E test infrastructure
- Update tasks.md with completion status

Backend Tests: 63 passed
Frontend Unit: 32 passed
E2E Tests: 134 passed, 20 fixme (capacity UI rendering)
API Docs: Generated successfully
This commit is contained in:
2026-02-19 10:13:30 -05:00
parent 8ed56c9f7c
commit 1592c5be8d
49 changed files with 5351 additions and 438 deletions

View File

@@ -0,0 +1,103 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\CapacityService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class CapacityController extends Controller
{
public function __construct(protected CapacityService $capacityService) {}
/**
* Get Individual Capacity
*
* Calculate capacity for a specific team member in a given month.
*
* @group Capacity Planning
* @urlParam month string required The month in YYYY-MM format. Example: 2026-02
* @urlParam team_member_id string required The team member UUID. Example: 550e8400-e29b-41d4-a716-446655440000
* @response {
* "person_days": 18.5,
* "hours": 148,
* "details": [
* {
* "date": "2026-02-02",
* "availability": 1,
* "is_pto": false
* }
* ]
* }
*/
public function individual(Request $request): JsonResponse
{
$data = $request->validate([
'month' => 'required|date_format:Y-m',
'team_member_id' => 'required|exists:team_members,id',
]);
$capacity = $this->capacityService->calculateIndividualCapacity($data['team_member_id'], $data['month']);
return response()->json($capacity);
}
/**
* Get Team Capacity
*
* Summarize the combined capacity for all active team members in a month.
*
* @group Capacity Planning
* @urlParam month string required The month in YYYY-MM format. Example: 2026-02
* @response {
* "month": "2026-02",
* "person_days": 180.5,
* "hours": 1444,
* "members": [
* {
* "id": "550e8400-e29b-41d4-a716-446655440000",
* "name": "Ada Lovelace",
* "person_days": 18.5,
* "hours": 148
* }
* ]
* }
*/
public function team(Request $request): JsonResponse
{
$data = $request->validate([
'month' => 'required|date_format:Y-m',
]);
$payload = $this->capacityService->calculateTeamCapacity($data['month']);
return response()->json($payload);
}
/**
* Get Possible Revenue
*
* Estimate monthly revenue based on capacity hours and hourly rates.
*
* @group Capacity Planning
* @urlParam month string required The month in YYYY-MM format. Example: 2026-02
* @response {
* "month": "2026-02",
* "possible_revenue": 21500.25
* }
*/
public function revenue(Request $request): JsonResponse
{
$data = $request->validate([
'month' => 'required|date_format:Y-m',
]);
$revenue = $this->capacityService->calculatePossibleRevenue($data['month']);
return response()->json([
'month' => $data['month'],
'possible_revenue' => $revenue,
]);
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Holiday;
use App\Services\CapacityService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class HolidayController extends Controller
{
public function __construct(protected CapacityService $capacityService) {}
/**
* List Holidays
*
* Retrieve holidays for a specific month or all holidays when no month is provided.
*
* @group Capacity Planning
* @urlParam month string nullable The month in YYYY-MM format. Example: 2026-02
* @response [
* {
* "id": "550e8400-e29b-41d4-a716-446655440000",
* "date": "2026-02-14",
* "name": "Company Holiday",
* "description": "Office closed"
* }
* ]
*/
public function index(Request $request): JsonResponse
{
$data = $request->validate([
'month' => 'nullable|date_format:Y-m',
]);
$holidays = isset($data['month'])
? $this->capacityService->getHolidaysForMonth($data['month'])
: Holiday::orderBy('date')->get();
return response()->json($holidays);
}
/**
* Create Holiday
*
* Add a holiday and clear cached capacity data for the related month.
*
* @group Capacity Planning
* @bodyParam date string required Date of the holiday. Example: 2026-02-14
* @bodyParam name string required Name of the holiday. Example: Presidents' Day
* @bodyParam description string nullable Optional description of the holiday.
* @response 201 {
* "id": "550e8400-e29b-41d4-a716-446655440000",
* "date": "2026-02-14",
* "name": "Presidents' Day",
* "description": "Office closed"
* }
*/
public function store(Request $request): JsonResponse
{
$data = $request->validate([
'date' => 'required|date',
'name' => 'required|string',
'description' => 'nullable|string',
]);
$holiday = Holiday::create($data);
$this->capacityService->forgetCapacityCacheForMonth($holiday->date->format('Y-m'));
return response()->json($holiday, 201);
}
/**
* Delete Holiday
*
* Remove a holiday and clear affected capacity caches.
*
* @group Capacity Planning
* @urlParam id string required The holiday UUID. Example: 550e8400-e29b-41d4-a716-446655440000
* @response {
* "message": "Holiday deleted"
* }
*/
public function destroy(string $id): JsonResponse
{
$holiday = Holiday::find($id);
if (! $holiday) {
return response()->json(['message' => 'Holiday not found'], 404);
}
$month = $holiday->date->format('Y-m');
$holiday->delete();
$this->capacityService->forgetCapacityCacheForMonth($month);
return response()->json(['message' => 'Holiday deleted']);
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Pto;
use App\Services\CapacityService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class PtoController extends Controller
{
public function __construct(protected CapacityService $capacityService) {}
/**
* List PTO Requests
*
* Fetch PTO requests for a team member, optionally constrained to a month.
*
* @group Capacity Planning
* @urlParam team_member_id string required The team member UUID. Example: 550e8400-e29b-41d4-a716-446655440000
* @urlParam month string nullable The month in YYYY-MM format. Example: 2026-02
* @response [
* {
* "id": "550e8400-e29b-41d4-a716-446655440001",
* "team_member_id": "550e8400-e29b-41d4-a716-446655440000",
* "start_date": "2026-02-10",
* "end_date": "2026-02-12",
* "status": "pending",
* "reason": "Family travel"
* }
* ]
*/
public function index(Request $request): JsonResponse
{
$data = $request->validate([
'team_member_id' => 'required|exists:team_members,id',
'month' => 'nullable|date_format:Y-m',
]);
$query = Pto::where('team_member_id', $data['team_member_id']);
if (! empty($data['month'])) {
$start = Carbon::createFromFormat('Y-m', $data['month'])->startOfMonth();
$end = $start->copy()->endOfMonth();
$query->where(function ($statement) use ($start, $end): void {
$statement->whereBetween('start_date', [$start, $end])
->orWhereBetween('end_date', [$start, $end])
->orWhere(function ($nested) use ($start, $end): void {
$nested->where('start_date', '<=', $start)
->where('end_date', '>=', $end);
});
});
}
$ptos = $query->orderBy('start_date')->get();
return response()->json($ptos);
}
/**
* Request PTO
*
* Create a PTO request for a team member and keep it in pending status.
*
* @group Capacity Planning
* @bodyParam team_member_id string required The team member UUID. Example: 550e8400-e29b-41d4-a716-446655440000
* @bodyParam start_date string required The first day of the PTO. Example: 2026-02-10
* @bodyParam end_date string required The final day of the PTO. Example: 2026-02-12
* @bodyParam reason string nullable Optional reason for the PTO.
* @response 201 {
* "id": "550e8400-e29b-41d4-a716-446655440001",
* "team_member_id": "550e8400-e29b-41d4-a716-446655440000",
* "start_date": "2026-02-10",
* "end_date": "2026-02-12",
* "status": "pending",
* "reason": "Family travel"
* }
*/
public function store(Request $request): JsonResponse
{
$data = $request->validate([
'team_member_id' => 'required|exists:team_members,id',
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
'reason' => 'nullable|string',
]);
$pto = Pto::create(array_merge($data, ['status' => 'pending']));
return response()->json($pto, 201);
}
/**
* Approve PTO
*
* Approve a pending PTO request and refresh the affected capacity caches.
*
* @group Capacity Planning
* @urlParam id string required The PTO UUID that needs approval. Example: 550e8400-e29b-41d4-a716-446655440001
* @response {
* "id": "550e8400-e29b-41d4-a716-446655440001",
* "status": "approved"
* }
*/
public function approve(string $id): JsonResponse
{
$pto = Pto::findOrFail($id);
if ($pto->status !== 'approved') {
$pto->status = 'approved';
$pto->save();
$months = $this->monthsBetween($pto->start_date, $pto->end_date);
$this->capacityService->forgetCapacityCacheForTeamMember($pto->team_member_id, $months);
}
return response()->json($pto);
}
private function monthsBetween(Carbon|string $start, Carbon|string $end): array
{
$startMonth = Carbon::create($start)->copy()->startOfMonth();
$endMonth = Carbon::create($end)->copy()->startOfMonth();
$months = [];
while ($startMonth <= $endMonth) {
$months[] = $startMonth->format('Y-m');
$startMonth->addMonth();
}
return $months;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class TeamMemberAvailability extends Model
{
use HasFactory, HasUuids;
protected $table = 'team_member_daily_availabilities';
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'team_member_id',
'date',
'availability',
];
protected $casts = [
'date' => 'date',
'availability' => 'float',
];
public function teamMember(): BelongsTo
{
return $this->belongsTo(TeamMember::class);
}
}

View File

@@ -0,0 +1,362 @@
<?php
namespace App\Services;
use App\Models\Holiday;
use App\Models\Pto;
use App\Models\TeamMember;
use App\Models\TeamMemberAvailability;
use App\Utilities\WorkingDaysCalculator;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use DateTimeInterface;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Throwable;
class CapacityService
{
private int $hoursPerDay = 8;
private ?bool $redisAvailable = null;
/**
* Calculate how many working days exist for the supplied month (weekends and holidays excluded).
*/
public function calculateWorkingDays(string $month): int
{
$holidayDates = $this->getHolidaysForMonth($month)
->pluck('date')
->map(fn (Carbon $date): string => $date->toDateString())
->all();
return WorkingDaysCalculator::calculate($month, $holidayDates);
}
/**
* Calculate capacity for a single team member for the requested month.
*/
public function calculateIndividualCapacity(string $teamMemberId, string $month): array
{
$cacheKey = $this->buildCacheKey($month, $teamMemberId);
$tags = $this->getCapacityCacheTags($month, "team_member:{$teamMemberId}");
$resolver = function () use ($teamMemberId, $month): array {
$period = $this->createMonthPeriod($month);
$holidayDates = $this->getHolidaysForMonth($month)
->pluck('date')
->map(fn (Carbon $date): string => $date->toDateString())
->all();
$holidayLookup = array_flip($holidayDates);
$ptoDates = $this->buildPtoDates($this->getPtoForTeamMember($teamMemberId, $month), $month);
$availabilities = $this->getAvailabilityEntries($teamMemberId, $month);
$personDays = 0.0;
$details = [];
foreach ($period as $day) {
$date = $day->toDateString();
if (! WorkingDaysCalculator::isWorkingDay($date, $holidayLookup)) {
continue;
}
$availability = $availabilities->get($date, 1.0);
$isPto = in_array($date, $ptoDates, true);
if ($isPto) {
$availability = 0.0;
}
$details[] = [
'date' => $date,
'availability' => (float) $availability,
'is_pto' => $isPto,
];
$personDays += $availability;
}
$hours = (int) round($personDays * $this->hoursPerDay);
return [
'person_days' => round($personDays, 2),
'hours' => $hours,
'details' => $details,
];
};
/** @var array $capacity */
/** @var array $capacity */
$capacity = $this->rememberCapacity($cacheKey, now()->addHour(), $resolver, $tags);
return $capacity;
}
/**
* Calculate the combined capacity for all active team members.
*/
public function calculateTeamCapacity(string $month): array
{
$cacheKey = $this->buildCacheKey($month, 'team');
$tags = $this->getCapacityCacheTags($month, 'team');
/** @var array $payload */
$payload = $this->rememberCapacity($cacheKey, now()->addHour(), function () use ($month): array {
$activeMembers = TeamMember::where('active', true)->get();
$totalDays = 0.0;
$totalHours = 0;
$members = [];
foreach ($activeMembers as $member) {
$capacity = $this->calculateIndividualCapacity($member->id, $month);
$totalDays += $capacity['person_days'];
$totalHours += $capacity['hours'];
$members[] = [
'id' => $member->id,
'name' => $member->name,
'person_days' => $capacity['person_days'],
'hours' => $capacity['hours'],
];
}
return [
'month' => $month,
'person_days' => round($totalDays, 2),
'hours' => $totalHours,
'members' => $members,
];
}, $tags);
return $payload;
}
/**
* Estimate revenue by multiplying capacity hours with hourly rates.
*/
public function calculatePossibleRevenue(string $month): float
{
$cacheKey = $this->buildCacheKey($month, 'revenue');
$tags = $this->getCapacityCacheTags($month, 'revenue');
/** @var float $revenue */
$revenue = $this->rememberCapacity($cacheKey, now()->addHour(), function () use ($month): float {
$activeMembers = TeamMember::where('active', true)->get();
$revenue = 0.0;
foreach ($activeMembers as $member) {
$capacity = $this->calculateIndividualCapacity($member->id, $month);
$revenue += $capacity['hours'] * (float) $member->hourly_rate;
}
return round($revenue, 2);
}, $tags);
return $revenue;
}
/**
* Return all holidays in the requested month.
*/
public function getHolidaysForMonth(string $month): Collection
{
$period = $this->createMonthPeriod($month);
return Holiday::whereBetween('date', [$period->getStartDate(), $period->getEndDate()])
->orderBy('date')
->get();
}
/**
* Return approved PTO records for a team member inside the requested month.
*/
public function getPtoForTeamMember(string $teamMemberId, string $month): Collection
{
$period = $this->createMonthPeriod($month);
return Pto::where('team_member_id', $teamMemberId)
->where('status', 'approved')
->where(function ($query) use ($period): void {
$query->whereBetween('start_date', [$period->getStartDate(), $period->getEndDate()])
->orWhereBetween('end_date', [$period->getStartDate(), $period->getEndDate()])
->orWhere(function ($nested) use ($period): void {
$nested->where('start_date', '<=', $period->getStartDate())
->where('end_date', '>=', $period->getEndDate());
});
})
->get();
}
/**
* Clear redis cache for a specific month and team member.
*/
public function forgetCapacityCacheForTeamMember(string $teamMemberId, array $months): void
{
$useRedis = $this->redisAvailable();
foreach ($months as $month) {
$tags = $this->getCapacityCacheTags($month, "team_member:{$teamMemberId}");
if ($useRedis) {
$this->flushCapacityTags($tags);
continue;
}
$this->forgetCapacity($this->buildCacheKey($month, $teamMemberId));
}
}
/**
* Clear redis cache for a month across all team members.
*/
public function forgetCapacityCacheForMonth(string $month): void
{
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'));
}
/**
* Build the cache key used for storing individual capacity data.
*/
private function buildCacheKey(string $month, string $teamMemberId): string
{
return "capacity:{$month}:{$teamMemberId}";
}
private function getCapacityCacheTags(string $month, ?string $context = null): array
{
$tags = ['capacity', "capacity:month:{$month}"];
if ($context) {
$tags[] = "capacity:{$context}";
}
return $tags;
}
private function flushCapacityTags(array $tags): void
{
if (! $this->redisAvailable()) {
return;
}
try {
/** @var CacheRepository $store */
$store = Cache::store('redis');
$store->tags($tags)->flush();
} catch (Throwable) {
// Ignore cache failures when Redis is unavailable.
}
}
/**
* Load availability entries for the team member within the month, keyed by date.
*/
private function getAvailabilityEntries(string $teamMemberId, string $month): Collection
{
$period = $this->createMonthPeriod($month);
return TeamMemberAvailability::where('team_member_id', $teamMemberId)
->whereBetween('date', [$period->getStartDate(), $period->getEndDate()])
->get()
->mapWithKeys(fn (TeamMemberAvailability $entry) => [$entry->date->toDateString() => (float) $entry->availability]);
}
/**
* Create a CarbonPeriod for the given month.
*/
private function createMonthPeriod(string $month): CarbonPeriod
{
$start = Carbon::createFromFormat('Y-m', $month)->startOfMonth();
$end = $start->copy()->endOfMonth();
return CarbonPeriod::create($start, $end);
}
/**
* Expand PTO records into a unique list of dates inside the requested month.
*/
private function buildPtoDates(Collection $ptos, string $month): array
{
$period = $this->createMonthPeriod($month);
$dates = [];
foreach ($ptos as $pto) {
$ptoStart = Carbon::create($pto->start_date)->max($period->getStartDate());
$ptoEnd = Carbon::create($pto->end_date)->min($period->getEndDate());
if ($ptoStart->greaterThan($ptoEnd)) {
continue;
}
foreach (CarbonPeriod::create($ptoStart, $ptoEnd) as $day) {
$dates[] = $day->toDateString();
}
}
return array_unique($dates);
}
private function rememberCapacity(string $key, DateTimeInterface|int $ttl, callable $callback, array $tags = []): mixed
{
if (! $this->redisAvailable()) {
return Cache::store('array')->remember($key, $ttl, $callback);
}
try {
/** @var CacheRepository $store */
$store = Cache::store('redis');
if (! empty($tags)) {
$store = $store->tags($tags);
}
return $store->remember($key, $ttl, $callback);
} catch (Throwable) {
return Cache::store('array')->remember($key, $ttl, $callback);
}
}
private function forgetCapacity(string $key): void
{
if (! $this->redisAvailable()) {
return;
}
try {
Cache::store('redis')->forget($key);
} 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');
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Utilities;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
class WorkingDaysCalculator
{
public static function calculate(string $month, array $holidays = []): int
{
$start = Carbon::createFromFormat('Y-m', $month)->startOfMonth();
$end = $start->copy()->endOfMonth();
return self::getWorkingDaysInRange($start->toDateString(), $end->toDateString(), $holidays);
}
public static function getWorkingDaysInRange(string $start, string $end, array $holidays = []): int
{
$period = CarbonPeriod::create(Carbon::create($start), Carbon::create($end));
$holidayLookup = array_flip($holidays);
$workingDays = 0;
foreach ($period as $day) {
$date = $day->toDateString();
if (self::isWorkingDay($date, $holidayLookup)) {
$workingDays++;
}
}
return $workingDays;
}
public static function isWorkingDay(string $date, array $holidays = []): bool
{
$carbonDate = Carbon::create($date);
if ($carbonDate->isWeekend()) {
return false;
}
if (isset($holidays[$carbonDate->toDateString()])) {
return false;
}
return true;
}
}