fix(transport) : securiser la synchro QUALIMAT (revue ERP-39)

- garde-fou anti-desactivation de masse : fetchRemote leve sur un payload
  non-list (2xx inattendu) et la commande abandonne sans ecriture si aucune
  ligne exploitable, au lieu de soft-delete tout le referentiel
- verrou consultatif pg_try_advisory_lock pour serialiser les runs (anti-overlap)
- deduplication par SIRET dans le mapper (rows_upserted = transporteurs distincts)
- upsert par paquets (INSERT groupe) au lieu d'un aller-retour par ligne
- migration des tables qualimat deplacee vers le namespace modulaire Transport
  (+ enregistrement du path dans doctrine_migrations.yaml)
- tests : deduplication + abandon sur source vide
This commit is contained in:
2026-06-15 10:20:53 +02:00
parent c8bff68373
commit 0b9aaef38e
6 changed files with 169 additions and 44 deletions
@@ -73,6 +73,21 @@ final class QualimatRowMapperTest extends TestCase
self::assertSame(2, $result['skipped']);
}
public function testMapManyDeduplicatesBySiretLastWins(): void
{
// Memes chiffres a separateurs pres : un seul transporteur, derniere
// occurrence gagnante (le compte ne doit pas surcompter les doublons).
$result = QualimatRowMapper::mapMany([
['Nom' => 'PREMIER', 'Siret' => '111 111 111 00011', 'Statut' => 'Audité'],
['Nom' => 'DERNIER', 'Siret' => '11111111100011', 'Statut' => 'Valide'],
]);
self::assertCount(1, $result['rows']);
self::assertSame(0, $result['skipped']);
self::assertSame('DERNIER', $result['rows'][0]['name']);
self::assertSame('Valide', $result['rows'][0]['status']);
}
public function testEmptyOptionalFieldsBecomeNull(): void
{
$row = QualimatRowMapper::mapOne([
@@ -7,6 +7,7 @@ namespace App\Tests\Module\Transport\Infrastructure\Console;
use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use const JSON_THROW_ON_ERROR;
@@ -108,6 +109,30 @@ final class SyncQualimatCommandTest extends KernelTestCase
self::assertSame(0, (int) $log['rows_skipped']);
}
public function testEmptySourceAbortsWithoutMassDeactivation(): void
{
// Premier run : 2 transporteurs actifs.
$a = ['Nom' => 'A', 'Siret' => '111 111 111 00011', 'Validite' => '01/01/2030', 'Statut' => 'Audité'];
$b = ['Nom' => 'B', 'Siret' => '222 222 222 00022', 'Validite' => '01/01/2030', 'Statut' => 'Audité'];
$this->runSync([$a, $b])->assertCommandIsSuccessful();
self::assertSame(2, $this->countRows('SELECT COUNT(*) FROM qualimat_carrier WHERE is_active = TRUE'));
// Source ne contenant que des items inexploitables (zero ligne mappee) :
// la commande doit ECHOUER sans toucher le referentiel (pas de soft-delete
// de masse) et sans journaliser de run.
$logsBefore = $this->countRows('SELECT COUNT(*) FROM qualimat_sync_log');
$tester = $this->runSync([
['Nom' => 'SANS SIRET 1', 'Siret' => null],
['Nom' => 'SANS SIRET 2', 'Siret' => ' '],
]);
self::assertSame(Command::FAILURE, $tester->getStatusCode());
// Les 2 transporteurs restent ACTIFS (aucune desactivation de masse).
self::assertSame(2, $this->countRows('SELECT COUNT(*) FROM qualimat_carrier WHERE is_active = TRUE'));
// Aucun journal supplementaire (abandon avant la transaction).
self::assertSame($logsBefore, $this->countRows('SELECT COUNT(*) FROM qualimat_sync_log'));
}
/**
* @param array<int, array<string, mixed>> $items
*/