2026-11-10 18:08:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Cqs\Actions\Employee;
|
|
|
|
|
|
|
|
|
|
use App\Contracts\Action;
|
|
|
|
|
use App\Contracts\ActionInput;
|
|
|
|
|
use App\Services\ArcadiaEmployee;
|
2026-11-10 19:29:20 +01:00
|
|
|
use App\Services\EmployeeCsvImportLineService as ServicesEmployeeCsvImportLine;
|
2026-11-10 18:08:41 +01:00
|
|
|
use App\Services\EmployeeCsvReaderService;
|
|
|
|
|
use App\Services\EmployeeUpsertService;
|
|
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-10 19:29:20 +01:00
|
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Employee\ImportEmployeesInput, \App\Cqs\Actions\Employee\ImportEmployeesOutput>
|
2026-11-10 18:08:41 +01:00
|
|
|
*/
|
|
|
|
|
final class ImportEmployees implements Action
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly LoggerInterface $logger,
|
|
|
|
|
private readonly EmployeeCsvReaderService $employeeCsvReaderService,
|
|
|
|
|
private readonly EmployeeUpsertService $employeeUpsertService,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param \App\Cqs\Actions\Employee\ImportEmployeesInput $input */
|
2026-11-10 19:29:20 +01:00
|
|
|
public function __invoke(ActionInput $input): ImportEmployeesOutput
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
|
|
|
|
Assert::isAOf($input, ImportEmployeesInput::class);
|
|
|
|
|
|
|
|
|
|
$csvLines = $this->employeeCsvReaderService->importEmployeesFromCsv($input->inputStream);
|
|
|
|
|
|
|
|
|
|
$this->logger->debug('Retrieved employees from CSV.', [
|
|
|
|
|
'count' => count([...$csvLines]),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-15 16:44:54 +01:00
|
|
|
$csvErrors = array_filter(
|
2026-11-10 18:08:41 +01:00
|
|
|
$csvLines,
|
|
|
|
|
fn ($employee) => is_string($employee)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$importedEmployees = array_map(
|
|
|
|
|
fn (ServicesEmployeeCsvImportLine $csvLine) => $this->mapToArcadiaEmployee($csvLine),
|
|
|
|
|
array_filter(
|
|
|
|
|
$csvLines,
|
|
|
|
|
fn ($employee) => $employee instanceof ServicesEmployeeCsvImportLine
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$employees = array_filter(
|
|
|
|
|
$importedEmployees,
|
|
|
|
|
fn ($employee) => $employee instanceof ArcadiaEmployee
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-15 16:44:54 +01:00
|
|
|
$mappingErrors = array_filter(
|
2026-11-10 18:08:41 +01:00
|
|
|
$importedEmployees,
|
|
|
|
|
fn ($employee) => is_string($employee)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $this->employeeUpsertService->upsertEmployees($employees);
|
|
|
|
|
|
|
|
|
|
$totalInserted = $result->insertedCount;
|
|
|
|
|
$totalUpdated = $result->updatedCount;
|
2026-11-15 16:44:54 +01:00
|
|
|
$errors = array_values([
|
|
|
|
|
...$csvErrors,
|
|
|
|
|
...$mappingErrors,
|
|
|
|
|
...$result->errors,
|
|
|
|
|
]);
|
2026-11-10 18:08:41 +01:00
|
|
|
|
|
|
|
|
$this->logger->info('Employee synchronization completed successfully.', [
|
|
|
|
|
'total_inserted' => $totalInserted,
|
|
|
|
|
'total_updated' => $totalUpdated,
|
2026-11-15 16:44:54 +01:00
|
|
|
'errors_count' => count($errors),
|
2026-11-10 18:08:41 +01:00
|
|
|
]);
|
|
|
|
|
|
2026-11-10 19:29:20 +01:00
|
|
|
return new ImportEmployeesOutput(
|
2026-11-10 18:08:41 +01:00
|
|
|
insertedCount: $totalInserted,
|
|
|
|
|
updatedCount: $totalUpdated,
|
|
|
|
|
errors: $errors,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function mapToArcadiaEmployee(ServicesEmployeeCsvImportLine $csvLine): ArcadiaEmployee | string
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return new ArcadiaEmployee(
|
|
|
|
|
matricule: new Matricule($csvLine->matricule),
|
|
|
|
|
lastname: $csvLine->lastName,
|
|
|
|
|
firstname: $csvLine->firstName,
|
|
|
|
|
startDate: $csvLine->startDate,
|
2026-11-15 16:44:54 +01:00
|
|
|
endDate: $csvLine->endDate ?? $csvLine->expectedEndDate,
|
2026-11-10 18:08:41 +01:00
|
|
|
etablissement: new ShopperEstablishmentCode($csvLine->establishment),
|
|
|
|
|
site: new ShopperSiteCode($csvLine->site),
|
|
|
|
|
service: new ShopperServiceCode($csvLine->service),
|
|
|
|
|
);
|
|
|
|
|
} catch (\Throwable $e) {
|
2026-11-17 10:32:25 +01:00
|
|
|
return "Matricule {$csvLine->matricule}: {$e->getMessage()}";
|
2026-11-10 18:08:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|