Skip to content

Commit

Permalink
[Admin][Order] Add resending an order confirmation email
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Dec 13, 2019
1 parent 96bd353 commit 1db2c65
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ Feature: Resending an order confirmation email for a chosen order
Scenario: Resending a confirmation email for a given order
When I view the summary of the order "#00000666"
And I resend the order confirmation email
Then an email with the confirmation of the order "#00000666" should be sent to "[email protected]"
Then I should be notified that the order confirmation email has been successfully resent to the customer
And an email with the confirmation of the order "#00000666" should be sent to "[email protected]"

@ui @email
Scenario: Sending a confirmation email after shipping an order in different locale than the default one
Given the order "#00000666" has been placed in "Polish (Poland)" locale
When I view the summary of the order "#00000666"
And I resend the order confirmation email
Then an email with the confirmation of the order "#00000666" should be sent to "[email protected]" in "Polish (Poland)" locale
Then I should be notified that the order confirmation email has been successfully resent to the customer
And an email with the confirmation of the order "#00000666" should be sent to "[email protected]" in "Polish (Poland)" locale
11 changes: 11 additions & 0 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,17 @@ public function iShouldSeeInformationAboutNoPayments(): void
Assert::true($this->showPage->hasInformationAboutNoPayment());
}

/**
* @Then I should be notified that the order confirmation email has been successfully resent to the customer
*/
public function iShouldBeNotifiedThatTheOrderConfirmationEmailHasBeenSuccessfullyResentToTheCustomer(): void
{
$this->notificationChecker->checkNotification(
'Order confirmation has been successfully resent to the customer.',
NotificationType::success()
);
}

/**
* @param string $type
* @param string $element
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Page/Admin/Order/ShowPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function hasInformationAboutNoPayment(): bool

public function resendOrderConfirmationEmail(): void
{
return $this->getElement('resend_order_confirmation_email')->click();
$this->getElement('resend_order_confirmation_email')->click();
}

protected function getDefinedElements(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\AdminBundle\Action;

use Sylius\Bundle\AdminBundle\EmailManager\OrderEmailManagerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

final class ResendOrderConfirmationEmailAction
{
/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var OrderEmailManagerInterface */
private $orderEmailManager;

/** @var CsrfTokenManagerInterface */
private $csrfTokenManager;

/** @var Session */
private $session;

public function __construct(
OrderRepositoryInterface $orderRepository,
OrderEmailManagerInterface $orderEmailManager,
CsrfTokenManagerInterface $csrfTokenManager,
SessionInterface $session
) {
$this->orderRepository = $orderRepository;
$this->orderEmailManager = $orderEmailManager;
$this->csrfTokenManager = $csrfTokenManager;
$this->session = $session;
}

public function __invoke(Request $request): Response
{
$orderId = $request->attributes->get('id');

if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($orderId, $request->query->get('_csrf_token')))) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}

/** @var OrderInterface|null $order */
$order = $this->orderRepository->find($orderId);
if ($order === null) {
throw new NotFoundHttpException(sprintf('The order with id %s has not been found', $orderId));
}

$this->orderEmailManager->sendConfirmationEmail($order);

$this->session->getFlashBag()->add(
'success',
'sylius.email.order_confirmation_resent'
);

return new RedirectResponse($request->headers->get('referer'));
}
}
42 changes: 42 additions & 0 deletions src/Sylius/Bundle/AdminBundle/EmailManager/OrderEmailManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\AdminBundle\EmailManager;

use Sylius\Bundle\CoreBundle\Mailer\Emails;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;

final class OrderEmailManager implements OrderEmailManagerInterface
{
/** @var SenderInterface */
private $emailSender;

public function __construct(SenderInterface $emailSender)
{
$this->emailSender = $emailSender;
}

public function sendConfirmationEmail(OrderInterface $order): void
{
$this->emailSender->send(
Emails::ORDER_CONFIRMATION_RESENT,
[$order->getCustomer()->getEmail()],
[
'order' => $order,
'channel' => $order->getChannel(),
'localeCode' => $order->getLocaleCode(),
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\AdminBundle\EmailManager;

use Sylius\Component\Core\Model\OrderInterface;

interface OrderEmailManagerInterface
{
public function sendConfirmationEmail(OrderInterface $order): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

sylius_mailer:
emails:
order_confirmation_resent:
subject: sylius.emails.order_confirmation.subject
template: "@SyliusAdmin/Email/orderConfirmation.html.twig"
shipment_confirmation:
subject: sylius.emails.shipment_confirmation.subject
template: "@SyliusAdmin/Email/shipmentConfirmation.html.twig"
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,9 @@ sylius_admin_order_shipment_ship:
parameters:
orderId: $orderId
id: $id

sylius_admin_order_resend_confirmation_email:
path: /{id}/resend-confirmation-email
methods: [GET]
defaults:
_controller: Sylius\Bundle\AdminBundle\Action\ResendOrderConfirmationEmailAction
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<argument type="service" id="security.csrf.token_manager" />
</service>

<service id="Sylius\Bundle\AdminBundle\Action\ResendOrderConfirmationEmailAction" public="true">
<argument type='service' id="sylius.repository.order" />
<argument type="service" id="Sylius\Bundle\AdminBundle\EmailManager\OrderEmailManagerInterface" />
<argument type="service" id="security.csrf.token_manager" />
<argument type="service" id="session" />
</service>

<service id="sylius.controller.admin.dashboard" class="Sylius\Bundle\AdminBundle\Controller\DashboardController" public="true">
<argument type="service" id="sylius.dashboard.statistics_provider" />
<argument type="service" id="sylius.repository.channel" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<services>
<defaults public="true" />

<service id="Sylius\Bundle\AdminBundle\EmailManager\OrderEmailManagerInterface" class="Sylius\Bundle\AdminBundle\EmailManager\OrderEmailManager">
<argument type="service" id="sylius.email_sender" />
</service>

<service id="sylius.email_manager.shipment" class="Sylius\Bundle\AdminBundle\EmailManager\ShipmentEmailManager">
<argument type="service" id="sylius.email_sender" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
# (c) Paweł Jędrzejewski

sylius:
email:
order_confirmation_resent: 'Order confirmation has been successfully resent to the customer.'
product_variant:
cannot_generate_variants: 'Cannot generate variants for a product without options values.'
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends '@SyliusAdmin/Email/layout.html.twig' %}

{% block subject %}
{{ 'sylius.email.order_confirmation.subject'|trans({}, null, localeCode) }}
{% endblock %}

{% block content %}
{% set url = channel.hostname is not null ? 'http://' ~ channel.hostname ~ path('sylius_shop_order_show', {'tokenValue': order.tokenValue, '_locale': localeCode}) : url('sylius_shop_order_show', {'tokenValue': order.tokenValue, '_locale': localeCode}) %}

<div style="text-align: center; margin-bottom: 30px;">
{{ 'sylius.email.order_confirmation.your_order_number'|trans({}, null, localeCode) }}
<div style="margin: 10px 0;">
<span style="border: 1px solid #eee; padding: 10px; color: #1abb9c; font-size: 28px;">
{{ order.number }}
</span>
</div>
{{ 'sylius.email.order_confirmation.has_been_successfully_placed'|trans({}, null, localeCode) }}
</div>

<div style="text-align: center; margin-bottom: 30px;">
<a href="{{ url|raw }}" style="display: inline-block; text-align: center; background: #1abb9c; padding: 18px 28px; color: #fff; text-decoration: none; border-radius: 3px;">
{{ 'sylius.email.order_confirmation.view_order_or_change_payment_method'|trans({}, null, localeCode) }}
</a>
</div>

<div style="text-align: center;">
{{ 'sylius.email.order_confirmation.thank_you'|trans({}, null, localeCode) }}
</div>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="ui segment">
{% set path = path('sylius_admin_order_resend_confirmation_email', {'id': order.id, '_csrf_token': csrf_token(order.id)}) %}
<a href="{{ path }}" class="ui icon labeled tiny fluid button" {{ sylius_test_html_attribute('resend-order-confirmation-email') }}>
<i class="send icon"></i> {{ 'sylius.ui.resend_the_order_confirmation_email'|trans }}
</a>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

{% include '@SyliusAdmin/Order/Show/_payments.html.twig' %}
{% include '@SyliusAdmin/Order/Show/_shipments.html.twig' %}
{% include '@SyliusAdmin/Order/Show/_resendEmail.html.twig' %}

{{ sonata_block_render_event('sylius.admin.order.show.after_shipments', {'resource': resource}) }}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\AdminBundle\EmailManager;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\AdminBundle\EmailManager\OrderEmailManagerInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;

final class OrderEmailManagerSpec extends ObjectBehavior
{
function let(SenderInterface $sender): void
{
$this->beConstructedWith($sender);
}

function it_implements_an_order_email_manager_interface(): void
{
$this->shouldImplement(OrderEmailManagerInterface::class);
}

function it_sends_an_order_confirmation_email(
SenderInterface $sender,
OrderInterface $order,
ChannelInterface $channel,
CustomerInterface $customer
): void {
$order->getChannel()->willReturn($channel);
$order->getLocaleCode()->willReturn('en_US');
$order->getCustomer()->willReturn($customer);
$customer->getEmail()->willReturn('[email protected]');

$sender
->send('order_confirmation_resent', ['[email protected]'], [
'order' => $order,
'channel' => $channel,
'localeCode' => 'en_US',
])
->shouldBeCalled()
;

$this->sendConfirmationEmail($order);
}
}
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Mailer/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final class Emails

public const ORDER_CONFIRMATION = 'order_confirmation';

public const ORDER_CONFIRMATION_RESENT = 'order_confirmation_resent';

public const SHIPMENT_CONFIRMATION = 'shipment_confirmation';

public const USER_REGISTRATION = 'user_registration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ sylius:
repeat_new_password: 'Repeat new password'
report_details: 'Report details'
reports: 'Reports'
resend_the_order_confirmation_email: 'Resend the order confirmation email'
reserved: 'Reserved'
reset: 'Reset'
reset_button: 'Reset'
Expand Down

0 comments on commit 1db2c65

Please sign in to comment.