mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-13 02:32:15 +01:00
Added filled gaps to time entry aggregation; Moved aggregation to service
This commit is contained in:
@@ -8,10 +8,11 @@ use App\Enums\Role;
|
||||
use App\Exceptions\Api\TimeEntryCanNotBeRestartedApiException;
|
||||
use App\Models\Member;
|
||||
use App\Models\Project;
|
||||
use App\Models\Tag;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Testing\Fluent\AssertableJson;
|
||||
@@ -293,7 +294,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
'time-entries:view:own',
|
||||
]);
|
||||
$timeEntriesDay1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now()->subDay()->startOfDay(), Carbon::now()->subDay()->endOfDay())
|
||||
->startBetween(Carbon::now()->subDay()->startOfDay(), Carbon::now()->subDay()->endOfDay(), true)
|
||||
->createMany(7);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -306,11 +307,11 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(7, 'data');
|
||||
Log::assertLogged(fn (LogEntry $log) => $log->level === 'warning'
|
||||
&& $log->message === 'User has has more than 5 time entries on one date'
|
||||
);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(7, 'data');
|
||||
}
|
||||
|
||||
public function test_index_endpoint_before_filter_returns_time_entries_before_date(): void
|
||||
@@ -331,6 +332,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
Carbon::now()->timezone($data->user->timezone)->subDays(2)->endOfDay()->utc()
|
||||
)
|
||||
->createMany(3);
|
||||
$timeEntriesBeforeSorted = $timeEntriesBefore->sortByDesc('start')->values();
|
||||
$timeEntriesDirectlyBeforeLimit = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->create([
|
||||
'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->endOfDay()->utc(),
|
||||
@@ -350,9 +352,9 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
->has('data')
|
||||
->count('data', 4)
|
||||
->where('data.0.id', $timeEntriesDirectlyBeforeLimit->getKey())
|
||||
->where('data.1.id', $timeEntriesBefore->sortByDesc('start')->get(0)->getKey())
|
||||
->where('data.2.id', $timeEntriesBefore->sortByDesc('start')->get(1)->getKey())
|
||||
->where('data.3.id', $timeEntriesBefore->sortByDesc('start')->get(2)->getKey())
|
||||
->where('data.1.id', $timeEntriesBeforeSorted->get(0)->getKey())
|
||||
->where('data.2.id', $timeEntriesBeforeSorted->get(1)->getKey())
|
||||
->where('data.3.id', $timeEntriesBeforeSorted->get(2)->getKey())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -365,6 +367,7 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$timeEntriesAfter = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now($data->user->timezone)->startOfDay()->utc(), Carbon::now($data->user->timezone)->utc())
|
||||
->createMany(3);
|
||||
$timeEntriesAfterSorted = $timeEntriesAfter->sortByDesc('start')->values();
|
||||
$timeEntriesBefore = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now($data->user->timezone)->subDay()->startOfDay()->utc(), Carbon::now($data->user->timezone)->subDay()->endOfDay()->utc())
|
||||
->createMany(3);
|
||||
@@ -386,13 +389,61 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$response->assertJson(fn (AssertableJson $json) => $json
|
||||
->has('data')
|
||||
->count('data', 4)
|
||||
->where('data.0.id', $timeEntriesAfter->sortByDesc('start')->get(0)->getKey())
|
||||
->where('data.1.id', $timeEntriesAfter->sortByDesc('start')->get(1)->getKey())
|
||||
->where('data.2.id', $timeEntriesAfter->sortByDesc('start')->get(2)->getKey())
|
||||
->where('data.0.id', $timeEntriesAfterSorted->get(0)->getKey())
|
||||
->where('data.1.id', $timeEntriesAfterSorted->get(1)->getKey())
|
||||
->where('data.2.id', $timeEntriesAfterSorted->get(2)->getKey())
|
||||
->where('data.3.id', $timeEntriesDirectlyAfterLimit->getKey())
|
||||
);
|
||||
}
|
||||
|
||||
public function test_index_endpoint_with_all_available_filters(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
'time-entries:view:own',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$task = Task::factory()->forOrganization($data->organization)->forProject($project)->create();
|
||||
$tag = Tag::factory()->forOrganization($data->organization)->create();
|
||||
$timeEntry1 = TimeEntry::factory()
|
||||
->forOrganization($data->organization)
|
||||
->forProject($project)
|
||||
->forTask($task)
|
||||
->forMember($data->member)
|
||||
->billable()
|
||||
->active()
|
||||
->create([
|
||||
'start' => Carbon::now()->subHour(),
|
||||
'tags' => [$tag->getKey()],
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.time-entries.index', [
|
||||
$data->organization->getKey(),
|
||||
'member_id' => $data->member->getKey(),
|
||||
'member_ids' => [$data->member->getKey()],
|
||||
'project_ids' => [$project->getKey()],
|
||||
'task_ids' => [$task->getKey()],
|
||||
'tag_ids' => [$tag->getKey()],
|
||||
'before' => Carbon::now()->toIso8601ZuluString(),
|
||||
'after' => Carbon::now()->subDay()->toIso8601ZuluString(),
|
||||
'active' => 'true',
|
||||
'only_full_dates' => 'true',
|
||||
'limit' => 1,
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertValid();
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(fn (AssertableJson $json) => $json
|
||||
->has('data')
|
||||
->count('data', 1)
|
||||
->where('data.0.id', $timeEntry1->getKey())
|
||||
);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_fails_if_user_has_no_permission_to_view_time_entries(): void
|
||||
{
|
||||
// Arrange
|
||||
@@ -413,12 +464,13 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->createMany(3);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->createMany(3);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->state([
|
||||
'start' => $timeEntries->get(0)->start,
|
||||
])->createMany(3);
|
||||
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1)->utc();
|
||||
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3)->utc();
|
||||
$timeEntry1NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day2, 10)->create();
|
||||
$timeEntry1WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day2, 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -430,6 +482,154 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson([
|
||||
'data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $day2->format('Y-m-d'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $project->getKey(),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => null,
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
'key' => $day1->format('Y-m-d'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $project->getKey(),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => null,
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'grouped_type' => 'day',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_groups_by_two_groups_with_fill_gaps_argument(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$day1 = Carbon::now()->timezone($data->user->timezone)->subDays(1)->utc();
|
||||
$day2 = Carbon::now()->timezone($data->user->timezone)->subDays(3)->utc();
|
||||
$timeEntry1NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2NoProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($day2, 10)->create();
|
||||
$timeEntry1WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day1, 10)->create();
|
||||
$timeEntry2WithProject = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->startWithDuration($day2, 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.time-entries.aggregate', [
|
||||
$data->organization->getKey(),
|
||||
'group' => 'project',
|
||||
'sub_group' => 'day',
|
||||
'fill_gaps_in_time_groups' => 'true',
|
||||
'after' => $day2->copy()->subSecond()->toIso8601ZuluString(),
|
||||
'before' => $day1->copy()->addSecond()->toIso8601ZuluString(),
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson(['data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'project',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $project->getKey(),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'day',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $day2->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => $day2->copy()->addDay()->format('Y-m-d'),
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
2 => [
|
||||
'key' => $day1->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
'key' => null,
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'day',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $day2->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => $day2->copy()->addDay()->format('Y-m-d'),
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
2 => [
|
||||
'key' => $day1->format('Y-m-d'),
|
||||
'seconds' => 10,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_groups_by_one_group(): void
|
||||
@@ -438,12 +638,12 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->createMany(3);
|
||||
$project = Project::factory()->forOrganization($data->organization)->create();
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->forProject($project)->createMany(3);
|
||||
$timeEntries = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->state([
|
||||
'start' => $timeEntries->get(0)->start,
|
||||
])->createMany(3);
|
||||
$week1 = Carbon::now()->timezone($data->user->timezone)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
|
||||
$week2 = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
|
||||
$timeEntry1Week1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week1->copy()->addDays(1), 10)->create();
|
||||
$timeEntry2Week1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week1->copy()->addDays(2), 10)->create();
|
||||
$timeEntry1Week2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week2->copy()->addDays(3), 10)->create();
|
||||
$timeEntry2Week2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week2->copy()->addDays(4), 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -454,6 +654,88 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson([
|
||||
'data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'week',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => $week2->format('Y-m-d H:i:s'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => $week1->format('Y-m-d H:i:s'),
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_groups_by_one_group_with_fill_gaps_argument(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$laterWeekEnd = Carbon::now()->timezone($data->user->timezone)->endOfWeek($data->user->week_start->toEndOfWeek()->carbonWeekDay())->utc();
|
||||
$earlierWeekStart = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
|
||||
|
||||
$timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($laterWeekEnd->copy()->subDays(1), 10)->create();
|
||||
$timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($laterWeekEnd->copy()->subDays(2), 10)->create();
|
||||
$timeEntry3 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($earlierWeekStart->copy()->addDays(1), 10)->create();
|
||||
$timeEntry4 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($earlierWeekStart->copy()->addDays(2), 10)->create();
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
$response = $this->getJson(route('api.v1.time-entries.aggregate', [
|
||||
$data->organization->getKey(),
|
||||
'group' => 'week',
|
||||
'fill_gaps_in_time_groups' => 'true',
|
||||
'after' => $earlierWeekStart->toIso8601ZuluString(),
|
||||
'before' => $laterWeekEnd->toIso8601ZuluString(),
|
||||
]));
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertExactJson([
|
||||
'data' => [
|
||||
'seconds' => 40,
|
||||
'cost' => 0,
|
||||
'grouped_type' => 'week',
|
||||
'grouped_data' => [
|
||||
0 => [
|
||||
'key' => '2024-05-05 22:00:00',
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
1 => [
|
||||
'key' => '2024-05-12 22:00:00',
|
||||
'seconds' => 0,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
2 => [
|
||||
'key' => '2024-05-19 22:00:00',
|
||||
'seconds' => 20,
|
||||
'cost' => 0,
|
||||
'grouped_type' => null,
|
||||
'grouped_data' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function test_aggregate_endpoint_with_no_group(): void
|
||||
|
||||
Reference in New Issue
Block a user