mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-10 09:12:15 +01:00
Added endpoint for current time entry
This commit is contained in:
50
app/Http/Controllers/Api/V1/UserTimeEntryController.php
Normal file
50
app/Http/Controllers/Api/V1/UserTimeEntryController.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Resources\V1\TimeEntry\TimeEntryResource;
|
||||
use App\Models\Organization;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class UserTimeEntryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get the active time entry of the current user
|
||||
*
|
||||
* This endpoint is independent of organization.
|
||||
*
|
||||
* @operationId getMyActiveTimeEntry
|
||||
*/
|
||||
public function myActive(): JsonResource
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = Auth::user();
|
||||
|
||||
$activeTimeEntriesOfUser = TimeEntry::query()
|
||||
->whereBelongsTo($user, 'user')
|
||||
->whereNull('end')
|
||||
->orderBy('start', 'desc')
|
||||
->get();
|
||||
|
||||
if ($activeTimeEntriesOfUser->count() > 1) {
|
||||
Log::warning('User has more than one active time entry.', [
|
||||
'user' => $user->getKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
$activeTimeEntry = $activeTimeEntriesOfUser->first();
|
||||
|
||||
if ($activeTimeEntry !== null) {
|
||||
return new TimeEntryResource($activeTimeEntry);
|
||||
} else {
|
||||
throw new ModelNotFoundException('No active time entry');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user