Enforce billable rate restriction in controllers and importers instead of model hooks

This commit is contained in:
Constantin Graf
2026-10-14 13:29:32 +02:00
parent ab0158f77a
commit 24b937767e
18 changed files with 66 additions and 257 deletions

View File

@@ -119,7 +119,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
$timeEntry->task_id = $taskId;
$timeEntry->project_id = $projectId;
$timeEntry->client_id = $clientId;
$timeEntry->organization()->associate($this->organization);
$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 ClockifyTimeEntriesImporter extends DefaultImporter
if (! in_array($record['Billable'], ['Yes', 'No'], true)) {
throw new ImportException('Invalid billable value');
}
$timeEntry->billable = $record['Billable'] === 'Yes';
$timeEntry->billable = $this->canUseBillableRates && $record['Billable'] === 'Yes';
}
if ($isBreak) {
// Breaks can not be billable or belong to a project/task (already skipped above)

View File

@@ -74,9 +74,16 @@ abstract class DefaultImporter implements ImporterContract
protected BillableRateService $billableRateService;
/**
* Organizations that can not use billable rates import everything without billable flags and rates
*/
protected bool $canUseBillableRates;
public function init(Organization $organization): void
{
$this->organization = $organization;
$this->billableRateService = app(BillableRateService::class);
$this->canUseBillableRates = $this->billableRateService->canUseBillableRates($organization);
$this->userImportHelper = new ImportDatabaseHelper(User::class, ['email'], true, function (Builder $builder) {
/** @var Builder<User> $builder */
return $builder->belongsToOrganization($this->organization);
@@ -99,7 +106,11 @@ abstract class DefaultImporter implements ImporterContract
'string',
'in:placeholder',
],
]);
], beforeSave: function (Member $member): void {
if (! $this->canUseBillableRates) {
$member->billable_rate = null;
}
});
$this->projectImportHelper = new ImportDatabaseHelper(Project::class, ['name', 'client_id', 'organization_id'], true, function (Builder $builder) {
/** @var Builder<Project> $builder */
return $builder->where('organization_id', $this->organization->id);
@@ -126,8 +137,10 @@ abstract class DefaultImporter implements ImporterContract
if ($project->billable_rate === 0) {
$project->billable_rate = null;
}
// Note: Prevents a query per project for the billable rate restriction (see Project::booted)
$project->setRelation('organization', $this->organization);
if (! $this->canUseBillableRates) {
$project->is_billable = false;
$project->billable_rate = null;
}
});
$this->projectMemberImportHelper = new ImportDatabaseHelper(ProjectMember::class, ['project_id', 'member_id'], true, function (Builder $builder): Builder {
/** @var Builder<ProjectMember> $builder */
@@ -142,10 +155,8 @@ abstract class DefaultImporter implements ImporterContract
if ($projectMember->billable_rate === 0) {
$projectMember->billable_rate = null;
}
// Note: Prevents queries per project member for the billable rate restriction (see ProjectMember::booted)
$project = $this->projectImportHelper->getModelById($projectMember->project_id);
if ($project !== null) {
$projectMember->setRelation('project', $project->setRelation('organization', $this->organization));
if (! $this->canUseBillableRates) {
$projectMember->billable_rate = null;
}
});
$this->tagImportHelper = new ImportDatabaseHelper(Tag::class, ['name', 'organization_id'], true, function (Builder $builder): Builder {
@@ -188,7 +199,6 @@ abstract class DefaultImporter implements ImporterContract
$this->timeEntriesCreated = 0;
$this->colorService = app(ColorService::class);
$this->timezoneService = app(TimezoneService::class);
$this->billableRateService = app(BillableRateService::class);
}
/**

View File

@@ -129,12 +129,12 @@ class GenericTimeEntriesImporter extends DefaultImporter
$timeEntry->task_id = $taskId;
$timeEntry->project_id = $projectId;
$timeEntry->client_id = $clientId;
$timeEntry->organization()->associate($this->organization);
$timeEntry->organization_id = $this->organization->id;
$timeEntry->description = $record['description'];
if (! in_array($record['billable'], ['true', 'false'], true)) {
throw new ImportException('Invalid billable value');
}
$timeEntry->billable = $record['billable'] === 'true';
$timeEntry->billable = $this->canUseBillableRates && $record['billable'] === 'true';
$timeEntry->tags = $this->getTags($record['tags']);
$timeEntry->is_imported = true;
try {

View File

@@ -107,7 +107,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter
$timeEntry->task_id = $taskId;
$timeEntry->project_id = $projectId;
$timeEntry->client_id = $clientId;
$timeEntry->organization()->associate($this->organization);
$timeEntry->organization_id = $this->organization->id;
if (strlen($record['Notes']) > 5000) {
throw new ImportException('Time entry note is too long');
}
@@ -115,7 +115,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter
if (! in_array($record['Billable?'], ['Yes', 'No'], true)) {
throw new ImportException('Invalid billable value');
}
$timeEntry->billable = $record['Billable?'] === 'Yes';
$timeEntry->billable = $this->canUseBillableRates && $record['Billable?'] === 'Yes';
$timeEntry->tags = [];
$timeEntry->is_imported = true;

View File

@@ -260,7 +260,7 @@ class SolidtimeImporter extends DefaultImporter
$timeEntry->task_id = $taskId;
$timeEntry->project_id = $projectId;
$timeEntry->client_id = $clientId;
$timeEntry->organization()->associate($this->organization);
$timeEntry->organization_id = $this->organization->id;
if (strlen($timeEntryRow['description']) > 5000) {
throw new ImportException('Time entry description is too long');
}
@@ -268,7 +268,7 @@ class SolidtimeImporter extends DefaultImporter
if (! in_array($timeEntryRow['billable'], ['true', 'false'], true)) {
throw new ImportException('Invalid billable value');
}
$timeEntry->billable = $timeEntryRow['billable'] === 'true';
$timeEntry->billable = $this->canUseBillableRates && $timeEntryRow['billable'] === 'true';
// The type column does not exist in old exports
if (($timeEntryRow['type'] ?? '') !== '') {
$type = TimeEntryType::tryFrom($timeEntryRow['type']);

View File

@@ -113,12 +113,12 @@ class TogglTimeEntriesImporter extends DefaultImporter
$timeEntry->task_id = $taskId;
$timeEntry->project_id = $projectId;
$timeEntry->client_id = $clientId;
$timeEntry->organization()->associate($this->organization);
$timeEntry->organization_id = $this->organization->id;
$timeEntry->description = $record['Description'];
if (! in_array($record['Billable'], ['Yes', 'No'], true)) {
throw new ImportException('Invalid billable value');
}
$timeEntry->billable = $record['Billable'] === 'Yes';
$timeEntry->billable = $this->canUseBillableRates && $record['Billable'] === 'Yes';
$timeEntry->tags = $this->getTags($record['Tags']);
$timeEntry->is_imported = true;
try {