- Added AssociateProductIncoOutput and SyncProductIncoOutput classes to handle action results. - Updated AssociateProductInco and SyncProductInco actions to return structured output instead of raw models. - Enhanced error handling for API interactions in both actions. - Refactored ProductIncosController to utilize new output structures for better response management. - Introduced new enums for status handling in actions and queries. - Added tests for new output handling and error scenarios in Associate and Sync actions.
131 lines
4.4 KiB
PHP
131 lines
4.4 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\Enums\ArcadiaIncoPreviewStatus;
|
|
use App\Enums\ProductIncoSyncStatus;
|
|
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 App\Models\ProductInco;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
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);
|
|
|
|
$output = $this->executor->execute(
|
|
$query,
|
|
$request->toGetArcadiaIncoPreviewQueryInput(),
|
|
);
|
|
|
|
return match ($output->status) {
|
|
ArcadiaIncoPreviewStatus::FOUND => response()->json($output->preview),
|
|
ArcadiaIncoPreviewStatus::NOT_FOUND => response()->json([
|
|
'message' => __('No INCO sheet found on Arcadia for this article number.'),
|
|
], 404),
|
|
ArcadiaIncoPreviewStatus::API_ERROR => response()->json([
|
|
'message' => __('Arcadia API error: :error', ['error' => $output->errorMessage ?? '']),
|
|
], 502),
|
|
};
|
|
}
|
|
|
|
public function associate(AssociateProductIncoRequest $request, Product $product, AssociateProductInco $action): RedirectResponse
|
|
{
|
|
$this->authorize('associateInco', $product);
|
|
|
|
$output = $this->executor->execute(
|
|
$action,
|
|
$request->toAssociateProductIncoInput($product->getId()),
|
|
);
|
|
|
|
return $this->buildSyncRedirect(
|
|
$output->status,
|
|
$product,
|
|
$output->inco,
|
|
$output->errorMessage,
|
|
__('The INCO sheet has been successfully associated with the product.'),
|
|
);
|
|
}
|
|
|
|
public function sync(SyncProductIncoRequest $request, Product $product, SyncProductInco $action): RedirectResponse
|
|
{
|
|
$this->authorize('syncInco', $product);
|
|
|
|
$output = $this->executor->execute(
|
|
$action,
|
|
$request->toSyncProductIncoInput($product->getId()),
|
|
);
|
|
|
|
return $this->buildSyncRedirect(
|
|
$output->status,
|
|
$product,
|
|
$output->inco,
|
|
$output->errorMessage,
|
|
__('The INCO sheet has been successfully synchronized from Arcadia.'),
|
|
);
|
|
}
|
|
|
|
private function buildSyncRedirect(
|
|
ProductIncoSyncStatus $status,
|
|
Product $product,
|
|
?ProductInco $inco,
|
|
?string $errorMessage,
|
|
string $successMessage,
|
|
): RedirectResponse {
|
|
return match ($status) {
|
|
ProductIncoSyncStatus::SUCCESS => $this->redirectToEditSuccess($product, $inco, $successMessage),
|
|
ProductIncoSyncStatus::NOT_ASSOCIATED,
|
|
ProductIncoSyncStatus::NOT_FOUND_ON_ARCADIA,
|
|
ProductIncoSyncStatus::API_ERROR => back()->with(
|
|
'danger',
|
|
__('Arcadia API error: :error', ['error' => $errorMessage ?? '']),
|
|
),
|
|
};
|
|
}
|
|
|
|
private function redirectToEditSuccess(Product $product, ?ProductInco $inco, string $message): RedirectResponse
|
|
{
|
|
Assert::notNull($inco);
|
|
|
|
return to_route('bo.products.incos.edit', [
|
|
'product' => $product,
|
|
'inco' => $inco,
|
|
])->with('success', $message);
|
|
}
|
|
|
|
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,
|
|
]);
|
|
}
|
|
}
|