diff --git a/src/Model/Customer/CheckCustomerAccount.php b/src/Model/Customer/CheckCustomerAccount.php new file mode 100644 index 0000000..aecd85e --- /dev/null +++ b/src/Model/Customer/CheckCustomerAccount.php @@ -0,0 +1,116 @@ +authentication = $authentication; + $this->customerRepository = $customerRepository; + $this->accountManagement = $accountManagement; + } + + /** + * Check customer account + * + * @param int|null $customerId + * @param int|null $customerType + * @return void + * @throws GraphQlAuthorizationException + * @throws GraphQlNoSuchEntityException + * @throws GraphQlAuthenticationException + */ + public function execute(?int $customerId, ?int $customerType): bool + { + if ($this->isCustomerGuest($customerId, $customerType)) { + throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); + return false; + } + + try { + $this->customerRepository->getById($customerId); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException( + __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), + $e + ); + return false; + } + + if ($this->authentication->isLocked($customerId)) { + throw new GraphQlAuthenticationException(__('The account is locked.')); + return false; + } + + $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); + if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { + throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); + return false; + } + + return true; + } + + /** + * Checking if current customer is guest + * + * @param int|null $customerId + * @param int|null $customerType + * @return bool + */ + private function isCustomerGuest(?int $customerId, ?int $customerType): bool + { + if ($customerId === null || $customerType === null) { + return true; + } + return $customerId == 0 || $customerType == UserContextInterface::USER_TYPE_GUEST; + } +} diff --git a/src/Model/Resolver/ExpandedOrderResolver.php b/src/Model/Resolver/ExpandedOrderResolver.php new file mode 100755 index 0000000..a0370cb --- /dev/null +++ b/src/Model/Resolver/ExpandedOrderResolver.php @@ -0,0 +1,121 @@ +collectionFactory = $collectionFactory; + $this->checkCustomerAccount = $checkCustomerAccount; + $this->orderRepository = $orderRepository; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + $itemsData = []; + $trackNumbers = []; + + $customerId = $context->getUserId(); + $this->checkCustomerAccount->execute($customerId, $context->getUserType()); + + $orderId = $args['id']; + $order = $this->orderRepository->get($orderId); + + if ($customerId != $order->getCustomerId()) { + throw new GraphQlNoSuchEntityException(__('Customer ID is invalid.')); + } + + foreach ($order->getAllVisibleItems() as $item) { + $itemsData[] = $item; + } + + $tracksCollection = $order->getTracksCollection(); + + foreach ($tracksCollection->getItems() as $track) { + $trackNumbers[] = $track->getTrackNumber(); + } + + $shippingInfo = [ + 'shipping_amount' => $order->getShippingAmount(), + 'shipping_method' => $order->getShippingMethod(), + 'shipping_address' => $order->getShippingAddress(), + 'shipping_description' => $order->getShippingDescription(), + 'tracking_numbers' => $trackNumbers + ]; + + $base_info = [ + 'id' => $order->getId(), + 'increment_id' => $order->getIncrementId(), + 'created_at' => $order->getCreatedAt(), + 'grand_total' => $order->getGrandTotal(), + 'sub_total' => $order->getBaseSubtotalInclTax(), + 'status' => $order->getStatus(), + 'status_label' => $order->getStatusLabel(), + 'total_qty_ordered' => $order->getTotalQtyOrdered(), + ]; + + return [ + 'base_order_info' => $base_info, + 'shipping_info' => $shippingInfo, + 'payment_info' => $order->getPayment()->getData(), + 'products' => $itemsData + ]; + } +} diff --git a/src/Model/Resolver/OrderListResolver.php b/src/Model/Resolver/OrderListResolver.php new file mode 100755 index 0000000..fbe3861 --- /dev/null +++ b/src/Model/Resolver/OrderListResolver.php @@ -0,0 +1,104 @@ +collectionFactory = $collectionFactory; + $this->checkCustomerAccount = $checkCustomerAccount; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + $items = []; + + $customerId = $context->getUserId(); + + $this->checkCustomerAccount->execute($customerId, $context->getUserType()); + + $orders = $this->collectionFactory->create($customerId); + + foreach ($orders as $order) { + $trackNumbers = []; + $tracksCollection = $order->getTracksCollection(); + + foreach ($tracksCollection->getItems() as $track) { + $trackNumbers[] = $track->getTrackNumber(); + } + + $shippingInfo = [ + 'shipping_amount' => $order->getShippingAmount(), + 'shipping_method' => $order->getShippingMethod(), + 'shipping_address' => $order->getShippingAddress(), + 'shipping_description' => $order->getShippingDescription(), + 'tracking_numbers' => $trackNumbers + ]; + + $base_info = [ + 'id' => $order->getId(), + 'increment_id' => $order->getIncrementId(), + 'created_at' => $order->getCreatedAt(), + 'grand_total' => $order->getGrandTotal(), + 'sub_total' => $order->getBaseSubtotalInclTax(), + 'status' => $order->getStatus(), + 'status_label' => $order->getStatusLabel(), + 'total_qty_ordered' => $order->getTotalQtyOrdered(), + ]; + + $items[] = [ + 'base_order_info' => $base_info, + 'shipping_info' => $shippingInfo, + 'payment_info' => $order->getPayment()->getData() + ]; + } + + return ['items' => $items]; + } +} diff --git a/src/Model/Resolver/ProductResolver.php b/src/Model/Resolver/ProductResolver.php new file mode 100755 index 0000000..aa7ec1a --- /dev/null +++ b/src/Model/Resolver/ProductResolver.php @@ -0,0 +1,66 @@ +productRepository = $productRepository; + } + + /** + * Get All Product Items of Order. + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($value['products'])) { + return null; + } + + foreach ($value['products'] as $key => $item) { + $product = $this->productRepository->get($item['sku']); + + $productData = $product->toArray(); + $productData['model'] = $product; + + $data[$key] = $productData; + $data[$key]['qty'] = $item->getQtyOrdered(); + $data[$key]['row_total'] = $item->getBaseRowTotalInclTax(); + $data[$key]['original_price'] = $item->getBaseOriginalPrice(); + $data[$key]['license_key'] = $item['license_key']; + } + + return $data; + } +} diff --git a/src/etc/schema.graphqls b/src/etc/schema.graphqls index f1c620e..9ce2537 100755 --- a/src/etc/schema.graphqls +++ b/src/etc/schema.graphqls @@ -23,6 +23,8 @@ type Mutation { type Query { getCartForCustomer(guestCartId: String): QuoteData @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\GetCartForCustomer") + getOrderList: OrderList @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\OrderListResolver") @doc(description: "The Sales Order query returns information about a Sales order") + getOrderById(id: Int!): Order @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ExpandedOrderResolver") @doc(description: "The Sales Order query returns information about a Sales order") } input CartItemInput { @@ -208,4 +210,93 @@ type ShippingMethod { price_excl_tax: Float price_incl_tax: Float available: Boolean -} \ No newline at end of file +} + +type OrderList { + items: [Order] +} + +type BaseOrderInfo { + id: Int + total_qty_ordered: Int + increment_id: String + created_at: String + status: String + status_label: String + grand_total: Float + sub_total: Float +} + +type Order { + base_order_info: BaseOrderInfo + payment_info: PaymentInfo + shipping_info: ShippingInfo + order_products: [ProductInterface] @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ProductResolver") +} + +type PaymentInfo { + method: String + cc_owner: String + cc_last_4: String + cc_type: String + additional_information: AdditionalCustomerInfo +} + +type AdditionalCustomerInfo { + bank: String + method_title: String + credit_type: String + month: Int + customer_info: CreditCustomerInfo +} + +type CreditCustomerInfo { + first_name: String + last_name: String + iin_number: String + middle_name: String + phone: String +} + +type ShippingInfo { + shipping_method: String + shipping_address: OrderCustomerAddress + shipping_description: String + shipping_amount: Float + tracking_numbers: [String] +} + +type OrderCustomerAddress { + city: String + company: String + country_id: String + customer_id: Int + firstname: String + id: Int + lastname: String + middlename: String + prefix: String + region: String + street: String + telephone: String + district: String + house_number: String + apartment_number: String + postomat_code: String + store_pickup_code: String + post_office_code: String + is_b2b: String + postcode: String + organizationname: String + organizationbin: String + organizationaddress: String + organizationiic: String + organizationbik: String +} + +interface ProductInterface { + qty: Int + row_total: Float + original_price: Float + license_key: String +}