feat(logistique) : pesée pont bascule stub + allocateur DSD + endpoint (ERP-184)

- WeighbridgeReaderInterface (contrat) + RandomWeighbridgeReader (stub,
  poids aléatoire ∈ [10000,50000] kg, RG-5.06) + WeighbridgeUnavailableException
- DsdAllocator : compteur DSD par site (weighbridge_dsd_counter) incrémenté
  sous verrou ligne SELECT ... FOR UPDATE (RG-5.04, § 2.7)
- endpoint POST /api/weighbridge_readings : ressource virtuelle
  WeighbridgeReadingResource + WeighbridgeReadingProcessor (pas de controller)
  - AUTO -> {weight, dsd, mode} ; MANUAL -> {weight, dsd, manualNumber, mode}
  - WeighbridgeUnavailableException -> HTTP 503 explicite (RG-5.06)
  - site courant via CurrentSiteProviderInterface (contrat Sites)
  - is_granted('logistique.weighing_tickets.manage')
- dsd renvoyé prévisionnel : attribution autoritaire refaite à la création
  du ticket (ERP-185)
- tests : WeighbridgeReaderStubTest, DsdAllocatorTest, processor (503/400),
  WeighbridgeReadingApiTest (RBAC + AUTO/MANUAL + 422)
This commit is contained in:
Matthieu
2026-06-17 18:09:54 +02:00
parent 312c119c06
commit e88bb059e6
13 changed files with 812 additions and 0 deletions
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Application\Service;
use App\Shared\Domain\Contract\SiteInterface;
/**
* Allocateur du compteur DSD du pont bascule (RG-5.04, § 2.7).
*
* Le DSD est un index sequentiel de pesee, propre a CHAQUE site (un pont par
* site). Chaque pesee — bascule (AUTO) ou manuelle (MANUAL) — consomme une
* valeur : la suivante = dernier DSD du site + 1.
*
* Port (interface en couche Application) ; l'implementation (DsdAllocator,
* Infrastructure) incremente le compteur sous verrou ligne `SELECT ... FOR
* UPDATE` pour garantir l'unicite en concurrence.
*/
interface DsdAllocatorInterface
{
/**
* Attribue et renvoie la prochaine valeur DSD pour le site (dernier + 1),
* en persistant l'increment de maniere atomique (verrou ligne).
*/
public function next(SiteInterface $site): int;
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Domain\Contract;
use App\Module\Logistique\Domain\Exception\WeighbridgeUnavailableException;
use App\Module\Logistique\Domain\Weighbridge\WeighbridgeReading;
use App\Shared\Domain\Contract\SiteInterface;
/**
* Contrat de lecture du pont bascule (§ 2.6).
*
* Abstraction posee au M5 pour decoupler l'API du materiel : l'implementation
* livree est un stub (RandomWeighbridgeReader, poids aleatoire ∈ [10000,50000]
* kg). Le driver materiel reel (protocole serie/TCP de l'indicateur de pesage)
* est hors perimetre M5 (HP-M5-02) : le jour venu on substitue l'implementation
* derriere cette interface — zero impact sur les ecrans / l'API.
*/
interface WeighbridgeReaderInterface
{
/**
* Effectue une pesee « bascule » (AUTO) pour le site donne : renvoie le poids
* lu et le DSD (index de pesee du pont) attribue pour ce site (RG-5.04).
*
* @throws WeighbridgeUnavailableException si la bascule ne repond pas
* (le Processor traduit en HTTP 503 →
* bascule manuelle, RG-5.06)
*/
public function read(SiteInterface $site): WeighbridgeReading;
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Domain\Exception;
use RuntimeException;
/**
* Levee lorsque le pont bascule ne repond pas / est indisponible (RG-5.06).
*
* Exception de DOMAINE (pure, sans dependance HTTP) : c'est le Processor de
* l'endpoint de pesee qui la traduit en reponse HTTP 503 « Pont bascule
* indisponible — passez en pesee manuelle » (cf. WeighbridgeReadingProcessor).
*
* Au M5, le stub (RandomWeighbridgeReader) ne la leve jamais, mais le chemin
* d'erreur est implemente et teste pour le jour ou un driver materiel reel
* (HP-M5-02) sera branche derriere WeighbridgeReaderInterface.
*/
final class WeighbridgeUnavailableException extends RuntimeException {}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Domain\Weighbridge;
/**
* Resultat immuable d'une lecture du pont bascule (§ 2.6 / RG-5.06).
*
* Porte le couple {poids, DSD} renvoye par une pesee « bascule » (AUTO) :
* - weight : poids brut lu, en kilogrammes ;
* - dsd : index de pesee du pont (compteur par site, RG-5.04).
*
* Au M5 le pont est un stub (RandomWeighbridgeReader) ; un driver materiel reel
* (HP-M5-02) produira le meme objet derriere WeighbridgeReaderInterface, sans
* impact sur l'API.
*/
final readonly class WeighbridgeReading
{
public function __construct(
public int $weight,
public int $dsd,
) {}
}
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Infrastructure\ApiPlatform\Resource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Module\Logistique\Infrastructure\ApiPlatform\State\Processor\WeighbridgeReadingProcessor;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* Ressource API Platform virtuelle (non mappee Doctrine) portant l'action de
* pesee au pont bascule : `POST /api/weighbridge_readings` (§ 4.2).
*
* Action AUTONOME : declenchee depuis le formulaire AVANT que le ticket existe.
* Le site est resolu serveur (site courant) — jamais envoye par le client.
*
* - AUTO (`{ "mode": "AUTO" }`) → `{ weight, dsd, mode }` (stub : poids
* aleatoire ∈ [10000,50000] kg + DSD du site, RG-5.04 / RG-5.06).
* - MANUAL (`{ "mode": "MANUAL", "weight": <int>, "manualNumber": "<str>" }`)
* → `{ weight, dsd, manualNumber, mode }` (DSD = dernier DSD du site + 1).
*
* `read: false` : pas de chargement d'entite existante — le payload est
* denormalise directement dans cette ressource, puis le Processor prend le relais.
*
* ⚠ Le `dsd` renvoye ici est PREVISIONNEL : l'attribution AUTORITAIRE du DSD
* (et du numero de ticket) est refaite/verrouillee a la creation du ticket
* (`POST /api/weighing_tickets`, ERP-185) pour eviter les collisions si deux
* postes pesent en parallele. Le front affiche cette valeur, mais c'est le
* ticket persiste qui fait foi.
*/
#[ApiResource(
shortName: 'WeighbridgeReading',
operations: [
new Post(
uriTemplate: '/weighbridge_readings',
// Action de lecture du pont (pas une creation de ressource) : 200, pas 201.
status: 200,
security: "is_granted('logistique.weighing_tickets.manage')",
normalizationContext: ['groups' => ['weighbridge_reading:read']],
denormalizationContext: ['groups' => ['weighbridge_reading:write']],
processor: WeighbridgeReadingProcessor::class,
read: false,
),
],
)]
final class WeighbridgeReadingResource
{
/** AUTO (pesee bascule) | MANUAL (pesee manuelle) — pilote le comportement (§ 4.2). */
#[Assert\NotBlank(message: 'Le mode de pesée est obligatoire.')]
#[Assert\Choice(choices: ['AUTO', 'MANUAL'], message: 'Mode de pesée invalide (AUTO ou MANUAL).')]
#[Groups(['weighbridge_reading:write', 'weighbridge_reading:read'])]
public ?string $mode = null;
/**
* Poids en kg. En entree : requis et saisi en MANUAL, ignore en AUTO (le pont
* fournit le poids). En sortie : poids effectif de la pesee.
*/
#[Assert\Positive(message: 'Le poids doit être un entier positif (kg).')]
#[Groups(['weighbridge_reading:write', 'weighbridge_reading:read'])]
public ?int $weight = null;
/** Numero de pesee papier saisi en MANUAL (distinct du DSD, RG-5.04). */
#[Assert\Length(max: 50, maxMessage: 'Le numéro de pesée ne peut pas dépasser {{ limit }} caractères.', normalizer: 'trim')]
#[Groups(['weighbridge_reading:write', 'weighbridge_reading:read'])]
public ?string $manualNumber = null;
/** DSD attribue par le serveur (lecture seule) — previsionnel (cf. docbloc classe). */
#[Groups(['weighbridge_reading:read'])]
public ?int $dsd = null;
/**
* RG metier : en pesee MANUAL, le poids est saisi par l'operateur (le pont
* n'est pas lu) → il est obligatoire. Porte par un Callback pour que le 422
* cible le propertyPath `weight` (mapping inline front, ERP-101). En AUTO,
* le poids fourni par le client est ignore (renseigne par le pont).
*/
#[Assert\Callback]
public function validateManualWeight(ExecutionContextInterface $context): void
{
if ('MANUAL' === $this->mode && null === $this->weight) {
$context->buildViolation('Le poids est obligatoire en pesée manuelle.')
->atPath('weight')
->addViolation()
;
}
}
}
@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Infrastructure\ApiPlatform\State\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Module\Logistique\Application\Service\DsdAllocatorInterface;
use App\Module\Logistique\Domain\Contract\WeighbridgeReaderInterface;
use App\Module\Logistique\Domain\Exception\WeighbridgeUnavailableException;
use App\Module\Logistique\Infrastructure\ApiPlatform\Resource\WeighbridgeReadingResource;
use App\Module\Sites\Application\Service\CurrentSiteProviderInterface;
use LogicException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* Processor de l'action `POST /api/weighbridge_readings` (§ 4.2).
*
* Resout le site courant (CurrentSiteProviderInterface — contrat Sites, seule
* logique cross-module autorisee, regle ABSOLUE n°1) puis :
* - AUTO : lit le pont (WeighbridgeReaderInterface) → poids + DSD. Si la
* bascule est indisponible (WeighbridgeUnavailableException) → HTTP 503
* « Pont bascule indisponible — passez en pesee manuelle » (RG-5.06).
* - MANUAL : conserve le poids saisi et alloue le DSD (dernier + 1, RG-5.04).
*
* @implements ProcessorInterface<WeighbridgeReadingResource, WeighbridgeReadingResource>
*/
final class WeighbridgeReadingProcessor implements ProcessorInterface
{
public function __construct(
private readonly CurrentSiteProviderInterface $currentSiteProvider,
private readonly WeighbridgeReaderInterface $weighbridgeReader,
private readonly DsdAllocatorInterface $dsdAllocator,
) {}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): WeighbridgeReadingResource
{
if (!$data instanceof WeighbridgeReadingResource) {
throw new LogicException(sprintf(
'WeighbridgeReadingProcessor attend une instance de %s, %s recu.',
WeighbridgeReadingResource::class,
get_debug_type($data),
));
}
// Site courant resolu serveur (jamais envoye par le client). Absent =
// aucun site selectionne dans le sélecteur → on ne peut pas peser.
$site = $this->currentSiteProvider->get();
if (null === $site) {
throw new BadRequestHttpException('Aucun site courant sélectionné — sélectionnez un site avant de peser.');
}
if ('AUTO' === $data->mode) {
try {
$reading = $this->weighbridgeReader->read($site);
} catch (WeighbridgeUnavailableException $e) {
// RG-5.06 : le pont ne repond pas → 503 explicite, le front bascule
// en pesee manuelle. (Le stub M5 ne leve jamais — chemin teste.)
throw new ServiceUnavailableHttpException(
null,
'Pont bascule indisponible — passez en pesée manuelle.',
$e,
);
}
$data->weight = $reading->weight;
$data->dsd = $reading->dsd;
$data->manualNumber = null; // pas de numero papier en mode bascule
return $data;
}
// MANUAL : le poids est saisi (validateManualWeight garantit sa presence),
// seul le DSD est attribue serveur (dernier DSD du site + 1, RG-5.04).
$data->dsd = $this->dsdAllocator->next($site);
return $data;
}
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Infrastructure\Service;
use App\Module\Logistique\Application\Service\DsdAllocatorInterface;
use App\Shared\Domain\Contract\SiteInterface;
use Doctrine\DBAL\Connection;
use LogicException;
/**
* Implementation DBAL de l'allocateur DSD (RG-5.04, § 2.7).
*
* Le compteur vit dans la table `weighbridge_dsd_counter (site_id PK,
* last_value)` — jamais mappee en ORM (DBAL brut, exclue du schema_filter).
* L'increment est realise dans une transaction avec verrou ligne
* `SELECT ... FOR UPDATE` : deux postes pesant en parallele sur le meme site
* sont serialises, ce qui garantit des DSD distincts (pas de collision).
*
* AUTO comme MANUAL passent par le meme increment (« dernier DSD du site + 1 ») :
* la seule difference fonctionnelle est l'origine du poids (lu par le pont en
* AUTO, saisi en MANUAL), pas la sequence DSD.
*
* La ligne compteur n'est pas seedee a la creation du site : on la cree a la
* volee (INSERT ... ON CONFLICT DO NOTHING) avant de prendre le verrou.
*/
final class DsdAllocator implements DsdAllocatorInterface
{
public function __construct(private readonly Connection $connection) {}
public function next(SiteInterface $site): int
{
$siteId = $site->getId();
if (null === $siteId) {
// Garde defensive : un site non persiste n'a pas de compteur (et la FK
// weighbridge_dsd_counter.site_id -> site(id) rejetterait l'INSERT).
throw new LogicException('Impossible d\'allouer un DSD pour un site non persiste (id null).');
}
return $this->connection->transactional(function (Connection $conn) use ($siteId): int {
// Garantit l'existence de la ligne compteur du site sans ecraser une
// valeur deja presente (idempotent, concurrence-safe).
$conn->executeStatement(
'INSERT INTO weighbridge_dsd_counter (site_id, last_value) VALUES (:site, 0) ON CONFLICT (site_id) DO NOTHING',
['site' => $siteId],
);
// Verrou ligne : serialise les pesees concurrentes du meme site.
$current = (int) $conn->fetchOne(
'SELECT last_value FROM weighbridge_dsd_counter WHERE site_id = :site FOR UPDATE',
['site' => $siteId],
);
$next = $current + 1;
$conn->executeStatement(
'UPDATE weighbridge_dsd_counter SET last_value = :value WHERE site_id = :site',
['value' => $next, 'site' => $siteId],
);
return $next;
});
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Module\Logistique\Infrastructure\Weighbridge;
use App\Module\Logistique\Application\Service\DsdAllocatorInterface;
use App\Module\Logistique\Domain\Contract\WeighbridgeReaderInterface;
use App\Module\Logistique\Domain\Weighbridge\WeighbridgeReading;
use App\Shared\Domain\Contract\SiteInterface;
/**
* Stub du pont bascule livre au M5 (DECISION Matthieu 17/06, § 2.6 / RG-5.06).
*
* Aucune liaison materielle : la pesee « bascule » est simulee par un poids
* aleatoire ∈ [10000, 50000] kg, et le DSD est attribue par l'allocateur de
* site (DsdAllocator, RG-5.04). Le driver materiel reel (HP-M5-02) remplacera
* cette classe derriere WeighbridgeReaderInterface sans impact sur l'API.
*
* Ce stub ne leve jamais WeighbridgeUnavailableException ; le chemin d'erreur
* (→ 503) reste implemente et teste cote Processor.
*/
final class RandomWeighbridgeReader implements WeighbridgeReaderInterface
{
public function __construct(private readonly DsdAllocatorInterface $dsdAllocator) {}
public function read(SiteInterface $site): WeighbridgeReading
{
return new WeighbridgeReading(
weight: random_int(10000, 50000),
dsd: $this->dsdAllocator->next($site),
);
}
}