Skip to content

Commit 01f6f0f

Browse files
committed
Add chart endpoints
1 parent aa3c64e commit 01f6f0f

File tree

5 files changed

+463
-0
lines changed

5 files changed

+463
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers\Api\V1;
6+
7+
use App\Enums\Role;
8+
use App\Models\Organization;
9+
use App\Service\DashboardService;
10+
use App\Service\PermissionStore;
11+
use Illuminate\Auth\Access\AuthorizationException;
12+
use Illuminate\Http\JsonResponse;
13+
14+
class ChartController extends Controller
15+
{
16+
/**
17+
* @throws AuthorizationException
18+
*/
19+
public function weeklyProjectOverview(Organization $organization, DashboardService $dashboardService): JsonResponse
20+
{
21+
$this->checkPermission($organization, 'charts:view:own');
22+
$user = $this->user();
23+
24+
$weeklyProjectOverview = $dashboardService->weeklyProjectOverview($user, $organization);
25+
26+
return response()->json($weeklyProjectOverview);
27+
}
28+
29+
/**
30+
* @throws AuthorizationException
31+
*/
32+
public function latestTasks(Organization $organization, DashboardService $dashboardService): JsonResponse
33+
{
34+
$this->checkPermission($organization, 'charts:view:own');
35+
$user = $this->user();
36+
37+
$latestTasks = $dashboardService->latestTasks($user, $organization);
38+
39+
return response()->json($latestTasks);
40+
}
41+
42+
/**
43+
* @throws AuthorizationException
44+
*/
45+
public function lastSevenDays(Organization $organization, DashboardService $dashboardService): JsonResponse
46+
{
47+
$this->checkPermission($organization, 'charts:view:own');
48+
$user = $this->user();
49+
50+
$lastSevenDays = $dashboardService->lastSevenDays($user, $organization);
51+
52+
return response()->json($lastSevenDays);
53+
}
54+
55+
/**
56+
* @throws AuthorizationException
57+
*/
58+
public function latestTeamActivity(Organization $organization, DashboardService $dashboardService, PermissionStore $permissionStore): JsonResponse
59+
{
60+
$this->checkPermission($organization, 'charts:view:all');
61+
62+
$latestTeamActivity = $dashboardService->latestTeamActivity($organization);
63+
64+
return response()->json($latestTeamActivity);
65+
}
66+
67+
/**
68+
* @throws AuthorizationException
69+
*/
70+
public function dailyTrackedHours(Organization $organization, DashboardService $dashboardService): JsonResponse
71+
{
72+
$this->checkPermission($organization, 'charts:view:own');
73+
$user = $this->user();
74+
75+
$dailyTrackedHours = $dashboardService->getDailyTrackedHours($user, $organization, 60);
76+
77+
return response()->json($dailyTrackedHours);
78+
}
79+
80+
/**
81+
* @throws AuthorizationException
82+
*/
83+
public function totalWeeklyTime(Organization $organization, DashboardService $dashboardService): JsonResponse
84+
{
85+
$this->checkPermission($organization, 'charts:view:own');
86+
$user = $this->user();
87+
88+
$totalWeeklyTime = $dashboardService->totalWeeklyTime($user, $organization);
89+
90+
return response()->json($totalWeeklyTime);
91+
}
92+
93+
/**
94+
* @throws AuthorizationException
95+
*/
96+
public function totalWeeklyBillableTime(Organization $organization, DashboardService $dashboardService): JsonResponse
97+
{
98+
$this->checkPermission($organization, 'charts:view:own');
99+
$user = $this->user();
100+
101+
$totalWeeklyBillableTime = $dashboardService->totalWeeklyBillableTime($user, $organization);
102+
103+
return response()->json($totalWeeklyBillableTime);
104+
}
105+
106+
/**
107+
* @throws AuthorizationException
108+
*/
109+
public function totalWeeklyBillableAmount(Organization $organization, DashboardService $dashboardService): JsonResponse
110+
{
111+
$this->checkPermission($organization, 'charts:view:own');
112+
$user = $this->user();
113+
114+
$showBillableRate = $this->member($organization)->role !== Role::Employee->value || $organization->employees_can_see_billable_rates;
115+
if (! $showBillableRate) {
116+
throw new AuthorizationException('You do not have permission to view billable rates.');
117+
}
118+
119+
$totalWeeklyBillableAmount = $dashboardService->totalWeeklyBillableAmount($user, $organization);
120+
121+
return response()->json($totalWeeklyBillableAmount);
122+
}
123+
124+
/**
125+
* @throws AuthorizationException
126+
*/
127+
public function weeklyHistory(Organization $organization, DashboardService $dashboardService): JsonResponse
128+
{
129+
$this->checkPermission($organization, 'charts:view:own');
130+
$user = $this->user();
131+
132+
$weeklyHistory = $dashboardService->getWeeklyHistory($user, $organization);
133+
134+
return response()->json($weeklyHistory);
135+
}
136+
}

app/Providers/JetstreamServiceProvider.php

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ protected function configurePermissions(): void
8080
Jetstream::defaultApiTokenPermissions([]);
8181

8282
Jetstream::role(Role::Owner->value, 'Owner', [
83+
'charts:view:own',
84+
'charts:view:all',
8385
'projects:view',
8486
'projects:view:all',
8587
'projects:create',
@@ -134,6 +136,8 @@ protected function configurePermissions(): void
134136
])->description('Owner users can perform any action. There is only one owner per organization.');
135137

136138
Jetstream::role(Role::Admin->value, 'Administrator', [
139+
'charts:view:own',
140+
'charts:view:all',
137141
'projects:view',
138142
'projects:view:all',
139143
'projects:create',
@@ -184,6 +188,8 @@ protected function configurePermissions(): void
184188
])->description('Administrator users can perform any action, except accessing the billing dashboard.');
185189

186190
Jetstream::role(Role::Manager->value, 'Manager', [
191+
'charts:view:own',
192+
'charts:view:all',
187193
'projects:view',
188194
'projects:view:all',
189195
'projects:create',
@@ -224,6 +230,7 @@ protected function configurePermissions(): void
224230
])->description('Managers have full access to all projects, time entries, ect. but cannot manage the organization (add/remove member, edit the organization, ect.).');
225231

226232
Jetstream::role(Role::Employee->value, 'Employee', [
233+
'charts:view:own',
227234
'projects:view',
228235
'tags:view',
229236
'tasks:view',

routes/api.php

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use App\Http\Controllers\Api\V1\ApiTokenController;
6+
use App\Http\Controllers\Api\V1\ChartController;
67
use App\Http\Controllers\Api\V1\ClientController;
78
use App\Http\Controllers\Api\V1\ExportController;
89
use App\Http\Controllers\Api\V1\ImportController;
@@ -123,6 +124,19 @@
123124
Route::delete('/reports/{report}', [ReportController::class, 'destroy'])->name('destroy');
124125
});
125126

127+
// Chart routes
128+
Route::name('charts.')->prefix('/organizations/{organization}/charts')->group(static function (): void {
129+
Route::get('/weekly-project-overview', [ChartController::class, 'weeklyProjectOverview'])->name('weekly-project-overview');
130+
Route::get('/latest-tasks', [ChartController::class, 'latestTasks'])->name('latest-tasks');
131+
Route::get('/last-seven-days', [ChartController::class, 'lastSevenDays'])->name('last-seven-days');
132+
Route::get('/latest-team-activity', [ChartController::class, 'latestTeamActivity'])->name('latest-team-activity');
133+
Route::get('/daily-tracked-hours', [ChartController::class, 'dailyTrackedHours'])->name('daily-tracked-hours');
134+
Route::get('/total-weekly-time', [ChartController::class, 'totalWeeklyTime'])->name('total-weekly-time');
135+
Route::get('/total-weekly-billable-time', [ChartController::class, 'totalWeeklyBillableTime'])->name('total-weekly-billable-time');
136+
Route::get('/total-weekly-billable-amount', [ChartController::class, 'totalWeeklyBillableAmount'])->name('total-weekly-billable-amount');
137+
Route::get('/weekly-history', [ChartController::class, 'weeklyHistory'])->name('weekly-history');
138+
});
139+
126140
// Tag routes
127141
Route::name('tags.')->prefix('/organizations/{organization}')->group(static function (): void {
128142
Route::get('/tags', [TagController::class, 'index'])->name('index');

tests/TestCaseWithDatabase.php

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ protected function createUserWithPermission(array $permissions = [], bool $isOwn
5353
];
5454
}
5555

56+
/**
57+
* @return object{user: User, organization: Organization, member: Member, owner: User, ownerMember: Member}
58+
*/
5659
public function createUserWithRole(Role $role): object
5760
{
5861
$owner = User::factory()->create();

0 commit comments

Comments
 (0)