- Implement automated synchronization of employees via Arcadia API (EmployeeSyncService, SynchronizeEmployeesWithArcadiaApi, FetchAndUpsertArcadiaEmployees) - Add Back-Office application settings for employee sites (Setting singleton, GetSettingsQuery, UpdateSettings, SettingsController, EmployeeSitesInputComponent.vue) - Strongly type Employee attributes using Domain Value Objects (ShopperEstablishmentCode, ShopperSiteCode, ShopperServiceCode) and Eloquent Casts - Migrate pagination from Domain to Shared\Pagination namespace across queries and inputs - Remove legacy and dead CSV employee import/export code - Restore Domain\Ports\Services\CsvGenerator interface for Regate order generation - Add comprehensive Pest unit, integration, and feature test coverage
116 lines
3.8 KiB
PHP
116 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
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 Uwil\ArcadiaApiPhpClient\Error\ApiError;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee as LibEmployee;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId as LibArcadiaEmployeeId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId as LibSiteId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\PageSize as LibPageSize;
|
|
use Uwil\ArcadiaApiPhpClient\Service\EmployeeService as LibEmployeeService;
|
|
|
|
class ArcadiaEmployeeService
|
|
{
|
|
private const DEFAULT_PAGE_SIZE = 100;
|
|
|
|
public function __construct(
|
|
private readonly LibEmployeeService $employeeService,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
/** @return \App\Services\ArcadiaEmployee[]|false Returns false when fetching fails. */
|
|
public function getEmployeesOfSiteSince(
|
|
ShopperSiteCode $site,
|
|
\DateTimeInterface $since
|
|
): array | false {
|
|
$action = "employees of site '{$site->value}' since '{$since->format(\DATE_ATOM)}'";
|
|
|
|
$this->logger->debug("Fetching {$action}");
|
|
|
|
$result = $this->employeeService->streamAllOfSitesSince(
|
|
sites: $this->mapSiteCodeToLib($site),
|
|
pageSize: new LibPageSize(self::DEFAULT_PAGE_SIZE),
|
|
since: $since
|
|
);
|
|
|
|
$this->logger->debug("Succeed fetching {$action}");
|
|
|
|
/** @var \App\Services\ArcadiaEmployee[] $output */
|
|
$output = [];
|
|
$apiError = $result->each(
|
|
function (LibEmployee $libEmployee) use (&$output) {
|
|
$output[] = $this->mapLibEmployees($libEmployee);
|
|
}
|
|
);
|
|
|
|
if ($apiError instanceof ApiError) {
|
|
$message = "Failed to fetch {$action}, reason {$apiError->message()}";
|
|
$this->logger->error($message);
|
|
report(new \RuntimeException($message));
|
|
|
|
return false;
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
/** @return \App\Services\ArcadiaEmployee|null|false Returns false when fetching fails. */
|
|
public function employeeExists(Matricule $matricule): ArcadiaEmployee | null | false
|
|
{
|
|
$action = 'specific employee using matricule';
|
|
|
|
$this->logger->debug("Fetching {$action}");
|
|
|
|
$arcadiaEmployeeId = $this->mapMatriculeToLib($matricule);
|
|
|
|
$result = $this->employeeService->findBy($arcadiaEmployeeId);
|
|
|
|
if ($result instanceof ApiError) {
|
|
$message = "Failed to fetch {$action}, reason {$result->message()}";
|
|
$this->logger->error($message);
|
|
report(new \RuntimeException($message));
|
|
|
|
return false;
|
|
}
|
|
|
|
if ($result instanceof LibEmployee) {
|
|
return $this->mapLibEmployees($result);
|
|
}
|
|
|
|
$this->logger->debug("Succeed fething {$action}");
|
|
|
|
return null;
|
|
}
|
|
|
|
private function mapLibEmployees(LibEmployee $libEmployee): ArcadiaEmployee
|
|
{
|
|
return new ArcadiaEmployee(
|
|
new Matricule($libEmployee->matriculePublique->value),
|
|
$libEmployee->lastname,
|
|
$libEmployee->firstname,
|
|
$libEmployee->start_date,
|
|
$libEmployee->end_date,
|
|
new ShopperEstablishmentCode($libEmployee->establishment->value),
|
|
new ShopperSiteCode($libEmployee->site->value),
|
|
new ShopperServiceCode($libEmployee->service->value),
|
|
);
|
|
}
|
|
|
|
private function mapSiteCodeToLib(ShopperSiteCode $site): LibSiteId
|
|
{
|
|
return new LibSiteId($site->value);
|
|
}
|
|
|
|
private function mapMatriculeToLib(Matricule $value): LibArcadiaEmployeeId
|
|
{
|
|
return new LibArcadiaEmployeeId($value->value);
|
|
}
|
|
}
|