Added filled gaps to time entry aggregation; Moved aggregation to service

This commit is contained in:
Constantin Graf
2024-05-21 17:31:45 +02:00
parent efd3fef0c5
commit 2a8ab12017
30 changed files with 1017 additions and 218 deletions

View File

@@ -48,7 +48,7 @@ class ProjectController extends Controller
->whereBelongsTo($organization, 'organization');
if (! $canViewAllProjects) {
$projectsQuery->visibleByUser($user);
$projectsQuery->visibleByEmployee($user);
}
$projects = $projectsQuery->paginate(config('app.pagination_per_page_default'));
@@ -131,7 +131,7 @@ class ProjectController extends Controller
}
DB::transaction(function () use (&$project) {
$project->members()->each(function (ProjectMember $member) {
$project->members->each(function (ProjectMember $member) {
$member->delete();
});

View File

@@ -51,7 +51,7 @@ class TaskController extends Controller
}
if (! $canViewAllTasks) {
$query->visibleByUser($user);
$query->visibleByEmployee($user);
}
$tasks = $query->paginate(config('app.pagination_per_page_default'));

View File

@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Http\Controllers\Api\V1;
use App\Enums\Weekday;
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
use App\Exceptions\Api\TimeEntryStillRunningApiException;
use App\Http\Requests\V1\TimeEntry\TimeEntryAggregateRequest;
@@ -17,13 +16,12 @@ use App\Http\Resources\V1\TimeEntry\TimeEntryResource;
use App\Models\Member;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Service\TimeEntryAggregationService;
use App\Service\TimeEntryFilter;
use App\Service\TimezoneService;
use Carbon\CarbonTimeZone;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
@@ -82,7 +80,7 @@ class TimeEntryController extends Controller
$timeEntries = $timeEntriesQuery->get();
if ($timeEntries->count() === $limit && $request->has('only_full_dates') && (bool) $request->get('only_full_dates') === true) {
if ($timeEntries->count() === $limit && $request->getOnlyFullDates()) {
$user = $this->user();
$timezone = app(TimezoneService::class)->getTimezoneFromUser($user);
$lastDate = null;
@@ -126,16 +124,18 @@ class TimeEntryController extends Controller
*
* @return array{
* data: array{
* grouped_type: string|null,
* grouped_data: null|array<array{
* type: string,
* key: string|null,
* seconds: int,
* cost: int,
* grouped_type: string|null,
* grouped_data: null|array<array{
* type: string,
* key: string|null,
* seconds: int,
* cost: int
* cost: int,
* grouped_type: null,
* grouped_data: null
* }>
* }>,
* seconds: int,
@@ -145,7 +145,7 @@ class TimeEntryController extends Controller
*
* @throws AuthorizationException
*/
public function aggregate(Organization $organization, TimeEntryAggregateRequest $request): array
public function aggregate(Organization $organization, TimeEntryAggregateRequest $request, TimeEntryAggregationService $aggregationService): array
{
/** @var Member|null $member */
$member = $request->has('member_id') ? Member::query()->findOrFail($request->get('member_id')) : null;
@@ -173,132 +173,25 @@ class TimeEntryController extends Controller
$user = $this->user();
/** @var string|null $group1Type */
$group1Type = $request->get('group');
/** @var string|null $group2Type */
$group2Type = $request->get('sub_group');
$group1Type = $request->getGroup();
$group2Type = $request->getSubGroup();
$group1Select = null;
$group2Select = null;
$groupBy = null;
if ($group1Type !== null) {
$group1Select = $this->getGroupByQuery($group1Type, $user->timezone, $user->week_start);
$groupBy = ['group_1'];
if ($group2Type !== null) {
$group2Select = $this->getGroupByQuery($group2Type, $user->timezone, $user->week_start);
$groupBy = ['group_1', 'group_2'];
}
}
$timeEntriesQuery->selectRaw(
($group1Select !== null ? $group1Select.' as group_1,' : '').
($group2Select !== null ? $group2Select.' as group_2,' : '').
' round(sum(extract(epoch from (coalesce("end", now()) - start)))) as aggregate,'.
' round(
sum(
extract(epoch from (coalesce("end", now()) - start)) * (coalesce(billable_rate, 0)::float/60/60)
)
) as cost'
$aggregatedData = $aggregationService->getAggregatedTimeEntries(
$timeEntriesQuery,
$group1Type,
$group2Type,
$user->timezone,
$user->week_start,
$request->getFillGapsInTimeGroups(),
$request->getStart(),
$request->getEnd()
);
if ($groupBy !== null) {
$timeEntriesQuery->groupBy($groupBy);
}
$timeEntriesAggregates = $timeEntriesQuery->get();
if ($group1Select !== null) {
$groupedAggregates = $timeEntriesAggregates->groupBy($group2Select !== null ? ['group_1', 'group_2'] : ['group_1']);
$group1Response = [];
$group1ResponseSum = 0;
$group1ResponseCost = 0;
foreach ($groupedAggregates as $group1 => $group1Aggregates) {
/** @var string $group1 */
$group2Response = [];
if ($group2Select !== null) {
$group2ResponseSum = 0;
$group2ResponseCost = 0;
foreach ($group1Aggregates as $group2 => $aggregate) {
/** @var string $group2 */
/** @var Collection<int, object{aggregate: int, cost: int}> $aggregate */
/** @var string $group2Type */
$group2Response[] = [
'type' => $group2Type,
'key' => $group2 === '' ? null : (string) $group2,
'seconds' => (int) $aggregate->get(0)->aggregate,
'cost' => (int) $aggregate->get(0)->cost,
];
$group2ResponseSum += (int) $aggregate->get(0)->aggregate;
$group2ResponseCost += (int) $aggregate->get(0)->cost;
}
} else {
/** @var Collection<int, object{aggregate: int, cost: int}> $group1Aggregates */
$group2ResponseSum = (int) $group1Aggregates->get(0)->aggregate;
$group2ResponseCost = (int) $group1Aggregates->get(0)->cost;
$group2Response = null;
}
/** @var string $group1Type */
$group1Response[] = [
'type' => $group1Type,
'key' => $group1 === '' ? null : (string) $group1,
'seconds' => $group2ResponseSum,
'cost' => $group2ResponseCost,
'grouped_data' => $group2Response,
];
$group1ResponseSum += $group2ResponseSum;
$group1ResponseCost += $group2ResponseCost;
}
} else {
$group1Response = null;
/** @var Collection<int, object{aggregate: int, cost: int}> $timeEntriesAggregates */
$group1ResponseSum = (int) $timeEntriesAggregates->get(0)->aggregate;
$group1ResponseCost = (int) $timeEntriesAggregates->get(0)->cost;
}
return [
'data' => [
'grouped_data' => $group1Response,
'seconds' => $group1ResponseSum,
'cost' => $group1ResponseCost,
],
'data' => $aggregatedData,
];
}
private function getGroupByQuery(string $group, string $timezone, Weekday $startOfWeek): string
{
$timezoneShift = app(TimezoneService::class)->getShiftFromUtc(new CarbonTimeZone($timezone));
if ($timezoneShift > 0) {
$dateWithTimeZone = 'start + INTERVAL \''.$timezoneShift.' second\'';
} elseif ($timezoneShift < 0) {
$dateWithTimeZone = 'start - INTERVAL \''.abs($timezoneShift).' second\'';
} else {
$dateWithTimeZone = 'start';
}
$startOfWeek = Carbon::now()->setTimezone($timezone)->startOfWeek($startOfWeek->carbonWeekDay())->utc()->toDateTimeString();
if ($group === 'day') {
return 'date('.$dateWithTimeZone.')';
} elseif ($group === 'week') {
return "to_char(date_bin('7 days', ".$dateWithTimeZone.", timestamp '".$startOfWeek."'), 'YYYY-MM-DD HH24:MI:SS')";
} elseif ($group === 'month') {
return 'to_char('.$dateWithTimeZone.', \'YYYY-MM\')';
} elseif ($group === 'year') {
return 'to_char('.$dateWithTimeZone.', \'YYYY\')';
} elseif ($group === 'user') {
return 'user_id';
} elseif ($group === 'project') {
return 'project_id';
} elseif ($group === 'task') {
return 'task_id';
} elseif ($group === 'client') {
return 'client_id';
} elseif ($group === 'billable') {
return 'billable';
}
throw new \LogicException('Invalid group');
}
/**
* Create time entry
*

View File

@@ -6,9 +6,9 @@ namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

View File

@@ -33,7 +33,7 @@ class TaskIndexRequest extends FormRequest
$builder = $builder->whereBelongsTo($this->organization, 'organization');
if (! app(PermissionStore::class)->has($this->organization, 'tasks:view:all')) {
$builder = $builder->visibleByUser(Auth::user());
$builder = $builder->visibleByEmployee(Auth::user());
}
return $builder;

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Http\Requests\V1\TimeEntry;
use App\Enums\TimeEntryAggregationType;
use App\Models\Member;
use App\Models\Organization;
use App\Models\Project;
@@ -13,7 +14,8 @@ use App\Models\User;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
/**
@@ -24,7 +26,7 @@ class TimeEntryAggregateRequest extends FormRequest
/**
* Get the validation rules that apply to the request.
*
* @return array<string, array<string|ValidationRule>>
* @return array<string, array<string|ValidationRule|\Illuminate\Contracts\Validation\Rule>>
*/
public function rules(): array
{
@@ -32,12 +34,12 @@ class TimeEntryAggregateRequest extends FormRequest
'group' => [
'nullable',
'required_with:group_2',
'in:day,week,month,year,user,project,task,client,billable',
Rule::enum(TimeEntryAggregationType::class),
],
'sub_group' => [
'nullable',
'in:day,week,month,year,user,project,task,client,billable',
Rule::enum(TimeEntryAggregationType::class),
],
// Filter by member ID
'member_id' => [
@@ -81,7 +83,7 @@ class TimeEntryAggregateRequest extends FormRequest
'uuid',
new ExistsEloquent(Project::class, null, function (Builder $builder): Builder {
/** @var Builder<Project> $builder */
return $builder->visibleByUser(Auth::user());
return $builder->whereBelongsTo($this->organization, 'organization');
}),
],
// Filter by tag IDs, tag IDs are AND combined
@@ -106,8 +108,7 @@ class TimeEntryAggregateRequest extends FormRequest
'string',
'uuid',
new ExistsEloquent(Task::class, null, function (Builder $builder): Builder {
/** @var Builder<Task> $builder */
return $builder->visibleByUser(Auth::user());
return $builder->whereBelongsTo($this->organization, 'organization');
}),
],
// Filter only time entries that have a start date before the given timestamp in UTC (example: 2021-01-01T00:00:00Z)
@@ -133,6 +134,35 @@ class TimeEntryAggregateRequest extends FormRequest
'string',
'in:true,false',
],
'fill_gaps_in_time_groups' => [
'string',
'in:true,false',
],
];
}
public function getGroup(): ?TimeEntryAggregationType
{
return $this->get('group') !== null ? TimeEntryAggregationType::from($this->get('group')) : null;
}
public function getSubGroup(): ?TimeEntryAggregationType
{
return $this->get('sub_group') !== null ? TimeEntryAggregationType::from($this->get('sub_group')) : null;
}
public function getFillGapsInTimeGroups(): bool
{
return $this->has('fill_gaps_in_time_groups') && $this->get('fill_gaps_in_time_groups') === 'true';
}
public function getStart(): ?Carbon
{
return $this->get('after') !== null ? Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $this->get('after'), 'UTC') : null;
}
public function getEnd(): ?Carbon
{
return $this->get('before') !== null ? Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $this->get('before'), 'UTC') : null;
}
}

View File

@@ -12,7 +12,6 @@ use App\Models\Task;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
/**
@@ -60,7 +59,7 @@ class TimeEntryIndexRequest extends FormRequest
'uuid',
new ExistsEloquent(Project::class, null, function (Builder $builder): Builder {
/** @var Builder<Project> $builder */
return $builder->visibleByUser(Auth::user());
return $builder->whereBelongsTo($this->organization, 'organization');
}),
],
// Filter by tag IDs, tag IDs are AND combined
@@ -86,7 +85,7 @@ class TimeEntryIndexRequest extends FormRequest
'uuid',
new ExistsEloquent(Task::class, null, function (Builder $builder): Builder {
/** @var Builder<Task> $builder */
return $builder->visibleByUser(Auth::user());
return $builder->whereBelongsTo($this->organization, 'organization');
}),
],
// Filter only time entries that have a start date before the given timestamp in UTC (example: 2021-01-01T00:00:00Z)
@@ -125,4 +124,9 @@ class TimeEntryIndexRequest extends FormRequest
],
];
}
public function getOnlyFullDates(): bool
{
return $this->input('only_full_dates', 'false') === 'true';
}
}

View File

@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace App\Http\Resources\V1;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
abstract class BaseResource extends JsonResource
{