-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added time entry api endpoints; Increased phpstan level; renamed to s…
…olidtime
- Loading branch information
Showing
36 changed files
with
1,717 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
APP_NAME=Laravel | ||
APP_NAME=solidtime | ||
APP_ENV=local | ||
APP_KEY=base64:UNQNf1SXeASNkWux01Rj8EnHYx8FO0kAxWNDwktclkk= | ||
APP_DEBUG=true | ||
APP_URL=http://localhost | ||
APP_URL=https://solidtime.test | ||
APP_FORCE_HTTPS=true | ||
|
||
SUPER_ADMINS=[email protected] | ||
|
||
|
@@ -60,6 +61,6 @@ VITE_PUSHER_PORT="${PUSHER_PORT}" | |
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}" | ||
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" | ||
|
||
NGINX_HOST_NAME=timetracker.test | ||
NGINX_HOST_NAME=solidtime.test | ||
NETWORK_NAME=reverse-proxy-docker-traefik_routing | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
|
||
class ApiException extends Exception | ||
{ | ||
/** | ||
* Render the exception into an HTTP response. | ||
*/ | ||
public function render(Request $request): JsonResponse | ||
{ | ||
return response() | ||
->json([ | ||
'error' => true, | ||
'message' => $this->getMessage(), | ||
], 400); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions; | ||
|
||
class TimeEntryStillRunning extends ApiException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Exceptions\TimeEntryStillRunning; | ||
use App\Http\Requests\V1\TimeEntry\TimeEntryIndexRequest; | ||
use App\Http\Requests\V1\TimeEntry\TimeEntryStoreRequest; | ||
use App\Http\Requests\V1\TimeEntry\TimeEntryUpdateRequest; | ||
use App\Http\Resources\V1\TimeEntry\TimeEntryCollection; | ||
use App\Http\Resources\V1\TimeEntry\TimeEntryResource; | ||
use App\Models\Organization; | ||
use App\Models\TimeEntry; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class TimeEntryController extends Controller | ||
{ | ||
protected function checkPermission(Organization $organization, string $permission, ?TimeEntry $timeEntry = null): void | ||
{ | ||
parent::checkPermission($organization, $permission); | ||
if ($timeEntry !== null && $timeEntry->organization_id !== $organization->getKey()) { | ||
throw new AuthorizationException('Time entry does not belong to organization'); | ||
} | ||
} | ||
|
||
/** | ||
* Get time entries | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function index(Organization $organization, TimeEntryIndexRequest $request): JsonResource | ||
{ | ||
if ($request->has('user_id') && $request->get('user_id') === Auth::id()) { | ||
$this->checkPermission($organization, 'time-entries:view:own'); | ||
} else { | ||
$this->checkPermission($organization, 'time-entries:view:all'); | ||
} | ||
|
||
$timeEntriesQuery = TimeEntry::query() | ||
->whereBelongsTo($organization, 'organization'); | ||
|
||
if ($request->has('before')) { | ||
|
||
} | ||
|
||
if ($request->has('after')) { | ||
|
||
} | ||
|
||
if ($request->has('user_id')) { | ||
$timeEntriesQuery->where('user_id', $request->input('user_id')); | ||
} | ||
|
||
$timeEntries = $timeEntriesQuery->get(); | ||
|
||
return new TimeEntryCollection($timeEntries); | ||
} | ||
|
||
/** | ||
* Create time entry | ||
* | ||
* @throws AuthorizationException|TimeEntryStillRunning | ||
*/ | ||
public function store(Organization $organization, TimeEntryStoreRequest $request): JsonResource | ||
{ | ||
if ($request->get('user_id') === Auth::id()) { | ||
$this->checkPermission($organization, 'time-entries:create:own'); | ||
} else { | ||
$this->checkPermission($organization, 'time-entries:create:all'); | ||
} | ||
|
||
if ($request->get('end') === null && TimeEntry::where('user_id', $request->get('user_id'))->where('end', null)->exists()) { | ||
// TODO: documentation | ||
throw new TimeEntryStillRunning('User already has an active time entry'); | ||
} | ||
|
||
$timeEntry = new TimeEntry(); | ||
$timeEntry->fill($request->validated()); | ||
$timeEntry->organization()->associate($organization); | ||
$timeEntry->save(); | ||
|
||
return new TimeEntryResource($timeEntry); | ||
} | ||
|
||
/** | ||
* Update time entry | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function update(Organization $organization, TimeEntry $timeEntry, TimeEntryUpdateRequest $request): JsonResource | ||
{ | ||
if ($timeEntry->user_id === Auth::id() && $request->get('user_id') === Auth::id()) { | ||
$this->checkPermission($organization, 'time-entries:update:own', $timeEntry); | ||
} else { | ||
$this->checkPermission($organization, 'time-entries:update:all', $timeEntry); | ||
} | ||
|
||
$timeEntry->fill($request->validated()); | ||
$timeEntry->save(); | ||
|
||
return new TimeEntryResource($timeEntry); | ||
} | ||
|
||
/** | ||
* Delete time entry | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function destroy(Organization $organization, TimeEntry $timeEntry): JsonResponse | ||
{ | ||
if ($timeEntry->user_id === Auth::id()) { | ||
$this->checkPermission($organization, 'time-entries:delete:own', $timeEntry); | ||
} else { | ||
$this->checkPermission($organization, 'time-entries:delete:all', $timeEntry); | ||
} | ||
|
||
$timeEntry->delete(); | ||
|
||
return response() | ||
->json(null, 204); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.