Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Order] Fix race condition problem with multiple order recalculations #12558

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Migrations/Version20210422105530.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210422105530 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add version field to order item';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_order_item ADD version INT DEFAULT 1 NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_order_item DROP version');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<mapped-superclass name="Sylius\Component\Core\Model\OrderItem" table="sylius_order_item">
<field name="productName" column="product_name" type="string" nullable="true"/>
<field name="variantName" column="variant_name" type="string" nullable="true"/>
<field name="version" type="integer" version="true" />

<many-to-one field="variant" target-entity="Sylius\Component\Product\Model\ProductVariantInterface">
<join-column name="variant_id" referenced-column-name="id" nullable="false" />
Expand Down
13 changes: 13 additions & 0 deletions src/Sylius/Component/Core/Model/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

class OrderItem extends BaseOrderItem implements OrderItemInterface
{
/** @var int */
protected $version = 1;

/** @var ProductVariantInterface */
protected $variant;

Expand All @@ -28,6 +31,16 @@ class OrderItem extends BaseOrderItem implements OrderItemInterface
/** @var string */
protected $variantName;

public function getVersion(): ?int
{
return $this->version;
}

public function setVersion(?int $version): void
{
$this->version = $version;
}

public function getVariant(): ?ProductVariantInterface
{
return $this->variant;
Expand Down
3 changes: 2 additions & 1 deletion src/Sylius/Component/Core/Model/OrderItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
namespace Sylius\Component\Core\Model;

use Sylius\Component\Order\Model\OrderItemInterface as BaseOrderItemInterface;
use Sylius\Component\Resource\Model\VersionedInterface;

interface OrderItemInterface extends BaseOrderItemInterface
interface OrderItemInterface extends BaseOrderItemInterface, VersionedInterface
{
public function getProduct(): ?ProductInterface;

Expand Down
11 changes: 11 additions & 0 deletions src/Sylius/Component/Core/spec/Model/OrderItemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\Component\Resource\Model\VersionedInterface;

final class OrderItemSpec extends ObjectBehavior
{
function it_implements_versioned_interface(): void
{
$this->shouldImplement(VersionedInterface::class);
}

function it_returns_0_tax_total_when_there_are_no_units(): void
{
$this->getTaxTotal()->shouldReturn(0);
Expand Down Expand Up @@ -115,4 +121,9 @@ function it_has_no_variant_by_default(): void
{
$this->getVariant()->shouldReturn(null);
}

function it_has_version_1_by_default(): void
{
$this->getVersion()->shouldReturn(1);
}
}
2 changes: 2 additions & 0 deletions src/Sylius/Component/Order/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public function removeAdjustments(?string $type = null): void

$this->removeAdjustment($adjustment);
}

$this->recalculateAdjustmentsTotal();
}

public function removeAdjustmentsRecursively(?string $type = null): void
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Component/Order/Model/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ public function removeAdjustments(?string $type = null): void
foreach ($this->getAdjustments($type) as $adjustment) {
$this->removeAdjustment($adjustment);
}

$this->recalculateAdjustmentsTotal();
}

public function removeAdjustmentsRecursively(?string $type = null): void
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Component/Order/Model/OrderItemUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public function removeAdjustments(?string $type = null): void
foreach ($this->getAdjustments($type) as $adjustment) {
$this->removeAdjustment($adjustment);
}

$this->recalculateAdjustmentsTotal();
}

public function recalculateAdjustmentsTotal(): void
Expand Down
3 changes: 3 additions & 0 deletions src/Sylius/Component/Order/spec/Model/OrderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function it_removes_adjustments_properly(AdjustmentInterface $adjustment): void
$this->removeAdjustment($adjustment);

$this->hasAdjustment($adjustment)->shouldReturn(false);
$this->getAdjustmentsTotal()->shouldReturn(0);
}

function it_removes_adjustments_recursively_properly(
Expand All @@ -197,6 +198,7 @@ function it_removes_adjustments_recursively_properly(
$this->removeAdjustmentsRecursively();

$this->hasAdjustment($orderAdjustment)->shouldReturn(false);
$this->getAdjustmentsTotal()->shouldReturn(0);
}

function it_removes_adjustments_recursively_by_type_properly(
Expand Down Expand Up @@ -232,6 +234,7 @@ function it_removes_adjustments_recursively_by_type_properly(

$this->hasAdjustment($orderPromotionAdjustment)->shouldReturn(true);
$this->hasAdjustment($orderTaxAdjustment)->shouldReturn(false);
$this->getAdjustmentsTotal('tax')->shouldReturn(0);
}

function it_returns_adjustments_recursively(
Expand Down