mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-12-06 17:52:16 +00:00
Keep the client of time entries in sync with their project
This commit is contained in:
committed by
Constantin Graf
parent
eab05eb92e
commit
0e1ed2e114
@@ -8,6 +8,7 @@ use App\Filament\Resources\TimeEntryResource\Pages\CreateTimeEntry;
|
||||
use App\Filament\Resources\TimeEntryResource\Pages\EditTimeEntry;
|
||||
use App\Filament\Resources\TimeEntryResource\Pages\ListTimeEntries;
|
||||
use App\Models\Member;
|
||||
use App\Models\Project;
|
||||
use App\Models\TimeEntry;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
@@ -132,6 +133,20 @@ class TimeEntryResource extends Resource
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The client of a time entry is always the client of its project.
|
||||
*/
|
||||
public static function getClientIdForProject(mixed $projectId): ?string
|
||||
{
|
||||
if ($projectId === null || $projectId === '') {
|
||||
return null;
|
||||
}
|
||||
/** @var Project|null $project */
|
||||
$project = Project::query()->find($projectId);
|
||||
|
||||
return $project?->client_id;
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -26,6 +26,7 @@ class CreateTimeEntry extends CreateRecord
|
||||
$data['organization_id'] = $member->organization_id;
|
||||
}
|
||||
}
|
||||
$data['client_id'] = TimeEntryResource::getClientIdForProject($data['project_id'] ?? null);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ class EditTimeEntry extends EditRecord
|
||||
$data['organization_id'] = $member->organization_id;
|
||||
}
|
||||
}
|
||||
$data['client_id'] = TimeEntryResource::getClientIdForProject($data['project_id'] ?? null);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -140,6 +140,10 @@ class TimeEntry extends Model implements AuditableContract
|
||||
$timeEntry->client_id = null;
|
||||
$timeEntry->tags = [];
|
||||
}
|
||||
// The client of a time entry is always the client of its project.
|
||||
if ($timeEntry->project_id === null) {
|
||||
$timeEntry->client_id = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->member_id = $memberId;
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->client_id = $project?->client_id;
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
if (strlen($record['Description']) > 5000) {
|
||||
throw new ImportException('Time entry description is too long');
|
||||
|
||||
@@ -128,7 +128,7 @@ class GenericTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->member_id = $memberId;
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->client_id = $project?->client_id;
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
$timeEntry->description = $record['description'];
|
||||
if (! in_array($record['billable'], ['true', 'false'], true)) {
|
||||
|
||||
@@ -106,7 +106,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->member_id = $memberId;
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->client_id = $project?->client_id;
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
if (strlen($record['Notes']) > 5000) {
|
||||
throw new ImportException('Time entry note is too long');
|
||||
|
||||
@@ -224,12 +224,8 @@ class SolidtimeImporter extends DefaultImporter
|
||||
throw new ImportException('Time entry references a member that does not exist in the import');
|
||||
}
|
||||
$member = $this->memberImportHelper->getModelById($memberId);
|
||||
$clientId = null;
|
||||
if ($timeEntryRow['client_id'] !== '') {
|
||||
$clientId = $this->clientImportHelper->getKeyByExternalIdentifier($timeEntryRow['client_id']);
|
||||
if ($clientId === null) {
|
||||
throw new ImportException('Time entry references a client that does not exist in the import');
|
||||
}
|
||||
if ($timeEntryRow['client_id'] !== '' && $this->clientImportHelper->getKeyByExternalIdentifier($timeEntryRow['client_id']) === null) {
|
||||
throw new ImportException('Time entry references a client that does not exist in the import');
|
||||
}
|
||||
$project = null;
|
||||
$projectId = null;
|
||||
@@ -259,7 +255,7 @@ class SolidtimeImporter extends DefaultImporter
|
||||
$timeEntry->member_id = $memberId;
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->client_id = $project?->client_id;
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
if (strlen($timeEntryRow['description']) > 5000) {
|
||||
throw new ImportException('Time entry description is too long');
|
||||
|
||||
@@ -112,7 +112,7 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
$timeEntry->member_id = $memberId;
|
||||
$timeEntry->task_id = $taskId;
|
||||
$timeEntry->project_id = $projectId;
|
||||
$timeEntry->client_id = $clientId;
|
||||
$timeEntry->client_id = $project?->client_id;
|
||||
$timeEntry->organization_id = $this->organization->id;
|
||||
$timeEntry->description = $record['Description'];
|
||||
if (! in_array($record['Billable'], ['Yes', 'No'], true)) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* The client of a time entry is always the client of its project. The importers used to take
|
||||
* the client from the import file, so time entries without a project could end up with a
|
||||
* client, and the admin panel did not remove the client when the project of a time entry was removed.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
DB::table('time_entries')
|
||||
->whereNull('project_id')
|
||||
->whereNotNull('client_id')
|
||||
->update([
|
||||
'client_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
};
|
||||
@@ -106,8 +106,10 @@ class SelfHostDatabaseConsistencyCommandTest extends TestCaseWithDatabase
|
||||
$client1 = Client::factory()->forOrganization($user->organization)->create();
|
||||
$project = Project::factory()->forOrganization($user->organization)->forClient($client1)->create();
|
||||
$timeEntry = TimeEntry::factory()->forMember($user->member)->create([
|
||||
'client_id' => $client1->getKey(),
|
||||
'project_id' => null,
|
||||
]);
|
||||
// The model removes the client of time entries without a project, so the inconsistent state is written directly
|
||||
TimeEntry::query()->whereKey($timeEntry->getKey())->update(['client_id' => $client1->getKey()]);
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('self-host:database-consistency');
|
||||
|
||||
@@ -5215,18 +5215,19 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
'time-entries:view:all',
|
||||
]);
|
||||
$client = Client::factory()->forOrganization($data->organization)->create();
|
||||
$project = Project::factory()->forOrganization($data->organization)->forClient($client)->create();
|
||||
$timeEntryWithClient = TimeEntry::factory()
|
||||
->forOrganization($data->organization)
|
||||
->forMember($data->member)
|
||||
->forProject($project)
|
||||
->create([
|
||||
'client_id' => $client->getKey(),
|
||||
'start' => Carbon::now()->subHour(),
|
||||
]);
|
||||
$timeEntryWithoutClient = TimeEntry::factory()
|
||||
->forOrganization($data->organization)
|
||||
->forMember($data->member)
|
||||
->forProject(null)
|
||||
->create([
|
||||
'client_id' => null,
|
||||
'start' => Carbon::now()->subHour(),
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
@@ -5,8 +5,10 @@ declare(strict_types=1);
|
||||
namespace Tests\Unit\Filament\Resources;
|
||||
|
||||
use App\Filament\Resources\TimeEntryResource;
|
||||
use App\Models\Client;
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Project;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
@@ -197,4 +199,54 @@ class TimeEntryResourceTest extends FilamentTestCase
|
||||
$this->assertSame($newUser->getKey(), $timeEntry->user_id);
|
||||
$this->assertSame($newOrganization->getKey(), $timeEntry->organization_id);
|
||||
}
|
||||
|
||||
public function test_update_time_entry_sets_client_of_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$organization = Organization::factory()->create();
|
||||
$member = Member::factory()->forOrganization($organization)->create();
|
||||
$oldProject = Project::factory()->forOrganization($organization)->forClient(Client::factory()->forOrganization($organization)->create())->create();
|
||||
$newClient = Client::factory()->forOrganization($organization)->create();
|
||||
$newProject = Project::factory()->forOrganization($organization)->forClient($newClient)->create();
|
||||
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($oldProject)->create();
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntryResource\Pages\EditTimeEntry::class, ['record' => $timeEntry->getKey()])
|
||||
->fillForm([
|
||||
'member_id' => $member->getKey(),
|
||||
'project_id' => $newProject->getKey(),
|
||||
])
|
||||
->call('save')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$timeEntry->refresh();
|
||||
$this->assertSame($newProject->getKey(), $timeEntry->project_id);
|
||||
$this->assertSame($newClient->getKey(), $timeEntry->client_id);
|
||||
}
|
||||
|
||||
public function test_update_time_entry_removes_client_if_project_is_removed(): void
|
||||
{
|
||||
// Arrange
|
||||
$organization = Organization::factory()->create();
|
||||
$member = Member::factory()->forOrganization($organization)->create();
|
||||
$project = Project::factory()->forOrganization($organization)->forClient(Client::factory()->forOrganization($organization)->create())->create();
|
||||
$timeEntry = TimeEntry::factory()->forMember($member)->forProject($project)->create();
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntryResource\Pages\EditTimeEntry::class, ['record' => $timeEntry->getKey()])
|
||||
->fillForm([
|
||||
'member_id' => $member->getKey(),
|
||||
'project_id' => null,
|
||||
])
|
||||
->call('save')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$timeEntry->refresh();
|
||||
$this->assertNull($timeEntry->project_id);
|
||||
$this->assertNull($timeEntry->client_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Model;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Organization;
|
||||
use App\Models\Project;
|
||||
use App\Models\Tag;
|
||||
@@ -162,6 +163,20 @@ class TimeEntryModelTest extends ModelTestAbstract
|
||||
$this->assertNull($clientId);
|
||||
}
|
||||
|
||||
public function test_saving_removes_client_if_time_entry_has_no_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$client = Client::factory()->create();
|
||||
$timeEntry = TimeEntry::factory()->forProject(null)->make();
|
||||
$timeEntry->client_id = $client->getKey();
|
||||
|
||||
// Act
|
||||
$timeEntry->save();
|
||||
|
||||
// Assert
|
||||
$this->assertNull($timeEntry->refresh()->client_id);
|
||||
}
|
||||
|
||||
public function test_computed_client_id_returns_project_client_id(): void
|
||||
{
|
||||
// Arrange
|
||||
|
||||
@@ -114,6 +114,25 @@ class GenericTimeEntriesImporterTest extends ImporterTestAbstract
|
||||
$this->fail();
|
||||
}
|
||||
|
||||
public function test_import_does_not_set_client_on_time_entry_without_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$organization = Organization::factory()->create();
|
||||
$timezone = 'Europe/Vienna';
|
||||
$importer = new GenericTimeEntriesImporter;
|
||||
$importer->init($organization);
|
||||
$data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n".
|
||||
'"Working hard","false","Big Company","","","2024-03-04T09:23:00Z","2024-03-04T10:23:00Z","","Peter Tester","peter.test@email.test"';
|
||||
|
||||
// Act
|
||||
$importer->importData($data, $timezone);
|
||||
|
||||
// Assert
|
||||
$timeEntry = TimeEntry::query()->sole();
|
||||
$this->assertNull($timeEntry->project_id);
|
||||
$this->assertNull($timeEntry->client_id);
|
||||
}
|
||||
|
||||
public function test_import_drops_billable_flags_and_rates_if_organization_can_not_use_billable_rates(): void
|
||||
{
|
||||
// Arrange
|
||||
|
||||
@@ -144,6 +144,25 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
|
||||
$this->fail();
|
||||
}
|
||||
|
||||
public function test_import_does_not_set_client_on_time_entry_without_project(): void
|
||||
{
|
||||
// Arrange
|
||||
$organization = Organization::factory()->create();
|
||||
$timezone = 'Europe/Vienna';
|
||||
$importer = new TogglTimeEntriesImporter;
|
||||
$importer->init($organization);
|
||||
$data = "User,Email,Client,Project,Task,Description,Billable,Start date,Start time,End date,End time,Duration,Tags,Amount (EUR)\n".
|
||||
'Peter Tester,peter.test@email.test,Big Company,,,"",No,2024-03-04,10:23:52,2024-03-04,11:23:52,01:00:00,"",';
|
||||
|
||||
// Act
|
||||
$importer->importData($data, $timezone);
|
||||
|
||||
// Assert
|
||||
$timeEntry = TimeEntry::query()->sole();
|
||||
$this->assertNull($timeEntry->project_id);
|
||||
$this->assertNull($timeEntry->client_id);
|
||||
}
|
||||
|
||||
public function test_import_drops_billable_flags_and_rates_if_organization_can_not_use_billable_rates(): void
|
||||
{
|
||||
// Arrange
|
||||
|
||||
@@ -174,13 +174,11 @@ class TimeEntryFilterTest extends TestCaseWithDatabase
|
||||
{
|
||||
// Arrange
|
||||
$client = Client::factory()->create();
|
||||
$timeEntryWithClient = TimeEntry::factory()->create([
|
||||
'client_id' => $client->getKey(),
|
||||
$project = Project::factory()->forClient($client)->create(['organization_id' => $client->organization_id]);
|
||||
$timeEntryWithClient = TimeEntry::factory()->forProject($project)->create([
|
||||
'organization_id' => $client->organization_id,
|
||||
]);
|
||||
$timeEntryWithoutClient = TimeEntry::factory()->create([
|
||||
'client_id' => null,
|
||||
]);
|
||||
$timeEntryWithoutClient = TimeEntry::factory()->forProject(null)->create();
|
||||
|
||||
$builder = TimeEntry::query();
|
||||
$filter = new TimeEntryFilter($builder);
|
||||
@@ -475,11 +473,11 @@ class TimeEntryFilterTest extends TestCaseWithDatabase
|
||||
{
|
||||
// Arrange
|
||||
$client = Client::factory()->create();
|
||||
TimeEntry::factory()->create([
|
||||
'client_id' => $client->getKey(),
|
||||
$project = Project::factory()->forClient($client)->create(['organization_id' => $client->organization_id]);
|
||||
TimeEntry::factory()->forProject($project)->create([
|
||||
'organization_id' => $client->organization_id,
|
||||
]);
|
||||
TimeEntry::factory()->create(['client_id' => null]);
|
||||
TimeEntry::factory()->forProject(null)->create();
|
||||
|
||||
$builder = TimeEntry::query();
|
||||
$filter = new TimeEntryFilter($builder);
|
||||
|
||||
Reference in New Issue
Block a user