Skip to content

Commit

Permalink
PISHPS-367: reduced ajax calls on order details page (#894)
Browse files Browse the repository at this point in the history
* PISHPS-367: reduced ajax calls on order details page

* PISHPS-367: fixed import issue
  • Loading branch information
m-muxfeld-diw authored Dec 4, 2024
1 parent 030266f commit 2ff7b44
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 31 deletions.
125 changes: 125 additions & 0 deletions src/Controller/Api/Controller/OrderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Controller\Api\Controller;

use Kiener\MolliePayments\Controller\Api\Order\CancelLineController;
use Kiener\MolliePayments\Controller\Api\Order\OrderControllerBase;
use Kiener\MolliePayments\Controller\Api\Order\ShippingControllerBase;
use Kiener\MolliePayments\Controller\Api\PluginConfig\ConfigControllerBase;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class OrderController extends AbstractController
{
/**
* @var RequestBagFactory
*/
private $requestBagFactory;

/**
* @var ConfigControllerBase
*/
private $baseController;

/**
* @var ShippingControllerBase
*/
private $shippingController;

/**
* @var OrderControllerBase
*/
private $orderController;

/**
* @var CancelLineController
*/
private $cancelLineController;

/**
* @var EntityRepository
*/
private $orderRepository;

public function __construct(
RequestBagFactory $requestBagFactory,
ConfigControllerBase $baseController,
ShippingControllerBase $shippingController,
OrderControllerBase $orderController,
CancelLineController $cancelLineController,
EntityRepository $orderRepository
) {
$this->baseController = $baseController;
$this->shippingController = $shippingController;
$this->requestBagFactory = $requestBagFactory;
$this->orderController = $orderController;
$this->cancelLineController = $cancelLineController;
$this->orderRepository = $orderRepository;
}

/**
* Requires orderId, salesChannelId and mollieOrderId to be set in the request.
*/
public function getOrderDetails(Request $request, Context $context): JsonResponse
{
$orderId = $request->get('orderId');
$scId = $this->getSalesChannelId($orderId, $context);
$mollieOrderId = $this->getMollieOrderId($orderId, $context);

$request->request->set('mollieOrderId', $mollieOrderId);
$request->request->set('salesChannelId', $scId);

$config = $this->baseController->getRefundManagerConfig($request, $context);
$shipping = $this->shippingController->total($this->requestBagFactory->createForShipping($request), $context);
$payment = $this->orderController->paymentUrl($request, $context);
$cancelStatus = $this->cancelLineController->statusAction($request, $context);

$result = [
'config' => $config->getContent(),
'shipping' => $shipping->getContent(),
'payment' => $payment->getContent(),
'cancelStatus' => $cancelStatus->getContent(),
];

foreach ($result as &$item) {
if (is_string($item)) {
$item = json_decode($item);
}
}

return new JsonResponse(array_merge(['success' => true,], $result));
}

private function getSalesChannelId(string $orderId, Context $context): string
{
$orders = $this->orderRepository->search(new Criteria([$orderId]), $context);

if ($orders->count() === 0) {
throw new \RuntimeException('Order not found');
}

$order = $orders->first();

return $order->getSalesChannelId();
}

private function getMollieOrderId(string $orderId, Context $context): string
{
$orders = $this->orderRepository->search(new Criteria([$orderId]), $context);

if ($orders->count() === 0) {
throw new \RuntimeException('Order not found');
}

$order = $orders->first();

$customFields = $order->getCustomFieldsValue('mollie_payments');

return $customFields['order_id'];
}
}
18 changes: 18 additions & 0 deletions src/Controller/Api/Controller/RequestBagFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\Controller\Api\Controller;

use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Symfony\Component\HttpFoundation\Request;

class RequestBagFactory
{
public function createForShipping(Request $request): RequestDataBag
{
$result = new RequestDataBag();
$result->set('orderId', $request->get('orderId'));

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

import MolliePaymentsRefundBundleRepositoryService from './mollie-payments-refund-bundle-repository.service';
// eslint-disable-next-line no-undef
const ApiService = Shopware.Classes.ApiService;

export default class MolliePaymentsConfigService extends ApiService {
repository;


/**
Expand Down Expand Up @@ -78,19 +81,11 @@ export default class MolliePaymentsConfigService extends ApiService {
* @returns {*}
*/
getRefundManagerConfig(salesChannelId, orderId) {
return this.httpClient
.post(
`_action/${this.getApiBasePath()}/config/refund-manager`,
{
'salesChannelId': salesChannelId,
'orderId': orderId,
},
{
headers: this.getBasicHeaders(),
}
).then((response) => {
return ApiService.handleResponse(response);
});
MolliePaymentsRefundBundleRepositoryService.setOrderId(orderId);
MolliePaymentsRefundBundleRepositoryService.setClient(this.httpClient);
MolliePaymentsRefundBundleRepositoryService.setHeaders(this.getBasicHeaders());

return MolliePaymentsRefundBundleRepositoryService.fetch()
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

import MolliePaymentsRefundBundleRepositoryService from './mollie-payments-refund-bundle-repository.service';
// eslint-disable-next-line no-undef
const ApiService = Shopware.Classes.ApiService;

Expand All @@ -9,17 +11,13 @@ class MolliePaymentsOrderService extends ApiService {
getPaymentUrl(data = {orderId: null}) {
const headers = this.getBasicHeaders();

return this.httpClient
.post(
`_action/${this.getApiBasePath()}/order/payment-url`,
JSON.stringify(data),
{
headers: headers,
}
)
.then((response) => {
return ApiService.handleResponse(response);
});
MolliePaymentsRefundBundleRepositoryService.setOrderId(data.orderId);
MolliePaymentsRefundBundleRepositoryService.setHeaders(headers);
MolliePaymentsRefundBundleRepositoryService.setClient(this.httpClient);

return MolliePaymentsRefundBundleRepositoryService.fetch().then((response) => {
return ApiService.handleResponse(response.data.payment);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// eslint-disable-next-line no-undef
const ApiService = Shopware.Classes.ApiService;

export default class MolliePaymentsRefundBundleRepositoryService extends ApiService {
static response = null;
static orderId = null;
static headers = null
static client = null;

static setOrderId(orderId) {
if (orderId !== null) {
MolliePaymentsRefundBundleRepositoryService.orderId = orderId;
}
}

static setHeaders(headers) {
if (headers !== null) {
MolliePaymentsRefundBundleRepositoryService.headers = headers;
}
}

static setClient(client) {
if (client !== null) {
MolliePaymentsRefundBundleRepositoryService.client = client;
}
}

static fetch() {
if (!MolliePaymentsRefundBundleRepositoryService.client) {
throw new Error('Client not set. Please set the client using setClient() method.');
}

if (!MolliePaymentsRefundBundleRepositoryService.orderId) {
throw new Error('orderId not set. Please set the orderId using setOrderId() method.');
}

if (MolliePaymentsRefundBundleRepositoryService.response !== null) {
return MolliePaymentsRefundBundleRepositoryService.response;
}

MolliePaymentsRefundBundleRepositoryService.response = MolliePaymentsRefundBundleRepositoryService.client.post(
'_action/mollie/refund-manager/bundled',
{
orderId: MolliePaymentsRefundBundleRepositoryService.orderId,
},
{
headers: MolliePaymentsRefundBundleRepositoryService.headers,
}
);

return MolliePaymentsRefundBundleRepositoryService.response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export default class RefundManager {

let refundManagerPossible = false;

await this._configService.getRefundManagerConfig(salesChannelId, orderId).then((response) => {
refundManagerPossible = response.enabled;
});
await this._configService.getRefundManagerConfig(salesChannelId, orderId)
.then((response) => {
refundManagerPossible = response.data.config.enabled;
});

return refundManagerPossible;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Component.override('sw-order-user-card', {

this.MolliePaymentsOrderService.getPaymentUrl({orderId: this.currentOrder.id})
.then(response => {
this.molliePaymentUrl = (response.url !== null) ? response.url : '';
this.molliePaymentUrl = (response.data.payment.url !== null) ? response.data.payment.url : '';
})
.finally(() => {
this.isMolliePaymentUrlLoading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import OrderAttributes from '../../../../../../core/models/OrderAttributes';
import RefundManager from '../../../../components/mollie-refund-manager/RefundManager';
import MollieShipping from '../../../../components/mollie-ship-order/MollieShipping';
import MollieShippingEvents from '../../../../components/mollie-ship-order/MollieShippingEvents';
import MolliePaymentsRefundBundleRepositoryService
from '../../../../../../core/service/api/mollie-payments-refund-bundle-repository.service';

// eslint-disable-next-line no-undef
const {Component, Mixin, Filter} = Shopware;
Expand Down Expand Up @@ -259,8 +261,9 @@ Component.override('sw-order-detail-general', {
this.molliePaymentUrl = (response.url !== null) ? response.url : '';
});

this.shippingManagerService.isShippingPossible(this.order).then((enabled) => {
this.isShippingPossible = enabled;
MolliePaymentsRefundBundleRepositoryService.setOrderId(this.order.id);
MolliePaymentsRefundBundleRepositoryService.fetch().then((response) => {
this.isShippingPossible = response.data.shipping;
});

this.refundedManagerService.isRefundManagerAvailable(this.order.salesChannelId, this.order.id).then((possible)=>{
Expand Down
11 changes: 10 additions & 1 deletion src/Resources/config/routes/admin-api/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<default key="_auth_enabled">true</default>
</route>

<route id="api.action.mollie.config.refund-manager"
<route id="api.action.mollie.config.refund-manager.config"
path="/api/_action/mollie/config/refund-manager"
methods="POST">
<default key="_controller">Kiener\MolliePayments\Controller\Api\PluginConfig\ConfigControllerBase::getRefundManagerConfig</default>
Expand All @@ -41,6 +41,15 @@
<default key="_auth_enabled">true</default>
</route>

<route id="api.action.mollie.config.refund-manager"
path="/api/_action/mollie/refund-manager/bundled"
methods="POST">
<default key="_controller">Kiener\MolliePayments\Controller\Api\Controller\OrderController::getOrderDetails</default>
<default key="_routeScope"><list><string>api</string></list></default>
<default key="_auth_required">true</default>
<default key="_auth_enabled">true</default>
</route>

<route id="api.action.mollie.config.refund-manager.legacy"
path="/api/v{version}/_action/mollie/config/refund-manager"
methods="POST">
Expand Down
15 changes: 15 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\Order"/>
</service>

<service id="Kiener\MolliePayments\Controller\Api\Controller\RequestBagFactory"/>

<service id="Kiener\MolliePayments\Controller\Api\Controller\OrderController">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<tag name="controller.service_arguments"/>
<argument type="service" id="Kiener\MolliePayments\Controller\Api\Controller\RequestBagFactory"/>
<argument type="service" id="Kiener\MolliePayments\Controller\Api\PluginConfig\ConfigControllerBase"/>
<argument type="service" id="Kiener\MolliePayments\Controller\Api\Order\ShippingControllerBase"/>
<argument type="service" id="Kiener\MolliePayments\Controller\Api\Order\OrderControllerBase"/>
<argument type="service" id="Kiener\MolliePayments\Controller\Api\Order\CancelLineController"/>
<argument type="service" id="order.repository"/>
</service>


<service id="Kiener\MolliePayments\Controller\Api\PluginConfig\ConfigControllerBase">
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
Expand Down

0 comments on commit 2ff7b44

Please sign in to comment.