7
7
8
8
namespace Magento \SalesGraphQl \Model \Resolver ;
9
9
10
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
10
11
use Magento \Framework \Exception \InputException ;
11
12
use Magento \Framework \Exception \LocalizedException ;
12
13
use Magento \Framework \GraphQl \Config \Element \Field ;
13
14
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
14
15
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
16
use Magento \Framework \GraphQl \Query \ResolverInterface ;
16
17
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
17
- use Magento \GraphQl \Model \Query \ContextInterface ;
18
18
use Magento \Sales \Api \Data \OrderInterface ;
19
- use Magento \SalesGraphQl \Model \Resolver \CustomerOrders \Query \SearchQuery ;
19
+ use Magento \Sales \Api \OrderRepositoryInterface ;
20
+ use Magento \SalesGraphQl \Model \Resolver \CustomerOrders \Query \OrderFilter ;
20
21
use Magento \Store \Api \Data \StoreInterface ;
21
22
22
23
/**
25
26
class CustomerOrders implements ResolverInterface
26
27
{
27
28
/**
28
- * @var SearchQuery
29
+ * @var SearchCriteriaBuilder
29
30
*/
30
- private $ searchQuery ;
31
+ private $ searchCriteriaBuilder ;
31
32
32
33
/**
33
- * @param SearchQuery $searchQuery
34
+ * @var OrderRepositoryInterface
35
+ */
36
+ private $ orderRepository ;
37
+
38
+ /**
39
+ * @var OrderFilter
40
+ */
41
+ private $ orderFilter ;
42
+
43
+ /**
44
+ * @param OrderRepositoryInterface $orderRepository
45
+ * @param SearchCriteriaBuilder $searchCriteriaBuilder
46
+ * @param OrderFilter $orderFilter
34
47
*/
35
48
public function __construct (
36
- SearchQuery $ searchQuery
49
+ OrderRepositoryInterface $ orderRepository ,
50
+ SearchCriteriaBuilder $ searchCriteriaBuilder ,
51
+ OrderFilter $ orderFilter
37
52
) {
38
- $ this ->searchQuery = $ searchQuery ;
53
+ $ this ->orderRepository = $ orderRepository ;
54
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
55
+ $ this ->orderFilter = $ orderFilter ;
39
56
}
40
57
41
58
/**
@@ -48,7 +65,6 @@ public function resolve(
48
65
array $ value = null ,
49
66
array $ args = null
50
67
) {
51
- /** @var ContextInterface $context */
52
68
if (false === $ context ->getExtensionAttributes ()->getIsCustomer ()) {
53
69
throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
54
70
}
@@ -62,13 +78,31 @@ public function resolve(
62
78
/** @var StoreInterface $store */
63
79
$ store = $ context ->getExtensionAttributes ()->getStore ();
64
80
try {
65
- $ searchResultDto = $ this ->searchQuery ->getResult ($ args , $ userId , $ store );
81
+ $ filterGroups = $ this ->orderFilter ->createFilterGroups ($ args , $ userId , (int )$ store ->getId ());
82
+ $ this ->searchCriteriaBuilder ->setFilterGroups ($ filterGroups );
83
+ if (isset ($ args ['currentPage ' ])) {
84
+ $ this ->searchCriteriaBuilder ->setCurrentPage ($ args ['currentPage ' ]);
85
+ }
86
+ if (isset ($ args ['pageSize ' ])) {
87
+ $ this ->searchCriteriaBuilder ->setPageSize ($ args ['pageSize ' ]);
88
+ }
89
+
90
+ $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
91
+ $ searchResult = $ this ->orderRepository ->getList ($ searchCriteria );
92
+ $ orderArray = [];
93
+ /** @var OrderInterface $order */
94
+ foreach ($ searchResult ->getItems () as $ key => $ order ) {
95
+ $ orderArray [$ key ] = $ order ->getData ();
96
+ $ orderArray [$ key ]['model ' ] = $ order ;
97
+ }
98
+
99
+ $ maxPages = (int )ceil ($ searchResult ->getTotalCount () / $ searchResult ->getPageSize ());
66
100
} catch (InputException $ e ) {
67
101
throw new GraphQlInputException (__ ($ e ->getMessage ()));
68
102
}
69
103
70
104
$ orders = [];
71
- foreach (( $ searchResultDto -> getItems () ?? []) as $ order ) {
105
+ foreach ($ orderArray as $ order ) {
72
106
if (!($ order ['model ' ] ?? null instanceof OrderInterface)) {
73
107
throw new LocalizedException (__ ('"model" value should be specified ' ));
74
108
}
@@ -89,12 +123,12 @@ public function resolve(
89
123
}
90
124
91
125
return [
92
- 'total_count ' => $ searchResultDto ->getTotalCount (),
126
+ 'total_count ' => $ searchResult ->getTotalCount (),
93
127
'items ' => $ orders ,
94
128
'page_info ' => [
95
- 'page_size ' => $ searchResultDto ->getPageSize (),
96
- 'current_page ' => $ searchResultDto -> getCurrentPage (),
97
- 'total_pages ' => $ searchResultDto -> getTotalPages () ,
129
+ 'page_size ' => $ searchResult ->getPageSize (),
130
+ 'current_page ' => $ searchResult -> getCurPage (),
131
+ 'total_pages ' => $ maxPages ,
98
132
]
99
133
];
100
134
}
0 commit comments