- 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.
112 lines
3.4 KiB
PHP
112 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Contracts\QueryInputEmpty;
|
|
use App\Cqs\Queries\Settings\GetSettingsQuery;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
|
use Illuminate\Support\Carbon;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class EmployeeSyncService
|
|
{
|
|
private const SYNC_TIMESTAMP_CACHE = 'EMPLOYEE_LAST_SYNC';
|
|
|
|
public function __construct(
|
|
private readonly LoggerInterface $logger,
|
|
private readonly CacheRepository $cacheRepository,
|
|
private readonly ArcadiaEmployeeService $arcadiaEmployeeService,
|
|
private readonly EmployeeUpsertService $employeeUpsertService,
|
|
private readonly GetSettingsQuery $getSettingsQuery
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param array<int, \Domain\Models\ValueObjects\ShopperSiteCode>|null $sites
|
|
*/
|
|
public function synchronize(
|
|
?\DateTimeInterface $since = null,
|
|
?array $sites = null
|
|
): void {
|
|
$sitesToSync = $sites ?? ($this->getSettingsQuery)(new QueryInputEmpty())->getShopperSiteCodes();
|
|
$sinceDate = $since ?? Carbon::createFromTimestamp(0);
|
|
|
|
$this->logger->info('Starting employee synchronization.', [
|
|
'since' => $sinceDate->format(Carbon::RFC3339),
|
|
'sites_count' => count($sitesToSync),
|
|
]);
|
|
|
|
$totalInserted = 0;
|
|
$totalUpdated = 0;
|
|
|
|
foreach ($sitesToSync as $siteCode) {
|
|
$siteIdentifier = method_exists($siteCode, 'value') ? $siteCode->value : (string) $siteCode;
|
|
|
|
$this->logger->debug("Fetching employees for site: {$siteIdentifier}", [
|
|
'site' => $siteIdentifier,
|
|
'since' => $sinceDate->format(Carbon::RFC3339),
|
|
]);
|
|
|
|
$employees = $this->arcadiaEmployeeService->getEmployeesOfSiteSince(
|
|
$siteCode,
|
|
$sinceDate
|
|
);
|
|
|
|
if ($employees === false) {
|
|
$this->logger->warning("Failed to fetch employees from Arcadia for site: {$siteIdentifier}", [
|
|
'site' => $siteIdentifier,
|
|
]);
|
|
|
|
continue;
|
|
}
|
|
|
|
$this->logger->debug("Retrieved employees for site {$siteIdentifier}.", [
|
|
'site' => $siteIdentifier,
|
|
'count' => count($employees),
|
|
]);
|
|
|
|
$result = $this->employeeUpsertService->upsertEmployees($employees);
|
|
|
|
$totalInserted += $result->insertedCount;
|
|
$totalUpdated += $result->updatedCount;
|
|
}
|
|
|
|
$this->markLastSync();
|
|
|
|
$this->logger->info('Employee synchronization completed successfully.', [
|
|
'total_inserted' => $totalInserted,
|
|
'total_updated' => $totalUpdated,
|
|
]);
|
|
}
|
|
|
|
public function getLastSync(): ?CarbonInterface
|
|
{
|
|
$timestamp = $this->cacheRepository->get(self::SYNC_TIMESTAMP_CACHE);
|
|
|
|
if (! is_string($timestamp)) {
|
|
$this->logger->debug('No last synchronization timestamp found in cache.');
|
|
|
|
return null;
|
|
}
|
|
|
|
return Carbon::parse($timestamp);
|
|
}
|
|
|
|
private function markLastSync(): void
|
|
{
|
|
$now = Carbon::now()->format(Carbon::RFC3339);
|
|
|
|
$this->cacheRepository->put(
|
|
self::SYNC_TIMESTAMP_CACHE,
|
|
$now,
|
|
3600 * 36 // Cache for 36 hours
|
|
);
|
|
|
|
$this->logger->debug('Last sync timestamp updated in cache.', [
|
|
'timestamp' => $now,
|
|
]);
|
|
}
|
|
}
|