ldc-boutique-cnc-sable/app/Queries/Dashboard/GetBoDashboardQuery.php

54 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace App\Queries\Dashboard;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Models\Employee;
use App\Services\StatsService;
use Domain\Enums\StatsCustomerType;
use Domain\Enums\StatsShopType;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Query<\App\Queries\Dashboard\GetBoDashboardQueryInput, \App\Queries\Dashboard\GetBoDashboardQueryOutput>
*/
final readonly class GetBoDashboardQuery implements Query
{
public function __construct(
private StatsService $statsService,
) {
}
#[\Override]
public function __invoke(QueryInput $input): GetBoDashboardQueryOutput
{
Assert::isInstanceOf($input, GetBoDashboardQueryInput::class);
$statsShop = [];
$adhesion = $this->statsService->getMembershipPercentage();
$statsCustomer = [];
$prevDates = null;
if ($input->start !== null && $input->end !== null) {
$prevDates = $this->statsService->getPreviousPeriod($input->start, $input->end);
$statsShop = $this->statsService->getStats(StatsShopType::cases(), $input->start, $input->end);
$statsCustomer = $this->statsService->getStats(StatsCustomerType::cases(), $input->start, $input->end);
}
$employeeCount = Employee::query()->active()->count();
$customersLinkedToEmployeeCount = Employee::query()->active()->whereHas('customer')->count();
return new GetBoDashboardQueryOutput(
employeeCount: $employeeCount,
customersLinkedToEmployeeCount: $customersLinkedToEmployeeCount,
adhesion: $adhesion,
prevDates: $prevDates,
statsShop: $statsShop,
statsCustomer: $statsCustomer,
);
}
}