mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-16 20:22:15 +01:00
Added more imports
This commit is contained in:
@@ -13,6 +13,7 @@ use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use App\Service\ColorService;
|
||||
use App\Service\Import\ImportDatabaseHelper;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use League\Csv\Exception as CsvException;
|
||||
@@ -22,14 +23,29 @@ class TogglTimeEntriesImporter implements ImporterContract
|
||||
{
|
||||
private Organization $organization;
|
||||
|
||||
/**
|
||||
* @var ImportDatabaseHelper<User>
|
||||
*/
|
||||
private ImportDatabaseHelper $userImportHelper;
|
||||
|
||||
/**
|
||||
* @var ImportDatabaseHelper<Project>
|
||||
*/
|
||||
private ImportDatabaseHelper $projectImportHelper;
|
||||
|
||||
/**
|
||||
* @var ImportDatabaseHelper<Tag>
|
||||
*/
|
||||
private ImportDatabaseHelper $tagImportHelper;
|
||||
|
||||
/**
|
||||
* @var ImportDatabaseHelper<Client>
|
||||
*/
|
||||
private ImportDatabaseHelper $clientImportHelper;
|
||||
|
||||
/**
|
||||
* @var ImportDatabaseHelper<Task>
|
||||
*/
|
||||
private ImportDatabaseHelper $taskImportHelper;
|
||||
|
||||
private int $timeEntriesCreated;
|
||||
@@ -39,14 +55,13 @@ class TogglTimeEntriesImporter implements ImporterContract
|
||||
{
|
||||
$this->organization = $organization;
|
||||
$this->userImportHelper = new ImportDatabaseHelper(User::class, ['email'], true, function (Builder $builder) {
|
||||
return $builder->whereHas('organizations', function (Builder $builder): Builder {
|
||||
/** @var Builder<Organization> $builder */
|
||||
return $builder->whereKey($this->organization->getKey());
|
||||
});
|
||||
/** @var Builder<User> $builder */
|
||||
return $builder->belongsToOrganization($this->organization);
|
||||
}, function (User $user) {
|
||||
$user->organizations()->attach([$this->organization->id]);
|
||||
$user->organizations()->attach($this->organization, [
|
||||
'role' => 'placeholder',
|
||||
]);
|
||||
});
|
||||
// TODO: user special after import
|
||||
$this->projectImportHelper = new ImportDatabaseHelper(Project::class, ['name', 'organization_id'], true, function (Builder $builder) {
|
||||
return $builder->where('organization_id', $this->organization->id);
|
||||
});
|
||||
@@ -62,6 +77,11 @@ class TogglTimeEntriesImporter implements ImporterContract
|
||||
$this->timeEntriesCreated = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*
|
||||
* @throws ImportException
|
||||
*/
|
||||
private function getTags(string $tags): array
|
||||
{
|
||||
if (trim($tags) === '') {
|
||||
@@ -87,7 +107,7 @@ class TogglTimeEntriesImporter implements ImporterContract
|
||||
* @throws ImportException
|
||||
*/
|
||||
#[\Override]
|
||||
public function importData(string $data, array $options): void
|
||||
public function importData(string $data): void
|
||||
{
|
||||
try {
|
||||
$colorService = app(ColorService::class);
|
||||
@@ -140,17 +160,34 @@ class TogglTimeEntriesImporter implements ImporterContract
|
||||
}
|
||||
$timeEntry->billable = $record['Billable'] === 'Yes';
|
||||
$timeEntry->tags = $this->getTags($record['Tags']);
|
||||
$timeEntry->start = Carbon::createFromFormat('Y-m-d H:i:s', $record['Start date'].' '.$record['Start time'], 'UTC');
|
||||
$timeEntry->end = Carbon::createFromFormat('Y-m-d H:i:s', $record['End date'].' '.$record['End time'], 'UTC');
|
||||
$start = Carbon::createFromFormat('Y-m-d H:i:s', $record['Start date'].' '.$record['Start time'], 'UTC');
|
||||
if ($start === false) {
|
||||
throw new ImportException('Start date ("'.$record['Start date'].'") or time ("'.$record['Start time'].'") are invalid');
|
||||
}
|
||||
$timeEntry->start = $start;
|
||||
$end = Carbon::createFromFormat('Y-m-d H:i:s', $record['End date'].' '.$record['End time'], 'UTC');
|
||||
if ($end === false) {
|
||||
throw new ImportException('End date ("'.$record['End date'].'") or time ("'.$record['End time'].'") are invalid');
|
||||
}
|
||||
$timeEntry->end = $end;
|
||||
$timeEntry->save();
|
||||
$this->timeEntriesCreated++;
|
||||
}
|
||||
} catch (ImportException $exception) {
|
||||
throw $exception;
|
||||
} catch (CsvException $exception) {
|
||||
throw new ImportException('Invalid CSV data');
|
||||
} catch (Exception $exception) {
|
||||
report($exception);
|
||||
throw new ImportException('Unknown error');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string> $header
|
||||
*
|
||||
* @throws ImportException
|
||||
*/
|
||||
private function validateHeader(array $header): void
|
||||
{
|
||||
$requiredFields = [
|
||||
|
||||
Reference in New Issue
Block a user