Added default importer; Fixed bug in clockify importer

This commit is contained in:
Constantin Graf
2024-03-12 17:46:27 +01:00
parent 66a1d8a38b
commit 21b09777a4
11 changed files with 157 additions and 326 deletions

View File

@@ -4,51 +4,12 @@ declare(strict_types=1);
namespace App\Service\Import\Importers;
use App\Models\Client;
use App\Models\Organization;
use App\Models\Project;
use App\Models\Task;
use App\Service\ColorService;
use App\Service\Import\ImportDatabaseHelper;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use League\Csv\Exception as CsvException;
use League\Csv\Reader;
class ClockifyProjectsImporter implements ImporterContract
class ClockifyProjectsImporter extends DefaultImporter
{
private Organization $organization;
/**
* @var ImportDatabaseHelper<Project>
*/
private ImportDatabaseHelper $projectImportHelper;
/**
* @var ImportDatabaseHelper<Client>
*/
private ImportDatabaseHelper $clientImportHelper;
/**
* @var ImportDatabaseHelper<Task>
*/
private ImportDatabaseHelper $taskImportHelper;
#[\Override]
public function init(Organization $organization): void
{
$this->organization = $organization;
$this->projectImportHelper = new ImportDatabaseHelper(Project::class, ['name', 'organization_id'], true, function (Builder $builder) {
return $builder->where('organization_id', $this->organization->id);
});
$this->clientImportHelper = new ImportDatabaseHelper(Client::class, ['name', 'organization_id'], true, function (Builder $builder) {
return $builder->where('organization_id', $this->organization->id);
});
$this->taskImportHelper = new ImportDatabaseHelper(Task::class, ['name', 'project_id', 'organization_id'], true, function (Builder $builder) {
return $builder->where('organization_id', $this->organization->id);
});
}
/**
* @throws ImportException
*/
@@ -56,7 +17,6 @@ class ClockifyProjectsImporter implements ImporterContract
public function importData(string $data): void
{
try {
$colorService = app(ColorService::class);
$reader = Reader::createFromString($data);
$reader->setHeaderOffset(0);
$reader->setDelimiter(',');
@@ -78,17 +38,14 @@ class ClockifyProjectsImporter implements ImporterContract
'organization_id' => $this->organization->id,
], [
'client_id' => $clientId,
'color' => $colorService->getRandomColor(),
'color' => $this->colorService->getRandomColor(),
]);
}
if ($record['Tasks'] !== '') {
$tasks = explode(', ', $record['Tasks']);
foreach ($tasks as $task) {
if (strlen($task) > 255) {
throw new ImportException('Task is too long');
}
$taskId = $this->taskImportHelper->getKey([
$this->taskImportHelper->getKey([
'name' => $task,
'project_id' => $projectId,
'organization_id' => $this->organization->id,
@@ -127,17 +84,4 @@ class ClockifyProjectsImporter implements ImporterContract
}
}
}
#[\Override]
public function getReport(): ReportDto
{
return new ReportDto(
clientsCreated: $this->clientImportHelper->getCreatedCount(),
projectsCreated: $this->projectImportHelper->getCreatedCount(),
tasksCreated: $this->taskImportHelper->getCreatedCount(),
timeEntriesCreated: 0,
tagsCreated: 0,
usersCreated: 0,
);
}
}