-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added endpoint for current time entry
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 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 |
---|---|---|
@@ -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'); | ||
} | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit\Endpoint\Api\V1; | ||
|
||
use App\Models\TimeEntry; | ||
use Laravel\Passport\Passport; | ||
|
||
class UserTimeEntryEndpointTest extends ApiEndpointTestAbstract | ||
{ | ||
public function test_my_active_endpoint_returns_unauthorized_if_user_is_not_logged_in(): void | ||
{ | ||
// Arrange | ||
$data = $this->createUserWithPermission([ | ||
]); | ||
|
||
// Act | ||
$response = $this->getJson(route('api.v1.users.time-entries.my-active')); | ||
|
||
// Assert | ||
$response->assertUnauthorized(); | ||
} | ||
|
||
public function test_my_active_endpoint_returns_current_time_entry_of_logged_in_user(): void | ||
{ | ||
// Arrange | ||
$data = $this->createUserWithPermission([ | ||
]); | ||
$activeTimeEntry = TimeEntry::factory()->forUser($data->user)->active()->create(); | ||
$inactiveTimeEntry = TimeEntry::factory()->forUser($data->user)->create(); | ||
Passport::actingAs($data->user); | ||
|
||
// Act | ||
$response = $this->getJson(route('api.v1.users.time-entries.my-active')); | ||
|
||
// Assert | ||
$response->assertSuccessful(); | ||
} | ||
|
||
public function test_my_active_endpoint_returns_not_found_if_user_has_no_active_time_entry(): void | ||
{ | ||
// Arrange | ||
$data = $this->createUserWithPermission([ | ||
]); | ||
$inactiveTimeEntry = TimeEntry::factory()->forUser($data->user)->create(); | ||
Passport::actingAs($data->user); | ||
|
||
// Act | ||
$response = $this->getJson(route('api.v1.users.time-entries.my-active')); | ||
|
||
// Assert | ||
$response->assertNotFound(); | ||
} | ||
} |