Added shareable reports

This commit is contained in:
Constantin Graf
2024-11-08 13:27:51 +01:00
committed by Constantin Graf
parent 9c82efdf07
commit 0860aa9d24
26 changed files with 986 additions and 107 deletions

View File

@@ -4,7 +4,18 @@ declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1\Public;
use App\Enums\TimeEntryAggregationType;
use App\Enums\TimeEntryAggregationTypeInterval;
use App\Enums\Weekday;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
use App\Models\Report;
use App\Models\Tag;
use App\Models\Task;
use App\Models\TimeEntry;
use App\Service\Dto\ReportPropertiesDto;
use Illuminate\Support\Str;
use Tests\Unit\Endpoint\Api\V1\ApiEndpointTestAbstract;
class PublicReportEndpointTest extends ApiEndpointTestAbstract
@@ -68,9 +79,29 @@ class PublicReportEndpointTest extends ApiEndpointTestAbstract
public function test_show_returns_detailed_information_about_the_report(): void
{
// Arrange
$report = Report::factory()->public()->create([
$reportDto = new ReportPropertiesDto;
$organization = Organization::factory()->create();
$reportDto->start = now()->subDays(2);
$reportDto->end = now();
$reportDto->group = TimeEntryAggregationType::Project;
$reportDto->subGroup = TimeEntryAggregationType::Task;
$reportDto->historyGroup = TimeEntryAggregationTypeInterval::Day;
$reportDto->weekStart = Weekday::Monday;
$reportDto->timezone = 'Europe/Vienna';
$report = Report::factory()->forOrganization($organization)->public()->create([
'public_until' => null,
'properties' => $reportDto,
]);
$project = Project::factory()->forOrganization($organization)->create();
$task1 = Task::factory()->forOrganization($organization)->forProject($project)->create([
'id' => '1b0f1b32-0def-4932-8829-b68f52161987',
]);
$task2 = Task::factory()->forOrganization($organization)->forProject($project)->create([
'id' => '3c54796d-5ab4-41e1-8f30-aa61a0a919ae',
]);
TimeEntry::factory()->forOrganization($organization)->forTask($task1)->startWithDuration(now()->subDay(), 100)->create();
TimeEntry::factory()->forOrganization($organization)->forTask($task2)->startWithDuration(now()->subDay(), 100)->create();
TimeEntry::factory()->forOrganization($organization)->startWithDuration(now()->subDay(), 100)->create();
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
@@ -79,12 +110,106 @@ class PublicReportEndpointTest extends ApiEndpointTestAbstract
// Assert
$response->assertOk();
$response->assertJsonFragment([
'id' => $report->id,
$response->assertExactJson([
'name' => $report->name,
'description' => $report->description,
'is_public' => $report->is_public,
'public_until' => $report->public_until?->toIso8601ZuluString(),
'currency' => $organization->currency,
'properties' => [
'group' => $reportDto->group->value,
'sub_group' => $reportDto->subGroup->value,
'history_group' => $reportDto->historyGroup->value,
'start' => $reportDto->start->toIso8601ZuluString(),
'end' => $reportDto->end->toIso8601ZuluString(),
],
'data' => [
'seconds' => 300,
'cost' => 0,
'grouped_type' => TimeEntryAggregationType::Project->value,
'grouped_data' => [
[
'key' => $project->id,
'seconds' => 200,
'cost' => 0,
'grouped_type' => TimeEntryAggregationType::Task->value,
'grouped_data' => [
[
'key' => $task1->id,
'seconds' => 100,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => $task1->name,
'color' => null,
],
[
'key' => $task2->id,
'seconds' => 100,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => $task2->name,
'color' => null,
],
],
'description' => $project->name,
'color' => $project->color,
],
[
'key' => null,
'seconds' => 100,
'cost' => 0,
'grouped_type' => TimeEntryAggregationType::Task->value,
'grouped_data' => [
[
'key' => null,
'seconds' => 100,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
],
'description' => null,
'color' => null,
],
],
],
'history_data' => [
'seconds' => 300,
'cost' => 0,
'grouped_type' => TimeEntryAggregationTypeInterval::Day->value,
'grouped_data' => [
[
'key' => now()->subDays(2)->toDateString(),
'seconds' => 0,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
[
'key' => now()->subDays(1)->toDateString(),
'seconds' => 300,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
[
'key' => now()->toDateString(),
'seconds' => 0,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
],
],
]);
}
@@ -103,11 +228,188 @@ class PublicReportEndpointTest extends ApiEndpointTestAbstract
// Assert
$response->assertOk();
$response->assertJsonFragment([
'id' => $report->id,
'name' => $report->name,
'description' => $report->description,
'is_public' => $report->is_public,
'public_until' => $report->public_until?->toIso8601ZuluString(),
]);
}
public function test_show_returns_detailed_information_about_the_report_with_all_available_filters(): void
{
// Arrange
$organization = Organization::factory()->create();
$client = Client::factory()->forOrganization($organization)->create();
$otherClient = Client::factory()->forOrganization($organization)->create();
$project = Project::factory()->forClient($client)->forOrganization($organization)->create();
$otherProject = Project::factory()->forOrganization($organization)->create();
$otherProjectWithClient = Project::factory()->forClient($client)->forOrganization($organization)->create();
$task = Task::factory()->forOrganization($organization)->forProject($project)->create();
$tag = Tag::factory()->forOrganization($organization)->create();
$otherTag = Tag::factory()->forOrganization($organization)->create();
// Match for all filters
TimeEntry::factory()->forOrganization($organization)
->forTask($task)
->billable()
->startWithDuration(now()->subDay(), 100)
->create([
'tags' => [$tag->getKey()],
]);
// No match for task filter
TimeEntry::factory()->forOrganization($organization)
->forProject($otherProject)
->startWithDuration(now()->subDay(), 100)
->create();
// No match for client filter
TimeEntry::factory()->forOrganization($organization)
->forProject($otherProjectWithClient)
->startWithDuration(now()->subDay(), 100)
->create();
$reportDto = new ReportPropertiesDto;
$reportDto->start = now()->subDays(2);
$reportDto->end = now();
$reportDto->group = TimeEntryAggregationType::Project;
$reportDto->subGroup = TimeEntryAggregationType::Task;
$reportDto->historyGroup = TimeEntryAggregationTypeInterval::Day;
$reportDto->weekStart = Weekday::Monday;
$reportDto->timezone = 'Europe/Vienna';
$reportDto->active = false;
$reportDto->billable = true;
$reportDto->setMemberIds(null);
$reportDto->setClientIds([$client->getKey()]);
$reportDto->setProjectIds([$project->getKey()]);
$reportDto->setTagIds([$tag->getKey()]);
$reportDto->setTaskIds([$task->getKey()]);
$report = Report::factory()->forOrganization($organization)->public()->create([
'public_until' => null,
'properties' => $reportDto,
]);
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertOk();
$response->assertJson([
'name' => $report->name,
'description' => $report->description,
'public_until' => $report->public_until?->toIso8601ZuluString(),
'properties' => [
'group' => $reportDto->group->value,
'sub_group' => $reportDto->subGroup->value,
'history_group' => $reportDto->historyGroup->value,
'start' => $reportDto->start->toIso8601ZuluString(),
'end' => $reportDto->end->toIso8601ZuluString(),
],
'data' => [
'seconds' => 100,
'cost' => 0,
'grouped_type' => TimeEntryAggregationType::Project->value,
],
'history_data' => [
'seconds' => 100,
'cost' => 0,
'grouped_type' => TimeEntryAggregationTypeInterval::Day->value,
],
]);
}
public function test_if_the_resources_behind_the_filters_no_longer_exist_the_report_ignores_those_filters_but_this_does_not_increase_the_visible_data(): void
{
// Arrange
$organization = Organization::factory()->create();
$client = Client::factory()->forOrganization($organization)->create();
$project = Project::factory()->forClient($client)->forOrganization($organization)->create();
$task = Task::factory()->forOrganization($organization)->forProject($project)->create();
$tag = Tag::factory()->forOrganization($organization)->create();
TimeEntry::factory()->forOrganization($organization)
->forTask($task)
->billable()
->startWithDuration(now()->subDay(), 100)
->create([
'tags' => [$tag->getKey()],
]);
$reportDto = new ReportPropertiesDto;
$reportDto->start = now()->subDays(2);
$reportDto->end = now();
$reportDto->group = TimeEntryAggregationType::Project;
$reportDto->subGroup = TimeEntryAggregationType::Task;
$reportDto->historyGroup = TimeEntryAggregationTypeInterval::Day;
$reportDto->weekStart = Weekday::Monday;
$reportDto->timezone = 'Europe/Vienna';
$reportDto->setMemberIds([Str::uuid()->toString()]);
$reportDto->setClientIds([Str::uuid()->toString()]);
$reportDto->setProjectIds([Str::uuid()->toString()]);
$reportDto->setTagIds([Str::uuid()->toString()]);
$reportDto->setTaskIds([Str::uuid()->toString()]);
$report = Report::factory()->forOrganization($organization)->public()->create([
'public_until' => null,
'properties' => $reportDto,
]);
// Act
$response = $this->getJson(route('api.v1.public.reports.show'), [
'X-Api-Key' => $report->share_secret,
]);
// Assert
$response->assertOk();
$response->assertJson([
'name' => $report->name,
'description' => $report->description,
'public_until' => $report->public_until?->toIso8601ZuluString(),
'properties' => [
'group' => $reportDto->group->value,
'sub_group' => $reportDto->subGroup->value,
'history_group' => $reportDto->historyGroup->value,
'start' => $reportDto->start->toIso8601ZuluString(),
'end' => $reportDto->end->toIso8601ZuluString(),
],
'data' => [
'seconds' => 0,
'cost' => 0,
'grouped_type' => TimeEntryAggregationType::Project->value,
'grouped_data' => [],
],
'history_data' => [
'seconds' => 0,
'cost' => 0,
'grouped_type' => TimeEntryAggregationTypeInterval::Day->value,
'grouped_data' => [
[
'key' => now()->subDays(2)->toDateString(),
'seconds' => 0,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
[
'key' => now()->subDays(1)->toDateString(),
'seconds' => 0,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
[
'key' => now()->toDateString(),
'seconds' => 0,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
'description' => null,
'color' => null,
],
],
],
]);
}
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Unit\Endpoint\Api\V1;
use App\Enums\TimeEntryAggregationType;
use App\Enums\Weekday;
use App\Http\Controllers\Api\V1\ReportController;
use App\Models\Report;
use Illuminate\Support\Carbon;
@@ -70,6 +71,9 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
'properties' => [
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
'history_group' => TimeEntryAggregationType::Day->value,
'start' => Carbon::now()->subDays(30)->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
],
]);
@@ -92,6 +96,9 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
'properties' => [
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
'history_group' => TimeEntryAggregationType::Day->value,
'start' => Carbon::now()->subDays(30)->toIso8601ZuluString(),
'end' => Carbon::now()->toIso8601ZuluString(),
],
]);
@@ -117,7 +124,7 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
Passport::actingAs($data->user);
// Act
$response = $this->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
$response = $this->withoutExceptionHandling()->postJson(route('api.v1.reports.store', [$data->organization->getKey()]), [
'name' => 'Test Report',
'description' => 'Test description',
'is_public' => true,
@@ -134,6 +141,9 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
'task_ids' => [],
'group' => TimeEntryAggregationType::Project->value,
'sub_group' => TimeEntryAggregationType::Task->value,
'history_group' => TimeEntryAggregationType::Day->value,
'week_start' => Weekday::Monday->value,
'timezone' => 'Europe/Berlin',
],
]);
@@ -251,7 +261,7 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
);
}
public function test_update_endpoint_can_set_a_report_to_public_which_generates_a_new_secret(): void
public function test_update_endpoint_can_set_a_report_from_private_to_public_which_generates_a_new_secret(): void
{
// Arrange
$data = $this->createUserWithPermission([
@@ -269,7 +279,7 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
$report->refresh();
$this->assertTrue($report->is_public);
$this->assertNotNull($report->share_secret);
$response->assertStatus(200);
$this->assertResponseCode($response, 200);
$response->assertJson(fn (AssertableJson $json) => $json
->has('data')
->where('data.is_public', true)
@@ -277,7 +287,7 @@ class ReportEndpointTest extends ApiEndpointTestAbstract
);
}
public function test_update_endpoint_can_set_a_report_to_private_which_resets_the_secret(): void
public function test_update_endpoint_can_set_a_report_from_public_to_private_which_resets_the_secret(): void
{
// Arrange
$data = $this->createUserWithPermission([

View File

@@ -318,4 +318,92 @@ class TimeEntryAggregationServiceTest extends TestCaseWithDatabase
],
], $result);
}
public function test_aggregate_time_entries_by_client_and_project_with_filled_gaps(): void
{
// Arrange
$client1 = Client::factory()->create();
$client2 = Client::factory()->create();
$project1 = Project::factory()->forClient($client1)->create();
$project2 = Project::factory()->forClient($client2)->create();
$project3 = Project::factory()->create();
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project1)->create();
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project2)->create();
TimeEntry::factory()->startWithDuration(now(), 10)->forProject($project3)->create();
TimeEntry::factory()->startWithDuration(now(), 10)->create();
$query = TimeEntry::query();
// Act
$result = $this->service->getAggregatedTimeEntries(
$query,
TimeEntryAggregationType::Client,
TimeEntryAggregationType::Project,
'Europe/Vienna',
Weekday::Monday,
true,
null,
null
);
// Assert
$this->assertEqualsCanonicalizing([
'seconds' => 40,
'cost' => 0,
'grouped_type' => 'client',
'grouped_data' => [
[
'key' => null,
'seconds' => 20,
'cost' => 0,
'grouped_type' => 'project',
'grouped_data' => [
[
'key' => null,
'seconds' => 10,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
[
'key' => $project3->getKey(),
'seconds' => 10,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
],
],
[
'key' => $client1->getKey(),
'seconds' => 10,
'cost' => 0,
'grouped_type' => 'project',
'grouped_data' => [
[
'key' => $project1->getKey(),
'seconds' => 10,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
],
],
[
'key' => $client2->getKey(),
'seconds' => 10,
'cost' => 0,
'grouped_type' => 'project',
'grouped_data' => [
[
'key' => $project2->getKey(),
'seconds' => 10,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
],
],
],
], $result);
}
}