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

Add optional remark param to placeOrder mutation #9

Merged
merged 17 commits into from
May 9, 2022
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
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - undecided
## [2.1.0] - Unreleased

### Added
New Event ``OxidEsales\GraphQL\Storefront\Basket\Event\AfterRemoveItem``
- New Event ``OxidEsales\GraphQL\Storefront\Basket\Event\AfterRemoveItem``
- Not mandatory ``remark`` parameter added for ``placeOrder`` mutation [PR-9](https://github.com/OXID-eSales/graphql-storefront-module/pull/9)

## [2.0.1] - 2022-01-03

### Added
New event``BeforeBasketRemoveOnPlaceOrder``
- New event``BeforeBasketRemoveOnPlaceOrder``

## [2.0.0] - 2021-12-08

Expand Down Expand Up @@ -99,6 +100,7 @@ New event``BeforeBasketRemoveOnPlaceOrder``
- `oxid-esales/graphql-account`
- `oxid-esales/graphql-checkout`

[2.1.0]: https://github.com/OXID-eSales/graphql-storefront-module/compare/v2.0.1...b-6.5.x
[2.0.1]: https://github.com/OXID-eSales/graphql-storefront-module/compare/v2.0.0...v2.0.1
[2.0.0]: https://github.com/OXID-eSales/graphql-storefront-module/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/OXID-eSales/graphql-storefront-module/compare/v1.0.0-rc1...v1.0.0
Expand Down
9 changes: 6 additions & 3 deletions src/Basket/Controller/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ public function basketPayments(ID $basketId): array
* @Mutation()
* @Right("PLACE_ORDER")
*/
public function placeOrder(ID $basketId, ?bool $confirmTermsAndConditions = null): OrderDataType
{
return $this->placeOrderService->placeOrder($basketId, $confirmTermsAndConditions);
public function placeOrder(
ID $basketId,
?bool $confirmTermsAndConditions = null,
?string $remark = null
): OrderDataType {
return $this->placeOrderService->placeOrder($basketId, $confirmTermsAndConditions, $remark);
}
}
42 changes: 42 additions & 0 deletions src/Basket/Event/AbstractItemEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\Storefront\Basket\Event;

use Symfony\Contracts\EventDispatcher\Event;
use TheCodingMachine\GraphQLite\Types\ID;

abstract class AbstractItemEvent extends Event
{
public const NAME = self::class;

/** @var ID */
protected $basketId;

/** @var float */
protected $amount;

public function __construct(
ID $basketId,
float $amount
) {
$this->basketId = $basketId;
$this->amount = $amount;
}

public function getBasketId(): ID
{
return $this->basketId;
}

public function getAmount(): float
{
return $this->amount;
}
}
24 changes: 3 additions & 21 deletions src/Basket/Event/AfterAddItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,26 @@

namespace OxidEsales\GraphQL\Storefront\Basket\Event;

use Symfony\Component\EventDispatcher\Event;
use TheCodingMachine\GraphQLite\Types\ID;

final class AfterAddItem extends Event implements BasketModifyInterface
final class AfterAddItem extends AbstractItemEvent implements BasketModifyInterface
{
public const NAME = self::class;

/** @var ID */
private $basketId;

/** @var ID */
private $productId;

/** @var float */
private $amount;
protected $productId;

public function __construct(
ID $basketId,
ID $productId,
float $amount
) {
$this->basketId = $basketId;
$this->productId = $productId;
$this->amount = $amount;
}

public function getBasketId(): ID
{
return $this->basketId;
parent::__construct($basketId, $amount);
}

public function getProductId(): ID
{
return $this->productId;
}

public function getAmount(): float
{
return $this->amount;
}
}
24 changes: 3 additions & 21 deletions src/Basket/Event/AfterRemoveItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,26 @@

namespace OxidEsales\GraphQL\Storefront\Basket\Event;

use Symfony\Contracts\EventDispatcher\Event;
use TheCodingMachine\GraphQLite\Types\ID;

final class AfterRemoveItem extends Event implements BasketModifyInterface
final class AfterRemoveItem extends AbstractItemEvent implements BasketModifyInterface
{
public const NAME = self::class;

/** @var ID */
private $basketId;

/** @var ID */
private $basketItemId;

/** @var float */
private $amount;
protected $basketItemId;

public function __construct(
ID $basketId,
ID $basketItemId,
float $amount
) {
$this->basketId = $basketId;
$this->basketItemId = $basketItemId;
$this->amount = $amount;
}

public function getBasketId(): ID
{
return $this->basketId;
parent::__construct($basketId, $amount);
}

public function getBasketItemId(): ID
{
return $this->basketItemId;
}

public function getAmount(): float
{
return $this->amount;
}
}
24 changes: 3 additions & 21 deletions src/Basket/Event/BeforeAddItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,29 @@

namespace OxidEsales\GraphQL\Storefront\Basket\Event;

use Symfony\Component\EventDispatcher\Event;
use TheCodingMachine\GraphQLite\Types\ID;

final class BeforeAddItem extends Event implements BasketModifyInterface
final class BeforeAddItem extends AbstractItemEvent implements BasketModifyInterface
{
public const NAME = self::class;

/** @var ID */
private $basketId;

/** @var ID */
private $productId;

/** @var float */
private $amount;
protected $productId;

public function __construct(
ID $basketId,
ID $productId,
float $amount
) {
$this->basketId = $basketId;
$this->productId = $productId;
$this->amount = $amount;
}

public function getBasketId(): ID
{
return $this->basketId;
parent::__construct($basketId, $amount);
}

public function getProductId(): ID
{
return $this->productId;
}

public function getAmount(): float
{
return $this->amount;
}

public function setAmount(float $amount): void
{
$this->amount = $amount;
Expand Down
24 changes: 3 additions & 21 deletions src/Basket/Event/BeforeRemoveItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,29 @@

namespace OxidEsales\GraphQL\Storefront\Basket\Event;

use Symfony\Component\EventDispatcher\Event;
use TheCodingMachine\GraphQLite\Types\ID;

final class BeforeRemoveItem extends Event implements BasketModifyInterface
final class BeforeRemoveItem extends AbstractItemEvent implements BasketModifyInterface
{
public const NAME = self::class;

/** @var ID */
private $basketItemId;

/** @var float */
private $amount;

/** @var ID */
private $basketId;
protected $basketItemId;

public function __construct(
ID $basketId,
ID $basketItemId,
float $amount
) {
$this->basketId = $basketId;
$this->basketItemId = $basketItemId;
$this->amount = $amount;
}

public function getBasketId(): ID
{
return $this->basketId;
parent::__construct($basketId, $amount);
}

public function getBasketItemId(): ID
{
return $this->basketItemId;
}

public function getAmount(): float
{
return $this->amount;
}

public function setAmount(float $amount): void
{
$this->amount = $amount;
Expand Down
46 changes: 31 additions & 15 deletions src/Basket/Infrastructure/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace OxidEsales\GraphQL\Storefront\Basket\Infrastructure;

use OxidEsales\Eshop\Application\Model\Address as EshopAddressModel;
use OxidEsales\Eshop\Application\Model\Article as EshopAricleModel;
use OxidEsales\Eshop\Application\Model\Article as EshopArticleModel;
use OxidEsales\Eshop\Application\Model\Basket as EshopBasketModel;
use OxidEsales\Eshop\Application\Model\BasketItem;
use OxidEsales\Eshop\Application\Model\DeliveryList as EshopDeliveryListModel;
Expand All @@ -35,6 +35,7 @@
use OxidEsales\GraphQL\Storefront\DeliveryMethod\DataType\BasketDeliveryMethod as BasketDeliveryMethodDataType;
use OxidEsales\GraphQL\Storefront\Order\DataType\Order as OrderDataType;
use OxidEsales\GraphQL\Storefront\Payment\DataType\BasketPayment;
use OxidEsales\GraphQL\Storefront\Product\Exception\ProductNotOrderable;
use OxidEsales\GraphQL\Storefront\Shared\DataType\Price as PriceDataType;
use OxidEsales\GraphQL\Storefront\Shared\Infrastructure\Basket as SharedBasketInfrastructure;
use OxidEsales\GraphQL\Storefront\Shared\Infrastructure\Repository;
Expand Down Expand Up @@ -62,8 +63,14 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
}

public function addBasketItem(BasketDataType $basket, ID $productId, float $amount): bool
{
public function addBasketItem(
BasketDataType $basket,
ID $productId,
float $amount,
?array $persParams = null,
?array $select = null,
bool $forceOverride = false
): bool {
$model = $basket->getEshopModel();

$item = $this->getBasketItemByProductId($model, (string) $productId);
Expand All @@ -73,14 +80,14 @@ public function addBasketItem(BasketDataType $basket, ID $productId, float $amou
$alreadyInBasket = (int) $item->getRawFieldData('oxamount');
}

/** @var EshopAricleModel */
$product = oxNew(EshopAricleModel::class);
/** @var EshopArticleModel */
$product = oxNew(EshopArticleModel::class);
$product->load((string) $productId);
$productStock = $product->getStock();
$onStock = $product->checkForStock($amount, $alreadyInBasket);
$blOverride = false;

if ($onStock === false) {
if ($onStock !== true) {
$blOverride = true;

if ($productStock == 0) {
Expand All @@ -98,16 +105,17 @@ public function addBasketItem(BasketDataType $basket, ID $productId, float $amou
BasketItemAmountLimitedStock::limitedAvailability((string) $productId, $productStock, $item ? $item->getId() : null)
);
}
} elseif ($onStock !== true) {
$amount = $onStock;
$blOverride = true;
}

if (!$product->isBuyable()) {
$amount = 0;

GraphQLQueryHandler::addError(
BasketItemAmountLimitedStock::limitedAvailability((string) $productId, $onStock, $item ? $item->getId() : null)
new ProductNotOrderable((string) $productId)
);
}

$model->addItemToBasket((string) $productId, $amount, null, $blOverride);
$model->addItemToBasket((string) $productId, $amount, $select, $forceOverride ?: $blOverride, $persParams);

return true;
}
Expand All @@ -130,8 +138,8 @@ public function removeBasketItem(BasketDataType $basket, ID $basketItemId, float
$productId = (string) $basketItem->getRawFieldData('oxartid');
$params = $basketItem->getPersParams();

/** @var EshopAricleModel */
$product = oxNew(EshopAricleModel::class);
/** @var EshopArticleModel */
$product = oxNew(EshopArticleModel::class);
$product->load($productId);
$onStock = $product->checkForStock($amountRemaining);

Expand Down Expand Up @@ -257,7 +265,8 @@ public function getBasketAvailableDeliveryMethods(

public function placeOrder(
CustomerDataType $customer,
BasketDataType $userBasket
BasketDataType $userBasket,
?string $remark = null
): OrderDataType {

/** @var EshopUserModel $userModel */
Expand Down Expand Up @@ -290,7 +299,14 @@ public function placeOrder(

/** @var OrderModel $orderModel */
$orderModel = oxNew(OrderModel::class);
$status = $orderModel->finalizeOrder($basketModel, $userModel);

if ($remark) {
$orderModel->assign([
'oxremark' => EshopRegistry::getRequest()->checkParamSpecialChars($remark),
]);
}

$status = $orderModel->finalizeOrder($basketModel, $userModel);

// performing special actions after user finishes order (assignment to special user groups)
$userModel->onOrderExecute($basketModel, $status);
Expand Down
Loading