Skip to content

Commit 4d4076e

Browse files
ENGCOM-4139: Get downloadable products for customer #273
- Merge Pull Request magento/graphql-ce#273 from magento/graphql-ce:customer-downloadable-products - Merged commits: 1. 7efedf0 2. 556e70c 3. 9fa6a28
2 parents 8dfe26a + 9fa6a28 commit 4d4076e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\DownloadableGraphQl\Model\Resolver;
9+
10+
use Magento\DownloadableGraphQl\Model\ResourceModel\GetPurchasedDownloadableProducts;
11+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\UrlInterface;
16+
17+
/**
18+
* @inheritdoc
19+
*
20+
* Returns available downloadable products for customer
21+
*/
22+
class CustomerDownloadableProducts implements ResolverInterface
23+
{
24+
/**
25+
* @var GetPurchasedDownloadableProducts
26+
*/
27+
private $getPurchasedDownloadableProducts;
28+
29+
/**
30+
* @var UrlInterface
31+
*/
32+
private $urlBuilder;
33+
34+
/**
35+
* @param GetPurchasedDownloadableProducts $getPurchasedDownloadableProducts
36+
* @param UrlInterface $urlBuilder
37+
*/
38+
public function __construct(
39+
GetPurchasedDownloadableProducts $getPurchasedDownloadableProducts,
40+
UrlInterface $urlBuilder
41+
) {
42+
$this->getPurchasedDownloadableProducts = $getPurchasedDownloadableProducts;
43+
$this->urlBuilder = $urlBuilder;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function resolve(
50+
Field $field,
51+
$context,
52+
ResolveInfo $info,
53+
array $value = null,
54+
array $args = null
55+
) {
56+
$currentUserId = $context->getUserId();
57+
$purchasedProducts = $this->getPurchasedDownloadableProducts->execute($currentUserId);
58+
$productsData = [];
59+
60+
/* The fields names are hardcoded since there's no existing name reference in the code */
61+
foreach ($purchasedProducts as $purchasedProduct) {
62+
if ($purchasedProduct['number_of_downloads_bought']) {
63+
$remainingDownloads = $purchasedProduct['number_of_downloads_bought'] -
64+
$purchasedProduct['number_of_downloads_used'];
65+
} else {
66+
$remainingDownloads = __('Unlimited');
67+
}
68+
69+
$productsData[] = [
70+
'order_increment_id' => $purchasedProduct['order_increment_id'],
71+
'date' => $purchasedProduct['created_at'],
72+
'status' => $purchasedProduct['status'],
73+
'download_url' => $this->urlBuilder->getUrl(
74+
'downloadable/download/link',
75+
['id' => $purchasedProduct['link_hash'], '_secure' => true]
76+
),
77+
'remaining_downloads' => $remainingDownloads
78+
];
79+
}
80+
81+
return ['items' => $productsData];
82+
}
83+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\DownloadableGraphQl\Model\ResourceModel;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Downloadable\Model\Link\Purchased\Item;
12+
13+
/**
14+
* Class GetPurchasedDownloadableProducts
15+
*
16+
* The model returns all purchased products for the specified customer
17+
*/
18+
class GetPurchasedDownloadableProducts
19+
{
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
/**
26+
* @param ResourceConnection $resourceConnection
27+
*/
28+
public function __construct(
29+
ResourceConnection $resourceConnection
30+
) {
31+
$this->resourceConnection = $resourceConnection;
32+
}
33+
34+
/**
35+
* Return available purchased products for customer
36+
*
37+
* @param int $customerId
38+
* @return array
39+
*/
40+
public function execute(int $customerId): array
41+
{
42+
$connection = $this->resourceConnection->getConnection();
43+
$allowedItemsStatuses = [Item::LINK_STATUS_PENDING_PAYMENT, Item::LINK_STATUS_PAYMENT_REVIEW];
44+
$downloadablePurchasedTable = $connection->getTableName('downloadable_link_purchased');
45+
46+
/* The fields names are hardcoded since there's no existing name reference in the code */
47+
$selectQuery = $connection->select()
48+
->from($downloadablePurchasedTable)
49+
->joinLeft(
50+
['item' => $connection->getTableName('downloadable_link_purchased_item')],
51+
"$downloadablePurchasedTable.purchased_id = item.purchased_id"
52+
)
53+
->where("$downloadablePurchasedTable.customer_id = ?", $customerId)
54+
->where('item.status NOT IN (?)', $allowedItemsStatuses);
55+
56+
return $connection->fetchAll($selectQuery);
57+
}
58+
}

app/code/Magento/DownloadableGraphQl/etc/schema.graphqls

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# Copyright © Magento, Inc. All rights reserved.
22
# See COPYING.txt for license details.
33

4+
type Query {
5+
customerDownloadableProducts: CustomerDownloadableProducts @resolver(class: "Magento\\DownloadableGraphQl\\Model\\Resolver\\CustomerDownloadableProducts") @doc(description: "The query returns the contents of a customer's downloadable products")
6+
}
7+
8+
type CustomerDownloadableProducts {
9+
items: [CustomerDownloadableProduct] @doc(description: "List of purchased downloadable items")
10+
}
11+
12+
type CustomerDownloadableProduct {
13+
order_increment_id: String
14+
date: String
15+
status: String
16+
download_url: String
17+
remaining_downloads: String
18+
}
19+
420
type DownloadableProduct implements ProductInterface, CustomizableProductInterface @doc(description: "DownloadableProduct defines a product that the customer downloads") {
521
downloadable_product_samples: [DownloadableProductSamples] @resolver(class: "Magento\\DownloadableGraphQl\\Model\\Resolver\\Product\\DownloadableOptions") @doc(description: "An array containing information about samples of this downloadable product.")
622
downloadable_product_links: [DownloadableProductLinks] @resolver(class: "Magento\\DownloadableGraphQl\\Model\\Resolver\\Product\\DownloadableOptions") @doc(description: "An array containing information about the links for this downloadable product")

0 commit comments

Comments
 (0)