Skip to content

Commit bdd8861

Browse files
Ensure that invoice comments are returned when requested by GraphQL queries
1 parent 63434d2 commit bdd8861

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,44 @@ public function testPartialInvoiceForCustomerWithTaxesAndDiscounts()
410410
$this->deleteOrder();
411411
}
412412

413+
/**
414+
* @magentoApiDataFixture Magento/Sales/_files/customer_invoice_comments_for_search.php
415+
*/
416+
public function testInvoiceCommentsQuery()
417+
{
418+
$query =
419+
<<<QUERY
420+
{
421+
customer {
422+
orders {
423+
items {
424+
invoices {
425+
comments {
426+
message
427+
timestamp
428+
}
429+
}
430+
}
431+
}
432+
}
433+
}
434+
QUERY;
435+
436+
$currentEmail = '[email protected]';
437+
$currentPassword = 'password';
438+
$response = $this->graphQlQuery(
439+
$query,
440+
[],
441+
'',
442+
$this->customerAuthenticationHeader->execute($currentEmail, $currentPassword)
443+
);
444+
445+
$invoice = $response['customer']['orders']['items'][0]['invoices'][0];
446+
$this->assertCount(1, $invoice['comments']);
447+
$this->assertEquals('visible_comment', $invoice['comments'][0]['message']);
448+
$this->assertNotEmpty($invoice['comments'][0]['timestamp']);
449+
}
450+
413451
/**
414452
* Prepare invoice for the order
415453
*
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Sales\Api\InvoiceCommentRepositoryInterface;
8+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
9+
use Magento\Sales\Model\Order;
10+
use Magento\Sales\Model\Order\Invoice;
11+
use Magento\Sales\Model\Order\Invoice\Comment;
12+
use Magento\Sales\Model\ResourceModel\Order\Invoice\Comment as CommentResource;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
15+
16+
Resolver::getInstance()->requireDataFixture(
17+
'Magento/Sales/_files/customers_with_invoices.php'
18+
);
19+
20+
$objectManager = Bootstrap::getObjectManager();
21+
22+
/** @var Order $order */
23+
$order = $objectManager->get(OrderInterfaceFactory::class)->create()->loadByIncrementId('100000001');
24+
/** @var Invoice $invoice */
25+
$invoice = $order->getInvoiceCollection()->getFirstItem();
26+
27+
$comments = [
28+
[
29+
'comment' => 'visible_comment',
30+
'is_visible_on_front' => 1
31+
],
32+
[
33+
'comment' => 'non_visible_comment',
34+
'is_visible_on_front' => 0,
35+
],
36+
];
37+
38+
/** @var CommentResource $commentResource */
39+
$commentResource = $objectManager->get(CommentResource::class);
40+
41+
foreach ($comments as $data) {
42+
/** @var Comment $comment */
43+
$comment = $objectManager->create(Comment::class);
44+
$comment->setParentId($invoice->getId());
45+
$comment->setComment($data['comment']);
46+
$comment->setIsVisibleOnFront($data['is_visible_on_front']);
47+
$comment->setIsCustomerNotified(false);
48+
$commentResource->save($comment);
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
8+
9+
Resolver::getInstance()->requireDataFixture('Magento/Sales/_files/customers_with_invoices_rollback.php');

0 commit comments

Comments
 (0)