2026-11-10 18:08:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use App\Contracts\Executor;
|
|
|
|
|
use App\Cqs\Actions\Employee\ImportEmployees;
|
|
|
|
|
use App\Cqs\Actions\Employee\ImportEmployeesInput;
|
|
|
|
|
use Illuminate\Console\Attributes\Description;
|
|
|
|
|
use Illuminate\Console\Attributes\Signature;
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
|
|
#[Description('Synchronize all employees from a CSV file with Arcadia API')]
|
|
|
|
|
#[Signature('uwil:employees:upsert {file : The CSV file to import}')]
|
|
|
|
|
class UpsertEmployeesFormFile extends Command
|
|
|
|
|
{
|
|
|
|
|
public function handle(
|
|
|
|
|
Executor $executor,
|
|
|
|
|
ImportEmployees $action
|
|
|
|
|
): int {
|
|
|
|
|
$file = $this->argument('file');
|
|
|
|
|
|
|
|
|
|
if (! file_exists($file)) {
|
|
|
|
|
$this->error("File not found: {$file}");
|
|
|
|
|
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->info('Starting synchronous employee import from CSV...');
|
|
|
|
|
|
2026-11-10 19:29:20 +01:00
|
|
|
$stream = new \SplFileObject($file, 'r');
|
2026-11-10 18:08:41 +01:00
|
|
|
|
2026-11-10 19:29:20 +01:00
|
|
|
if (! $stream->isReadable()) {
|
2026-11-10 18:08:41 +01:00
|
|
|
$this->error("Failed to open file: {$file}");
|
|
|
|
|
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$importResult = $executor->execute($action, new ImportEmployeesInput($stream));
|
|
|
|
|
|
|
|
|
|
$this->info("Successfully inserted {$importResult->insertedCount} employees.");
|
|
|
|
|
$this->info("Successfully updated {$importResult->updatedCount} employees.");
|
|
|
|
|
|
|
|
|
|
if (! empty($importResult->errors)) {
|
|
|
|
|
$this->error('Errors occurred during import:');
|
|
|
|
|
foreach ($importResult->errors as $error) {
|
|
|
|
|
$this->error($error);
|
|
|
|
|
}
|
2026-11-17 10:32:25 +01:00
|
|
|
|
|
|
|
|
return self::FAILURE;
|
2026-11-10 18:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|