Skip to content

Commit f04a94a

Browse files
committed
Merge branch 'feature/graphql_invoice_comments' of github.com:aligent-lturner/magento2-1 into 2.4-develop-prs
2 parents 359bc06 + a5d5b8d commit f04a94a

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Sales\Api\Data\CommentInterface;
12+
use Magento\Sales\Api\Data\EntityInterface;
13+
use Magento\Sales\Api\Data\InvoiceCommentInterface;
14+
use Magento\Sales\Api\InvoiceCommentRepositoryInterface;
15+
use Magento\TestFramework\Fixture\Api\ServiceFactory;
16+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
17+
18+
class InvoiceComment implements RevertibleDataFixtureInterface
19+
{
20+
private const DEFAULT_DATA = [
21+
InvoiceCommentInterface::IS_CUSTOMER_NOTIFIED => 0,
22+
InvoiceCommentInterface::PARENT_ID => 0,
23+
CommentInterface::COMMENT => 'Test Comment',
24+
CommentInterface::IS_VISIBLE_ON_FRONT => 0,
25+
EntityInterface::ENTITY_ID => 0,
26+
EntityInterface::CREATED_AT => "0000-00-00 00:00:00",
27+
];
28+
29+
/**
30+
* @var ServiceFactory
31+
*/
32+
private $serviceFactory;
33+
34+
/**
35+
* @var InvoiceCommentRepositoryInterface
36+
*/
37+
private $invoiceCommentRepository;
38+
39+
/**
40+
* @param ServiceFactory $serviceFactory
41+
* @param InvoiceCommentRepositoryInterface $invoiceCommentRepository
42+
*/
43+
public function __construct(
44+
ServiceFactory $serviceFactory,
45+
InvoiceCommentRepositoryInterface $invoiceCommentRepository
46+
) {
47+
$this->serviceFactory = $serviceFactory;
48+
$this->invoiceCommentRepository = $invoiceCommentRepository;
49+
}
50+
51+
public function apply(array $data = []): ?DataObject
52+
{
53+
$service = $this->serviceFactory->create(InvoiceCommentRepositoryInterface::class, 'save');
54+
$invoiceComment = $service->execute($this->prepareData($data));
55+
56+
return $this->invoiceCommentRepository->get($invoiceComment->getId());
57+
}
58+
59+
public function revert(DataObject $data): void
60+
{
61+
$invoice = $this->invoiceCommentRepository->get($data->getId());
62+
$this->invoiceCommentRepository->delete($invoice);
63+
}
64+
65+
/**
66+
* Prepare invoice data
67+
*
68+
* @param array $data
69+
* @return array
70+
*/
71+
private function prepareData(array $data): array
72+
{
73+
$data['entity'] = array_merge(self::DEFAULT_DATA, $data);
74+
75+
return $data;
76+
}
77+
}

app/code/Magento/SalesGraphQl/Model/Resolver/Invoices.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,31 @@ public function resolve(
4141
$invoices[] = [
4242
'id' => base64_encode($invoice->getEntityId()),
4343
'number' => $invoice['increment_id'],
44+
'comments' => $this->getInvoiceComments($invoice),
4445
'model' => $invoice,
4546
'order' => $orderModel
4647
];
4748
}
4849
return $invoices;
4950
}
51+
52+
/**
53+
* Get invoice comments in proper format
54+
*
55+
* @param InvoiceInterface $invoice
56+
* @return array
57+
*/
58+
private function getInvoiceComments(InvoiceInterface $invoice): array
59+
{
60+
$comments = [];
61+
foreach ($invoice->getComments() as $comment) {
62+
if ($comment->getIsVisibleOnFront()) {
63+
$comments[] = [
64+
'timestamp' => $comment->getCreatedAt(),
65+
'message' => $comment->getComment()
66+
];
67+
}
68+
}
69+
return $comments;
70+
}
5071
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Sales/InvoiceTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@
77

88
namespace Magento\GraphQl\Sales;
99

10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture;
12+
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
13+
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
14+
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
15+
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
16+
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
17+
use Magento\Customer\Test\Fixture\Customer;
1018
use Magento\Framework\Registry;
19+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
20+
use Magento\Quote\Test\Fixture\CustomerCart;
21+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
1122
use Magento\Sales\Api\OrderRepositoryInterface;
1223
use Magento\Sales\Model\ResourceModel\Order\Collection;
24+
use Magento\Sales\Test\Fixture\Invoice as InvoiceFixture;
25+
use Magento\Sales\Test\Fixture\InvoiceComment as InvoiceCommentFixture ;
26+
use Magento\TestFramework\Fixture\DataFixture;
1327
use Magento\TestFramework\Helper\Bootstrap;
1428
use Magento\TestFramework\TestCase\GraphQlAbstract;
1529
use Magento\GraphQl\GetCustomerAuthenticationHeader;
@@ -410,6 +424,64 @@ public function testPartialInvoiceForCustomerWithTaxesAndDiscounts()
410424
$this->deleteOrder();
411425
}
412426

427+
#[
428+
DataFixture(Customer::class, ['email' => 'customer@search.example.com'], as: 'customer'),
429+
DataFixture(ProductFixture::class, as: 'product'),
430+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'),
431+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
432+
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
433+
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
434+
DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']),
435+
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
436+
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
437+
DataFixture(PlaceOrderFixture::class, ['cart_id' => '$cart.id$'], 'order'),
438+
DataFixture(InvoiceFixture::class, ['order_id' => '$order.id$'], 'invoice'),
439+
DataFixture(InvoiceCommentFixture::class, [
440+
'parent_id' => '$invoice.id$',
441+
'comment' => 'visible_comment',
442+
'is_visible_on_front' => 1,
443+
]),
444+
DataFixture(InvoiceCommentFixture::class, [
445+
'parent_id' => '$invoice.id$',
446+
'comment' => 'non_visible_comment',
447+
'is_visible_on_front' => 0,
448+
]),
449+
]
450+
public function testInvoiceCommentsQuery()
451+
{
452+
$query =
453+
<<<QUERY
454+
{
455+
customer {
456+
orders {
457+
items {
458+
invoices {
459+
comments {
460+
message
461+
timestamp
462+
}
463+
}
464+
}
465+
}
466+
}
467+
}
468+
QUERY;
469+
470+
$currentEmail = 'customer@search.example.com';
471+
$currentPassword = 'password';
472+
$response = $this->graphQlQuery(
473+
$query,
474+
[],
475+
'',
476+
$this->customerAuthenticationHeader->execute($currentEmail, $currentPassword)
477+
);
478+
479+
$invoice = $response['customer']['orders']['items'][0]['invoices'][0];
480+
$this->assertCount(1, $invoice['comments']);
481+
$this->assertEquals('visible_comment', $invoice['comments'][0]['message']);
482+
$this->assertNotEmpty($invoice['comments'][0]['timestamp']);
483+
}
484+
413485
/**
414486
* Prepare invoice for the order
415487
*

0 commit comments

Comments
 (0)