feat(commercial) : sous-ressources M2 fournisseurs (contacts/adresses/ribs) (ERP-88) (#67)
Auto Tag Develop / tag (push) Successful in 7s
Auto Tag Develop / tag (push) Successful in 7s
## ERP-88 — Sous-ressources M2 (contacts / adresses / ribs) Étape 4/7 du pipeline M2. Dépend de #86 (entités) et #87 (Provider/Processor). Bloque #92. ### Contenu Opérations API Platform + Processors d'écriture des sous-collections du fournisseur (POST/PATCH/DELETE + GET unitaire). **SupplierContactProcessor** - Rattachement au fournisseur parent (404 si absent). - Normalisation serveur RG-2.12 (Title Case nom/prénom, téléphones chiffres seuls, email lowercase). - RG-2.04 : firstName **ou** lastName obligatoire (422 sur `firstName`). - DELETE libre (RG-2.13 front-driven : collection peut rester vide côté back). **SupplierAddressProcessor** - Rattachement au fournisseur parent. - RG-2.05 (CP `^[0-9]{4,5}$`), RG-2.06 (≥1 site), RG-2.09 (type d'adresse) portées par les contraintes d'entité (ERP-86). - RG-2.10 (catégorie de type FOURNISSEUR) ajoutée via `Assert\Callback validateCategoryType` (propertyPath=`categories`). **SupplierRibProcessor** - Rattachement au fournisseur parent. - RG-2.08 : refus du DELETE du dernier RIB quand `paymentType.code = LCR` → **409**. ### Security différenciée | Sous-ressource | Écriture | Lecture | |---|---|---| | contacts / adresses | `commercial.suppliers.manage` | `commercial.suppliers.view` | | ribs | `commercial.suppliers.accounting.manage` | `commercial.suppliers.accounting.view` | POST en `read:false` (parent rattaché manuellement) — parade NonUniqueResult héritée du M1. Messages FR (ERP-107) + `violations[].propertyPath` aligné (ERP-101). ### Vérifications - `make php-cs-fixer-allow-risky` : 0 fichier à corriger - `make test` : 483 tests OK - `debug:router` : 12 routes générées (4 par sous-ressource) ### Hors périmètre (tickets suivants) - Déclaration RBAC `commercial.suppliers.*` dans `CommercialModule` (#7) — sans elle, l'accès reste 403. - Tests fonctionnels de la matrice RG (#8) — dépendent du RBAC + fixtures Supplier. ### Notes de review (non bloquantes, alignées M1) - `position` des sous-collections non exposé à l'API (décision ERP-86, géré serveur). - M2M `SupplierAddress.contacts` non vérifié same-supplier — comportement identique au M1 (ClientAddress), à traiter globalement si besoin. --------- Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #67 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
This commit was merged in pull request #67.
This commit is contained in:
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\DeleteOperationInterface;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
||||
use App\Module\Commercial\Domain\Entity\SupplierAddress;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Processor d'ecriture de la sous-ressource Adresse d'un fournisseur (M2,
|
||||
* spec-back § 4.5). Jumeau du ClientAddressProcessor (M1), recentre sur le
|
||||
* perimetre ERP-88.
|
||||
*
|
||||
* Sequence :
|
||||
* - POST / PATCH : rattachement au fournisseur parent. Aucune normalisation
|
||||
* specifique (pas d'email de facturation au M2). Les regles de l'onglet
|
||||
* Adresse sont garanties en amont par des contraintes sur l'entite, jouees
|
||||
* par API Platform avant ce processor : RG-2.05 (code postal, Assert\Regex),
|
||||
* RG-2.06 (>= 1 site, Assert\Count), RG-2.09 (type d'adresse, Assert\Choice +
|
||||
* CHECK BDD), RG-2.10 (categorie de type FOURNISSEUR, Assert\Callback
|
||||
* SupplierAddress::validateCategoryType).
|
||||
* - DELETE : aucune regle metier specifique (suppression physique directe).
|
||||
*
|
||||
* La security de l'operation (commercial.suppliers.manage) est appliquee par API
|
||||
* Platform en amont, de meme que la validation Symfony des contraintes d'attribut.
|
||||
*
|
||||
* @implements ProcessorInterface<SupplierAddress, null|SupplierAddress>
|
||||
*/
|
||||
final class SupplierAddressProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private readonly ProcessorInterface $persistProcessor,
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
|
||||
private readonly ProcessorInterface $removeProcessor,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||
{
|
||||
if (!$data instanceof SupplierAddress) {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
if ($operation instanceof DeleteOperationInterface) {
|
||||
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$this->linkParent($data, $uriVariables);
|
||||
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rattache l'adresse au fournisseur parent de la sous-ressource POST
|
||||
* (/suppliers/{supplierId}/addresses) : la relation n'est pas peuplee
|
||||
* automatiquement par le Link sur une ecriture. Sur PATCH, no-op.
|
||||
*/
|
||||
private function linkParent(SupplierAddress $address, array $uriVariables): void
|
||||
{
|
||||
if (null !== $address->getSupplier()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$supplierId = $uriVariables['supplierId'] ?? null;
|
||||
if (null === $supplierId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$supplier = $supplierId instanceof Supplier
|
||||
? $supplierId
|
||||
: $this->em->getRepository(Supplier::class)->find($supplierId);
|
||||
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||
// contrainte supplier_id NOT NULL).
|
||||
if (!$supplier instanceof Supplier) {
|
||||
throw new NotFoundHttpException('Fournisseur introuvable.');
|
||||
}
|
||||
|
||||
$address->setSupplier($supplier);
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\DeleteOperationInterface;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use ApiPlatform\Validator\Exception\ValidationException;
|
||||
use App\Module\Commercial\Application\Service\SupplierFieldNormalizer;
|
||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
||||
use App\Module\Commercial\Domain\Entity\SupplierContact;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
/**
|
||||
* Processor d'ecriture de la sous-ressource Contact d'un fournisseur (M2,
|
||||
* spec-back § 4.5). Jumeau du ClientContactProcessor (M1), recentre sur le
|
||||
* perimetre ERP-88.
|
||||
*
|
||||
* Sequence :
|
||||
* - POST / PATCH : rattachement au fournisseur parent, normalisation serveur
|
||||
* (RG-2.12 : prenom/nom Title Case, telephones reduits aux chiffres, email
|
||||
* lowercase) via le SupplierFieldNormalizer partage, puis validation RG-2.04
|
||||
* (au moins prenom OU nom) avant persistance.
|
||||
* - DELETE : aucune garde « dernier contact » au M2 — contrairement au M1, la
|
||||
* collection peut rester vide cote back (RG-2.13 front-driven, spec § 4.5).
|
||||
* Suppression physique directe.
|
||||
*
|
||||
* La security de l'operation (commercial.suppliers.manage) est appliquee par API
|
||||
* Platform en amont, de meme que la validation Symfony des contraintes d'attribut
|
||||
* (Assert\Email, Assert\Length...).
|
||||
*
|
||||
* @implements ProcessorInterface<SupplierContact, null|SupplierContact>
|
||||
*/
|
||||
final class SupplierContactProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private readonly ProcessorInterface $persistProcessor,
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
|
||||
private readonly ProcessorInterface $removeProcessor,
|
||||
private readonly SupplierFieldNormalizer $normalizer,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||
{
|
||||
if (!$data instanceof SupplierContact) {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
if ($operation instanceof DeleteOperationInterface) {
|
||||
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$this->linkParent($data, $uriVariables);
|
||||
$this->normalize($data);
|
||||
$this->validateName($data);
|
||||
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rattache le contact au fournisseur parent de la sous-ressource POST
|
||||
* (/suppliers/{supplierId}/contacts). La relation n'est pas peuplee
|
||||
* automatiquement par le Link sur une operation d'ecriture : on resout le
|
||||
* parent depuis l'uri variable. Sur PATCH (entite existante), le fournisseur
|
||||
* est deja present -> no-op.
|
||||
*/
|
||||
private function linkParent(SupplierContact $contact, array $uriVariables): void
|
||||
{
|
||||
if (null !== $contact->getSupplier()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$supplierId = $uriVariables['supplierId'] ?? null;
|
||||
if (null === $supplierId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$supplier = $supplierId instanceof Supplier
|
||||
? $supplierId
|
||||
: $this->em->getRepository(Supplier::class)->find($supplierId);
|
||||
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||
// contrainte supplier_id NOT NULL).
|
||||
if (!$supplier instanceof Supplier) {
|
||||
throw new NotFoundHttpException('Fournisseur introuvable.');
|
||||
}
|
||||
|
||||
$contact->setSupplier($supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalisation serveur (RG-2.12). Toutes les methodes du normalizer sont
|
||||
* null-safe : une chaine vide apres trim devient null.
|
||||
*/
|
||||
private function normalize(SupplierContact $contact): void
|
||||
{
|
||||
$contact->setFirstName($this->normalizer->normalizePersonName($contact->getFirstName()));
|
||||
$contact->setLastName($this->normalizer->normalizePersonName($contact->getLastName()));
|
||||
$contact->setPhonePrimary($this->normalizer->normalizePhone($contact->getPhonePrimary()));
|
||||
$contact->setPhoneSecondary($this->normalizer->normalizePhone($contact->getPhoneSecondary()));
|
||||
$contact->setEmail($this->normalizer->normalizeEmail($contact->getEmail()));
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-2.04 : au moins le prenom OU le nom est obligatoire (double garde avec le
|
||||
* CHECK BDD chk_supplier_contact_name — leve une 422 propre rattachee au champ
|
||||
* `firstName` plutot qu'une 500 SQL). Joue apres normalisation, donc les
|
||||
* chaines vides sont deja ramenees a null.
|
||||
*/
|
||||
private function validateName(SupplierContact $contact): void
|
||||
{
|
||||
if (null === $contact->getFirstName() && null === $contact->getLastName()) {
|
||||
$violations = new ConstraintViolationList();
|
||||
$violations->add(new ConstraintViolation(
|
||||
'Le prénom ou le nom du contact est obligatoire.',
|
||||
null,
|
||||
[],
|
||||
$contact,
|
||||
'firstName',
|
||||
null,
|
||||
));
|
||||
|
||||
throw new ValidationException($violations);
|
||||
}
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\DeleteOperationInterface;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Module\Commercial\Domain\Entity\Supplier;
|
||||
use App\Module\Commercial\Domain\Entity\SupplierRib;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Processor d'ecriture de la sous-ressource RIB d'un fournisseur (M2, spec-back
|
||||
* § 4.5). Jumeau du ClientRibProcessor (M1), recentre sur le perimetre ERP-88.
|
||||
*
|
||||
* Sequence :
|
||||
* - POST / PATCH : rattachement au fournisseur parent. Aucune normalisation
|
||||
* specifique ; la validite de l'IBAN et du BIC est garantie par Assert\Iban /
|
||||
* Assert\Bic sur l'entite (jouees en amont par API Platform). Aucun
|
||||
* #[AuditIgnore] sur iban/bic : la tracabilite comptable est volontaire
|
||||
* (decision M1 reportee, spec § 2.7).
|
||||
* - DELETE : RG-2.08 — si le fournisseur est en reglement LCR, la suppression de
|
||||
* son DERNIER RIB est refusee (409), car LCR exige au moins un RIB.
|
||||
*
|
||||
* La security de l'operation (commercial.suppliers.accounting.manage) est
|
||||
* appliquee par API Platform en amont : un utilisateur sans cette permission
|
||||
* recoit 403 sur POST/PATCH/DELETE avant d'atteindre ce processor — c'est le
|
||||
* niveau de gating renforce des donnees bancaires (distinct de manage, spec
|
||||
* § 4.5).
|
||||
*
|
||||
* @implements ProcessorInterface<SupplierRib, null|SupplierRib>
|
||||
*/
|
||||
final class SupplierRibProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private readonly ProcessorInterface $persistProcessor,
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
|
||||
private readonly ProcessorInterface $removeProcessor,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||
{
|
||||
if (!$data instanceof SupplierRib) {
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
if ($operation instanceof DeleteOperationInterface) {
|
||||
$this->guardLastRibDeletionUnderLcr($data);
|
||||
|
||||
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$this->linkParent($data, $uriVariables);
|
||||
|
||||
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rattache le RIB au fournisseur parent de la sous-ressource POST
|
||||
* (/suppliers/{supplierId}/ribs) : la relation n'est pas peuplee
|
||||
* automatiquement par le Link sur une ecriture. Sur PATCH, no-op.
|
||||
*/
|
||||
private function linkParent(SupplierRib $rib, array $uriVariables): void
|
||||
{
|
||||
if (null !== $rib->getSupplier()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$supplierId = $uriVariables['supplierId'] ?? null;
|
||||
if (null === $supplierId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$supplier = $supplierId instanceof Supplier
|
||||
? $supplierId
|
||||
: $this->em->getRepository(Supplier::class)->find($supplierId);
|
||||
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||
// contrainte supplier_id NOT NULL).
|
||||
if (!$supplier instanceof Supplier) {
|
||||
throw new NotFoundHttpException('Fournisseur introuvable.');
|
||||
}
|
||||
|
||||
$rib->setSupplier($supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-2.08 : un fournisseur dont le type de reglement est LCR doit conserver au
|
||||
* moins un RIB. La collection inclut le RIB en cours de suppression : un
|
||||
* effectif <= 1 signifie qu'il ne resterait aucun RIB -> 409. Pour tout autre
|
||||
* type de reglement, les RIBs sont optionnels (suppression libre).
|
||||
*/
|
||||
private function guardLastRibDeletionUnderLcr(SupplierRib $rib): void
|
||||
{
|
||||
$supplier = $rib->getSupplier();
|
||||
if (null === $supplier) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ('LCR' === $supplier->getPaymentType()?->getCode() && $supplier->getRibs()->count() <= 1) {
|
||||
throw new ConflictHttpException(
|
||||
'Impossible de supprimer le dernier RIB : le type de règlement LCR exige au moins un RIB.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user