Skip to content

Commit

Permalink
Merge pull request #17 from donmo-roundup/split-guest-and-customer-ca…
Browse files Browse the repository at this point in the history
…rt-functionality

Split guest and customer cart functionality
  • Loading branch information
DavidVin357 committed Aug 10, 2023
2 parents 19f4a3c + 5adf8ad commit c2bd26a
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 121 deletions.
24 changes: 24 additions & 0 deletions Api/CartDonationManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Donmo\Roundup\Api;

interface CartDonationManagementInterface
{
/**
* Add Donmo donation to customer cart
*
* @param int $cartId
* @param float $donationAmount
* @return string
*/
public function addDonationToCart(int $cartId, float $donationAmount): string;

/**
* Remove Donmo donation from customer cart
*
* @api
* @param int $cartId
* @return string
*/
public function removeDonationFromCart(int $cartId) : string;
}
20 changes: 0 additions & 20 deletions Api/DonationManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,4 @@ public function createDonation(Order $order): void;
* @return DonationInterface
*/
public function getByOrder(Order $order): DonationInterface;

// Rest API services

/**
* Add Donmo donation to quote
*
* @param string $cartId (masked quote ID)
* @param float $donationAmount
* @return string
*/
public function addDonationToQuote(string $cartId, float $donationAmount): string;

/**
* Remove Donmo donation from quote
*
* @api
* @param string $cartId (masked quote ID)
* @return string
*/
public function removeDonationFromQuote(string $cartId) : string;
}
24 changes: 24 additions & 0 deletions Api/GuestCartDonationManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Donmo\Roundup\Api;

interface GuestCartDonationManagementInterface
{
/**
* Add Donmo donation to guest cart
*
* @param string $cartId (masked quote ID)
* @param float $donationAmount
* @return string
*/
public function addDonationToCart(string $cartId, float $donationAmount): string;

/**
* Remove Donmo donation from guest cart
*
* @api
* @param string $cartId (masked quote ID)
* @return string
*/
public function removeDonationFromCart(string $cartId) : string;
}
62 changes: 62 additions & 0 deletions Model/CartDonationManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Donmo\Roundup\Model;

use Donmo\Roundup\Api\CartDonationManagementInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Quote\Api\CartRepositoryInterface;

class CartDonationManagement implements CartDonationManagementInterface
{
public function __construct(
CartRepositoryInterface $cartRepository,
SerializerInterface $serializer,
)
{
$this->cartRepository = $cartRepository;
$this->serializer = $serializer;
}

/**
@inheritdoc
*/
public function addDonationToCart(int $cartId, float $donationAmount): string
{
try {
$quote = $this->cartRepository->get($cartId);

if ($donationAmount > 0) {
$quote->setDonmodonation($donationAmount)->collectTotals();
$this->cartRepository->save($quote);

return $this->serializer->serialize(['message' => 'Success']);
} else {
return $this->serializer->serialize(['message' => 'Invalid donation']);
}
} catch (NoSuchEntityException) {
throw new NoSuchEntityException(__('The quote could not be loaded'));
} catch (\Exception $e) {
throw new \Exception('Some error has occurred');
}
}

/**
* @inheritdoc
*/
public function removeDonationFromCart(int $cartId): string
{
try {
$quote = $this->cartRepository->get($cartId);

$quote->setDonmodonation(0)->collectTotals();
$this->cartRepository->save($quote);

return $this->serializer->serialize(['message' => 'Success']);
} catch (NoSuchEntityException) {
throw new NoSuchEntityException(__('The quote could not be loaded'));
} catch (\Exception $e) {
throw new \Exception('Some error has occurred');
}
}
}
5 changes: 3 additions & 2 deletions Model/DonationManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getByOrder(Order $order): DonationInterface

// REST API Services

public function addDonationToQuote(string $cartId, float $donationAmount): string
public function addDonationToCart(string $cartId, float $donationAmount): string
{
try {
$quoteId = $this->maskedQuoteIdToQuoteId->execute($cartId);
Expand All @@ -126,7 +126,7 @@ public function addDonationToQuote(string $cartId, float $donationAmount): strin
* @param string $cartId
* @return string
*/
public function removeDonationFromQuote(string $cartId): string
public function removeDonationFromCart(string $cartId): string
{
try {
$quoteId = $this->maskedQuoteIdToQuoteId->execute($cartId);
Expand All @@ -142,4 +142,5 @@ public function removeDonationFromQuote(string $cartId): string
return $this->serializer->serialize(["message" => "An error has occurred: " . $e->getMessage()]);
}
}

}
42 changes: 42 additions & 0 deletions Model/GuestCartDonationManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Donmo\Roundup\Model;

use Donmo\Roundup\Api\GuestCartDonationManagementInterface;
use Donmo\Roundup\Logger\Logger;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Donmo\Roundup\Api\CartDonationManagementInterface;
class GuestCartDonationManagement implements GuestCartDonationManagementInterface
{
private Logger $logger;
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;
private CartDonationManagementInterface $cartDonationManagement;

public function __construct(
Logger $logger,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartDonationManagementInterface $cartDonationManagement
)
{
$this->logger = $logger;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartDonationManagement = $cartDonationManagement;
}

/**
* @inheritdoc
*/
public function addDonationToCart(string $cartId, float $donationAmount): string
{
$this->logger->info('addDonationToCart guest');
return $this->cartDonationManagement->addDonationToCart($this->maskedQuoteIdToQuoteId->execute($cartId), $donationAmount);
}

/**
* @inheritdoc
*/
public function removeDonationFromCart(string $cartId): string
{
return $this->cartDonationManagement->removeDonationFromCart($this->maskedQuoteIdToQuoteId->execute($cartId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,41 @@
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;

use Magento\Quote\Api\CartRepositoryInterface;

class AddDonationToQuote implements ResolverInterface
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;


class AddDonationToCart implements ResolverInterface
{
private Logger $logger;
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;
private CartRepositoryInterface $cartRepository;
private GetCartForUser $getCartForUser;
public function __construct(
Logger $logger,
CartRepositoryInterface $cartRepository,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
GetCartForUser $getCartForUser
) {
$this->logger = $logger;
$this->cartRepository = $cartRepository;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->getCartForUser = $getCartForUser;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
{
$maskedId = $args['cartId'];
$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedId);
$quote = $this->cartRepository->get($quoteId);

$donationAmount = $args['donationAmount'];
$donationAmount = $args['input']['donation_amount'];
if (!is_numeric($donationAmount) || $donationAmount < 0) {
throw new GraphQlInputException(__('Invalid donation'));
}

$quote->setDonmodonation($donationAmount)->collectTotals();
$this->cartRepository->save($quote);
$maskedCartId = $args['input']['cart_id'];
$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);

$cart->setDonmodonation($donationAmount)->collectTotals();
$this->cartRepository->save($cart);

return ['message' => 'success'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

class RemoveDonationFromQuote implements ResolverInterface
class RemoveDonationFromCart implements ResolverInterface
{
private Logger $logger;
private CartRepositoryInterface $cartRepository;
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;
private GetCartForUser $getCartForUser;

public function __construct(
Logger $logger,
CartRepositoryInterface $cartRepository,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
GetCartForUser $getCartForUser,
) {
$this->logger = $logger;
$this->cartRepository = $cartRepository;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->getCartForUser = $getCartForUser;
}


public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
{
$maskedId = $args['cartId'];
$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedId);
$quote = $this->cartRepository->get($quoteId);
$maskedCartId = $args['cart_id'];
$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);

$quote->setDonmodonation(0)->collectTotals();
$this->cartRepository->save($quote);
$cart->setDonmodonation(0)->collectTotals();
$this->cartRepository->save($cart);

return ['message' => 'success'];
}
Expand Down
4 changes: 3 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Donmo\Roundup\Api\Data\DonationInterface" type="Donmo\Roundup\Model\Donation" />
<preference for="Donmo\Roundup\Api\DonationManagementInterface" type="Donmo\Roundup\Model\DonationManagement" />
<preference for="Donmo\Roundup\Api\DonationRepositoryInterface" type="Donmo\Roundup\Model\DonationRepository" />
<preference for="Donmo\Roundup\Api\Data\DonationSearchResultsInterface" type="Donmo\Roundup\Model\DonationSearchResults" />
<preference for="Donmo\Roundup\Api\DonationManagementInterface" type="Donmo\Roundup\Model\DonationManagement" />
<preference for="Donmo\Roundup\Api\CartDonationManagementInterface" type="Donmo\Roundup\Model\CartDonationManagement" />
<preference for="Donmo\Roundup\Api\GuestCartDonationManagementInterface" type="Donmo\Roundup\Model\GuestCartDonationManagement" />

<!-- Reports Table Grid Collection and DataProvider-->

Expand Down
9 changes: 7 additions & 2 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ type CartPrices @doc(description: "Contains details about the final price of it
}

type Mutation {
addDonationToQuote(cartId: String!, donationAmount: Float!): DonmoDonationOutput @resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\AddDonationToQuote") @doc(description:"Add Donmo donation to quote")
addDonationToCart(input: AddDonationToCartInput): DonmoDonationOutput @resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\AddDonationToCart") @doc(description:"Add Donmo donation to cart")

removeDonationFromQuote(cartId: String!): DonmoDonationOutput @resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\RemoveDonationFromQuote") @doc(description:"Remove Donmo donation from quote")
removeDonationFromCart(cart_id: String!): DonmoDonationOutput @resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\RemoveDonationFromCart") @doc(description:"Remove Donmo donation from cart")
}

input AddDonationToCartInput @doc(description: "Specifies the coupon code to apply to the cart.") {
cart_id: String! @doc(description: "The unique ID of a `Cart` object.")
donation_amount: Float! @doc(description: "Donation amount.")
}

type DonmoDonationOutput {
Expand Down
28 changes: 24 additions & 4 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Webapi/etc/webapi.xsd">

<route url="/V1/donmo/add_donation" method="POST">
<service class="Donmo\Roundup\Api\DonationManagementInterface" method="addDonationToQuote"/>
<route url="V1/guest-carts/:cartId/add_donmo_donation" method="POST">
<service class="Donmo\Roundup\Api\GuestCartDonationManagementInterface" method="addDonationToCart"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>

<route url="/V1/donmo/remove_donation/:cartId" method="DELETE">
<service class="Donmo\Roundup\Api\DonationManagementInterface" method="removeDonationFromQuote"/>
<route url="/V1/guest-carts/:cartId/remove_donmo_donation" method="DELETE">
<service class="Donmo\Roundup\Api\GuestCartDonationManagementInterface" method="removeDonationFromCart"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>

<route url="/V1/carts/mine/add_donmo_donation" method="POST">
<service class="Donmo\Roundup\Api\CartDonationManagementInterface" method="addDonationToCart"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="cartId" force="true">%cart_id%</parameter>
</data>
</route>

<route url="/V1/carts/mine/remove_donmo_donation" method="DELETE">
<service class="Donmo\Roundup\Api\CartDonationManagementInterface" method="removeDonationFromCart"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="cartId" force="true">%cart_id%</parameter>
</data>
</route>
</routes>
Loading

0 comments on commit c2bd26a

Please sign in to comment.