From 2dacdbabd608d255e2f552ef21f1503c05d223ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 10 Nov 2026 15:14:24 +0100 Subject: [PATCH] refactor(service): reintroduce EmployeeUpsertService and related classes - 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. --- app/Services/EmployeeSyncService.php | 90 +-------------- app/Services/EmployeeUpsertService.php | 105 ++++++++++++++++++ ...lt.php => EmployeeUpsertServiceResult.php} | 2 +- tests/Architecture/NamingConventionsTest.php | 4 +- .../App/Services/EmployeeSyncServiceTest.php | 51 +++++---- .../Services/EmployeeUpsertServiceTest.php | 73 ++++++++++++ 6 files changed, 214 insertions(+), 111 deletions(-) create mode 100644 app/Services/EmployeeUpsertService.php rename app/Services/{EmployeeSyncServiceResult.php => EmployeeUpsertServiceResult.php} (88%) create mode 100644 tests/Integration/WithApp/App/Services/EmployeeUpsertServiceTest.php diff --git a/app/Services/EmployeeSyncService.php b/app/Services/EmployeeSyncService.php index 8de82f88..6c042ecd 100644 --- a/app/Services/EmployeeSyncService.php +++ b/app/Services/EmployeeSyncService.php @@ -6,11 +6,7 @@ namespace App\Services; use App\Contracts\QueryInputEmpty; use App\Cqs\Queries\Settings\GetSettingsQuery; -use Carbon\CarbonImmutable; use Carbon\CarbonInterface; -use Domain\Models\ValueObjects\EmployeeUpsertData; -use Domain\Models\ValueObjects\Matricule; -use Domain\Ports\Services\EmployeeImportStorageService; use Illuminate\Contracts\Cache\Repository as CacheRepository; use Illuminate\Support\Carbon; use Psr\Log\LoggerInterface; @@ -23,8 +19,7 @@ class EmployeeSyncService private readonly LoggerInterface $logger, private readonly CacheRepository $cacheRepository, private readonly ArcadiaEmployeeService $arcadiaEmployeeService, - private readonly CodeGenerationService $codeService, - private readonly EmployeeImportStorageService $employeeImportStorageService, + private readonly EmployeeUpsertService $employeeUpsertService, private readonly GetSettingsQuery $getSettingsQuery ) { } @@ -73,7 +68,7 @@ class EmployeeSyncService 'count' => count($employees), ]); - $result = $this->upsertEmployees($employees); + $result = $this->employeeUpsertService->upsertEmployees($employees); $totalInserted += $result->insertedCount; $totalUpdated += $result->updatedCount; @@ -114,85 +109,4 @@ class EmployeeSyncService 'timestamp' => $now, ]); } - - /** @param \App\Services\ArcadiaEmployee[] $employeeList */ - private function upsertEmployees(array $employeeList): EmployeeSyncServiceResult - { - $matriculesToInsert = []; - $errors = []; - $upsertData = []; - $importStartTime = CarbonImmutable::now()->subSecond(); - - foreach ($employeeList as $employeeImport) { - $matricule = $employeeImport->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: $employeeImport->matricule, - firstname: $employeeImport->firstname, - lastname: $employeeImport->lastname, - etablissement: $employeeImport->etablissement, - service: $employeeImport->service, - site: $employeeImport->site, - startDate: $employeeImport->startDate, - endDate: $employeeImport->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 EmployeeSyncServiceResult( - insertedCount: $insertedCount, - updatedCount: $updatedCount, - errors: $errors, - ); - } } diff --git a/app/Services/EmployeeUpsertService.php b/app/Services/EmployeeUpsertService.php new file mode 100644 index 00000000..38f5e036 --- /dev/null +++ b/app/Services/EmployeeUpsertService.php @@ -0,0 +1,105 @@ +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, + ); + } +} diff --git a/app/Services/EmployeeSyncServiceResult.php b/app/Services/EmployeeUpsertServiceResult.php similarity index 88% rename from app/Services/EmployeeSyncServiceResult.php rename to app/Services/EmployeeUpsertServiceResult.php index 4f41d902..7c84267a 100644 --- a/app/Services/EmployeeSyncServiceResult.php +++ b/app/Services/EmployeeUpsertServiceResult.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace App\Services; -class EmployeeSyncServiceResult +class EmployeeUpsertServiceResult { public function __construct( public readonly int $insertedCount, diff --git a/tests/Architecture/NamingConventionsTest.php b/tests/Architecture/NamingConventionsTest.php index 0e108688..982e6fd6 100644 --- a/tests/Architecture/NamingConventionsTest.php +++ b/tests/Architecture/NamingConventionsTest.php @@ -4,7 +4,7 @@ declare(strict_types=1); use App\Exceptions\GlobalHandler; use App\Services\ArcadiaEmployee; -use App\Services\EmployeeSyncServiceResult; +use App\Services\EmployeeUpsertServiceResult; use Symfony\Component\Finder\Finder; describe('naming conventions in app', function (): void { @@ -52,7 +52,7 @@ describe('naming conventions in app', function (): void { ->toHaveSuffix('Service') ->ignoring([ ArcadiaEmployee::class, - EmployeeSyncServiceResult::class, + EmployeeUpsertServiceResult::class, ]); arch('queries have Query, QueryInput or QueryOutput suffix', function (): void { diff --git a/tests/Integration/WithApp/App/Services/EmployeeSyncServiceTest.php b/tests/Integration/WithApp/App/Services/EmployeeSyncServiceTest.php index 10f21b3b..f14f8624 100644 --- a/tests/Integration/WithApp/App/Services/EmployeeSyncServiceTest.php +++ b/tests/Integration/WithApp/App/Services/EmployeeSyncServiceTest.php @@ -6,14 +6,14 @@ use App\Cqs\Queries\Settings\GetSettingsQuery; use App\Models\Setting; use App\Services\ArcadiaEmployee; use App\Services\ArcadiaEmployeeService; -use App\Services\CodeGenerationService; use App\Services\EmployeeSyncService; +use App\Services\EmployeeUpsertService; +use App\Services\EmployeeUpsertServiceResult; use Carbon\CarbonImmutable; use Domain\Models\ValueObjects\Matricule; use Domain\Models\ValueObjects\ShopperEstablishmentCode; use Domain\Models\ValueObjects\ShopperServiceCode; use Domain\Models\ValueObjects\ShopperSiteCode; -use Domain\Ports\Services\EmployeeImportStorageService; use Illuminate\Contracts\Cache\Repository as CacheRepository; use Illuminate\Foundation\Testing\RefreshDatabase; use Psr\Log\LoggerInterface; @@ -33,8 +33,7 @@ describe('EmployeeSyncService', function (): void { $logger = app(LoggerInterface::class); $cache = app(CacheRepository::class); - $codeService = app(CodeGenerationService::class); - $storageService = \Mockery::mock(EmployeeImportStorageService::class); + $upsertService = \Mockery::mock(EmployeeUpsertService::class); $employee = new ArcadiaEmployee( matricule: new Matricule('1234567'), @@ -47,19 +46,25 @@ describe('EmployeeSyncService', function (): void { service: new ShopperServiceCode('101') ); - $storageService->shouldReceive('upsertEmployees') - ->once() + $upsertService->shouldReceive('upsertEmployees') ->with(\Mockery::on(static function (array $data): bool { return count($data) === 1 && $data[0]->matricule->value === '1234567'; - })); - - $storageService->shouldReceive('getImportedAfter') + })) ->once() - ->andReturn((object) [ - 'insertedCount' => 1, - 'updatedCount' => 0, - 'matriculesInserted' => [new Matricule('1234567')], - ]); + ->andReturn(new EmployeeUpsertServiceResult( + insertedCount: 1, + updatedCount: 0, + errors: [] + )); + + $upsertService->shouldReceive('upsertEmployees') + ->with(\Mockery::on(static fn (array $data) => empty($data))) + ->once() + ->andReturn(new EmployeeUpsertServiceResult( + insertedCount: 0, + updatedCount: 0, + errors: [] + )); $arcadiaService = \Mockery::mock(ArcadiaEmployeeService::class); $arcadiaService->shouldReceive('getEmployeesOfSiteSince') @@ -75,8 +80,7 @@ describe('EmployeeSyncService', function (): void { $logger, $cache, $arcadiaService, - $codeService, - $storageService, + $upsertService, app(GetSettingsQuery::class) ); @@ -91,8 +95,16 @@ describe('EmployeeSyncService', function (): void { // GIVEN $logger = app(LoggerInterface::class); $cache = app(CacheRepository::class); - $codeService = app(CodeGenerationService::class); - $storageService = \Mockery::mock(EmployeeImportStorageService::class); + $upsertService = \Mockery::mock(EmployeeUpsertService::class); + + $upsertService->shouldReceive('upsertEmployees') + ->with(\Mockery::on(static fn (array $data) => empty($data))) + ->once() + ->andReturn(new EmployeeUpsertServiceResult( + insertedCount: 0, + updatedCount: 0, + errors: [] + )); $arcadiaService = \Mockery::mock(ArcadiaEmployeeService::class); $arcadiaService->shouldReceive('getEmployeesOfSiteSince') @@ -104,8 +116,7 @@ describe('EmployeeSyncService', function (): void { $logger, $cache, $arcadiaService, - $codeService, - $storageService, + $upsertService, app(GetSettingsQuery::class) ); diff --git a/tests/Integration/WithApp/App/Services/EmployeeUpsertServiceTest.php b/tests/Integration/WithApp/App/Services/EmployeeUpsertServiceTest.php new file mode 100644 index 00000000..08d662a2 --- /dev/null +++ b/tests/Integration/WithApp/App/Services/EmployeeUpsertServiceTest.php @@ -0,0 +1,73 @@ +employee_sites = ['PA', 'LY']; + $setting->saveOrFail(); + + $logger = app(LoggerInterface::class); + $codeService = app(CodeGenerationService::class); + $storageService = \Mockery::mock(EmployeeImportStorageService::class); + + $employee = new ArcadiaEmployee( + matricule: new Matricule('1234567'), + lastname: 'Dupont', + firstname: 'Jean', + startDate: CarbonImmutable::parse('2026-01-01'), + endDate: null, + etablissement: new ShopperEstablishmentCode('ETAB'), + site: new ShopperSiteCode('PA'), + service: new ShopperServiceCode('101') + ); + + $storageService->shouldReceive('upsertEmployees') + ->once() + ->with(\Mockery::on(static function (array $data): bool { + return count($data) === 1 && $data[0]->matricule->value === '1234567'; + })); + + $storageService->shouldReceive('getImportedAfter') + ->once() + ->andReturn((object) [ + 'insertedCount' => 1, + 'updatedCount' => 0, + 'matriculesInserted' => [new Matricule('1234567')], + ]); + + $service = new EmployeeUpsertService( + $logger, + $codeService, + $storageService, + ); + + // WHEN + $result = $service->upsertEmployees([$employee]); + + // THEN + expect($result)->toBeInstanceOf(EmployeeUpsertServiceResult::class); + }); +});