Add handling the GetCustomerStatistics query

This commit is contained in:
Jacob Tobiasz 2023-12-11 13:18:09 +01:00
parent d4640a6743
commit ac519402b9
No known key found for this signature in database
GPG key ID: 3F84290201B006E0
8 changed files with 200 additions and 0 deletions

View file

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\ApiBundle\Controller;
use Sylius\Bundle\ApiBundle\Query\GetCustomerStatistics;
use Sylius\Component\Core\Customer\Statistics\CustomerStatistics;
use Symfony\Component\Messenger\HandleTrait;
use Symfony\Component\Messenger\MessageBusInterface;
final class GetCustomerStatisticsAction
{
use HandleTrait;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
public function __invoke(int $id): CustomerStatistics
{
return $this->handle(
$this->messageBus->dispatch(
new GetCustomerStatistics($id),
),
);
}
}

View file

@ -71,6 +71,15 @@
</attribute> </attribute>
</itemOperation> </itemOperation>
<itemOperation name="admin_statistics_get">
<attribute name="method">GET</attribute>
<attribute name="path">/admin/customers/{id}/statistics</attribute>
<attribute name="controller">Sylius\Bundle\ApiBundle\Controller\GetCustomerStatisticsAction</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:customer:statistics:read</attribute>
</attribute>
</itemOperation>
<itemOperation name="admin_put"> <itemOperation name="admin_put">
<attribute name="method">PUT</attribute> <attribute name="method">PUT</attribute>
<attribute name="path">/admin/customers/{id}</attribute> <attribute name="path">/admin/customers/{id}</attribute>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" ?>
<!--
This file is part of the Sylius package.
(c) Sylius Sp. z o.o.
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="Sylius\Component\Core\Customer\Statistics\CustomerStatistics">
<attribute name="allOrdersCount">
<group>admin:customer:statistics:read</group>
</attribute>
<attribute name="perChannelsStatistics">
<group>admin:customer:statistics:read</group>
</attribute>
</class>
</serializer>

View file

@ -0,0 +1,32 @@
<?xml version="1.0" ?>
<!--
This file is part of the Sylius package.
(c) Sylius Sp. z o.o.
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="Sylius\Component\Core\Customer\Statistics\PerChannelCustomerStatistics">
<attribute name="channel">
<group>admin:customer:statistics:read</group>
</attribute>
<attribute name="ordersCount">
<group>admin:customer:statistics:read</group>
</attribute>
<attribute name="ordersValue">
<group>admin:customer:statistics:read</group>
</attribute>
<attribute name="averageOrderValue">
<group>admin:customer:statistics:read</group>
</attribute>
</class>
</serializer>

View file

@ -23,6 +23,11 @@
<argument type="service" id="sylius.repository.order_item" /> <argument type="service" id="sylius.repository.order_item" />
</service> </service>
<service id="Sylius\Bundle\ApiBundle\Controller\GetCustomerStatisticsAction">
<argument type="service" id="sylius.command_bus" />
<tag name="controller.service_arguments" />
</service>
<service id="Sylius\Bundle\ApiBundle\Controller\GetProductBySlugAction"> <service id="Sylius\Bundle\ApiBundle\Controller\GetProductBySlugAction">
<argument type="service" id="sylius.context.channel" /> <argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.context.locale" /> <argument type="service" id="sylius.context.locale" />

View file

@ -64,6 +64,33 @@ final class CustomersTest extends JsonApiTestCase
); );
} }
/** @test */
public function it_gets_customer_statistics(): void
{
$fixtures = $this->loadFixturesFromFiles([
'authentication/api_administrator.yaml',
'customer.yaml',
'channel.yaml',
'order/fulfilled_order.yaml',
]);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
/** @var CustomerInterface $customer */
$customer = $fixtures['customer_tony'];
$this->client->request(
method: 'GET',
uri: sprintf('/api/v2/admin/customers/%s/statistics', $customer->getId()),
server: $header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/customer/get_customer_statistics_response',
Response::HTTP_OK,
);
}
/** @test */ /** @test */
public function it_creates_a_customer(): void public function it_creates_a_customer(): void
{ {

View file

@ -0,0 +1,37 @@
Sylius\Component\Core\Model\Order:
order:
channel: "@channel_web"
currencyCode: "USD"
localeCode: "en_US"
state: "fulfilled"
tokenValue: "token"
customer: "@customer_tony"
billingAddress: "@billing_address"
shippingAddress: "@shipping_address"
Sylius\Component\Core\Model\Address:
billing_address:
customer: "@customer_tony"
firstName: "Andrzej"
lastName: "Legs"
company: "Polmotors office"
street: "Moniuszki 16/20"
countryCode: "PL"
city: "Pabianice"
postcode: "31-999"
phoneNumber: "111999222"
provinceCode: "PL-PL"
provinceName: "kujawsko-pomorskie"
shipping_address:
customer: "@customer_tony"
firstName: "Andrzej"
lastName: "Legs"
company: "Polmotors manufactory"
street: "Moniuszki 16/20"
countryCode: "PL"
city: "Pabianice"
postcode: "31-999"
phoneNumber: "111999222"
provinceCode: "PL-PL"
provinceName: "kujawsko-pomorskie"

View file

@ -0,0 +1,26 @@
{
"@context": {
"@vocab": "@string@/api/v2/docs.jsonld#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"perChannelsStatistics": "CustomerStatistics\/perChannelsStatistics",
"allOrdersCount": "CustomerStatistics\/allOrdersCount"
},
"@type": "CustomerStatistics",
"@id": "\/api\/v2\/.well-known\/genid\/@string@",
"perChannelsStatistics": {
"@context": "\/api\/v2\/contexts\/Customer",
"@id": "\/api\/v2\/admin\/customers",
"@type": "hydra:Collection",
"hydra:member": [
{
"@type": "PerChannelCustomerStatistics",
"ordersCount": 1,
"ordersValue": 0,
"channel": "\/api\/v2\/admin\/channels\/WEB",
"averageOrderValue": 0
}
],
"hydra:totalItems": 1
},
"allOrdersCount": 1
}