Skip to content

Commit

Permalink
Fixes #752 (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson authored Dec 5, 2022
1 parent 8d32958 commit b279c81
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/Actions/Carts/AssociateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class AssociateUser extends AbstractAction
public function execute(Cart $cart, User $user, $policy = 'merge'): self
{
if ($policy == 'merge') {
$userCart = Cart::whereUserId($user->getKey())->unMerged()->latest()->first();
$userCart = Cart::whereUserId($user->getKey())->active()->unMerged()->latest()->first();
if ($userCart) {
app(MergeCart::class)->execute($userCart, $cart);
}
}

if ($policy == 'override') {
$userCart = Cart::whereUserId($user->getKey())->unMerged()->latest()->first();
$userCart = Cart::whereUserId($user->getKey())->active()->unMerged()->latest()->first();
if ($userCart && $userCart->id != $cart->id) {
$userCart->update([
'merged_id' => $userCart->id,
Expand Down
46 changes: 46 additions & 0 deletions packages/core/tests/Unit/Actions/Carts/AssociateUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lunar\Actions\Carts\AssociateUser;
use Lunar\Models\Cart;
use Lunar\Models\Currency;
use Lunar\Models\Order;
use Lunar\Tests\Stubs\User;
use Lunar\Tests\TestCase;

Expand Down Expand Up @@ -45,4 +46,49 @@ public function can_associate_a_user()
'merged_id' => null,
]);
}

/**
* @test
*/
public function cant_associate_user_to_cart_with_order()
{
$currency = Currency::factory()->create();

$order = Order::factory()->create();

$user = User::factory()->create();

$userCart = Cart::factory()->create([
'user_id' => $user->id,
'currency_id' => $currency->id,
'order_id' => $order->id,
]);

$cart = Cart::factory()->create([
'currency_id' => $currency->id,
]);

$this->assertDatabaseHas((new Cart)->getTable(), [
'user_id' => null,
'id' => $cart->id,
'merged_id' => null,
]);

$this->assertDatabaseHas((new Cart)->getTable(), [
'user_id' => $user->id,
'id' => $userCart->id,
'merged_id' => null,
'order_id' => $order->id,
]);

$action = new AssociateUser;

$action->execute($cart, $user);

$this->assertDatabaseHas((new Cart)->getTable(), [
'user_id' => $user->id,
'id' => $cart->id,
'merged_id' => null,
]);
}
}

0 comments on commit b279c81

Please sign in to comment.