Skip to content

Commit

Permalink
[Downloadable purchased link issue fixed.]
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-webkul committed Aug 1, 2023
1 parent 18a6790 commit 8bea6dd
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 10 deletions.
27 changes: 21 additions & 6 deletions src/Mutations/Shop/Customer/AccountMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,17 @@ public function update($rootValue, array $args, GraphQLContext $context)
public function delete($rootValue, array $args, GraphQLContext $context)
{
if (! bagisto_graphql()->validateAPIUser($this->guard)) {
throw new Exception(trans('bagisto_graphql::app.admin.response.invalid-header'));
throw new CustomException(
trans('bagisto_graphql::app.admin.response.invalid-header'),
'Invalid request parameters.'
);
}

if (! bagisto_graphql()->guard($this->guard)->check() ) {
throw new Exception(trans('bagisto_graphql::app.shop.customer.no-login-customer'));
if (! bagisto_graphql()->guard($this->guard)->check()) {
throw new CustomException(
trans('bagisto_graphql::app.shop.customer.no-login-customer'),
'Customer not logged in'
);
}
$data = $args['input'];

Expand All @@ -257,7 +263,10 @@ public function delete($rootValue, array $args, GraphQLContext $context)
$orders = $customer->all_orders->whereIn('status', ['pending', 'processing'])->first();

if ($orders) {
throw new Exception(trans('admin::app.response.order-pending', ['name' => 'Customer']));
throw new CustomException(
trans('admin::app.response.order-pending', ['name' => 'Customer']),
'Orders are pending'
);
} else {
$this->customerRepository->delete($customer->id);

Expand All @@ -267,10 +276,16 @@ public function delete($rootValue, array $args, GraphQLContext $context)
];
}
} else {
throw new Exception(trans('shop::app.customer.account.address.delete.wrong-password'));
throw new CustomException(
trans('shop::app.customer.account.address.delete.wrong-password'),
'Provided Wrong Password.'
);
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
throw new CustomException(
$e->getMessage(),
'Something went wrong, try again'
);
}
}
}
128 changes: 127 additions & 1 deletion src/Mutations/Shop/Customer/DownloadableMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Storage;
use Webkul\Shop\Http\Controllers\Controller;
use Webkul\Sales\Repositories\DownloadableLinkPurchasedRepository;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
Expand All @@ -21,9 +22,10 @@ class DownloadableMutation extends Controller
/**
* Create a new controller instance.
*
* @param \Webkul\Sales\Repositories\DownloadableLinkPurchasedRepository $downloadableLinkPurchasedRepository
* @return void
*/
public function __construct()
public function __construct(protected DownloadableLinkPurchasedRepository $downloadableLinkPurchasedRepository)
{
$this->guard = 'api';

Expand Down Expand Up @@ -123,4 +125,128 @@ public function downloadLinks($rootValue, array $args , GraphQLContext $context)
);
}
}

/**
* Download the for the specified resource.
*
* @param null $rootValue
* @param array{} $args
* @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext $context
* @return \Illuminate\Http\Response
*/
public function download($rootValue, array $args, GraphQLContext $context)
{
if (! bagisto_graphql()->validateAPIUser($this->guard)) {
throw new CustomException(
trans('bagisto_graphql::app.admin.response.invalid-header'),
'Invalid request header parameter.'
);
}

if (! bagisto_graphql()->guard($this->guard)->check() ) {
throw new CustomException(
trans('bagisto_graphql::app.shop.customer.no-login-customer'),
'No Login Customer Found.'
);
}

$customer = bagisto_graphql()->guard($this->guard)->user();

try {
$downloadableLinkPurchased = $this->downloadableLinkPurchasedRepository->findOneByField([
'id' => $args['id'],
'customer_id' => $customer->id,
]);

if (
! $downloadableLinkPurchased
|| $downloadableLinkPurchased->status == 'pending'
) {
throw new CustomException(
trans('bagisto_graphql::app.shop.customer.not-authorized'),
'You are not authorized to perform this action.'
);
}

$totalInvoiceQty = 0;
if (isset($downloadableLinkPurchased->order->invoices)) {
foreach ($downloadableLinkPurchased->order->invoices as $invoice) {
$totalInvoiceQty = $totalInvoiceQty + $invoice->total_qty;
}
}

$orderedQty = $downloadableLinkPurchased->order->total_qty_ordered;
$totalInvoiceQty = $totalInvoiceQty * ($downloadableLinkPurchased->download_bought / $orderedQty);
$totalUsedAndCancelQty = $downloadableLinkPurchased->download_used + $downloadableLinkPurchased->download_canceled;

if (
$downloadableLinkPurchased->download_used == $totalInvoiceQty
|| $downloadableLinkPurchased->download_used > $totalInvoiceQty
) {
throw new CustomException(
trans('shop::app.customer.account.downloadable_products.payment-error'),
'Downloadable product payment error.'
);
}

if (
$downloadableLinkPurchased->download_bought
&& ($downloadableLinkPurchased->download_bought - $totalUsedAndCancelQty) <= 0
) {
throw new CustomException(
trans('shop::app.customer.account.downloadable_products.download-error'),
'Downloadable link product download error.'
);
}

$remainingDownloads = $downloadableLinkPurchased->download_bought - ($totalUsedAndCancelQty + 1);

if ($downloadableLinkPurchased->download_bought) {
$this->downloadableLinkPurchasedRepository->update([
'download_used' => $downloadableLinkPurchased->download_used + 1,
'status' => $remainingDownloads <= 0 ? 'expired' : $downloadableLinkPurchased->status,
], $downloadableLinkPurchased->id);
}

if ($downloadableLinkPurchased->type == 'url') {
$type = pathinfo($downloadableLinkPurchased->url, PATHINFO_EXTENSION);

$base64_code = base64_encode(file_get_contents($downloadableLinkPurchased->url));

$base64_str = 'data:image/' . $type . ';base64,' . $base64_code;

return [
'status' => true,
'string' => $base64_str,
'download' => $this->downloadableLinkPurchasedRepository->findOrFail($args['id'])
];
}

$privateDisk = Storage::disk('private');

if (! $privateDisk->exists($downloadableLinkPurchased->file)) {
throw new CustomException(
trans('shop::app.customer.account.downloadable_products.download-error'),
'Downloadable link product download error.'
);
}

$pathToFile = $privateDisk->path($downloadableLinkPurchased->file);

$type = pathinfo($pathToFile, PATHINFO_EXTENSION);

$base64_str = 'data:image/' . $type . ';base64,' . base64_encode(file_get_contents($pathToFile));

return [
'status' => true,
'string' => $base64_str,
'download' => $this->downloadableLinkPurchasedRepository->findOrFail($args['id'])
];
} catch (\Exception $e) {
throw new CustomException(
$e->getMessage(),
'Failed: Downloadable purchased link.'
);
}
}
}
19 changes: 19 additions & 0 deletions src/Queries/Catalog/ProductContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ public function checkIsInWishlist($rootValue, array $args, GraphQLContext $conte
return false;
}

/**
* Check product is in sale
*
* @param mixed $rootValue
* @param array $args
* @param GraphQLContext $context
* @return bool
*/
public function checkIsInSale($rootValue, array $args, GraphQLContext $context)
{
$productTypeInstance = $rootValue->getTypeInstance();

if ($productTypeInstance->haveSpecialPrice()) {
return true;
}

return false;
}

/**
* Get configurable data.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Queries/Shop/Product/ProductListingQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public function getFilterAttributes($rootValue, array $args, GraphQLContext $con

$availableSortOrders[$key] = [
'key' => $key,
'label' => $label,
'label' => trans('shop::app.products.' . $label),
'value' => [
'sort' => current($keys),
'order' => end($keys),
Expand Down
1 change: 1 addition & 0 deletions src/graphql/catalog/product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type Product {
videos: [Video!] @hasMany
additionalData: [ProductAdditionalData] @field(resolver: "Webkul\\GraphQLAPI\\Queries\\Catalog\\ProductContent@getAdditionalData")
isInWishlist: Boolean @field(resolver: "Webkul\\GraphQLAPI\\Queries\\Catalog\\ProductContent@checkIsInWishlist")
isInSale: Boolean @field(resolver: "Webkul\\GraphQLAPI\\Queries\\Catalog\\ProductContent@checkIsInSale")
priceHtml: ProductPriceHtml @field(resolver: "Webkul\\GraphQLAPI\\Queries\\Catalog\\ProductContent@getProductPriceHtml")
orderedInventories: [OrderInventory!] @hasMany(relation: "ordered_inventories")
reviews: [Review] @field(resolver: "Webkul\\GraphQLAPI\\Queries\\Catalog\\ProductContent@getReviews")
Expand Down
23 changes: 21 additions & 2 deletions src/graphql/shop/customer/downloadable_link_purchase.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ extend type Query @guard(with: ["api"]){
downloadableLinkPurchase(id: ID @eq): DownloadableLinkPurchased @field(resolver: "Webkul\\GraphQLAPI\\Mutations\\Shop\\Customer\\DownloadableMutation@downloadLinks")
}

extend type Mutation @guard(with: ["api"]) {
downloadLink(
id: ID!
): DownloadLinkResponse
@field(resolver: "Webkul\\GraphQLAPI\\Mutations\\Shop\\Customer\\DownloadableMutation@download")
}

input FilterDownloadablePurchaseInput {
page: Int
limit: Int
Expand All @@ -13,7 +20,7 @@ input FilterDownloadablePurchaseInput {
orderItemId: Int @rename(attribute: "order_item_id")
productName: String @rename(attribute: "product_name")
linkName: String @rename(attribute: "link_name")
status: String
status: DownloadLinkStatusEnum
downloadBought: Int @rename(attribute: "download_bought")
downloadUsed: Int @rename(attribute: "download_used")
}
Expand All @@ -37,4 +44,16 @@ type DownloadableLinkPurchased {
customer: Customer @belongsTo(relation: "customer")
order: Order! @belongsTo(relation: "order")
orderItem: OrderItem @belongsTo(relation: "order_item")
}
}

type DownloadLinkResponse {
status: Boolean
string: String
download: DownloadableLinkPurchased
}

enum DownloadLinkStatusEnum {
PENDING @enum(value: "pending")
AVAILABLE @enum(value: "available")
EXPIRED @enum(value: "expired")
}

0 comments on commit 8bea6dd

Please sign in to comment.