Skip to content

Commit

Permalink
Buyable checks the quantity in the basket (#111)
Browse files Browse the repository at this point in the history
* Buyable checks the quantity in the basket

* Fix tests
  • Loading branch information
daVitekPL authored Apr 19, 2023
1 parent f614ea9 commit c7b88fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function getTaxRate(): float
return $this->tax_rate ?? 0;
}

public function getBuyableByUserAttribute(?CoreUser $user = null, int $quantity = 1): bool
public function getBuyableByUserAttribute(?CoreUser $user = null, ?int $quantity = null): bool
{
return app(ProductServiceContract::class)->productIsBuyableByUser($this, $user ?? Auth::user(), false, $quantity);
}
Expand Down
22 changes: 21 additions & 1 deletion src/Services/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use EscolaLms\Cart\Events\ProductableDetached;
use EscolaLms\Cart\Events\ProductAttached;
use EscolaLms\Cart\Events\ProductDetached;
use EscolaLms\Cart\Models\Cart;
use EscolaLms\Cart\Models\Product;
use EscolaLms\Cart\Models\ProductProductable;
use EscolaLms\Cart\Models\ProductUser;
Expand Down Expand Up @@ -203,13 +204,17 @@ public function productProductablesAllOwnedByUser(Product $product, User $user):
return Product::where('products.id', $product->getKey())->whereDoesntHaveProductablesNotOwnedByUser($user)->exists();
}

public function productIsBuyableByUser(Product $product, User $user, bool $check_productables = false, int $quantity = 1): bool
public function productIsBuyableByUser(Product $product, User $user, bool $check_productables = false, ?int $quantity = null): bool
{
$limit_per_user = $product->limit_per_user;
$limit_total = $product->limit_total;

$is_purchasable = $product->purchasable;

if (is_null($quantity)) {
$quantity = $this->productQuantityInCart($user, $product) + 1;
}

$is_under_limit_per_user = is_null($limit_per_user) || ($product->getOwnedByUserQuantityAttribute($user) + $quantity <= $limit_per_user);
$is_under_limit_total = is_null($limit_total) || (($product->users_count ?? $product->users()->count()) + $quantity <= $limit_total);
$is_productables_buyable = !$check_productables || $this->productProductablesAllBuyableByUser($product, $user);
Expand Down Expand Up @@ -518,4 +523,19 @@ public function canDetachProductableFromUser(Productable $productable, User $use
{
return !$this->productableIsOwnedByUserThroughProduct($productable, $user);
}

private function productQuantityInCart(User $user, Product $product): int
{
$cart = Cart::where('user_id', $user->getAuthIdentifier())->latest()->firstOrCreate([
'user_id' => $user->getAuthIdentifier(),
]);
if (!is_null($cart)) {
$cartItem = $cart
->items()
->whereHas('buyable', fn (Builder $query) => $query->where('products.id', '=', $product->getKey()))
->first();
return !is_null($cartItem) ? $cartItem->quantity : 0;
}
return 0;
}
}
2 changes: 1 addition & 1 deletion tests/API/CartApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public function test_added_more_than_limit_per_user()
);

$this->response = $this->actingAs($user, 'api')->json('GET', '/api/cart');
$this->response->assertCreated();
$this->response->assertOk();
$this->response->assertJson(
fn (AssertableJson $json) =>
$json
Expand Down

0 comments on commit c7b88fd

Please sign in to comment.