Enhanced imports with billable rates and project members; Fixed error handling and import structure

This commit is contained in:
Constantin Graf
2024-04-25 22:44:10 +02:00
committed by Constantin Graf
parent 7433648077
commit a8ce994f08
11 changed files with 128 additions and 30 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Service\Import\Importers;
use Exception;
use Illuminate\Support\Str;
use League\Csv\Exception as CsvException;
use League\Csv\Reader;
@@ -22,6 +23,7 @@ class ClockifyProjectsImporter extends DefaultImporter
$reader->setDelimiter(',');
$header = $reader->getHeader();
$this->validateHeader($header);
$billableRateKey = $this->getBillableRateKey($header);
$records = $reader->getRecords();
foreach ($records as $record) {
$clientId = null;
@@ -32,18 +34,19 @@ class ClockifyProjectsImporter extends DefaultImporter
]);
}
$projectId = null;
if ($record['Name'] !== '') {
if ($record['Project'] !== '') {
$projectId = $this->projectImportHelper->getKey([
'name' => $record['Name'],
'name' => $record['Project'],
'organization_id' => $this->organization->id,
], [
'client_id' => $clientId,
'color' => $this->colorService->getRandomColor(),
'billable_rate' => $billableRateKey !== null && $record[$billableRateKey] !== '' ? (int) (((float) $record[$billableRateKey]) * 100) : null,
]);
}
if ($record['Tasks'] !== '') {
$tasks = explode(', ', $record['Tasks']);
if ($record['Task'] !== '') {
$tasks = explode(', ', $record['Task']);
foreach ($tasks as $task) {
$this->taskImportHelper->getKey([
'name' => $task,
@@ -71,12 +74,12 @@ class ClockifyProjectsImporter extends DefaultImporter
private function validateHeader(array $header): void
{
$requiredFields = [
'Name',
'Project',
'Client',
'Status',
'Visibility',
'Billability',
'Tasks',
'Task',
];
foreach ($requiredFields as $requiredField) {
if (! in_array($requiredField, $header, true)) {
@@ -85,6 +88,22 @@ class ClockifyProjectsImporter extends DefaultImporter
}
}
/**
* @param array<string> $header
*/
private function getBillableRateKey(array $header): ?string
{
$billableRateKey = null;
foreach ($header as $value) {
if (Str::startsWith($value, 'Billable Rate (')) {
$billableRateKey = $value;
break;
}
}
return $billableRateKey;
}
#[\Override]
public function getName(): string
{