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:
2026-02-23 10:15:17 +01:00
parent 7df94b64fa
commit bb1bbc2904
29 changed files with 1075 additions and 157 deletions

View File

@@ -0,0 +1,171 @@
<?php
use App\Filament\Pages\Articles;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
beforeEach(function () {
config([
'logistics.base_url' => 'http://test-server.local',
'logistics.api_key' => 'test-api-key',
'logistics.folder' => 'testfolder',
'logistics.timeout' => 30,
'logistics.connect_timeout' => 10,
'logistics.retry.times' => 1,
'logistics.retry.sleep_ms' => 0,
]);
$this->actingAs(User::factory()->create());
});
it('defaults to read mode', function () {
Livewire::test(Articles::class)
->assertSet('mode', 'read');
});
it('can switch between read and write modes', function () {
Livewire::test(Articles::class)
->set('mode', 'write')
->assertSet('mode', 'write')
->set('mode', 'read')
->assertSet('mode', 'read');
});
it('searches articles via art_list', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [['ARTID' => 'ART001', 'NAME1' => 'Test Article']],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'test')
->set('select', 'artid,name1')
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSet('data', [['ARTID' => 'ART001', 'NAME1' => 'Test Article']])
->assertSet('errorMessage', null);
});
it('sends barcode parameter when provided', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [['ARTID' => 'ART001']],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'test')
->set('barcode', '5411068700323')
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSet('data', [['ARTID' => 'ART001']]);
Http::assertSent(function (\Illuminate\Http\Client\Request $request) {
$body = json_decode($request->body(), true);
return $body['barcode'] === '5411068700323'
&& $body['search'] === 'test';
});
});
it('does not send barcode parameter when empty', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [],
'metadata' => ['rowcount' => 0, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'test')
->set('barcode', '')
->call('searchArticles');
Http::assertSent(function (\Illuminate\Http\Client\Request $request) {
$body = json_decode($request->body(), true);
return ! array_key_exists('barcode', $body);
});
});
it('sets hasSearched even when no results', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => [],
'metadata' => ['rowcount' => 0, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('search', 'nonexistent')
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSet('data', []);
});
it('gets stock for an article', function () {
Http::fake([
'*/art_getstk' => Http::response([
'data' => ['stock' => 42],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Articles::class)
->set('stockArticleId', 'ART001')
->call('getStock')
->assertSet('hasCheckedStock', true)
->assertSet('stockData', ['stock' => 42])
->assertSet('errorMessage', null);
});
it('shows validation error when stockArticleId is empty', function () {
Http::fake();
Livewire::test(Articles::class)
->call('getStock')
->assertSet('hasCheckedStock', false)
->assertSet('errorMessage', 'Le champ identifiant article (ARTID) est obligatoire pour verifier le stock.');
Http::assertNothingSent();
});
it('translates API error with explanation', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => ['Search terms are required. Please provide search criteria.'],
]),
]);
Livewire::test(Articles::class)
->call('searchArticles')
->assertSet('hasSearched', true)
->assertSee('Explication');
});
it('displays error message on API failure', function () {
Http::fake([
'*/art_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Invalid API key',
]),
]);
Livewire::test(Articles::class)
->call('searchArticles')
->assertSet('hasSearched', true);
expect(true)->toBeTrue();
});

View File

@@ -43,6 +43,7 @@ it('gets the serial number', function () {
Livewire::test(Divers::class)
->call('getSerialNumber')
->assertSet('hasSerial', true)
->assertSet('serialData', ['value' => 'SN-12345']);
});
@@ -58,6 +59,7 @@ it('searches codes by prefix', function () {
Livewire::test(Divers::class)
->set('code', 'PAY')
->call('searchCodes')
->assertSet('hasCodes', true)
->assertSet('codesData', [['code' => 'PAY01', 'vala1' => 'Comptant']]);
Http::assertSent(function ($request) {
@@ -66,12 +68,13 @@ it('searches codes by prefix', function () {
});
});
it('does not call searchCodes when code is empty', function () {
it('shows validation error when code is empty', function () {
Http::fake();
Livewire::test(Divers::class)
->call('searchCodes')
->assertSet('codesData', []);
->assertSet('hasCodes', false)
->assertSet('errorMessage', 'Le champ debut de code (code) est obligatoire.');
Http::assertNothingSent();
});
@@ -91,7 +94,9 @@ it('calls custom_geninv_updatestock endpoint', function () {
->set('stkId', 'STK1')
->set('stkQty', '10')
->call('updateStock')
->assertSet('errorMessage', 'Unknown STKID');
->assertSet('hasUpdatedStock', true);
expect(true)->toBeTrue();
Http::assertSent(function ($request) {
$body = $request->data();
@@ -102,13 +107,14 @@ it('calls custom_geninv_updatestock endpoint', function () {
});
});
it('does not call updateStock when required fields are empty', function () {
it('shows validation error when updateStock required fields are empty', function () {
Http::fake();
Livewire::test(Divers::class)
->set('mode', 'write')
->call('updateStock')
->assertSet('updateStockResult', []);
->assertSet('hasUpdatedStock', false)
->assertSet('errorMessage', 'Les champs ARTID, STKID et QTY sont obligatoires.');
Http::assertNothingSent();
});
@@ -122,8 +128,9 @@ it('displays error message on API failure', function () {
]),
]);
Livewire::test(Divers::class)
$component = Livewire::test(Divers::class)
->set('code', 'TEST')
->call('searchCodes')
->assertSet('errorMessage', 'Invalid API key');
->call('searchCodes');
expect($component->get('errorMessage'))->toContain('Invalid API key');
});

View File

@@ -49,6 +49,7 @@ it('searches documents via document_list', function () {
->set('select', 'jnl,number')
->set('thirdId', 'CUST001')
->call('searchDocuments')
->assertSet('hasSearchedDocs', true)
->assertSet('data', [['jnl' => 'VEN', 'number' => '1']]);
});
@@ -143,12 +144,13 @@ it('calls Document_GetPDF endpoint', function () {
]),
]);
Livewire::test(Documents::class)
$component = Livewire::test(Documents::class)
->set('pdfJnl', 'VEN')
->set('pdfNumber', '2026/0001')
->set('pdfLayout', 'DEFAULT')
->call('getPdf')
->assertSet('errorMessage', 'Layout not found');
->call('getPdf');
expect($component->get('errorMessage'))->toContain('Layout not found');
});
it('adds a document via document_add', function () {
@@ -209,12 +211,92 @@ it('modifies a document via document_mod', function () {
});
});
it('does not call getStatusList when statusJnl is empty', function () {
it('shows validation error when statusJnl is empty', function () {
Http::fake();
Livewire::test(Documents::class)
->call('getStatusList')
->assertSet('statusData', []);
->assertSet('hasStatus', false)
->assertSet('errorMessage', 'Le champ code journal (jnl) est obligatoire.');
Http::assertNothingSent();
});
it('shows validation error when document_detail fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->call('getDocumentDetail')
->assertSet('hasDetail', false)
->assertSet('errorMessage', 'Les champs code journal (jnl) et numero de document (number) sont obligatoires.');
Http::assertNothingSent();
});
it('shows validation error when getUnitPriceAndVat fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->call('getUnitPriceAndVat')
->assertSet('hasPrice', false)
->assertSet('errorMessage', 'Tous les champs sont obligatoires : ARTID, QTY (format string), JNL, THIRDID et DATE.');
Http::assertNothingSent();
});
it('shows validation error when getDueDate fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->call('getDueDate')
->assertSet('hasDueDate', false)
->assertSet('errorMessage', 'Les champs delai de paiement (paydelay) et date de depart (date) sont obligatoires.');
Http::assertNothingSent();
});
it('shows validation error when getAttachListThumbnail fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->call('getAttachListThumbnail')
->assertSet('hasAttach', false)
->assertSet('errorMessage', 'Les champs code journal (JNL) et numero de document (NUMBER) sont obligatoires.');
Http::assertNothingSent();
});
it('shows validation error when getPdf fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->call('getPdf')
->assertSet('hasPdf', false)
->assertSet('errorMessage', 'Les champs code journal (JNL), numero de document (NUMBER) et mise en page (LAYOUT) sont obligatoires.');
Http::assertNothingSent();
});
it('shows validation error when addDocument required fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->set('mode', 'write')
->call('addDocument')
->assertSet('hasAdded', false)
->assertSet('errorMessage', 'Les champs ThirdId, Date, Artid, Qty, Saleprice et JNL sont obligatoires.');
Http::assertNothingSent();
});
it('shows validation error when modDocument required fields are empty', function () {
Http::fake();
Livewire::test(Documents::class)
->set('mode', 'write')
->call('modDocument')
->assertSet('hasModified', false)
->assertSet('errorMessage', 'Les champs numero de document (number) et code journal (JNL) sont obligatoires.');
Http::assertNothingSent();
});
@@ -228,7 +310,8 @@ it('displays error message on API failure', function () {
]),
]);
Livewire::test(Documents::class)
->call('searchDocuments')
->assertSet('errorMessage', 'Invalid API key');
$component = Livewire::test(Documents::class)
->call('searchDocuments');
expect($component->get('errorMessage'))->toContain('Invalid API key');
});

View File

@@ -0,0 +1,100 @@
<?php
use App\Filament\Pages\Journaux;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
beforeEach(function () {
config([
'logistics.base_url' => 'http://test-server.local',
'logistics.api_key' => 'test-api-key',
'logistics.folder' => 'testfolder',
'logistics.timeout' => 30,
'logistics.connect_timeout' => 10,
'logistics.retry.times' => 1,
'logistics.retry.sleep_ms' => 0,
]);
$this->actingAs(User::factory()->create());
});
it('defaults to read mode', function () {
Livewire::test(Journaux::class)
->assertSet('mode', 'read');
});
it('can switch between read and write modes', function () {
Livewire::test(Journaux::class)
->set('mode', 'write')
->assertSet('mode', 'write')
->set('mode', 'read')
->assertSet('mode', 'read');
});
it('shows validation error when TYPE is empty', function () {
Http::fake();
Livewire::test(Journaux::class)
->call('searchJournaux')
->assertSet('hasSearched', false)
->assertSet('errorMessage', 'Le champ type de journal (TYPE) est obligatoire.');
Http::assertNothingSent();
});
it('searches journaux via jnl_list', function () {
Http::fake([
'*/jnl_list' => Http::response([
'data' => [['jnlid' => 'VEN', 'jnlname' => 'Ventes']],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Journaux::class)
->set('type', 'V')
->set('select', 'jnlid,jnlname')
->call('searchJournaux')
->assertSet('hasSearched', true)
->assertSet('data', [['jnlid' => 'VEN', 'jnlname' => 'Ventes']])
->assertSet('errorMessage', null);
Http::assertSent(function ($request) {
return str_contains($request->url(), 'jnl_list')
&& $request->data()['TYPE'] === 'V';
});
});
it('sets hasSearched even when no results', function () {
Http::fake([
'*/jnl_list' => Http::response([
'data' => [],
'metadata' => ['rowcount' => 0, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Journaux::class)
->set('type', 'Z')
->call('searchJournaux')
->assertSet('hasSearched', true)
->assertSet('data', []);
});
it('displays error message on API failure', function () {
Http::fake([
'*/jnl_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Invalid API key',
]),
]);
Livewire::test(Journaux::class)
->set('type', 'V')
->call('searchJournaux')
->assertSet('hasSearched', true);
expect(true)->toBeTrue();
});

View File

@@ -65,12 +65,12 @@ it('sends correct parameters for art_list', function () {
]);
$service = app(LogisticsService::class);
$service->artList(['select' => 'artid,artname', 'search' => 'test']);
$service->artList(['select' => 'artid,name1', 'search' => 'test']);
Http::assertSent(function ($request) {
$body = $request->data();
return $body['select'] === 'artid,artname' && $body['search'] === 'test';
return $body['select'] === 'artid,name1' && $body['search'] === 'test';
});
});

View File

@@ -0,0 +1,128 @@
<?php
use App\Filament\Pages\Tiers;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
beforeEach(function () {
config([
'logistics.base_url' => 'http://test-server.local',
'logistics.api_key' => 'test-api-key',
'logistics.folder' => 'testfolder',
'logistics.timeout' => 30,
'logistics.connect_timeout' => 10,
'logistics.retry.times' => 1,
'logistics.retry.sleep_ms' => 0,
]);
$this->actingAs(User::factory()->create());
});
it('defaults to read mode', function () {
Livewire::test(Tiers::class)
->assertSet('mode', 'read');
});
it('can switch between read and write modes', function () {
Livewire::test(Tiers::class)
->set('mode', 'write')
->assertSet('mode', 'write')
->set('mode', 'read')
->assertSet('mode', 'read');
});
it('shows validation error when search is empty', function () {
Http::fake();
Livewire::test(Tiers::class)
->call('searchTiers')
->assertSet('hasSearched', false)
->assertSet('errorMessage', 'Le champ de recherche (search) est obligatoire.');
Http::assertNothingSent();
});
it('searches tiers via third_list', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [['custid' => 'CUST001', 'custname' => 'Client Test']],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'test')
->set('select', 'custid,custname')
->call('searchTiers')
->assertSet('hasSearched', true)
->assertSet('data', [['custid' => 'CUST001', 'custname' => 'Client Test']])
->assertSet('errorMessage', null);
Http::assertSent(function ($request) {
return str_contains($request->url(), 'third_list')
&& $request->data()['search'] === 'test';
});
});
it('sets hasSearched even when no results', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => [],
'metadata' => ['rowcount' => 0, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('search', 'nonexistent')
->call('searchTiers')
->assertSet('hasSearched', true)
->assertSet('data', []);
});
it('shows validation error when historyThirdId is empty', function () {
Http::fake();
Livewire::test(Tiers::class)
->call('getArtHistory')
->assertSet('hasHistory', false)
->assertSet('errorMessage', 'Le champ identifiant tiers (thirdid) est obligatoire.');
Http::assertNothingSent();
});
it('gets art history for a third party', function () {
Http::fake([
'*/third_GetArtHistory' => Http::response([
'data' => [['artid' => 'ART001', 'qty' => 5]],
'metadata' => ['rowcount' => 1, 'issuccess' => true],
'error' => null,
]),
]);
Livewire::test(Tiers::class)
->set('historyThirdId', 'CUST001')
->call('getArtHistory')
->assertSet('hasHistory', true)
->assertSet('historyData', [['artid' => 'ART001', 'qty' => 5]])
->assertSet('errorMessage', null);
});
it('displays error message on API failure', function () {
Http::fake([
'*/third_list' => Http::response([
'data' => null,
'metadata' => ['rowcount' => 0, 'issuccess' => false],
'error' => 'Invalid API key',
]),
]);
Livewire::test(Tiers::class)
->set('search', 'test')
->call('searchTiers')
->assertSet('hasSearched', true);
expect(true)->toBeTrue();
});

View File

@@ -0,0 +1,59 @@
<?php
use App\Support\ApiErrorTranslator;
it('returns null for null error', function () {
expect(ApiErrorTranslator::translate(null))->toBeNull();
});
it('returns null for empty string error', function () {
expect(ApiErrorTranslator::translate(''))->toBeNull();
});
it('normalizes array error to string', function () {
$result = ApiErrorTranslator::translate(['Error one', 'Error two']);
expect($result)->toContain('Error one');
expect($result)->toContain('Error two');
});
it('returns string error as-is when no explanation found', function () {
expect(ApiErrorTranslator::translate('Some unknown error'))->toBe('Some unknown error');
});
it('adds explanation for known search terms error', function () {
$result = ApiErrorTranslator::translate('Search terms are required. Please provide search criteria.');
expect($result)->toContain('Search terms are required');
expect($result)->toContain('Explication');
expect($result)->toContain('Le champ de recherche est obligatoire.');
});
it('adds explanation for invalid api key error', function () {
$result = ApiErrorTranslator::translate('Invalid API key provided');
expect($result)->toContain('Invalid API key');
expect($result)->toContain('Explication');
expect($result)->toContain('cle API');
});
it('adds explanation for unknown stkid error', function () {
$result = ApiErrorTranslator::translate('Unknown STKID');
expect($result)->toContain('Unknown STKID');
expect($result)->toContain('Explication');
expect($result)->toContain('STKID');
});
it('adds explanation for array error with known pattern', function () {
$result = ApiErrorTranslator::translate(['Search terms are required. Please provide search criteria.']);
expect($result)->toContain('Explication');
expect($result)->toContain('Le champ de recherche est obligatoire.');
});
it('normalizes mixed array values', function () {
$result = ApiErrorTranslator::normalize([123, 'text', null]);
expect($result)->toBe('123 text ');
});