Refactor error handling and enhance API interactions across Filament pages
- Introduced `ApiErrorTranslator` to normalize and translate API error messages, providing clearer feedback in French. - Updated all Filament pages (Articles, Documents, Divers, Journaux, Tiers, TablesExplorer) to utilize the new error translation mechanism, improving user experience during API interactions. - Added validation for required fields before API calls, ensuring users receive immediate feedback when mandatory inputs are missing. - Implemented tracking properties to distinguish between "never searched" and "searched without results," enhancing the user interface. - Removed the obsolete `$results` property from the Articles page and added a new `$barcode` property to align with API requirements. - Updated documentation to reflect changes in API behavior and error handling, including new metadata returned by the `art_list` endpoint. - Added new tests to verify the functionality of the barcode handling and validation logic.
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
|
||||
|
||||
use App\Exceptions\LogisticsApiException;
|
||||
use App\Services\LogisticsService;
|
||||
use App\Support\ApiErrorTranslator;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
|
||||
@@ -46,50 +47,72 @@ class Divers extends Page
|
||||
|
||||
public ?string $errorMessage = null;
|
||||
|
||||
// Tracking
|
||||
public bool $hasSerial = false;
|
||||
|
||||
public bool $hasCodes = false;
|
||||
|
||||
public bool $hasUpdatedStock = false;
|
||||
|
||||
public function getSerialNumber(): void
|
||||
{
|
||||
$this->errorMessage = null;
|
||||
$this->hasSerial = true;
|
||||
|
||||
try {
|
||||
$service = app(LogisticsService::class);
|
||||
$response = $service->getSerialNumber();
|
||||
|
||||
$this->serialData = is_array($response['data'] ?? null) ? $response['data'] : ['value' => $response['data'] ?? null];
|
||||
$this->errorMessage = $response['error'] ?? null;
|
||||
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
|
||||
} catch (LogisticsApiException $e) {
|
||||
$this->errorMessage = $e->getMessage();
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->serialData = [];
|
||||
} catch (\Throwable $e) {
|
||||
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->serialData = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function searchCodes(): void
|
||||
{
|
||||
$this->errorMessage = null;
|
||||
|
||||
if (blank($this->code)) {
|
||||
$this->errorMessage = 'Le champ debut de code (code) est obligatoire.';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->hasCodes = true;
|
||||
|
||||
try {
|
||||
$service = app(LogisticsService::class);
|
||||
$response = $service->codesList(['code' => $this->code]);
|
||||
|
||||
$this->codesData = $response['data'] ?? [];
|
||||
$this->errorMessage = $response['error'] ?? null;
|
||||
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
|
||||
} catch (LogisticsApiException $e) {
|
||||
$this->errorMessage = $e->getMessage();
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->codesData = [];
|
||||
} catch (\Throwable $e) {
|
||||
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->codesData = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function updateStock(): void
|
||||
{
|
||||
$this->errorMessage = null;
|
||||
|
||||
if (blank($this->stkArtId) || blank($this->stkId) || blank($this->stkQty)) {
|
||||
$this->errorMessage = 'Les champs ARTID, STKID et QTY sont obligatoires.';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->hasUpdatedStock = true;
|
||||
|
||||
try {
|
||||
$service = app(LogisticsService::class);
|
||||
|
||||
@@ -105,12 +128,12 @@ class Divers extends Page
|
||||
$response = $service->customGeninvUpdatestock($params);
|
||||
|
||||
$this->updateStockResult = $response['data'] ?? [];
|
||||
$this->errorMessage = $response['error'] ?? null;
|
||||
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
|
||||
} catch (LogisticsApiException $e) {
|
||||
$this->errorMessage = $e->getMessage();
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->updateStockResult = [];
|
||||
} catch (\Throwable $e) {
|
||||
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->updateStockResult = [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user