- Moved employee upsert logic to a new service for better separation of concerns. - Updated EmployeeSyncService to utilize the new EmployeeUpsertService. - Added tests for EmployeeUpsertService to ensure correct functionality.
105 lines
3.5 KiB
PHP
105 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Models\ValueObjects\EmployeeUpsertData;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Ports\Services\EmployeeImportStorageService;
|
|
use Psr\Log\LoggerInterface;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
class EmployeeUpsertService
|
|
{
|
|
public function __construct(
|
|
private readonly LoggerInterface $logger,
|
|
private readonly CodeGenerationService $codeService,
|
|
private readonly EmployeeImportStorageService $employeeImportStorageService
|
|
) {
|
|
}
|
|
|
|
/** @param \App\Services\ArcadiaEmployee[] $employeeList */
|
|
public function upsertEmployees(array $employeeList): EmployeeUpsertServiceResult
|
|
{
|
|
Assert::allIsInstanceOf($employeeList, ArcadiaEmployee::class);
|
|
|
|
$matriculesToInsert = [];
|
|
$errors = [];
|
|
$upsertData = [];
|
|
$importStartTime = CarbonImmutable::now()->subSecond();
|
|
|
|
foreach ($employeeList as $employeeToImport) {
|
|
$matricule = $employeeToImport->matricule->value;
|
|
|
|
if (in_array($matricule, $matriculesToInsert, true)) {
|
|
$errorMsg = "Duplicate detected in the file: The matricule {$matricule} appears multiple times.";
|
|
$errors[] = $errorMsg;
|
|
|
|
$this->logger->warning($errorMsg, [
|
|
'matricule' => $matricule,
|
|
]);
|
|
|
|
continue;
|
|
}
|
|
|
|
$matriculesToInsert[] = $matricule;
|
|
|
|
// NOTE: We generate a new code for each row, but only insert it for new records.
|
|
$code = $this->codeService->generateFrom(new Matricule($matricule));
|
|
|
|
$upsertData[] = new EmployeeUpsertData(
|
|
matricule: $employeeToImport->matricule,
|
|
firstname: $employeeToImport->firstname,
|
|
lastname: $employeeToImport->lastname,
|
|
etablissement: $employeeToImport->etablissement,
|
|
service: $employeeToImport->service,
|
|
site: $employeeToImport->site,
|
|
startDate: $employeeToImport->startDate,
|
|
endDate: $employeeToImport->endDate,
|
|
code: $code
|
|
);
|
|
}
|
|
|
|
if (empty($upsertData) && empty($errors)) {
|
|
$errors[] = 'The file contains no valid data.';
|
|
$this->logger->notice('Upsert skipped: No valid employee data found in list.');
|
|
}
|
|
|
|
if (! empty($upsertData)) {
|
|
$this->logger->debug('Dispatching upsert batch to storage service.', [
|
|
'batch_size' => count($upsertData),
|
|
]);
|
|
|
|
$this->employeeImportStorageService->upsertEmployees($upsertData);
|
|
|
|
$result = $this->employeeImportStorageService
|
|
->getImportedAfter(
|
|
array_map(
|
|
fn ($m) => new Matricule($m),
|
|
$matriculesToInsert
|
|
),
|
|
$importStartTime
|
|
);
|
|
|
|
$insertedCount = $result->insertedCount;
|
|
$updatedCount = $result->updatedCount;
|
|
|
|
$this->logger->info('Batch upsert completed.', [
|
|
'inserted' => $insertedCount,
|
|
'updated' => $updatedCount,
|
|
'errors' => count($errors),
|
|
]);
|
|
} else {
|
|
$insertedCount = 0;
|
|
$updatedCount = 0;
|
|
}
|
|
|
|
return new EmployeeUpsertServiceResult(
|
|
insertedCount: $insertedCount,
|
|
updatedCount: $updatedCount,
|
|
errors: $errors,
|
|
);
|
|
}
|
|
}
|