Skip to content

Commit

Permalink
Fix potential bug when creating zero waste orders.
Browse files Browse the repository at this point in the history
- Store zero waste credentials at order or customer level "seamlessly".
- Stop clearing credentials when assigning customer.
  • Loading branch information
alexsegura committed Feb 7, 2025
1 parent c76fc6c commit 88b1a35
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
38 changes: 26 additions & 12 deletions src/Entity/Sylius/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
use AppBundle\Entity\ReusablePackaging;
use AppBundle\Entity\Task\RecurrenceRule;
use AppBundle\Entity\Vendor;
use AppBundle\LoopEat\OAuthCredentialsInterface as LoopeatOAuthCredentialsInterface;
use AppBundle\Payment\MercadopagoPreferenceResponse;
use AppBundle\Sylius\Order\AdjustmentInterface;
use AppBundle\Sylius\Order\OrderInterface;
Expand Down Expand Up @@ -628,15 +627,6 @@ public function getCustomer(): ?CustomerInterface
public function setCustomer(?CustomerInterface $customer): void
{
$this->customer = $customer;

if (null !== $customer && $this->hasLoopEatCredentials()) {

WMAssert::isInstanceOf($this->customer, LoopeatOAuthCredentialsInterface::class);

$this->customer->setLoopeatAccessToken($this->loopeatCredentials->getLoopeatAccessToken());
$this->customer->setLoopeatRefreshToken($this->loopeatCredentials->getLoopeatRefreshToken());
$this->clearLoopEatCredentials();
}
}

public function getTaxTotal(): int
Expand Down Expand Up @@ -1753,11 +1743,23 @@ public function getLoopeatAccessToken()
return null;
}

return $this->loopeatCredentials->getLoopeatAccessToken();
$accessToken = $this->loopeatCredentials->getLoopeatAccessToken();

// Guest checkout
if (null !== $accessToken) {

return $accessToken;
}

return $this->customer->getLoopeatAccessToken();
}

public function setLoopeatAccessToken($accessToken)
{
if (null !== $this->customer) {
$this->customer->setLoopeatAccessToken($accessToken);
}

if (null === $this->loopeatCredentials) {

$this->loopeatCredentials = new OrderCredentials();
Expand All @@ -1774,11 +1776,23 @@ public function getLoopeatRefreshToken()
return null;
}

return $this->loopeatCredentials->getLoopeatRefreshToken();
$refreshToken = $this->loopeatCredentials->getLoopeatRefreshToken();

// Guest checkout
if (null !== $refreshToken) {

return $refreshToken;
}

return $this->customer->getLoopeatRefreshToken();
}

public function setLoopeatRefreshToken($refreshToken)
{
if (null !== $this->customer) {
$this->customer->setLoopeatRefreshToken($refreshToken);
}

if (null === $this->loopeatCredentials) {

$this->loopeatCredentials = new OrderCredentials();
Expand Down
7 changes: 3 additions & 4 deletions src/LoopEat/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,13 @@ public function createOrder(OrderInterface $order)

$currentRestaurant = $this->currentRestaurant($restaurant);

// Assert::isInstanceOf($order->getCustomer(), CustomerInterface::class);
Assert::isInstanceOf($order->getCustomer(), OAuthCredentialsInterface::class);
Assert::isInstanceOf($order, OAuthCredentialsInterface::class);

$response = $this->client->request('POST', sprintf('/api/v1/partners/restaurants/%s/orders', $currentRestaurant['id']), [
'headers' => [
'Authorization' => sprintf('Bearer %s', $order->getCustomer()->getLoopeatAccessToken())
'Authorization' => sprintf('Bearer %s', $order->getLoopeatAccessToken())
],
'oauth_credentials' => $order->getCustomer(),
'oauth_credentials' => $order,
'json' => [
'order' => [
'external_id' => $order->getId(),
Expand Down
9 changes: 2 additions & 7 deletions tests/AppBundle/LoopEat/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public function testCreateOrder()
{
$restaurant = new Restaurant();

$customer = $this->prophesize(CustomerInterface::class);
$customer
->getLoopeatAccessToken()
->willReturn('123456abcdef');

$order = $this->prophesize(OrderInterface::class);

$order
Expand All @@ -76,8 +71,8 @@ public function testCreateOrder()
->willReturn($restaurant);

$order
->getCustomer()
->willReturn($customer->reveal());
->getLoopeatAccessToken()
->willReturn('123456abcdef');

$order
->getFormatsToDeliverForLoopeat()
Expand Down

0 comments on commit 88b1a35

Please sign in to comment.