50 lines
2.1 KiB
PHP
50 lines
2.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerMessage\Controller\Order;
|
|
|
|
use Esun\CustomerMessage\Model\ConversationService;
|
|
use Magento\Customer\Controller\AbstractAccount;
|
|
use Magento\Customer\Model\Session;
|
|
use Magento\Framework\App\Action\Context;
|
|
use Magento\Framework\App\Action\HttpGetActionInterface;
|
|
use Magento\Framework\Controller\Result\Redirect;
|
|
use Magento\Framework\Controller\ResultFactory;
|
|
use Magento\Sales\Api\OrderRepositoryInterface;
|
|
|
|
class Contact extends AbstractAccount implements HttpGetActionInterface
|
|
{
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly Session $session,
|
|
private readonly OrderRepositoryInterface $orders,
|
|
private readonly ConversationService $conversations
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$orderId = (int)$this->getRequest()->getParam('order_id');
|
|
try {
|
|
$order = $this->orders->get($orderId);
|
|
if ((int)$order->getCustomerId() !== (int)$this->session->getCustomerId()) {
|
|
throw new \Magento\Framework\Exception\LocalizedException(__('The requested order is not available.'));
|
|
}
|
|
$this->getRequest()->setParam('order_increment_id', (string)$order->getIncrementId());
|
|
$thread = $this->conversations->getOrderThreadForCustomer($orderId, (int)$this->session->getCustomerId());
|
|
if ($thread) {
|
|
$this->conversations->markCustomerRead((int)$thread['thread_id'], (int)$this->session->getCustomerId());
|
|
}
|
|
$page = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
|
|
$page->getConfig()->getTitle()->set(__('Order Messages #%1', $order->getIncrementId()));
|
|
return $page;
|
|
} catch (\Throwable $exception) {
|
|
$this->messageManager->addErrorMessage(__('The requested order is not available.'));
|
|
/** @var Redirect $redirect */
|
|
$redirect = $this->resultRedirectFactory->create();
|
|
return $redirect->setPath('sales/order/history');
|
|
}
|
|
}
|
|
}
|