mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-17 04:32:15 +01:00
Added filters to time-entries.index endpoint
This commit is contained in:
@@ -16,6 +16,7 @@ use Illuminate\Auth\Access\AuthorizationException;
|
|||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class TimeEntryController extends Controller
|
class TimeEntryController extends Controller
|
||||||
{
|
{
|
||||||
@@ -41,22 +42,60 @@ class TimeEntryController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$timeEntriesQuery = TimeEntry::query()
|
$timeEntriesQuery = TimeEntry::query()
|
||||||
->whereBelongsTo($organization, 'organization');
|
->whereBelongsTo($organization, 'organization')
|
||||||
|
->orderBy('start', 'desc');
|
||||||
|
|
||||||
if ($request->has('before')) {
|
if ($request->has('before')) {
|
||||||
|
$timeEntriesQuery->whereDate('start', '<', $request->input('before'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->has('after')) {
|
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')) {
|
if ($request->has('user_id')) {
|
||||||
$timeEntriesQuery->where('user_id', $request->input('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();
|
$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);
|
return new TimeEntryCollection($timeEntries);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,8 +112,8 @@ class TimeEntryController extends Controller
|
|||||||
$this->checkPermission($organization, 'time-entries:create:all');
|
$this->checkPermission($organization, 'time-entries:create:all');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->get('end') === null && TimeEntry::where('user_id', $request->get('user_id'))->where('end', null)->exists()) {
|
if ($request->get('end') === null && TimeEntry::query()->where('user_id', $request->get('user_id'))->where('end', null)->exists()) {
|
||||||
// TODO: documentation
|
// TODO: API documentation
|
||||||
throw new TimeEntryStillRunning('User already has an active time entry');
|
throw new TimeEntryStillRunning('User already has an active time entry');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ class TimeEntryIndexRequest extends FormRequest
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
// Filter by user ID
|
||||||
'user_id' => [
|
'user_id' => [
|
||||||
'string',
|
'string',
|
||||||
'uuid',
|
'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' => [
|
'before' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'string',
|
'string',
|
||||||
'date_format:Y-m-d',
|
'date_format:Y-m-d\TH:i:s\Z',
|
||||||
'before:after',
|
'before:after',
|
||||||
],
|
],
|
||||||
|
// Filter only time entries that have a start date after (not including) the given date (example: 2021-12-31)
|
||||||
'after' => [
|
'after' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'string',
|
'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',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class TimeEntryStoreRequest extends FormRequest
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
// ID of the user that the time entry should belong to
|
||||||
'user_id' => [
|
'user_id' => [
|
||||||
'required',
|
'required',
|
||||||
'string',
|
'string',
|
||||||
@@ -38,6 +39,7 @@ class TimeEntryStoreRequest extends FormRequest
|
|||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
// ID of the task that the time entry should belong to
|
||||||
'task_id' => [
|
'task_id' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'string',
|
'string',
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class TimeEntryUpdateRequest extends FormRequest
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
// ID of the task that the time entry should belong to
|
||||||
'task_id' => [
|
'task_id' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'string',
|
'string',
|
||||||
|
|||||||
16
app/Http/Resources/V1/BaseResource.php
Normal file
16
app/Http/Resources/V1/BaseResource.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,25 +4,31 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Http\Resources\V1\Project;
|
namespace App\Http\Resources\V1\Project;
|
||||||
|
|
||||||
|
use App\Http\Resources\V1\BaseResource;
|
||||||
use App\Models\Project;
|
use App\Models\Project;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property Project $resource
|
* @property Project $resource
|
||||||
*/
|
*/
|
||||||
class ProjectResource extends JsonResource
|
class ProjectResource extends BaseResource
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Transform the resource into an array.
|
* 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
|
public function toArray(Request $request): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
/** @var string $id ID of project */
|
||||||
'id' => $this->resource->id,
|
'id' => $this->resource->id,
|
||||||
|
/** @var string $name Name of project */
|
||||||
'name' => $this->resource->name,
|
'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,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,25 +4,45 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Http\Resources\V1\TimeEntry;
|
namespace App\Http\Resources\V1\TimeEntry;
|
||||||
|
|
||||||
|
use App\Http\Resources\V1\BaseResource;
|
||||||
use App\Models\TimeEntry;
|
use App\Models\TimeEntry;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property TimeEntry $resource
|
* @property TimeEntry $resource
|
||||||
*/
|
*/
|
||||||
class TimeEntryResource extends JsonResource
|
class TimeEntryResource extends BaseResource
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Transform the resource into an array.
|
* 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
|
public function toArray(Request $request): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
/** @var string $id ID of time entry */
|
/** @var string $id ID of time entry */
|
||||||
'id' => $this->resource->id,
|
'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,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
"keywords": [],
|
"keywords": [],
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "8.1.*",
|
"php": "8.3.*",
|
||||||
"dedoc/scramble": "^0.8.5",
|
"dedoc/scramble": "^0.8.5",
|
||||||
"filament/filament": "^3.2",
|
"filament/filament": "^3.2",
|
||||||
"guzzlehttp/guzzle": "^7.2",
|
"guzzlehttp/guzzle": "^7.2",
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
"korridor/laravel-model-validation-rules": "^3.0",
|
"korridor/laravel-model-validation-rules": "^3.0",
|
||||||
"laravel/framework": "^10.10",
|
"laravel/framework": "^10.10",
|
||||||
"laravel/jetstream": "^4.2",
|
"laravel/jetstream": "^4.2",
|
||||||
"laravel/passport": "*",
|
"laravel/passport": "^11.10.2",
|
||||||
"laravel/tinker": "^2.8",
|
"laravel/tinker": "^2.8",
|
||||||
"pxlrbt/filament-environment-indicator": "^2.0",
|
"pxlrbt/filament-environment-indicator": "^2.0",
|
||||||
"tightenco/ziggy": "^1.0",
|
"tightenco/ziggy": "^1.0",
|
||||||
@@ -30,7 +30,8 @@
|
|||||||
"mockery/mockery": "^1.4.4",
|
"mockery/mockery": "^1.4.4",
|
||||||
"nunomaduro/collision": "^7.0",
|
"nunomaduro/collision": "^7.0",
|
||||||
"phpunit/phpunit": "^10.1",
|
"phpunit/phpunit": "^10.1",
|
||||||
"spatie/laravel-ignition": "^2.0"
|
"spatie/laravel-ignition": "^2.0",
|
||||||
|
"timacdonald/log-fake": "^2.1"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
726
composer.lock
generated
726
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,7 @@ use App\Models\Tag;
|
|||||||
use App\Models\Task;
|
use App\Models\Task;
|
||||||
use App\Models\TimeEntry;
|
use App\Models\TimeEntry;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
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
|
public function forUser(User $user): self
|
||||||
{
|
{
|
||||||
return $this->state(function (array $attributes) use ($user) {
|
return $this->state(function (array $attributes) use ($user) {
|
||||||
|
|||||||
@@ -5,8 +5,15 @@ declare(strict_types=1);
|
|||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||||
|
use TiMacDonald\Log\LogFake;
|
||||||
|
|
||||||
abstract class TestCase extends BaseTestCase
|
abstract class TestCase extends BaseTestCase
|
||||||
{
|
{
|
||||||
use CreatesApplication;
|
use CreatesApplication;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
LogFake::bind();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user