Added filters to time-entries.index endpoint

This commit is contained in:
Constantin Graf
2024-02-27 12:05:18 +01:00
parent 9c5a238dda
commit 8b85f50e22
11 changed files with 538 additions and 349 deletions

View File

@@ -16,6 +16,7 @@ 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
{
@@ -41,22 +42,60 @@ class TimeEntryController extends Controller
}
$timeEntriesQuery = TimeEntry::query()
->whereBelongsTo($organization, 'organization');
->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);
}
@@ -73,8 +112,8 @@ class TimeEntryController extends Controller
$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
if ($request->get('end') === null && TimeEntry::query()->where('user_id', $request->get('user_id'))->where('end', null)->exists()) {
// TODO: API documentation
throw new TimeEntryStillRunning('User already has an active time entry');
}

View File

@@ -24,6 +24,7 @@ class TimeEntryIndexRequest extends FormRequest
public function rules(): array
{
return [
// Filter by user ID
'user_id' => [
'string',
'uuid',
@@ -35,16 +36,32 @@ class TimeEntryIndexRequest extends FormRequest
});
}),
],
// Filter only time entries that have a start date before (not including) the given date (example: 2021-12-31)
'before' => [
'nullable',
'string',
'date_format:Y-m-d',
'date_format:Y-m-d\TH:i:s\Z',
'before:after',
],
// Filter only time entries that have a start date after (not including) the given date (example: 2021-12-31)
'after' => [
'nullable',
'string',
'date_format:Y-m-d',
'date_format:Y-m-d\TH:i:s\Z',
],
// Filter only time entries that are active (have no end date, are still running)
'active' => [
'boolean',
],
// Limit the number of returned time entries
'limit' => [
'integer',
'min:1',
'max:500',
],
// Filter makes sure that only time entries of a whole date are returned
'only_full_dates' => [
'boolean',
],
];
}

View File

@@ -26,6 +26,7 @@ class TimeEntryStoreRequest extends FormRequest
public function rules(): array
{
return [
// ID of the user that the time entry should belong to
'user_id' => [
'required',
'string',
@@ -38,6 +39,7 @@ class TimeEntryStoreRequest extends FormRequest
});
}),
],
// ID of the task that the time entry should belong to
'task_id' => [
'nullable',
'string',

View File

@@ -25,6 +25,7 @@ class TimeEntryUpdateRequest extends FormRequest
public function rules(): array
{
return [
// ID of the task that the time entry should belong to
'task_id' => [
'nullable',
'string',

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Http\Resources\V1;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
abstract class BaseResource extends JsonResource
{
protected function formatDateTime(?Carbon $carbon): ?string
{
return $carbon?->toIso8601ZuluString();
}
}

View File

@@ -4,25 +4,31 @@ declare(strict_types=1);
namespace App\Http\Resources\V1\Project;
use App\Http\Resources\V1\BaseResource;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @property Project $resource
*/
class ProjectResource extends JsonResource
class ProjectResource extends BaseResource
{
/**
* Transform the resource into an array.
*
* @return array<string, string|boolean|integer>
* @return array<string, string|bool|int|null>
*/
public function toArray(Request $request): array
{
return [
/** @var string $id ID of project */
'id' => $this->resource->id,
/** @var string $name Name of project */
'name' => $this->resource->name,
/** @var string $color Color of project */
'color' => $this->resource->color,
/** @var string|null $client_id ID of client */
'client_id' => $this->resource->client_id,
];
}
}

View File

@@ -4,25 +4,45 @@ declare(strict_types=1);
namespace App\Http\Resources\V1\TimeEntry;
use App\Http\Resources\V1\BaseResource;
use App\Models\TimeEntry;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @property TimeEntry $resource
*/
class TimeEntryResource extends JsonResource
class TimeEntryResource extends BaseResource
{
/**
* Transform the resource into an array.
*
* @return array<string, string|boolean|integer>
* @return array<string, string|bool|int|null|array<string>>
*/
public function toArray(Request $request): array
{
return [
/** @var string $id ID of time entry */
'id' => $this->resource->id,
/**
* @var string $start Start of time entry (ISO 8601 format, UTC timezone, example: 2024-02-26T17:17:17Z)
*/
'start' => $this->formatDateTime($this->resource->start),
/**
* @var string|null $end End of time entry (ISO 8601 format, UTC timezone, example: 2024-02-26T17:17:17Z)
*/
'end' => $this->formatDateTime($this->resource->start),
/** @var int $duration Duration of time entry in seconds */
'duration' => $this->resource->getDuration()?->seconds,
/** @var string|null $description Description of time entry */
'description' => $this->resource->description,
/** @var string|null $task_id ID of task */
'task_id' => $this->resource->task_id,
/** @var string|null $project_id ID of project */
'project_id' => $this->resource->project_id,
/** @var string $user_id ID of user */
'user_id' => $this->resource->user_id,
/** @var array<string> $tags List of tag IDs */
'tags' => $this->resource->tags,
];
}
}

View File

@@ -5,7 +5,7 @@
"keywords": [],
"license": "AGPL-3.0-or-later",
"require": {
"php": "8.1.*",
"php": "8.3.*",
"dedoc/scramble": "^0.8.5",
"filament/filament": "^3.2",
"guzzlehttp/guzzle": "^7.2",
@@ -13,7 +13,7 @@
"korridor/laravel-model-validation-rules": "^3.0",
"laravel/framework": "^10.10",
"laravel/jetstream": "^4.2",
"laravel/passport": "*",
"laravel/passport": "^11.10.2",
"laravel/tinker": "^2.8",
"pxlrbt/filament-environment-indicator": "^2.0",
"tightenco/ziggy": "^1.0",
@@ -30,7 +30,8 @@
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^7.0",
"phpunit/phpunit": "^10.1",
"spatie/laravel-ignition": "^2.0"
"spatie/laravel-ignition": "^2.0",
"timacdonald/log-fake": "^2.1"
},
"autoload": {
"psr-4": {

726
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,6 +10,7 @@ use App\Models\Tag;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
@@ -49,6 +50,27 @@ class TimeEntryFactory extends Factory
});
}
public function startBetween(Carbon $rangeStart, Carbon $rangeEnd): self
{
$start = $this->faker->dateTimeBetween($rangeStart, $rangeEnd);
return $this->state(function (array $attributes) use ($start): array {
return [
'start' => $start,
'end' => $this->faker->dateTimeBetween($start, 'now'),
];
});
}
public function active(): self
{
return $this->state(function (array $attributes): array {
return [
'end' => null,
];
});
}
public function forUser(User $user): self
{
return $this->state(function (array $attributes) use ($user) {

View File

@@ -5,8 +5,15 @@ declare(strict_types=1);
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use TiMacDonald\Log\LogFake;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp(): void
{
parent::setUp();
LogFake::bind();
}
}