feat(catalog): scope availability policies by subject
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Domains\Catalog\Services;
|
||||
|
||||
use App\Domains\Catalog\Enums\AvailabilitySubject;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Purchase\Services\UserPurchaseLimitService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class CatalogItemAllowanceService
|
||||
@@ -42,17 +44,42 @@ class CatalogItemAllowanceService
|
||||
return min($availableStock, $remainingUserQuota);
|
||||
}
|
||||
|
||||
/** @param Builder<CatalogItem> $query */
|
||||
public function applyProductVisibilityPolicy(Builder $query): Builder
|
||||
{
|
||||
return $this->isVisible(
|
||||
0,
|
||||
null,
|
||||
AvailabilitySubject::Product,
|
||||
)
|
||||
? $query
|
||||
: $query->whereAvailableInCatalog();
|
||||
}
|
||||
|
||||
public function isVisible(
|
||||
?int $availableStock,
|
||||
?int $remainingUserQuota,
|
||||
AvailabilitySubject $subject,
|
||||
): bool {
|
||||
return $this->availability(
|
||||
$availableStock,
|
||||
$remainingUserQuota,
|
||||
$subject,
|
||||
)['capabilities']['display'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* subject: string,
|
||||
* maximum_quantity: int|null,
|
||||
* restrictions: list<array{code: string, message: string, scope: string}>,
|
||||
* capabilities: array{select_variant: bool, change_quantity: bool, add_to_cart: bool, buy_now: bool}
|
||||
* restrictions: list<array{code: string, message: string}>,
|
||||
* capabilities: array{display: bool, select_variant: bool, change_quantity: bool, add_to_cart: bool, buy_now: bool}
|
||||
* }
|
||||
*/
|
||||
public function availability(
|
||||
?int $availableStock,
|
||||
?int $remainingUserQuota,
|
||||
string $scope = 'product',
|
||||
AvailabilitySubject $subject = AvailabilitySubject::Product,
|
||||
): array {
|
||||
$restrictions = [];
|
||||
|
||||
@@ -60,7 +87,6 @@ class CatalogItemAllowanceService
|
||||
$restrictions[] = [
|
||||
'code' => 'user_quota_reached',
|
||||
'message' => self::USER_QUOTA_REACHED_MESSAGE,
|
||||
'scope' => $scope,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -68,11 +94,11 @@ class CatalogItemAllowanceService
|
||||
$restrictions[] = [
|
||||
'code' => 'out_of_stock',
|
||||
'message' => self::OUT_OF_STOCK_MESSAGE,
|
||||
'scope' => $scope,
|
||||
];
|
||||
}
|
||||
|
||||
$capabilities = [
|
||||
'display' => true,
|
||||
'select_variant' => true,
|
||||
'change_quantity' => true,
|
||||
'add_to_cart' => true,
|
||||
@@ -81,7 +107,10 @@ class CatalogItemAllowanceService
|
||||
|
||||
foreach ($restrictions as $restriction) {
|
||||
/** @var array<string, bool> $policy */
|
||||
$policy = config("catalog.availability_policies.{$restriction['code']}", []);
|
||||
$policy = config(
|
||||
"catalog.availability_policies.{$restriction['code']}.{$subject->value}",
|
||||
[],
|
||||
);
|
||||
|
||||
foreach ($capabilities as $capability => $allowed) {
|
||||
$capabilities[$capability] = $allowed && ($policy[$capability] ?? true);
|
||||
@@ -89,6 +118,7 @@ class CatalogItemAllowanceService
|
||||
}
|
||||
|
||||
return [
|
||||
'subject' => $subject->value,
|
||||
'maximum_quantity' => $this->maximumAddableQuantity(
|
||||
$availableStock,
|
||||
$remainingUserQuota,
|
||||
@@ -107,7 +137,6 @@ class CatalogItemAllowanceService
|
||||
$availability['restrictions'][] = [
|
||||
'code' => 'requested_quantity_exceeds_user_quota',
|
||||
'message' => $message,
|
||||
'scope' => 'product',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user