mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-06-15 13:32:43 +01:00
168 lines
6.0 KiB
PHP
168 lines
6.0 KiB
PHP
<?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;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
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')
|
|
->orderBy('start', 'desc');
|
|
|
|
if ($request->has('before')) {
|
|
$timeEntriesQuery->whereDate('start', '<', $request->input('before'));
|
|
}
|
|
|
|
if ($request->has('after')) {
|
|
$timeEntriesQuery->whereDate('start', '>', $request->input('after'));
|
|
}
|
|
|
|
if ($request->has('active') && (bool) $request->get('active') === true) {
|
|
$timeEntriesQuery->whereNull('end');
|
|
}
|
|
|
|
if ($request->has('user_id')) {
|
|
$timeEntriesQuery->where('user_id', $request->input('user_id'));
|
|
}
|
|
|
|
$limit = $request->has('limit') ? (int) $request->get('limit', 150) : null;
|
|
if ($limit !== null) {
|
|
$timeEntriesQuery->limit($limit);
|
|
}
|
|
|
|
$timeEntries = $timeEntriesQuery->get();
|
|
|
|
if ($timeEntries->count() === $limit && $request->has('only_full_dates') && (bool) $request->get('only_full_dates') === true) {
|
|
$lastDate = null;
|
|
/** @var TimeEntry $timeEntry */
|
|
foreach ($timeEntries as $timeEntry) {
|
|
if ($lastDate === null || $lastDate->diffInDays($timeEntry->start->startOfDay()) > 0) {
|
|
$lastDate = $timeEntry->start->startOfDay();
|
|
}
|
|
}
|
|
|
|
$timeEntries = $timeEntries->filter(function (TimeEntry $timeEntry) use ($lastDate): bool {
|
|
return $timeEntry->end === null || $timeEntry->start->toDateString() !== $lastDate->toDateString();
|
|
});
|
|
// TODO: fix edge case with current time entry that is more than one day running
|
|
|
|
if ($timeEntries->count() === 0) {
|
|
Log::warning('User has has more than '.$limit.' time entries on one date', [
|
|
'date' => $lastDate->toDateString(),
|
|
'user_id' => $request->input('user_id'),
|
|
'auth_user_id' => Auth::id(),
|
|
'limit' => $limit,
|
|
]);
|
|
$timeEntries = $timeEntriesQuery
|
|
->limit(5000)
|
|
->whereDate('start', '=', $lastDate->toDateString())
|
|
->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::query()->where('user_id', $request->get('user_id'))->where('end', null)->exists()) {
|
|
// TODO: API documentation
|
|
// TODO: Create concept for api exceptions
|
|
throw new TimeEntryStillRunning('User already has an active time entry');
|
|
}
|
|
|
|
$timeEntry = new TimeEntry();
|
|
$timeEntry->fill($request->validated());
|
|
$timeEntry->description = $request->get('description', '');
|
|
$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);
|
|
}
|
|
}
|