Implement GetProductIncoForEditQuery, its input and output classes to handle product INCO data retrieval. Update ProductIncosController to utilize the new query for editing product INCOs. Add tests for GetProductIncoForEditQuery to ensure correct behavior.
101 lines
3.5 KiB
PHP
101 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Contracts\Executor;
|
|
use App\Cqs\Actions\ProductIncos\AssociateProductInco;
|
|
use App\Cqs\Actions\ProductIncos\SyncProductInco;
|
|
use App\Cqs\Queries\ProductIncos\GetArcadiaIncoPreviewQuery;
|
|
use App\Cqs\Queries\ProductIncos\GetProductIncoForEditQuery;
|
|
use App\Cqs\Queries\ProductIncos\GetProductIncoForEditQueryInput;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\ProductIncos\AssociateProductIncoRequest;
|
|
use App\Http\Requests\Bo\ProductIncos\PreviewArcadiaIncoRequest;
|
|
use App\Http\Requests\Bo\ProductIncos\SyncProductIncoRequest;
|
|
use App\Models\Product;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
class ProductIncosController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly Executor $executor,
|
|
) {
|
|
}
|
|
|
|
public function preview(PreviewArcadiaIncoRequest $request, Product $product, GetArcadiaIncoPreviewQuery $query): JsonResponse
|
|
{
|
|
$this->authorize('associateInco', $product);
|
|
|
|
try {
|
|
$preview = $this->executor->execute(
|
|
$query,
|
|
$request->toGetArcadiaIncoPreviewQueryInput(),
|
|
);
|
|
} catch (\RuntimeException $e) {
|
|
return response()->json([
|
|
'message' => __('Arcadia API error: :error', ['error' => $e->getMessage()]),
|
|
], 502);
|
|
}
|
|
|
|
if ($preview === null) {
|
|
return response()->json([
|
|
'message' => __('No INCO sheet found on Arcadia for this article number.'),
|
|
], 404);
|
|
}
|
|
|
|
return response()->json($preview);
|
|
}
|
|
|
|
public function associate(AssociateProductIncoRequest $request, Product $product, AssociateProductInco $action): RedirectResponse
|
|
{
|
|
$this->authorize('associateInco', $product);
|
|
|
|
try {
|
|
$inco = $this->executor->execute(
|
|
$action,
|
|
$request->toAssociateProductIncoInput($product->getId()),
|
|
);
|
|
} catch (\RuntimeException $e) {
|
|
return back()->with('danger', __('Arcadia API error: :error', ['error' => $e->getMessage()]));
|
|
}
|
|
|
|
return to_route('bo.products.incos.edit', compact('product', 'inco'))
|
|
->with('success', __('The INCO sheet has been successfully associated with the product.'));
|
|
}
|
|
|
|
public function sync(SyncProductIncoRequest $request, Product $product, SyncProductInco $action): RedirectResponse
|
|
{
|
|
$this->authorize('syncInco', $product);
|
|
|
|
try {
|
|
$inco = $this->executor->execute(
|
|
$action,
|
|
$request->toSyncProductIncoInput($product->getId()),
|
|
);
|
|
} catch (\RuntimeException $e) {
|
|
return back()->with('danger', __('Arcadia API error: :error', ['error' => $e->getMessage()]));
|
|
}
|
|
|
|
return to_route('bo.products.incos.edit', compact('product', 'inco'))
|
|
->with('success', __('The INCO sheet has been successfully synchronized from Arcadia.'));
|
|
}
|
|
|
|
public function edit(Product $product, GetProductIncoForEditQuery $query): View
|
|
{
|
|
$this->authorize('associateInco', $product);
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
new GetProductIncoForEditQueryInput($product->getId()),
|
|
);
|
|
|
|
return view('bo.product-incos.edit', [
|
|
'product' => $output->product,
|
|
'inco' => $output->inco,
|
|
]);
|
|
}
|
|
}
|