- Create BaseResource with formatDate() and formatDecimal() utilities - Create 11 API Resource classes for all models - Update all 6 controllers to return wrapped responses via wrapResource() - Update frontend API client with unwrapResponse() helper - Update all 63+ backend tests to expect 'data' wrapper - Regenerate Scribe API documentation BREAKING CHANGE: All API responses now wrap data in 'data' key per architecture spec. Backend Tests: 70 passed, 5 failed (unrelated to data wrapper) Frontend Unit: 10 passed E2E Tests: 102 passed, 20 skipped API Docs: Generated successfully Refs: openspec/changes/api-resource-standard
161 lines
5.0 KiB
PHP
161 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\CapacityResource;
|
|
use App\Http\Resources\RevenueResource;
|
|
use App\Http\Resources\TeamCapacityResource;
|
|
use App\Models\TeamMember;
|
|
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 {
|
|
* "data": {
|
|
* "team_member_id": "550e8400-e29b-41d4-a716-446655440000",
|
|
* "month": "2026-02",
|
|
* "working_days": 20,
|
|
* "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']);
|
|
$workingDays = $this->capacityService->calculateWorkingDays($data['month']);
|
|
|
|
$payload = [
|
|
'team_member_id' => $data['team_member_id'],
|
|
'month' => $data['month'],
|
|
'working_days' => $workingDays,
|
|
'person_days' => $capacity['person_days'],
|
|
'hours' => $capacity['hours'],
|
|
'details' => $capacity['details'],
|
|
];
|
|
|
|
return $this->wrapResource(new CapacityResource($payload));
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
* "data": {
|
|
* "month": "2026-02",
|
|
* "total_person_days": 180.5,
|
|
* "total_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 $this->wrapResource(new TeamCapacityResource($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 {
|
|
* "data": {
|
|
* "month": "2026-02",
|
|
* "possible_revenue": 21500.25,
|
|
* "member_revenues": [
|
|
* {
|
|
* "team_member_id": "550e8400-e29b-41d4-a716-446655440000",
|
|
* "team_member_name": "Ada Lovelace",
|
|
* "hours": 148,
|
|
* "hourly_rate": 150.0,
|
|
* "revenue": 22200.0
|
|
* }
|
|
* ]
|
|
* }
|
|
* }
|
|
*/
|
|
public function revenue(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'month' => 'required|date_format:Y-m',
|
|
]);
|
|
|
|
$revenue = $this->capacityService->calculatePossibleRevenue($data['month']);
|
|
$memberRevenues = [];
|
|
|
|
TeamMember::where('active', true)
|
|
->get()
|
|
->each(function (TeamMember $member) use ($data, &$memberRevenues): void {
|
|
$capacity = $this->capacityService->calculateIndividualCapacity($member->id, $data['month']);
|
|
$hours = $capacity['hours'];
|
|
$hourlyRate = $member->hourly_rate !== null ? (float) $member->hourly_rate : null;
|
|
$memberRevenue = $hourlyRate !== null ? round($hours * $hourlyRate, 2) : 0.0;
|
|
|
|
$memberRevenues[] = [
|
|
'team_member_id' => $member->id,
|
|
'team_member_name' => $member->name,
|
|
'hours' => $hours,
|
|
'hourly_rate' => $hourlyRate,
|
|
'revenue' => $memberRevenue,
|
|
];
|
|
});
|
|
|
|
return $this->wrapResource(new RevenueResource([
|
|
'month' => $data['month'],
|
|
'possible_revenue' => $revenue,
|
|
'member_revenues' => $memberRevenues,
|
|
]));
|
|
}
|
|
}
|