Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 786638a02f | |||
| fcacde2a34 | |||
| fea325e10f | |||
| e139d234a9 | |||
| c437bc52a2 | |||
| 597101262d |
+19
-131
@@ -6,6 +6,13 @@
|
|||||||
- PHP CS Fixer : regles Symfony + PSR-12 + strict types (commande : `make php-cs-fixer-allow-risky`)
|
- PHP CS Fixer : regles Symfony + PSR-12 + strict types (commande : `make php-cs-fixer-allow-risky`)
|
||||||
- Commentaires (docblock, inline, bloc) **en francais** ; code (classes, methodes, variables) en anglais
|
- Commentaires (docblock, inline, bloc) **en francais** ; code (classes, methodes, variables) en anglais
|
||||||
|
|
||||||
|
## Messages de validation (obligatoire)
|
||||||
|
|
||||||
|
Toute contrainte `#[Assert\*]` d'une entite metier : **message FR explicite**, et `Assert\Length.max` = `length` de la colonne ORM (coherence 3 niveaux nullable DB <-> NotBlank back <-> required front, ERP-101). RG inter-champs via `#[Assert\Callback]->atPath('<champ>')` (mapping inline front, pas toast). Exceptions miroir Length (Bic/Iban/Regex borne) : whitelist `EntityConstraintsHaveFrenchMessageTest::EXCLUDED_LENGTH_MIRROR`.
|
||||||
|
|
||||||
|
Garde-fou : `tests/Architecture/EntityConstraintsHaveFrenchMessageTest` (casse `make test`).
|
||||||
|
→ patterns code + exemples + justification complete : skill `backend-entity-conventions`.
|
||||||
|
|
||||||
## API Platform (pas de controllers)
|
## API Platform (pas de controllers)
|
||||||
|
|
||||||
- Toujours utiliser `#[ApiResource]` + Providers + Processors — pas de controllers Symfony classiques
|
- Toujours utiliser `#[ApiResource]` + Providers + Processors — pas de controllers Symfony classiques
|
||||||
@@ -15,61 +22,10 @@
|
|||||||
|
|
||||||
## Pagination (obligatoire)
|
## Pagination (obligatoire)
|
||||||
|
|
||||||
**Regle** : toute collection API DOIT etre paginee. Aucun retour de collection complete cote serveur.
|
Toute collection API est paginee (defaut 10, max 50 ; `?pagination=false` = echappatoire selects, `?itemsPerPage=25` borne par le max). Standard global dans `config/packages/api_platform.yaml`. Jamais `paginationEnabled: false` hors whitelist `CollectionsArePaginatedTest::EXCLUDED`. Provider custom : ne jamais retourner un `array` brut sur une `CollectionOperationInterface` (court-circuite Hydra) — wrapper un Paginator (ORM : `ApiPlatform\Doctrine\Orm\Paginator` ; DBAL : `DbalPaginator`) et gerer `?pagination=false` via `$this->pagination->isEnabled(...)`.
|
||||||
|
|
||||||
### Standard global
|
Garde-fou : `tests/Architecture/CollectionsArePaginatedTest` (casse `make test`).
|
||||||
|
→ tableau des cles `pagination_*` + selects + providers ORM/DBAL detailles : skill `backend-entity-conventions`.
|
||||||
Pose dans `config/packages/api_platform.yaml` (section `defaults:`) et heritee par toutes les ressources :
|
|
||||||
|
|
||||||
| Cle | Valeur | Effet |
|
|
||||||
|---|---|---|
|
|
||||||
| `pagination_enabled` | `true` | Pagination Hydra active par defaut. |
|
|
||||||
| `pagination_items_per_page` | `10` | Taille de page par defaut, aligne sur l'UI `MalioDataTable`. |
|
|
||||||
| `pagination_maximum_items_per_page` | `50` | Borne dure : `?itemsPerPage=999` → ramene a 50. Anti deep-fetch. |
|
|
||||||
| `pagination_client_items_per_page` | `true` | Le client peut envoyer `?itemsPerPage=25` (bornee par le max). |
|
|
||||||
| `pagination_client_enabled` | `true` | Le client peut envoyer `?pagination=false` pour TOUT recuperer (echappatoire selects). |
|
|
||||||
|
|
||||||
### Override par ressource (rare)
|
|
||||||
|
|
||||||
Si une ressource a besoin d'un autre defaut (ex: payload lourd), utiliser les attributs sur l'operation. **JAMAIS `paginationEnabled: false`** sans whitelist explicite dans `tests/Architecture/CollectionsArePaginatedTest::EXCLUDED`.
|
|
||||||
|
|
||||||
```php
|
|
||||||
new GetCollection(
|
|
||||||
paginationItemsPerPage: 5, // override taille par defaut
|
|
||||||
paginationMaximumItemsPerPage: 20, // override borne max
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Selects et autocompletions
|
|
||||||
|
|
||||||
Pour alimenter un `<select>` ou un drawer RBAC (Role, Permission, Site, CategoryType), le front passe :
|
|
||||||
|
|
||||||
```ts
|
|
||||||
useApi().get('/api/roles?pagination=false')
|
|
||||||
```
|
|
||||||
|
|
||||||
Le serveur retourne toute la collection, sans `view`. C'est l'echappatoire prevue par `pagination_client_enabled: true`. Sur les ressources a forte volumetrie, preferer une saisie assistee (recherche serveur via `?q=`) — a planifier dans un ticket dedie.
|
|
||||||
|
|
||||||
Les tests fonctionnels qui exercent ce comportement doivent egalement passer `?pagination=false` (cf. `CategoryListTest`, `PermissionApiTest`).
|
|
||||||
|
|
||||||
### Providers customs et pagination
|
|
||||||
|
|
||||||
Un provider custom qui retourne un `array` brut sur une `CollectionOperationInterface` **court-circuite la pagination Hydra** (pas de `totalItems`, pas de `view`). Patterns supportes :
|
|
||||||
|
|
||||||
- **ORM** : injecter `ApiPlatform\State\Pagination\Pagination`, wrap un `Doctrine\ORM\Tools\Pagination\Paginator` dans `ApiPlatform\Doctrine\Orm\Paginator`. Exemple : `CategoryProvider`.
|
|
||||||
- **DBAL** : implementer un paginator local conforme a `PaginatorInterface`. Exemple : `DbalPaginator` (Core) + `AuditLogProvider`.
|
|
||||||
|
|
||||||
Gerer l'echappatoire `?pagination=false` :
|
|
||||||
|
|
||||||
```php
|
|
||||||
if (!$this->pagination->isEnabled($operation, $context)) {
|
|
||||||
return $qb->getQuery()->getResult(); // tout retourner
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Garde-fou architecture
|
|
||||||
|
|
||||||
`tests/Architecture/CollectionsArePaginatedTest` scanne reflexivement toutes les classes `#[ApiResource]` sous `src/` et echoue si une `GetCollection` pose `paginationEnabled: false` hors whitelist `EXCLUDED`. Ajouter une entree a la whitelist requiert une justification courte + un ticket Lesstime ouvert.
|
|
||||||
|
|
||||||
## Repositories
|
## Repositories
|
||||||
|
|
||||||
@@ -100,42 +56,17 @@ Format obligatoire : `module.resource[.subresource].action` en snake_case.
|
|||||||
|
|
||||||
### Libelle i18n du type d'entite (obligatoire avec `#[Auditable]`)
|
### Libelle i18n du type d'entite (obligatoire avec `#[Auditable]`)
|
||||||
|
|
||||||
**Toute entite `#[Auditable]` doit avoir son libelle FR dans le bloc `audit.entity` de `frontend/i18n/locales/fr.json`.** C'est la contrepartie i18n de l'attribut : sans elle, le filtre « Type d'entite » de l'audit-log affiche le type technique brut (ex: `commercial.Client`) au lieu d'un libelle lisible.
|
Toute entite `#[Auditable]` doit avoir sa cle `audit.entity.<module>_<entity>` dans `frontend/i18n/locales/fr.json` (cle = `strtolower(module)` + `_` + `strtolower(Entity)`, decision ERP-99). Sans elle, le filtre « Type d'entite » de l'audit-log retombe silencieusement sur le type technique brut (ex: `commercial.Client`). Fait partie de la definition de fini d'une entite auditee.
|
||||||
|
|
||||||
Pourquoi : le filtre est dynamique (`GET /audit-log-entity-types` renvoie les `entity_type` distincts presents en base) ; des qu'un module audite une entite, son type y apparait. Le front (`formatEntityType`, `audit-log.vue`) construit la cle `audit.entity.<module>_<entity>` et, faute de traduction, **retombe silencieusement** sur le type brut.
|
Garde-fou : `tests/Architecture/AuditableEntitiesHaveI18nLabelTest` (casse `make test`).
|
||||||
|
→ derivation detaillee + exemples : skill `backend-entity-conventions`.
|
||||||
Derivation de la cle (emplacement centralise + schema flat — decision ERP-99) :
|
|
||||||
|
|
||||||
| FQCN entite | `entity_type` (back) | Cle i18n (flat) |
|
|
||||||
|---|---|---|
|
|
||||||
| `App\Module\Commercial\Domain\Entity\Client` | `commercial.Client` | `commercial_client` |
|
|
||||||
| `App\Module\Commercial\Domain\Entity\ClientAddress` | `commercial.ClientAddress` | `commercial_clientaddress` |
|
|
||||||
| `App\Module\Catalog\Domain\Entity\Category` | `catalog.Category` | `catalog_category` |
|
|
||||||
|
|
||||||
Regle : `strtolower(module)` + `_` + `strtolower(Entity)`. Ajouter sa cle de libelle audit fait partie de la **definition de fini** d'une entite metier auditee.
|
|
||||||
|
|
||||||
**Garde-fou** : `tests/Architecture/AuditableEntitiesHaveI18nLabelTest` scanne les entites `#[Auditable]` et echoue si une seule n'a pas sa cle `audit.entity.*`. Conclusion : creer une entite `#[Auditable]` sans son libelle i18n casse `make test`.
|
|
||||||
|
|
||||||
## Timestampable + Blamable (obligatoire pour entites metier)
|
## Timestampable + Blamable (obligatoire pour entites metier)
|
||||||
|
|
||||||
Toute **nouvelle** entite metier sous `src/Module/*/Domain/Entity/` doit porter les 4 colonnes `created_at` / `updated_at` / `created_by` / `updated_by`, remplies automatiquement. Trois lignes a ajouter a l'entite :
|
Toute **nouvelle** entite metier sous `src/Module/*/Domain/Entity/` : `implements TimestampableInterface, BlamableInterface` + `use TimestampableBlamableTrait` (porte les 4 colonnes `created_at` / `updated_at` / `created_by` / `updated_by`, remplies par `TimestampableBlamableSubscriber` au prePersist/preUpdate). La migration cree les 4 colonnes (`created_at`/`updated_at` NOT NULL, `created_by`/`updated_by` nullable `ON DELETE SET NULL`). Referentiel statique justifie : whitelist `EntitiesAreTimestampableBlamableTest::EXCLUDED`.
|
||||||
|
|
||||||
```php
|
Garde-fou : `tests/Architecture/EntitiesAreTimestampableBlamableTest` (casse `make test`).
|
||||||
use App\Shared\Domain\Contract\BlamableInterface;
|
→ snippet complet : skill `backend-entity-conventions` ; spec : @docs/specs/M0-categories/spec-back.md § 2.8 + § 2.8.bis.
|
||||||
use App\Shared\Domain\Contract\TimestampableInterface;
|
|
||||||
use App\Shared\Domain\Trait\TimestampableBlamableTrait;
|
|
||||||
|
|
||||||
class MyEntity implements TimestampableInterface, BlamableInterface
|
|
||||||
{
|
|
||||||
use TimestampableBlamableTrait; // porte les 4 props + getters/setters
|
|
||||||
// ... reste metier
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
- Le `TimestampableBlamableSubscriber` (`Shared/Infrastructure/Doctrine/`) remplit les colonnes au `prePersist` / `preUpdate`. Hors contexte HTTP (CLI, cron, migration), le blame reste `null` (libelle « Systeme » cote front).
|
|
||||||
- La migration de l'entite doit creer les 4 colonnes (`created_at` / `updated_at` NOT NULL, `created_by` / `updated_by` nullable `ON DELETE SET NULL`).
|
|
||||||
- **Garde-fou CI** : `tests/Architecture/EntitiesAreTimestampableBlamableTest` echoue si une entite oublie le pattern. Un referentiel statique justifie (ex: `CategoryType`) doit etre explicitement whiteliste dans la constante `EXCLUDED` avec un commentaire.
|
|
||||||
- Spec complete : @docs/specs/M0-categories/spec-back.md § 2.8 + § 2.8.bis
|
|
||||||
|
|
||||||
## Serialization
|
## Serialization
|
||||||
|
|
||||||
@@ -153,50 +84,7 @@ Exemple : pour qu'`User.profile` soit embarque au lieu d'un lien IRI sous le gro
|
|||||||
|
|
||||||
## Migrations Doctrine
|
## Migrations Doctrine
|
||||||
|
|
||||||
### Documentation SQL obligatoire (`COMMENT ON COLUMN`)
|
Toute migration creant/modifiant une colonne d'une table metier pose un `COMMENT ON COLUMN` (FR, ≤ 200 caracteres, semantique + contrainte/RG, cible pour les FK). Les 4 colonnes Timestampable/Blamable recoivent leur description via le helper centralise `addStandardTimestampableBlamableComments($schema, 'table')`. Bonus : `COMMENT ON TABLE` pour decrire la table.
|
||||||
|
|
||||||
**Toute migration qui cree ou modifie une colonne d'une table metier doit poser un `COMMENT ON COLUMN` decrivant le champ.** La description est stockee dans `pg_description` et visible dans tous les outils d'admin BDD (DBeaver, DataGrip, pgAdmin), sans avoir a lire les annotations PHP.
|
Garde-fou : `tests/Architecture/ColumnsHaveSqlCommentTest` parcourt `information_schema.columns` (schema `public`) ; une seule colonne sans `col_description` casse `make test` (hors `EXCLUDED_TABLES`).
|
||||||
|
→ exemples SQL + textes du helper : skill `backend-entity-conventions`.
|
||||||
**Format de la description** :
|
|
||||||
- En francais
|
|
||||||
- ≤ 200 caracteres
|
|
||||||
- Semantique du champ — contraintes / lien RG si pertinent
|
|
||||||
- Pour les colonnes d'identifiant ou FK, mentionner la cible
|
|
||||||
|
|
||||||
Exemples :
|
|
||||||
|
|
||||||
```php
|
|
||||||
// Migration : creation d'une colonne avec son commentaire dans la meme migration
|
|
||||||
$this->addSql("ALTER TABLE client ADD COLUMN siren VARCHAR(9) DEFAULT NULL");
|
|
||||||
$this->addSql("COMMENT ON COLUMN client.siren IS 'SIREN (9 chiffres) — identifiant legal entreprise. Unique parmi non-archives (RG-1.15).'");
|
|
||||||
|
|
||||||
// Cas FK : preciser la cible
|
|
||||||
$this->addSql("COMMENT ON COLUMN client.legal_form_id IS 'Reference forme juridique (SARL, SAS, SA...) — FK -> legal_form.id, ON DELETE RESTRICT.'");
|
|
||||||
|
|
||||||
// Cas booleen : preciser le sens et la valeur par defaut
|
|
||||||
$this->addSql("COMMENT ON COLUMN user.is_admin IS 'Drapeau super-administrateur — bypass complet RBAC. Faux par defaut.'");
|
|
||||||
|
|
||||||
// Bonus : decrire la table elle-meme
|
|
||||||
$this->addSql("COMMENT ON TABLE client IS 'Repertoire clients (M1 Commercial) — entites archivables.'");
|
|
||||||
```
|
|
||||||
|
|
||||||
### Helper Timestampable/Blamable
|
|
||||||
|
|
||||||
Les 4 colonnes `created_at`, `updated_at`, `created_by`, `updated_by` ajoutees par `TimestampableBlamableTrait` recoivent une description **standardisee** via le helper centralise pour eviter la duplication. Helper a creer ou appeler :
|
|
||||||
|
|
||||||
```php
|
|
||||||
// Dans la migration, apres avoir ajoute les 4 colonnes :
|
|
||||||
$this->addStandardTimestampableBlamableComments($schema, 'client');
|
|
||||||
```
|
|
||||||
|
|
||||||
L'implementation du helper applique :
|
|
||||||
- `created_at` : « Horodatage de creation de la ligne (UTC, rempli automatiquement par TimestampableBlamableSubscriber). »
|
|
||||||
- `updated_at` : « Horodatage de derniere modification de la ligne (UTC, rempli automatiquement par TimestampableBlamableSubscriber). »
|
|
||||||
- `created_by` : « ID de l'utilisateur ayant cree la ligne — null pour les creations hors HTTP (CLI, migration, fixture). FK -> user.id, ON DELETE SET NULL. »
|
|
||||||
- `updated_by` : « ID de l'utilisateur ayant modifie la ligne en dernier — null pour les modifications hors HTTP. FK -> user.id, ON DELETE SET NULL. »
|
|
||||||
|
|
||||||
### Garde-fou architecture
|
|
||||||
|
|
||||||
`tests/Architecture/ColumnsHaveSqlCommentTest` parcourt `information_schema.columns` filtre sur le schema `public` et echoue si **une seule colonne** n'a pas de `col_description`. Seules les tables system (`doctrine_migration_versions`) et la whitelist `EXCLUDED_TABLES` explicite (commentaire de justification + ticket Lesstime ouvert pour le retrofit) sont tolerees.
|
|
||||||
|
|
||||||
Conclusion : si tu crees une colonne sans poser son `COMMENT ON COLUMN`, `make test` casse en CI.
|
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ Tout champ de formulaire / filtre doit utiliser les composants `Malio*` plutot q
|
|||||||
|
|
||||||
Toute autre exception requiert validation avant merge.
|
Toute autre exception requiert validation avant merge.
|
||||||
|
|
||||||
|
## Standard ecran formulaire — reference : ecran Client
|
||||||
|
|
||||||
|
**Tout nouvel ecran de formulaire doit ressembler au premier ecran Client** (`frontend/modules/commercial/pages/clients/new.vue` + `[id]/edit.vue`) : meme structure (bloc principal puis onglets), memes marges/espacements, memes blocs de collection (ajout/suppression inline), meme validation inline 422 par champ. C'est la reference visuelle et fonctionnelle des formulaires du projet — s'en inspirer avant d'en creer un nouveau.
|
||||||
|
|
||||||
## Validation des formulaires — useFormErrors obligatoire (erreur par champ)
|
## Validation des formulaires — useFormErrors obligatoire (erreur par champ)
|
||||||
|
|
||||||
**Tout formulaire qui soumet a une API DOIT afficher les erreurs de validation 422 sous le champ concerne, via `useFormErrors`** (`frontend/shared/composables/useFormErrors.ts`). C'est le pendant front de « le back renvoie TOUTES les violations d'une 422 d'un coup » : un seul aller-retour, chaque erreur affichee inline sous son champ (prop `:error` des `Malio*`), pas un toast fourre-tout.
|
**Tout formulaire qui soumet a une API DOIT afficher les erreurs de validation 422 sous le champ concerne, via `useFormErrors`** (`frontend/shared/composables/useFormErrors.ts`). C'est le pendant front de « le back renvoie TOUTES les violations d'une 422 d'un coup » : un seul aller-retour, chaque erreur affichee inline sous son champ (prop `:error` des `Malio*`), pas un toast fourre-tout.
|
||||||
@@ -142,6 +146,18 @@ A NE PAS faire :
|
|||||||
- Seuls les deep links "de navigation metier" (ex: ouvrir un detail precis `/users/42`) sont dans l'URL
|
- Seuls les deep links "de navigation metier" (ex: ouvrir un detail precis `/users/42`) sont dans l'URL
|
||||||
- Exceptions autorisees **sur demande explicite** de l'utilisateur
|
- Exceptions autorisees **sur demande explicite** de l'utilisateur
|
||||||
|
|
||||||
|
## Validation des formulaires (standard ERP-101)
|
||||||
|
|
||||||
|
Regle transverse a TOUS les formulaires front (et a rappeler a l'ecriture de chaque ticket back/front portant un formulaire). Decidee en ERP-101 (declencheur : ecran « Ajouter un client » ERP-63).
|
||||||
|
|
||||||
|
- **Champs obligatoires** : prop `required` du composant `Malio*` + etoile (asterisque) rouge dans le label. Ne JAMAIS griser le bouton « Valider » sans feedback : bouton toujours actif + erreurs affichees sous les champs.
|
||||||
|
- **Couche de validation autoritaire = le back** : les RG sont re-validees serveur (mode strict). Au `422`, mapper `violations[].propertyPath` vers la prop `error` du champ via `extractApiViolations` (deja utilise par `useCategoryForm`). Zero duplication de RG, zero drift.
|
||||||
|
- **Feedback instantane au blur** : uniquement requis / min / max / format (pas de re-implementation des RG metier cote front).
|
||||||
|
- **Regles front-only** : celles sans equivalent back (ex. FK nullable cote back mais obligatoire selon un choix UI) sont validees et affichees cote front.
|
||||||
|
- **Email — PAS de masque** : un email n'a pas de structure fixe. Normalisation via la prop `lowercase` de `MalioInputEmail` (trim + suppression des espaces + lowercase, coherent avec la normalisation serveur RG-1.21). Le format est valide par la prop `error` (violations serveur ou check au blur), jamais par un masque. Retirer tout shaping email ad hoc des ecrans.
|
||||||
|
- **Contrat back attendu** : tout `422` issu d'un Processor/Validator doit porter `violations[].propertyPath` aligne sur les noms de champs du formulaire, pour etre consommable par `extractApiViolations`.
|
||||||
|
- **Dependance** : le branchement des props `required` suppose `@malio/layer-ui` a jour (props `required` + etoile — MUI-41 / ERP-101).
|
||||||
|
|
||||||
## Interdits
|
## Interdits
|
||||||
|
|
||||||
- `modules-loader.ts`, `.module.ts` — le scan des layers est automatique
|
- `modules-loader.ts`, `.module.ts` — le scan des layers est automatique
|
||||||
|
|||||||
@@ -0,0 +1,251 @@
|
|||||||
|
---
|
||||||
|
name: backend-entity-conventions
|
||||||
|
description: Conventions détaillées des entités métier Starseed (back PHP/Symfony/API Platform) — messages de validation FR sur les contraintes, pagination API Platform et providers ORM/DBAL, libellé i18n du type d'entité auditée, Timestampable/Blamable, COMMENT ON COLUMN des migrations. Charger dès qu'on crée ou modifie une entité Domain, un ApiResource, un Provider/Processor, une contrainte de validation, ou une migration Doctrine. Le résumé court de chaque règle (+ nom du test garde-fou) reste dans .claude/rules/backend.md ; ce skill porte les patterns, tableaux et exemples complets.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Conventions entités métier — détail
|
||||||
|
|
||||||
|
Ce skill contient le détail (patterns code, tableaux, dérivations) des 5 règles back qui ont chacune
|
||||||
|
un test Architecture déterministe. L'énoncé court de chaque règle vit dans `.claude/rules/backend.md`
|
||||||
|
(chargé à chaque session) ; ici on trouve le « comment » complet.
|
||||||
|
|
||||||
|
> Règle d'or : le **test Architecture reste le juge** (il casse `make test`). Ce skill aide à écrire
|
||||||
|
> le code juste du premier coup, il ne remplace pas le garde-fou.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Messages de validation (Garde-fou : `EntityConstraintsHaveFrenchMessageTest`)
|
||||||
|
|
||||||
|
**Toute contrainte `#[Assert\*]` portée par une entité métier doit avoir un message FR explicite**, et
|
||||||
|
**`Assert\Length.max` doit refléter le `length` de la colonne ORM**. C'est le pendant back du mapping
|
||||||
|
d'erreur par champ côté front (ERP-101 : `useFormErrors` / `mapViolationsToRecord` affiche sous chaque
|
||||||
|
champ le `message` renvoyé par le back).
|
||||||
|
|
||||||
|
Pourquoi :
|
||||||
|
- Sans `message:` explicite, Symfony renvoie le défaut **anglais** (« This value is not a valid email
|
||||||
|
address. »). La locale FR globale (`default_locale: fr` dans `framework.yaml`) sert de FILET via
|
||||||
|
`validators.fr.xlf`, mais les contraintes métier portent en plus leur message FR pour un contrôle total.
|
||||||
|
- Une colonne string bornée **sans `Assert\Length`** échoue au niveau Postgres (500 générique, non
|
||||||
|
rattachée au champ) au lieu d'une 422 propre. Le `max` doit égaler le `length` ORM (anti-dérive).
|
||||||
|
|
||||||
|
Pattern par champ scalaire :
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Email métier
|
||||||
|
#[Assert\Email(message: 'L\'adresse email n\'est pas valide.')]
|
||||||
|
|
||||||
|
// Longueur calée sur la colonne (VARCHAR(120))
|
||||||
|
#[ORM\Column(length: 120)]
|
||||||
|
#[Assert\Length(max: 120, maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
|
|
||||||
|
// Obligatoire (aligner nullable DB / NotBlank back / required front)
|
||||||
|
#[Assert\NotBlank(message: 'Le téléphone est obligatoire.', normalizer: 'trim')]
|
||||||
|
```
|
||||||
|
|
||||||
|
Cohérence à 3 niveaux pour un champ obligatoire : colonne `nullable` (DB) <-> `Assert\NotBlank` (back)
|
||||||
|
<-> `:required` + astérisque (front ERP-101). Les trois doivent s'accorder.
|
||||||
|
|
||||||
|
Exceptions au miroir `Length` : un format déjà borné par `Assert\Bic` / `Assert\Iban` (longueur
|
||||||
|
garantie) ou par un `Assert\Regex` borné (ex. code postal `{4,5}`, couleur hex `#RRGGBB`) — whitelister
|
||||||
|
alors la propriété dans `EntityConstraintsHaveFrenchMessageTest::EXCLUDED_LENGTH_MIRROR` avec justification.
|
||||||
|
|
||||||
|
Les règles inter-champs (RG métier : exclusivité distributor/broker RG-1.03, billingEmail RG-1.11, etc.)
|
||||||
|
passent par un `#[Assert\Callback]` qui construit la violation avec `->atPath('<champ>')` — indispensable
|
||||||
|
pour que le front la mappe en inline plutôt qu'en toast.
|
||||||
|
|
||||||
|
### Garde-fou architecture
|
||||||
|
|
||||||
|
`tests/Architecture/EntityConstraintsHaveFrenchMessageTest` scanne réflexivement les entités sous
|
||||||
|
`src/Module/*/Domain/Entity/` et échoue si :
|
||||||
|
1. une contrainte connue n'a pas de message FR explicite (comparé au défaut Symfony) ;
|
||||||
|
2. une colonne string bornée writable n'a pas de `Assert\Length(max == ORM length)` (hors whitelist).
|
||||||
|
|
||||||
|
Une contrainte non gérée par le mapping du test le fait échouer : il faut l'ajouter explicitement
|
||||||
|
(anti faux positif vert).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Pagination (Garde-fou : `CollectionsArePaginatedTest`)
|
||||||
|
|
||||||
|
**Règle** : toute collection API DOIT être paginée. Aucun retour de collection complète côté serveur.
|
||||||
|
|
||||||
|
### Standard global
|
||||||
|
|
||||||
|
Posé dans `config/packages/api_platform.yaml` (section `defaults:`) et hérité par toutes les ressources :
|
||||||
|
|
||||||
|
| Clé | Valeur | Effet |
|
||||||
|
|---|---|---|
|
||||||
|
| `pagination_enabled` | `true` | Pagination Hydra active par défaut. |
|
||||||
|
| `pagination_items_per_page` | `10` | Taille de page par défaut, alignée sur l'UI `MalioDataTable`. |
|
||||||
|
| `pagination_maximum_items_per_page` | `50` | Borne dure : `?itemsPerPage=999` → ramené à 50. Anti deep-fetch. |
|
||||||
|
| `pagination_client_items_per_page` | `true` | Le client peut envoyer `?itemsPerPage=25` (bornée par le max). |
|
||||||
|
| `pagination_client_enabled` | `true` | Le client peut envoyer `?pagination=false` pour TOUT récupérer (échappatoire selects). |
|
||||||
|
|
||||||
|
### Override par ressource (rare)
|
||||||
|
|
||||||
|
Si une ressource a besoin d'un autre défaut (ex: payload lourd), utiliser les attributs sur l'opération.
|
||||||
|
**JAMAIS `paginationEnabled: false`** sans whitelist explicite dans
|
||||||
|
`tests/Architecture/CollectionsArePaginatedTest::EXCLUDED`.
|
||||||
|
|
||||||
|
```php
|
||||||
|
new GetCollection(
|
||||||
|
paginationItemsPerPage: 5, // override taille par défaut
|
||||||
|
paginationMaximumItemsPerPage: 20, // override borne max
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Selects et autocomplétions
|
||||||
|
|
||||||
|
Pour alimenter un `<select>` ou un drawer RBAC (Role, Permission, Site, CategoryType), le front passe :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
useApi().get('/api/roles?pagination=false')
|
||||||
|
```
|
||||||
|
|
||||||
|
Le serveur retourne toute la collection, sans `view`. C'est l'échappatoire prévue par
|
||||||
|
`pagination_client_enabled: true`. Sur les ressources à forte volumétrie, préférer une saisie assistée
|
||||||
|
(recherche serveur via `?q=`) — à planifier dans un ticket dédié.
|
||||||
|
|
||||||
|
Les tests fonctionnels qui exercent ce comportement doivent également passer `?pagination=false`
|
||||||
|
(cf. `CategoryListTest`, `PermissionApiTest`).
|
||||||
|
|
||||||
|
### Providers customs et pagination
|
||||||
|
|
||||||
|
Un provider custom qui retourne un `array` brut sur une `CollectionOperationInterface`
|
||||||
|
**court-circuite la pagination Hydra** (pas de `totalItems`, pas de `view`). Patterns supportés :
|
||||||
|
|
||||||
|
- **ORM** : injecter `ApiPlatform\State\Pagination\Pagination`, wrap un
|
||||||
|
`Doctrine\ORM\Tools\Pagination\Paginator` dans `ApiPlatform\Doctrine\Orm\Paginator`. Exemple : `CategoryProvider`.
|
||||||
|
- **DBAL** : implémenter un paginator local conforme à `PaginatorInterface`. Exemple : `DbalPaginator`
|
||||||
|
(Core) + `AuditLogProvider`.
|
||||||
|
|
||||||
|
Gérer l'échappatoire `?pagination=false` :
|
||||||
|
|
||||||
|
```php
|
||||||
|
if (!$this->pagination->isEnabled($operation, $context)) {
|
||||||
|
return $qb->getQuery()->getResult(); // tout retourner
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Garde-fou architecture
|
||||||
|
|
||||||
|
`tests/Architecture/CollectionsArePaginatedTest` scanne réflexivement toutes les classes
|
||||||
|
`#[ApiResource]` sous `src/` et échoue si une `GetCollection` pose `paginationEnabled: false` hors
|
||||||
|
whitelist `EXCLUDED`. Ajouter une entrée à la whitelist requiert une justification courte + un ticket
|
||||||
|
Lesstime ouvert.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Libellé i18n du type d'entité auditée (Garde-fou : `AuditableEntitiesHaveI18nLabelTest`)
|
||||||
|
|
||||||
|
**Toute entité `#[Auditable]` doit avoir son libellé FR dans le bloc `audit.entity` de
|
||||||
|
`frontend/i18n/locales/fr.json`.** C'est la contrepartie i18n de l'attribut : sans elle, le filtre
|
||||||
|
« Type d'entité » de l'audit-log affiche le type technique brut (ex: `commercial.Client`) au lieu d'un
|
||||||
|
libellé lisible.
|
||||||
|
|
||||||
|
Pourquoi : le filtre est dynamique (`GET /audit-log-entity-types` renvoie les `entity_type` distincts
|
||||||
|
présents en base) ; dès qu'un module audite une entité, son type y apparaît. Le front
|
||||||
|
(`formatEntityType`, `audit-log.vue`) construit la clé `audit.entity.<module>_<entity>` et, faute de
|
||||||
|
traduction, **retombe silencieusement** sur le type brut.
|
||||||
|
|
||||||
|
Dérivation de la clé (emplacement centralisé + schéma flat — décision ERP-99) :
|
||||||
|
|
||||||
|
| FQCN entité | `entity_type` (back) | Clé i18n (flat) |
|
||||||
|
|---|---|---|
|
||||||
|
| `App\Module\Commercial\Domain\Entity\Client` | `commercial.Client` | `commercial_client` |
|
||||||
|
| `App\Module\Commercial\Domain\Entity\ClientAddress` | `commercial.ClientAddress` | `commercial_clientaddress` |
|
||||||
|
| `App\Module\Catalog\Domain\Entity\Category` | `catalog.Category` | `catalog_category` |
|
||||||
|
|
||||||
|
Règle : `strtolower(module)` + `_` + `strtolower(Entity)`. Ajouter sa clé de libellé audit fait partie
|
||||||
|
de la **définition de fini** d'une entité métier auditée.
|
||||||
|
|
||||||
|
**Garde-fou** : `tests/Architecture/AuditableEntitiesHaveI18nLabelTest` scanne les entités `#[Auditable]`
|
||||||
|
et échoue si une seule n'a pas sa clé `audit.entity.*`. Conclusion : créer une entité `#[Auditable]`
|
||||||
|
sans son libellé i18n casse `make test`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Timestampable + Blamable (Garde-fou : `EntitiesAreTimestampableBlamableTest`)
|
||||||
|
|
||||||
|
Toute **nouvelle** entité métier sous `src/Module/*/Domain/Entity/` doit porter les 4 colonnes
|
||||||
|
`created_at` / `updated_at` / `created_by` / `updated_by`, remplies automatiquement. Trois lignes à
|
||||||
|
ajouter à l'entité :
|
||||||
|
|
||||||
|
```php
|
||||||
|
use App\Shared\Domain\Contract\BlamableInterface;
|
||||||
|
use App\Shared\Domain\Contract\TimestampableInterface;
|
||||||
|
use App\Shared\Domain\Trait\TimestampableBlamableTrait;
|
||||||
|
|
||||||
|
class MyEntity implements TimestampableInterface, BlamableInterface
|
||||||
|
{
|
||||||
|
use TimestampableBlamableTrait; // porte les 4 props + getters/setters
|
||||||
|
// ... reste métier
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Le `TimestampableBlamableSubscriber` (`Shared/Infrastructure/Doctrine/`) remplit les colonnes au
|
||||||
|
`prePersist` / `preUpdate`. Hors contexte HTTP (CLI, cron, migration), le blame reste `null`
|
||||||
|
(libellé « Système » côté front).
|
||||||
|
- La migration de l'entité doit créer les 4 colonnes (`created_at` / `updated_at` NOT NULL,
|
||||||
|
`created_by` / `updated_by` nullable `ON DELETE SET NULL`).
|
||||||
|
- **Garde-fou CI** : `tests/Architecture/EntitiesAreTimestampableBlamableTest` échoue si une entité
|
||||||
|
oublie le pattern. Un référentiel statique justifié (ex: `CategoryType`) doit être explicitement
|
||||||
|
whitelisté dans la constante `EXCLUDED` avec un commentaire.
|
||||||
|
- Spec complète : @docs/specs/M0-categories/spec-back.md § 2.8 + § 2.8.bis
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Migrations Doctrine — COMMENT ON COLUMN (Garde-fou : `ColumnsHaveSqlCommentTest`)
|
||||||
|
|
||||||
|
**Toute migration qui crée ou modifie une colonne d'une table métier doit poser un `COMMENT ON COLUMN`
|
||||||
|
décrivant le champ.** La description est stockée dans `pg_description` et visible dans tous les outils
|
||||||
|
d'admin BDD (DBeaver, DataGrip, pgAdmin), sans avoir à lire les annotations PHP.
|
||||||
|
|
||||||
|
**Format de la description** :
|
||||||
|
- En français
|
||||||
|
- ≤ 200 caractères
|
||||||
|
- Sémantique du champ — contraintes / lien RG si pertinent
|
||||||
|
- Pour les colonnes d'identifiant ou FK, mentionner la cible
|
||||||
|
|
||||||
|
Exemples :
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Migration : création d'une colonne avec son commentaire dans la même migration
|
||||||
|
$this->addSql("ALTER TABLE client ADD COLUMN siren VARCHAR(9) DEFAULT NULL");
|
||||||
|
$this->addSql("COMMENT ON COLUMN client.siren IS 'SIREN (9 chiffres) — identifiant legal entreprise. Unique parmi non-archives (RG-1.15).'");
|
||||||
|
|
||||||
|
// Cas FK : préciser la cible
|
||||||
|
$this->addSql("COMMENT ON COLUMN client.legal_form_id IS 'Reference forme juridique (SARL, SAS, SA...) — FK -> legal_form.id, ON DELETE RESTRICT.'");
|
||||||
|
|
||||||
|
// Cas booléen : préciser le sens et la valeur par défaut
|
||||||
|
$this->addSql("COMMENT ON COLUMN user.is_admin IS 'Drapeau super-administrateur — bypass complet RBAC. Faux par defaut.'");
|
||||||
|
|
||||||
|
// Bonus : décrire la table elle-même
|
||||||
|
$this->addSql("COMMENT ON TABLE client IS 'Repertoire clients (M1 Commercial) — entites archivables.'");
|
||||||
|
```
|
||||||
|
|
||||||
|
### Helper Timestampable/Blamable
|
||||||
|
|
||||||
|
Les 4 colonnes `created_at`, `updated_at`, `created_by`, `updated_by` ajoutées par
|
||||||
|
`TimestampableBlamableTrait` reçoivent une description **standardisée** via le helper centralisé pour
|
||||||
|
éviter la duplication. Helper à créer ou appeler :
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Dans la migration, après avoir ajouté les 4 colonnes :
|
||||||
|
$this->addStandardTimestampableBlamableComments($schema, 'client');
|
||||||
|
```
|
||||||
|
|
||||||
|
L'implémentation du helper applique :
|
||||||
|
- `created_at` : « Horodatage de creation de la ligne (UTC, rempli automatiquement par TimestampableBlamableSubscriber). »
|
||||||
|
- `updated_at` : « Horodatage de derniere modification de la ligne (UTC, rempli automatiquement par TimestampableBlamableSubscriber). »
|
||||||
|
- `created_by` : « ID de l'utilisateur ayant cree la ligne — null pour les creations hors HTTP (CLI, migration, fixture). FK -> user.id, ON DELETE SET NULL. »
|
||||||
|
- `updated_by` : « ID de l'utilisateur ayant modifie la ligne en dernier — null pour les modifications hors HTTP. FK -> user.id, ON DELETE SET NULL. »
|
||||||
|
|
||||||
|
### Garde-fou architecture
|
||||||
|
|
||||||
|
`tests/Architecture/ColumnsHaveSqlCommentTest` parcourt `information_schema.columns` filtré sur le
|
||||||
|
schéma `public` et échoue si **une seule colonne** n'a pas de `col_description`. Seules les tables
|
||||||
|
système (`doctrine_migration_versions`) et la whitelist `EXCLUDED_TABLES` explicite (commentaire de
|
||||||
|
justification + ticket Lesstime ouvert pour le retrofit) sont tolérées.
|
||||||
|
|
||||||
|
Conclusion : si tu crées une colonne sans poser son `COMMENT ON COLUMN`, `make test` casse en CI.
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
"symfony/runtime": "8.0.*",
|
"symfony/runtime": "8.0.*",
|
||||||
"symfony/security-bundle": "8.0.*",
|
"symfony/security-bundle": "8.0.*",
|
||||||
"symfony/serializer": "8.0.*",
|
"symfony/serializer": "8.0.*",
|
||||||
|
"symfony/translation": "8.0.*",
|
||||||
"symfony/twig-bundle": "8.0.*",
|
"symfony/twig-bundle": "8.0.*",
|
||||||
"symfony/uid": "8.0.*",
|
"symfony/uid": "8.0.*",
|
||||||
"symfony/validator": "8.0.*",
|
"symfony/validator": "8.0.*",
|
||||||
|
|||||||
Generated
+94
-1
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "aada2e60fd7563f1498b5505b37e3f4b",
|
"content-hash": "2dc5db01e7f5d6aecd5956749b21a092",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "api-platform/doctrine-common",
|
"name": "api-platform/doctrine-common",
|
||||||
@@ -7657,6 +7657,99 @@
|
|||||||
],
|
],
|
||||||
"time": "2026-03-30T15:14:47+00:00"
|
"time": "2026-03-30T15:14:47+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "symfony/translation",
|
||||||
|
"version": "v8.0.10",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/symfony/translation.git",
|
||||||
|
"reference": "f63e9342e12646a57c91ef8a366a4f9d8e557b67"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/symfony/translation/zipball/f63e9342e12646a57c91ef8a366a4f9d8e557b67",
|
||||||
|
"reference": "f63e9342e12646a57c91ef8a366a4f9d8e557b67",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.4",
|
||||||
|
"symfony/polyfill-mbstring": "^1.0",
|
||||||
|
"symfony/translation-contracts": "^3.6.1"
|
||||||
|
},
|
||||||
|
"conflict": {
|
||||||
|
"nikic/php-parser": "<5.0",
|
||||||
|
"symfony/http-client-contracts": "<2.5",
|
||||||
|
"symfony/service-contracts": "<2.5"
|
||||||
|
},
|
||||||
|
"provide": {
|
||||||
|
"symfony/translation-implementation": "2.3|3.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"nikic/php-parser": "^5.0",
|
||||||
|
"psr/log": "^1|^2|^3",
|
||||||
|
"symfony/config": "^7.4|^8.0",
|
||||||
|
"symfony/console": "^7.4|^8.0",
|
||||||
|
"symfony/dependency-injection": "^7.4|^8.0",
|
||||||
|
"symfony/finder": "^7.4|^8.0",
|
||||||
|
"symfony/http-client-contracts": "^2.5|^3.0",
|
||||||
|
"symfony/http-kernel": "^7.4|^8.0",
|
||||||
|
"symfony/intl": "^7.4|^8.0",
|
||||||
|
"symfony/polyfill-intl-icu": "^1.21",
|
||||||
|
"symfony/routing": "^7.4|^8.0",
|
||||||
|
"symfony/service-contracts": "^2.5|^3",
|
||||||
|
"symfony/yaml": "^7.4|^8.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"files": [
|
||||||
|
"Resources/functions.php"
|
||||||
|
],
|
||||||
|
"psr-4": {
|
||||||
|
"Symfony\\Component\\Translation\\": ""
|
||||||
|
},
|
||||||
|
"exclude-from-classmap": [
|
||||||
|
"/Tests/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Fabien Potencier",
|
||||||
|
"email": "fabien@symfony.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Symfony Community",
|
||||||
|
"homepage": "https://symfony.com/contributors"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Provides tools to internationalize your application",
|
||||||
|
"homepage": "https://symfony.com",
|
||||||
|
"support": {
|
||||||
|
"source": "https://github.com/symfony/translation/tree/v8.0.10"
|
||||||
|
},
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://symfony.com/sponsor",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/fabpot",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/nicolas-grekas",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||||
|
"type": "tidelift"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2026-05-06T11:30:54+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/translation-contracts",
|
"name": "symfony/translation-contracts",
|
||||||
"version": "v3.6.1",
|
"version": "v3.6.1",
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
framework:
|
||||||
|
# Locale par defaut FR (ERP-107) : les messages natifs des contraintes
|
||||||
|
# Symfony (Email, NotBlank, Length, Iban, Bic...) sont alors servis en
|
||||||
|
# francais via validators.fr.xlf. C'est le FILET ; les contraintes metier
|
||||||
|
# portent en plus un `message:` FR explicite, teste par
|
||||||
|
# tests/Architecture/EntityConstraintsHaveFrenchMessageTest.
|
||||||
|
default_locale: fr
|
||||||
|
translator:
|
||||||
|
default_path: '%kernel.project_dir%/translations'
|
||||||
|
fallbacks:
|
||||||
|
- fr
|
||||||
|
providers:
|
||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
parameters:
|
parameters:
|
||||||
app.version: '0.1.80'
|
app.version: '0.1.83'
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
# Validation « tous les blocs » sur les onglets à blocs dynamiques (Client M1)
|
||||||
|
|
||||||
|
> Date : 2026-06-04 · Module : Commercial (M1 Clients) · Tickets liés : ERP-101 / ERP-107
|
||||||
|
> Écrans : `clients/new.vue`, `clients/[id]/edit.vue` · Onglets concernés : Contacts, Adresses, RIB
|
||||||
|
|
||||||
|
## 1. Problème
|
||||||
|
|
||||||
|
À la soumission des onglets à **blocs d'ajout dynamiques** (Contacts / Adresses / RIB), la validation
|
||||||
|
par champ ne s'affiche pas correctement. Deux causes **distinctes et cumulées** :
|
||||||
|
|
||||||
|
### Cause A — 500 back qui court-circuite la validation (cause racine)
|
||||||
|
|
||||||
|
Les opérations `Post` des sous-ressources sont déclarées ainsi :
|
||||||
|
|
||||||
|
```php
|
||||||
|
new Post(
|
||||||
|
uriTemplate: '/clients/{clientId}/contacts',
|
||||||
|
uriVariables: ['clientId' => new Link(fromClass: Client::class, toProperty: 'client')],
|
||||||
|
processor: ClientContactProcessor::class,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Au stade « read » du POST, API Platform résout `clientId` via `LinksHandlerTrait` (branche `toProperty`,
|
||||||
|
`vendor/api-platform/doctrine-orm/State/LinksHandlerTrait.php:134-141`). La requête générée porte sur
|
||||||
|
l'entité **enfant** :
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT o FROM ClientContact o INNER JOIN o.client c WHERE c.id = :clientId
|
||||||
|
```
|
||||||
|
|
||||||
|
exécutée via `ItemProvider::provide` → `getOneOrNullResult()`
|
||||||
|
(`vendor/api-platform/doctrine-orm/State/ItemProvider.php:89`). Donc :
|
||||||
|
|
||||||
|
| Nb d'enfants du client | Lignes retournées | Résultat |
|
||||||
|
|---|---|---|
|
||||||
|
| 0 | 0 | `null` → OK (cas du test CI actuel) |
|
||||||
|
| 1 | 1 | OK |
|
||||||
|
| **≥ 2** | **≥ 2** | **`NonUniqueResultException` → HTTP 500** |
|
||||||
|
|
||||||
|
Conséquence : un client à ≥2 contacts (resp. adresses, RIB) ne peut plus en recevoir un nouveau.
|
||||||
|
La 500 survient **avant** la déserialisation/validation → aucune 422 n'est produite → `mapRowError`
|
||||||
|
(qui ne mappe que les 422) retombe sur un toast générique.
|
||||||
|
|
||||||
|
Les **3** sous-ressources ont strictement la même config → même bug latent (contacts est juste le
|
||||||
|
premier à sauter car les clients de démo ont 3 contacts).
|
||||||
|
|
||||||
|
### Cause B — la boucle front s'arrête au premier bloc en erreur
|
||||||
|
|
||||||
|
`submitContacts` / `submitAddresses` / boucle RIB de `submitAccounting` (dans `new.vue` ET `edit.vue`)
|
||||||
|
font `return` dans le `catch` du premier bloc en échec :
|
||||||
|
|
||||||
|
```js
|
||||||
|
catch (error) {
|
||||||
|
if (!mapRowError(error, contactErrors, index)) { toast(...) }
|
||||||
|
return // ← stoppe : les blocs suivants ne sont jamais validés ni affichés
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
→ même une fois le 500 corrigé, seules les erreurs du **premier** bloc fautif s'afficheraient.
|
||||||
|
|
||||||
|
## 2. Objectif
|
||||||
|
|
||||||
|
À la validation d'un onglet collection, **tenter tous les blocs** et **afficher l'erreur inline sous
|
||||||
|
chaque champ fautif, pour chaque bloc**, en un seul aller-retour de soumission. Pas de toast récapitulatif
|
||||||
|
(décision : inline seul, cohérent ERP-101). Pas de toast succès tant qu'au moins un bloc reste en erreur.
|
||||||
|
|
||||||
|
Hors périmètre : le workflow incrémental (créer le client, puis débloquer les onglets) reste inchangé ;
|
||||||
|
les onglets scalaires (Principal / Information / Comptabilité-scalaires) fonctionnent déjà et ne sont pas
|
||||||
|
touchés.
|
||||||
|
|
||||||
|
## 3. Conception
|
||||||
|
|
||||||
|
### 3.1 Back — supprimer le read cassé du POST (cause racine)
|
||||||
|
|
||||||
|
Sur les opérations `Post` de `ClientContact`, `ClientAddress`, `ClientRib` :
|
||||||
|
|
||||||
|
- Ajouter **`read: false`**. Le stade « read » est inutile : le `*Processor::linkParent` rattache déjà le
|
||||||
|
parent manuellement via `$em->getRepository(Client::class)->find($clientId)`. Pattern déjà employé dans
|
||||||
|
le projet (`Sites/.../CurrentSiteResource.php`).
|
||||||
|
- Durcir les 3 `linkParent` : si `find($clientId)` renvoie `null`, lever
|
||||||
|
`Symfony\Component\HttpKernel\Exception\NotFoundHttpException` (préserve le **404** sur parent
|
||||||
|
inexistant — sans le read, on régresserait sinon en 500 au persist sur `client_id NOT NULL`).
|
||||||
|
|
||||||
|
Effet : plus de `getOneOrNullResult` foireux → déserialisation + validation Symfony s'exécutent → **422
|
||||||
|
propre par champ** avec `violations[].propertyPath` (déjà garanti par ERP-107 : messages FR explicites).
|
||||||
|
|
||||||
|
Aucune autre modification (security, normalizationContext, processor restant) n'est nécessaire.
|
||||||
|
|
||||||
|
### 3.2 Front — collecter les erreurs de tous les blocs
|
||||||
|
|
||||||
|
Dans `submitContacts`, `submitAddresses`, et la boucle RIB de `submitAccounting`, **dans `new.vue` ET
|
||||||
|
`edit.vue`** :
|
||||||
|
|
||||||
|
- Conserver la réinitialisation du tableau d'erreurs en début de submit (`xxxErrors.value = []`).
|
||||||
|
- Introduire un drapeau local `hasError`. Dans le `catch`, remplacer `return` par
|
||||||
|
`hasError = true; continue` → la boucle tente/valide **tous** les blocs ; chaque 422 se mappe sur
|
||||||
|
`xxxErrors[index]` via `mapRowError` (mécanique existante, inchangée).
|
||||||
|
- Après la boucle : si `hasError` → **ne pas** appeler `completeTab(...)`, **pas** de toast succès. Sinon
|
||||||
|
→ comportement actuel (`completeTab` + toast succès).
|
||||||
|
- Les blocs déjà créés (id non-null) repassent en `PATCH` au resubmit → idempotent, pas de doublon.
|
||||||
|
- Awaits **séquentiels** conservés (volume faible, ordre des blocs préservé, pas de course).
|
||||||
|
|
||||||
|
Le binding inline est déjà en place côté template (`:errors="contactErrors[index]"` /
|
||||||
|
`:error="ribErrors[index]?.iban"` …). Aucun changement de composant `Malio*` requis.
|
||||||
|
|
||||||
|
### 3.3 Réutilisation / isolation
|
||||||
|
|
||||||
|
Le bloc « boucle de soumission d'une collection avec collecte d'erreurs par index » est dupliqué 3× × 2
|
||||||
|
pages. Pour rester testable et DRY, extraire un helper de soumission de collection (ex.
|
||||||
|
`submitCollection(rows, { buildBody, post, patch, errors })` retournant `{ hasError }`) consommé par les
|
||||||
|
6 sites d'appel. À acter dans le plan d'implémentation (option : garder inline si l'extraction dégrade la
|
||||||
|
lisibilité — décision lors du plan).
|
||||||
|
|
||||||
|
## 4. Tests
|
||||||
|
|
||||||
|
### Back (TDD — échouent d'abord)
|
||||||
|
|
||||||
|
Dans `tests/Module/Commercial/Api/ClientSubResourceApiTest` :
|
||||||
|
|
||||||
|
- `testPostContactToClientWithTwoExistingContactsReturns201` : seed un client + 2 contacts, POST un 3ᵉ →
|
||||||
|
attendu **201** (rouge aujourd'hui : 500).
|
||||||
|
- `testPostContactInvalidEmailOnClientWithExistingContactsReturns422` : même seed, POST email invalide →
|
||||||
|
**422** avec `propertyPath=email` et message FR (vérifie que la validation est bien atteinte).
|
||||||
|
- Variantes germes pour adresses et RIB (au moins une chacune) pour verrouiller les 3 sous-ressources.
|
||||||
|
|
||||||
|
Pré-requis : helper de seed de contacts/adresses/RIB dans `AbstractCommercialApiTestCase` (ajouter si
|
||||||
|
absent).
|
||||||
|
|
||||||
|
### Front (Vitest)
|
||||||
|
|
||||||
|
- Si helper `submitCollection` extrait : test unitaire « 3 blocs, le 2ᵉ renvoie 422 → les erreurs du 2ᵉ
|
||||||
|
sont mappées, les blocs 1 et 3 sont tentés, `hasError = true`, tab non complété ».
|
||||||
|
- Sinon : test de composant sur `ClientContactBlock` + page, vérifiant l'affichage inline multi-blocs.
|
||||||
|
|
||||||
|
### Vérifications finales
|
||||||
|
|
||||||
|
`make test` + `make php-cs-fixer-allow-risky` (back), `make nuxt-test` (front). Golden path manuel :
|
||||||
|
client à 3 contacts, ajouter un 4ᵉ avec email invalide → 422 inline sous l'email du bon bloc, pas de 500.
|
||||||
|
|
||||||
|
## 5. Impact / risques
|
||||||
|
|
||||||
|
- API contract : POST sous-ressource passe de 500→201/422 (correction) ; 404 préservé sur parent
|
||||||
|
inexistant. Pas de changement de payload ni de réponse de succès.
|
||||||
|
- Le test fonctionnel CI actuel (POST sur client à 0 contact) reste vert.
|
||||||
|
- Régression possible si un consommateur dépendait du read implicite du parent au POST : aucun identifié
|
||||||
|
(les 3 processors gèrent déjà le rattachement manuellement).
|
||||||
@@ -0,0 +1,633 @@
|
|||||||
|
# Validation « tous les blocs » — onglets à blocs dynamiques (Client M1) — Plan d'implémentation
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Permettre la validation 422 par champ sur TOUS les blocs des onglets Contacts / Adresses / RIB d'un client (création + édition), en supprimant la 500 `NonUniqueResultException` qui les bloque dès ≥2 enfants et en ne stoppant plus la boucle front au premier bloc en erreur.
|
||||||
|
|
||||||
|
**Architecture:** Côté back, on retire le stade « read » inutile du POST des 3 sous-ressources (`read: false`) — le parent est déjà rattaché manuellement par le processor — et on durcit ce rattachement (404 si parent absent). Côté front, on factorise la boucle de soumission de collection dans `useClientFormErrors().submitRows(...)` qui tente tous les blocs et collecte les erreurs par index, puis on branche les 6 sites d'appel (`new.vue` + `edit.vue` × contacts/adresses/RIB).
|
||||||
|
|
||||||
|
**Tech Stack:** Symfony 8 / API Platform 4 (PHP 8.4, PHPUnit) ; Nuxt 4 / Vue 3 / TypeScript / Vitest.
|
||||||
|
|
||||||
|
**Spec de référence :** `docs/superpowers/specs/2026-06-04-client-collection-blocks-validation-design.md`
|
||||||
|
|
||||||
|
**Pré-vol :** `make start` (containers up), branche de travail = celle de la MR (`feat/erp-107-validation-messages-fr`) ou une branche dédiée selon décision utilisateur.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Structure des fichiers
|
||||||
|
|
||||||
|
**Back — modifiés :**
|
||||||
|
- `src/Module/Commercial/Domain/Entity/ClientContact.php` — `read: false` sur `Post`
|
||||||
|
- `src/Module/Commercial/Domain/Entity/ClientAddress.php` — `read: false` sur `Post`
|
||||||
|
- `src/Module/Commercial/Domain/Entity/ClientRib.php` — `read: false` sur `Post`
|
||||||
|
- `src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientContactProcessor.php` — `linkParent` → 404
|
||||||
|
- `.../Processor/ClientAddressProcessor.php` — idem
|
||||||
|
- `.../Processor/ClientRibProcessor.php` — idem
|
||||||
|
- `tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php` — helper `seedContact()`
|
||||||
|
- `tests/Module/Commercial/Api/ClientSubResourceApiTest.php` — tests de non-régression
|
||||||
|
|
||||||
|
**Front — modifiés :**
|
||||||
|
- `frontend/modules/commercial/composables/useClientFormErrors.ts` — méthode `submitRows()`
|
||||||
|
- `frontend/modules/commercial/composables/__tests__/useClientFormErrors.spec.ts` — créé (test unitaire)
|
||||||
|
- `frontend/modules/commercial/pages/clients/new.vue` — branchements (3 submits)
|
||||||
|
- `frontend/modules/commercial/pages/clients/[id]/edit.vue` — branchements (3 submits)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 1 : Back — test rouge (POST sur client à ≥2 enfants)
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php`
|
||||||
|
- Test: `tests/Module/Commercial/Api/ClientSubResourceApiTest.php`
|
||||||
|
|
||||||
|
- [ ] **Step 1 : Ajouter un helper de seed de contact à la base de test**
|
||||||
|
|
||||||
|
Dans `AbstractCommercialApiTestCase.php`, ajouter (sous `seedClient`, avant `cleanupCommercialTestData`) :
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* Seede directement un ClientContact en base (sans passer par l'API), pour
|
||||||
|
* preparer un client deja dote de N contacts. Au moins le prenom est pose
|
||||||
|
* (RG-1.05 / CHECK chk_client_contact_name).
|
||||||
|
*/
|
||||||
|
protected function seedContact(ClientEntity $client, string $firstName): \App\Module\Commercial\Domain\Entity\ClientContact
|
||||||
|
{
|
||||||
|
$em = $this->getEm();
|
||||||
|
$contact = new \App\Module\Commercial\Domain\Entity\ClientContact();
|
||||||
|
$contact->setClient($client);
|
||||||
|
$contact->setFirstName($firstName);
|
||||||
|
$em->persist($contact);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
return $contact;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2 : Écrire les tests rouges**
|
||||||
|
|
||||||
|
Dans `ClientSubResourceApiTest.php`, ajouter dans la section `// === Contacts ===` :
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* Regression ERP (bug subresource Link toProperty) : POST d'un contact sur un
|
||||||
|
* client qui en a DEJA >= 2 ne doit pas exploser en 500
|
||||||
|
* (NonUniqueResultException sur la resolution du parent), mais creer (201).
|
||||||
|
*/
|
||||||
|
public function testPostContactOnClientWithTwoExistingContactsReturns201(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Contact Multi');
|
||||||
|
$this->seedContact($seed, 'Alpha');
|
||||||
|
$this->seedContact($seed, 'Beta');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['firstName' => 'Gamma'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meme contexte (>= 2 contacts existants) : un email invalide doit produire
|
||||||
|
* une 422 par champ (la validation est bien atteinte), pas une 500.
|
||||||
|
*/
|
||||||
|
public function testPostInvalidContactOnPopulatedClientReturns422OnField(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Contact Multi Bad');
|
||||||
|
$this->seedContact($seed, 'Alpha');
|
||||||
|
$this->seedContact($seed, 'Beta');
|
||||||
|
|
||||||
|
$response = $client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['firstName' => 'Gamma', 'email' => 'pas-un-email'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(422);
|
||||||
|
$byPath = [];
|
||||||
|
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||||
|
$byPath[$v['propertyPath']] = $v['message'];
|
||||||
|
}
|
||||||
|
self::assertArrayHasKey('email', $byPath);
|
||||||
|
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3 : Lancer les tests, vérifier qu'ils échouent (500 au lieu de 201/422)**
|
||||||
|
|
||||||
|
Run : `make test` (ou ciblé dans le container : `docker exec php-starseed-fpm php bin/phpunit --filter ClientSubResourceApiTest`)
|
||||||
|
Expected : les 2 nouveaux tests ÉCHOUENT (HTTP 500 `NonUniqueResultException`). `testPostContactOnClient...` reçoit 500, pas 201.
|
||||||
|
|
||||||
|
- [ ] **Step 4 : Commit (test rouge)**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php tests/Module/Commercial/Api/ClientSubResourceApiTest.php
|
||||||
|
git commit -m "test(commercial) : reproduit la 500 NonUniqueResult au POST contact sur client peuple (ERP-107)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 2 : Back — fix (read:false + linkParent durci) → tests verts
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/Module/Commercial/Domain/Entity/ClientContact.php:48-57`
|
||||||
|
- Modify: `src/Module/Commercial/Domain/Entity/ClientAddress.php:61-70`
|
||||||
|
- Modify: `src/Module/Commercial/Domain/Entity/ClientRib.php:52-61`
|
||||||
|
- Modify: `.../State/Processor/ClientContactProcessor.php:76-94`
|
||||||
|
- Modify: `.../State/Processor/ClientAddressProcessor.php:63-81`
|
||||||
|
- Modify: `.../State/Processor/ClientRibProcessor.php:65-83`
|
||||||
|
|
||||||
|
- [ ] **Step 1 : `read: false` sur les 3 opérations `Post`**
|
||||||
|
|
||||||
|
`ClientContact.php`, opération `Post` — ajouter la ligne `read: false,` :
|
||||||
|
|
||||||
|
```php
|
||||||
|
new Post(
|
||||||
|
uriTemplate: '/clients/{clientId}/contacts',
|
||||||
|
uriVariables: [
|
||||||
|
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||||
|
],
|
||||||
|
// read:false : pas de stade lecture du parent (le Link toProperty
|
||||||
|
// resoudrait l'enfant et casse en NonUniqueResult des >= 2 enfants).
|
||||||
|
// Le parent est rattache par ClientContactProcessor::linkParent.
|
||||||
|
read: false,
|
||||||
|
security: "is_granted('commercial.clients.manage')",
|
||||||
|
normalizationContext: ['groups' => ['client_contact:read']],
|
||||||
|
denormalizationContext: ['groups' => ['client_contact:write']],
|
||||||
|
processor: ClientContactProcessor::class,
|
||||||
|
),
|
||||||
|
```
|
||||||
|
|
||||||
|
`ClientAddress.php` — idem dans son `Post` (`security: commercial.clients.manage`, processor `ClientAddressProcessor`), commentaire pointant `ClientAddressProcessor::linkParent`.
|
||||||
|
|
||||||
|
`ClientRib.php` — idem dans son `Post` (`security: commercial.clients.accounting.manage`, processor `ClientRibProcessor`), commentaire pointant `ClientRibProcessor::linkParent`.
|
||||||
|
|
||||||
|
- [ ] **Step 2 : Durcir les 3 `linkParent` (404 si parent absent)**
|
||||||
|
|
||||||
|
Dans chaque processor, ajouter l'import en tête de fichier :
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
```
|
||||||
|
|
||||||
|
`ClientContactProcessor::linkParent` — remplacer le bloc final par :
|
||||||
|
|
||||||
|
```php
|
||||||
|
if (null === $clientId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = $clientId instanceof Client
|
||||||
|
? $clientId
|
||||||
|
: $this->em->getRepository(Client::class)->find($clientId);
|
||||||
|
|
||||||
|
// read:false sur le POST : sans stade lecture, un parent introuvable
|
||||||
|
// n'est plus intercepte en amont -> 404 explicite (sinon 500 au persist
|
||||||
|
// sur client_id NOT NULL).
|
||||||
|
if (!$client instanceof Client) {
|
||||||
|
throw new NotFoundHttpException('Client introuvable.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$contact->setClient($client);
|
||||||
|
```
|
||||||
|
|
||||||
|
`ClientAddressProcessor::linkParent` — idem avec `$address->setClient($client);`.
|
||||||
|
`ClientRibProcessor::linkParent` — idem avec `$rib->setClient($client);`.
|
||||||
|
|
||||||
|
- [ ] **Step 3 : Lancer les tests, vérifier qu'ils passent**
|
||||||
|
|
||||||
|
Run : `make test`
|
||||||
|
Expected : les 2 tests de Task 1 PASSENT (201 + 422 `propertyPath=email`). Aucun test existant cassé (notamment `testPostContactInvalidEmailReturns422WithFrenchMessageOnField` et les tests d'archi ERP-107 restent verts).
|
||||||
|
|
||||||
|
- [ ] **Step 4 : Lint PHP**
|
||||||
|
|
||||||
|
Run : `make php-cs-fixer-allow-risky`
|
||||||
|
Expected : 0 fichier à corriger (ou corrections appliquées et re-vérifiées).
|
||||||
|
|
||||||
|
- [ ] **Step 5 : Commit (fix back)**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/Module/Commercial/Domain/Entity/ClientContact.php src/Module/Commercial/Domain/Entity/ClientAddress.php src/Module/Commercial/Domain/Entity/ClientRib.php src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientContactProcessor.php src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientAddressProcessor.php src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientRibProcessor.php
|
||||||
|
git commit -m "fix(commercial) : POST sous-ressource client en read:false + parent 404 (corrige 500 NonUniqueResult, ERP-107)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 3 : Back — germes adresses + RIB (verrouille les 3 sous-ressources)
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php` (helpers `seedAddress`, `seedRib`)
|
||||||
|
- Test: `tests/Module/Commercial/Api/ClientSubResourceApiTest.php`
|
||||||
|
|
||||||
|
- [ ] **Step 1 : Helpers de seed adresse + RIB**
|
||||||
|
|
||||||
|
Dans `AbstractCommercialApiTestCase.php`, ajouter :
|
||||||
|
|
||||||
|
```php
|
||||||
|
/** Seede une adresse minimale valide (RG : CP/ville/rue requis). */
|
||||||
|
protected function seedAddress(ClientEntity $client, string $city): \App\Module\Commercial\Domain\Entity\ClientAddress
|
||||||
|
{
|
||||||
|
$em = $this->getEm();
|
||||||
|
$address = new \App\Module\Commercial\Domain\Entity\ClientAddress();
|
||||||
|
$address->setClient($client);
|
||||||
|
$address->setPostalCode('33000');
|
||||||
|
$address->setCity($city);
|
||||||
|
$address->setStreet('1 rue du Test');
|
||||||
|
$em->persist($address);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
return $address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Seede un RIB valide (BIC/IBAN conformes). */
|
||||||
|
protected function seedRib(ClientEntity $client, string $label): \App\Module\Commercial\Domain\Entity\ClientRib
|
||||||
|
{
|
||||||
|
$em = $this->getEm();
|
||||||
|
$rib = new \App\Module\Commercial\Domain\Entity\ClientRib();
|
||||||
|
$rib->setClient($client);
|
||||||
|
$rib->setLabel($label);
|
||||||
|
$rib->setBic('BNPAFRPPXXX');
|
||||||
|
$rib->setIban('FR1420041010050500013M02606');
|
||||||
|
$em->persist($rib);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
return $rib;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> Note : si une propriété est non-nullable et absente ci-dessus (ex. `position`, flags d'adresse), poser les setters correspondants avec une valeur par défaut neutre — vérifier les entités `ClientAddress` / `ClientRib` au moment de l'écriture.
|
||||||
|
|
||||||
|
- [ ] **Step 2 : Tests de non-régression adresses + RIB**
|
||||||
|
|
||||||
|
Dans `ClientSubResourceApiTest.php`, section adresses puis RIB :
|
||||||
|
|
||||||
|
```php
|
||||||
|
public function testPostAddressOnClientWithTwoExistingAddressesReturns201(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Addr Multi');
|
||||||
|
$this->seedAddress($seed, 'Bordeaux');
|
||||||
|
$this->seedAddress($seed, 'Lyon');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/'.$seed->getId().'/addresses', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['postalCode' => '75001', 'city' => 'Paris', 'street' => '2 rue Neuve'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPostRibOnClientWithTwoExistingRibsReturns201(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Rib Multi');
|
||||||
|
$this->seedRib($seed, 'Compte 1');
|
||||||
|
$this->seedRib($seed, 'Compte 2');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/'.$seed->getId().'/ribs', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['label' => 'Compte 3', 'bic' => self::VALID_BIC, 'iban' => self::VALID_IBAN],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(201);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> Le POST RIB exige `commercial.clients.accounting.manage` — `admin` (ROLE_ADMIN) l'a. Si une 403 apparaît, vérifier le compte de test.
|
||||||
|
|
||||||
|
- [ ] **Step 3 : Lancer, vérifier vert**
|
||||||
|
|
||||||
|
Run : `make test`
|
||||||
|
Expected : PASS (les 2 nouveaux tests verts grâce au fix de Task 2).
|
||||||
|
|
||||||
|
- [ ] **Step 4 : Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php tests/Module/Commercial/Api/ClientSubResourceApiTest.php
|
||||||
|
git commit -m "test(commercial) : verrouille POST adresses/RIB sur client peuple (ERP-107)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 4 : Front — helper `submitRows` + test unitaire
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `frontend/modules/commercial/composables/useClientFormErrors.ts`
|
||||||
|
- Create: `frontend/modules/commercial/composables/__tests__/useClientFormErrors.spec.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1 : Écrire le test rouge**
|
||||||
|
|
||||||
|
Créer `useClientFormErrors.spec.ts` :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { describe, it, expect, vi } from 'vitest'
|
||||||
|
import { useClientFormErrors } from '../useClientFormErrors'
|
||||||
|
|
||||||
|
// Construit une erreur facon useApi : 422 avec violations Hydra.
|
||||||
|
function http422(path: string, message: string) {
|
||||||
|
return { response: { status: 422, _data: { violations: [{ propertyPath: path, message }] } } }
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('useClientFormErrors.submitRows', () => {
|
||||||
|
it('tente TOUS les blocs et mappe les erreurs par index, sans stopper au premier echec', async () => {
|
||||||
|
const { contactErrors, submitRows } = useClientFormErrors()
|
||||||
|
const seen: number[] = []
|
||||||
|
const onUnmapped = vi.fn()
|
||||||
|
|
||||||
|
const saveRow = async (_row: unknown, index: number) => {
|
||||||
|
seen.push(index)
|
||||||
|
if (index === 1) throw http422('email', 'Email invalide')
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasError = await submitRows(
|
||||||
|
[{ a: 0 }, { a: 1 }, { a: 2 }],
|
||||||
|
contactErrors,
|
||||||
|
saveRow,
|
||||||
|
onUnmapped,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(seen).toEqual([0, 1, 2]) // tous les blocs tentes
|
||||||
|
expect(hasError).toBe(true)
|
||||||
|
expect(contactErrors.value[1]).toEqual({ email: 'Email invalide' })
|
||||||
|
expect(contactErrors.value[0]).toBeUndefined()
|
||||||
|
expect(onUnmapped).not.toHaveBeenCalled() // 422 mappee, pas de fallback
|
||||||
|
})
|
||||||
|
|
||||||
|
it('saute les lignes filtrees par shouldSkip et renvoie false si tout passe', async () => {
|
||||||
|
const { contactErrors, submitRows } = useClientFormErrors()
|
||||||
|
const saved: number[] = []
|
||||||
|
|
||||||
|
const hasError = await submitRows(
|
||||||
|
[{ skip: true }, { skip: false }],
|
||||||
|
contactErrors,
|
||||||
|
async (_row, index) => { saved.push(index) },
|
||||||
|
vi.fn(),
|
||||||
|
(row: { skip: boolean }) => row.skip,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(saved).toEqual([1])
|
||||||
|
expect(hasError).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2 : Lancer, vérifier l'échec**
|
||||||
|
|
||||||
|
Run : `make nuxt-test` (ou ciblé : `docker exec <node> npx vitest run useClientFormErrors`)
|
||||||
|
Expected : FAIL — `submitRows` n'existe pas encore.
|
||||||
|
|
||||||
|
- [ ] **Step 3 : Implémenter `submitRows`**
|
||||||
|
|
||||||
|
Dans `useClientFormErrors.ts`, ajouter la méthode (dans la fonction, après `mapRowError`) et l'exposer dans le `return` :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
/**
|
||||||
|
* Soumet TOUS les blocs d'une collection (contacts/adresses/RIB) en collectant
|
||||||
|
* les erreurs par index : on n'arrete PAS au premier bloc en echec (ERP-101).
|
||||||
|
* Reinitialise le tableau d'erreurs cible, tente chaque ligne via `saveRow`,
|
||||||
|
* mappe les 422 inline (mapRowError) ou delegue le fallback a `onUnmappedError`.
|
||||||
|
* Retourne true si au moins un bloc a echoue (le caller ne valide alors pas l'onglet).
|
||||||
|
*/
|
||||||
|
async function submitRows<T>(
|
||||||
|
rows: T[],
|
||||||
|
target: Ref<Record<string, string>[]>,
|
||||||
|
saveRow: (row: T, index: number) => Promise<void>,
|
||||||
|
onUnmappedError: (error: unknown, index: number) => void,
|
||||||
|
shouldSkip?: (row: T, index: number) => boolean,
|
||||||
|
): Promise<boolean> {
|
||||||
|
target.value = []
|
||||||
|
let hasError = false
|
||||||
|
for (let index = 0; index < rows.length; index++) {
|
||||||
|
if (shouldSkip?.(rows[index], index)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await saveRow(rows[index], index)
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (!mapRowError(error, target, index)) {
|
||||||
|
onUnmappedError(error, index)
|
||||||
|
}
|
||||||
|
hasError = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasError
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Ajouter `submitRows` à l'objet retourné par `useClientFormErrors`.
|
||||||
|
|
||||||
|
- [ ] **Step 4 : Lancer, vérifier vert**
|
||||||
|
|
||||||
|
Run : `make nuxt-test`
|
||||||
|
Expected : PASS (les 2 cas verts).
|
||||||
|
|
||||||
|
- [ ] **Step 5 : Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add frontend/modules/commercial/composables/useClientFormErrors.ts frontend/modules/commercial/composables/__tests__/useClientFormErrors.spec.ts
|
||||||
|
git commit -m "feat(commercial) : submitRows collecte les erreurs de tous les blocs de collection (ERP-101)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 5 : Front — brancher `submitRows` dans new.vue + edit.vue
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `frontend/modules/commercial/pages/clients/new.vue` (`submitContacts`, `submitAddresses`, boucle RIB de `submitAccounting`)
|
||||||
|
- Modify: `frontend/modules/commercial/pages/clients/[id]/edit.vue` (les 3 équivalents)
|
||||||
|
|
||||||
|
- [ ] **Step 1 : Récupérer `submitRows` du composable**
|
||||||
|
|
||||||
|
Dans `new.vue` ET `edit.vue`, ajouter `submitRows` à la déstructuration de `useClientFormErrors()` :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const {
|
||||||
|
mainErrors,
|
||||||
|
informationErrors,
|
||||||
|
accountingErrors,
|
||||||
|
contactErrors,
|
||||||
|
addressErrors,
|
||||||
|
ribErrors,
|
||||||
|
mapRowError,
|
||||||
|
submitRows,
|
||||||
|
} = useClientFormErrors()
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2 : Réécrire `submitContacts` (new.vue)**
|
||||||
|
|
||||||
|
Remplacer le corps de la boucle par un appel à `submitRows` :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
async function submitContacts(): Promise<void> {
|
||||||
|
if (clientId.value === null || !canValidateContacts.value || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
try {
|
||||||
|
const hasError = await submitRows(
|
||||||
|
contacts.value,
|
||||||
|
contactErrors,
|
||||||
|
async (contact) => {
|
||||||
|
const body = {
|
||||||
|
firstName: contact.firstName || null,
|
||||||
|
lastName: contact.lastName || null,
|
||||||
|
jobTitle: contact.jobTitle || null,
|
||||||
|
phonePrimary: contact.phonePrimary || null,
|
||||||
|
phoneSecondary: contact.hasSecondaryPhone ? (contact.phoneSecondary || null) : null,
|
||||||
|
email: contact.email || null,
|
||||||
|
}
|
||||||
|
if (contact.id === null) {
|
||||||
|
const created = await api.post<ContactResponse>(
|
||||||
|
`/clients/${clientId.value}/contacts`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
contact.id = created.id
|
||||||
|
contact.iri = created['@id'] ?? null
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(error) => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
(contact) => !isContactNamed(contact),
|
||||||
|
)
|
||||||
|
if (hasError) return
|
||||||
|
completeTab('contact')
|
||||||
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3 : Réécrire `submitAddresses` (new.vue)**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
async function submitAddresses(): Promise<void> {
|
||||||
|
if (clientId.value === null || !canValidateAddresses.value || tabSubmitting.value) return
|
||||||
|
tabSubmitting.value = true
|
||||||
|
try {
|
||||||
|
const hasError = await submitRows(
|
||||||
|
addresses.value,
|
||||||
|
addressErrors,
|
||||||
|
async (address) => {
|
||||||
|
const body = {
|
||||||
|
isProspect: address.isProspect,
|
||||||
|
isDelivery: address.isDelivery,
|
||||||
|
isBilling: address.isBilling,
|
||||||
|
country: address.country,
|
||||||
|
postalCode: address.postalCode || null,
|
||||||
|
city: address.city || null,
|
||||||
|
street: address.street || null,
|
||||||
|
streetComplement: address.streetComplement || null,
|
||||||
|
categories: address.categoryIris,
|
||||||
|
sites: address.siteIris,
|
||||||
|
contacts: address.contactIris,
|
||||||
|
billingEmail: isBillingEmailRequired(address) ? (address.billingEmail || null) : null,
|
||||||
|
}
|
||||||
|
if (address.id === null) {
|
||||||
|
const created = await api.post<{ id: number }>(
|
||||||
|
`/clients/${clientId.value}/addresses`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
address.id = created.id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(error) => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
)
|
||||||
|
if (hasError) return
|
||||||
|
completeTab('address')
|
||||||
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
tabSubmitting.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4 : Réécrire la boucle RIB de `submitAccounting` (new.vue)**
|
||||||
|
|
||||||
|
Garder le PATCH scalaire inchangé (1) ; remplacer la boucle (2) :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// 2) POST/PATCH des RIB (erreurs inline par ligne, tous les blocs).
|
||||||
|
const ribHasError = await submitRows(
|
||||||
|
ribs.value,
|
||||||
|
ribErrors,
|
||||||
|
async (rib) => {
|
||||||
|
const body = { label: rib.label, bic: rib.bic, iban: rib.iban }
|
||||||
|
if (rib.id === null) {
|
||||||
|
const created = await api.post<{ id: number }>(
|
||||||
|
`/clients/${clientId.value}/ribs`,
|
||||||
|
body,
|
||||||
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
|
)
|
||||||
|
rib.id = created.id
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(error) => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
(rib) => !ribIsComplete(rib),
|
||||||
|
)
|
||||||
|
if (ribHasError) return
|
||||||
|
|
||||||
|
completeTab('accounting')
|
||||||
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
|
```
|
||||||
|
|
||||||
|
> Retirer le `ribErrors.value = []` désormais fait par `submitRows`. Le `accountingErrors.clearErrors()` du PATCH scalaire reste.
|
||||||
|
|
||||||
|
- [ ] **Step 5 : Mirror dans edit.vue**
|
||||||
|
|
||||||
|
Appliquer les mêmes réécritures aux `submitContacts` / `submitAddresses` / boucle RIB de `submitAccounting` d'`edit.vue`. Conserver le **fallback d'erreur propre à edit.vue** (si edit.vue utilise `showError(...)` au lieu de `toast.error(...)`, passer ce fallback comme `onUnmappedError`). Vérifier les noms des refs (`clientId` peut y être l'id de route).
|
||||||
|
|
||||||
|
- [ ] **Step 6 : Vérifier le typecheck + tests front**
|
||||||
|
|
||||||
|
Run : `make nuxt-test`
|
||||||
|
Expected : PASS. Aucune régression des specs existantes (`ClientContactBlock.spec.ts`, etc.).
|
||||||
|
|
||||||
|
- [ ] **Step 7 : Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add frontend/modules/commercial/pages/clients/new.vue "frontend/modules/commercial/pages/clients/[id]/edit.vue"
|
||||||
|
git commit -m "feat(commercial) : valide tous les blocs contacts/adresses/RIB et affiche les erreurs par bloc (ERP-101)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 6 : Vérification finale + golden path manuel
|
||||||
|
|
||||||
|
- [ ] **Step 1 : Suite complète back**
|
||||||
|
|
||||||
|
Run : `make test` puis `make php-cs-fixer-allow-risky`
|
||||||
|
Expected : tout vert, 0 fichier à corriger.
|
||||||
|
|
||||||
|
- [ ] **Step 2 : Suite complète front**
|
||||||
|
|
||||||
|
Run : `make nuxt-test`
|
||||||
|
Expected : tout vert.
|
||||||
|
|
||||||
|
- [ ] **Step 3 : Golden path manuel (`make dev-nuxt`, port 3004)**
|
||||||
|
|
||||||
|
Scénario : ouvrir un client à 3 contacts (compte `admin`), onglet Contacts, ajouter un bloc avec email invalide + un autre bloc avec prénom/nom vides → Valider.
|
||||||
|
Attendu : **pas de 500** ; « L'adresse email n'est pas valide. » sous l'email du bon bloc ET « Le prénom ou le nom du contact est obligatoire. » sous le prénom de l'autre bloc, **affichés simultanément**. L'onglet ne se valide pas tant qu'une erreur subsiste. Idem à vérifier rapidement sur Adresses et RIB.
|
||||||
|
|
||||||
|
- [ ] **Step 4 : Si une vérif échoue ou ne peut être lancée, le dire explicitement** (ne pas annoncer « fini »).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Self-review (auteur du plan)
|
||||||
|
|
||||||
|
- **Couverture spec §3.1 (back)** : Task 2 (read:false + linkParent 404) ✓ ; §3.2 (front collect-all) : Tasks 4-5 ✓ ; §3.3 (helper réutilisable) : Task 4 `submitRows` ✓ ; §4 tests : Tasks 1, 3 (back), 4 (front) + Task 6 golden path ✓.
|
||||||
|
- **Périmètre 3 sous-ressources** : contacts (Task 1-2), adresses + RIB (Task 3 + branchements Task 5) ✓.
|
||||||
|
- **Décision « inline seul »** : aucun toast succès si `hasError` ; pas de toast récap ✓.
|
||||||
|
- **Pas de placeholder** : le seul point ouvert est la note Task 3 Step 1 (setters non-nullables éventuels d'adresse/RIB à compléter en lisant les entités) — à lever à l'écriture. Cohérence des noms : `submitRows` utilisé identiquement en Task 4 et Task 5.
|
||||||
@@ -883,6 +883,7 @@ Cf. § 2.6. Pattern Shared standard.
|
|||||||
|
|
||||||
### Onglet Comptabilité
|
### Onglet Comptabilité
|
||||||
|
|
||||||
|
- **RG-1.30** _(ajoutée — correctif incohérence spec-front/spec-back)_ : à la **validation complète de l'onglet Comptabilité**, les six champs scalaires `siren`, `accountNumber`, `tvaMode`, `nTva`, `paymentDelay`, `paymentType` sont **obligatoires** (alignement sur spec-front § Onglet Comptabilité). Colonnes `nullable` en base (l'onglet est rempli dans un second temps, et l'onglet principal ne les envoie pas) + validateur contextuel `ClientAccountingCompletenessValidator` invoqué par le `ClientProcessor` — même parti que RG-1.04 (Information). Déclenchement : uniquement quand **les six champs sont présents dans le payload** (le front les envoie toujours ensemble via « Valider ») ; un PATCH ciblant un sous-ensemble de champs comptables (édition ponctuelle) n'est pas soumis à la complétude. Chaque champ manquant → 422 sur son `propertyPath` (mapping inline front, ERP-101). `bank` reste hors complétude (conditionnel RG-1.12).
|
||||||
- **RG-1.12** : Le champ `bank` est visible et obligatoire **uniquement** si `paymentType.code = 'VIREMENT'`. Validation server-side dans le `ClientProcessor` : si `payment_type.code = VIREMENT` et `bank IS NULL` → 422.
|
- **RG-1.12** : Le champ `bank` est visible et obligatoire **uniquement** si `paymentType.code = 'VIREMENT'`. Validation server-side dans le `ClientProcessor` : si `payment_type.code = VIREMENT` et `bank IS NULL` → 422.
|
||||||
- **RG-1.13** : Les champs RIB (`label`, `bic`, `iban`) sont obligatoires si **au moins un bloc RIB est présent ET** `paymentType.code = 'LCR'`. C'est-à-dire :
|
- **RG-1.13** : Les champs RIB (`label`, `bic`, `iban`) sont obligatoires si **au moins un bloc RIB est présent ET** `paymentType.code = 'LCR'`. C'est-à-dire :
|
||||||
- Si `paymentType.code = LCR` ET `client.ribs.count() = 0` → 422 « Au moins un RIB est obligatoire pour le type LCR ».
|
- Si `paymentType.code = LCR` ET `client.ribs.count() = 0` → 422 « Au moins un RIB est obligatoire pour le type LCR ».
|
||||||
|
|||||||
@@ -168,13 +168,18 @@
|
|||||||
"prospect": "Prospect",
|
"prospect": "Prospect",
|
||||||
"delivery": "Adresse de livraison",
|
"delivery": "Adresse de livraison",
|
||||||
"billing": "Facturation",
|
"billing": "Facturation",
|
||||||
|
"addressType": "Type d'adresse",
|
||||||
|
"addressTypeProspect": "Prospect",
|
||||||
|
"addressTypeDelivery": "Livraison",
|
||||||
|
"addressTypeBilling": "Facturation",
|
||||||
|
"addressTypeDeliveryBilling": "Adresse + Facturation",
|
||||||
"categories": "Catégorie",
|
"categories": "Catégorie",
|
||||||
"country": "Pays",
|
"country": "Pays",
|
||||||
"postalCode": "Code postal",
|
"postalCode": "Code postal",
|
||||||
"city": "Ville",
|
"city": "Ville",
|
||||||
"street": "Adresse",
|
"street": "Adresse",
|
||||||
"streetComplement": "Adresse complémentaire",
|
"streetComplement": "Adresse complémentaire",
|
||||||
"sites": "Sites Starseed",
|
"sites": "Sites",
|
||||||
"contacts": "Contact(s) rattaché(s)",
|
"contacts": "Contact(s) rattaché(s)",
|
||||||
"billingEmail": "Email de facturation",
|
"billingEmail": "Email de facturation",
|
||||||
"remove": "Supprimer l'adresse",
|
"remove": "Supprimer l'adresse",
|
||||||
|
|||||||
@@ -10,34 +10,53 @@
|
|||||||
@click="$emit('remove')"
|
@click="$emit('remove')"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<!-- Usage de l'adresse : Prospect exclusif de Livraison/Facturation
|
<!-- Usage de l'adresse : Select unique (plus simple pour l'utilisateur)
|
||||||
(RG-1.06/07/08). L'exclusivite est appliquee au toggle (cocher l'un
|
remplacant les 3 cases. Les options encodent les combinaisons valides
|
||||||
decoche l'autre) plutot qu'en masquant les options. -->
|
(exclusivite Prospect, RG-1.06/07/08) ; le back recoit toujours les
|
||||||
<MalioCheckbox
|
drapeaux isProspect / isDelivery / isBilling (aucune RG modifiee). -->
|
||||||
:model-value="model.isProspect"
|
<MalioSelect
|
||||||
:label="t('commercial.clients.form.address.prospect')"
|
:model-value="addressType"
|
||||||
group-class="self-center"
|
:options="addressTypeOptions"
|
||||||
|
:label="t('commercial.clients.form.address.addressType')"
|
||||||
:readonly="readonly"
|
:readonly="readonly"
|
||||||
@update:model-value="(v: boolean) => toggleFlag('isProspect', v)"
|
:required="true"
|
||||||
/>
|
@update:model-value="onAddressTypeChange"
|
||||||
<MalioCheckbox
|
|
||||||
:model-value="model.isDelivery"
|
|
||||||
:label="t('commercial.clients.form.address.delivery')"
|
|
||||||
group-class="self-center"
|
|
||||||
:readonly="readonly"
|
|
||||||
@update:model-value="(v: boolean) => toggleFlag('isDelivery', v)"
|
|
||||||
/>
|
|
||||||
<MalioCheckbox
|
|
||||||
:model-value="model.isBilling"
|
|
||||||
:label="t('commercial.clients.form.address.billing')"
|
|
||||||
group-class="self-center"
|
|
||||||
:readonly="readonly"
|
|
||||||
@update:model-value="(v: boolean) => toggleFlag('isBilling', v)"
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<!-- Cellule vide : laisse un trou en position 4 (ligne 1) pour que
|
<!-- Sites Starseed : multiselect a tags (>= 1 obligatoire, RG-1.10). -->
|
||||||
Categorie reparte au debut de la ligne suivante. -->
|
<MalioSelectCheckbox
|
||||||
<div aria-hidden="true" />
|
:model-value="model.siteIris"
|
||||||
|
:options="siteOptions"
|
||||||
|
:label="t('commercial.clients.form.address.sites')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="readonly"
|
||||||
|
:required="true"
|
||||||
|
@update:model-value="(v: (string | number)[]) => update('siteIris', v.map(String))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<MalioSelectCheckbox
|
||||||
|
:model-value="model.contactIris"
|
||||||
|
:options="contactOptions"
|
||||||
|
:label="t('commercial.clients.form.address.contacts')"
|
||||||
|
:display-tag="true"
|
||||||
|
:readonly="readonly"
|
||||||
|
@update:model-value="(v: (string | number)[]) => update('contactIris', v.map(String))"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<!-- Email de facturation : ligne 1 colonne 4, visible/obligatoire
|
||||||
|
seulement si Facturation (RG-1.11). Sinon un filler comble la
|
||||||
|
colonne pour que Categorie reparte au debut de la ligne 2. -->
|
||||||
|
<MalioInputEmail
|
||||||
|
v-if="isBillingEmailRequired(model)"
|
||||||
|
:model-value="model.billingEmail"
|
||||||
|
:label="t('commercial.clients.form.address.billingEmail')"
|
||||||
|
:required="true"
|
||||||
|
:readonly="readonly"
|
||||||
|
:lowercase="true"
|
||||||
|
:error="errors?.billingEmail"
|
||||||
|
@update:model-value="(v: string) => update('billingEmail', v)"
|
||||||
|
/>
|
||||||
|
<div v-else aria-hidden="true" />
|
||||||
|
|
||||||
<MalioSelectCheckbox
|
<MalioSelectCheckbox
|
||||||
:model-value="model.categoryIris"
|
:model-value="model.categoryIris"
|
||||||
@@ -134,47 +153,15 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Sites Starseed : cases a cocher inline (>= 1 obligatoire, RG-1.10). -->
|
|
||||||
<div class="flex justify-between">
|
|
||||||
<MalioCheckbox
|
|
||||||
v-for="site in siteOptions"
|
|
||||||
:key="site.value"
|
|
||||||
:model-value="model.siteIris.includes(site.value)"
|
|
||||||
:label="site.label"
|
|
||||||
group-class="w-auto self-center"
|
|
||||||
:readonly="readonly"
|
|
||||||
@update:model-value="(v: boolean) => toggleSite(site.value, v)"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<MalioSelectCheckbox
|
|
||||||
:model-value="model.contactIris"
|
|
||||||
:options="contactOptions"
|
|
||||||
:label="t('commercial.clients.form.address.contacts')"
|
|
||||||
:display-tag="true"
|
|
||||||
:readonly="readonly"
|
|
||||||
@update:model-value="(v: (string | number)[]) => update('contactIris', v.map(String))"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<!-- Email de facturation : visible/obligatoire seulement si Facturation
|
|
||||||
est coche (RG-1.11). -->
|
|
||||||
<MalioInputText
|
|
||||||
v-if="isBillingEmailRequired(model)"
|
|
||||||
:model-value="model.billingEmail"
|
|
||||||
:label="t('commercial.clients.form.address.billingEmail')"
|
|
||||||
:required="true"
|
|
||||||
:readonly="readonly"
|
|
||||||
:error="errors?.billingEmail"
|
|
||||||
@update:model-value="(v: string) => update('billingEmail', v)"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {
|
import {
|
||||||
applyProspectExclusivity,
|
addressFlagsFromType,
|
||||||
|
addressTypeFromFlags,
|
||||||
isBillingEmailRequired,
|
isBillingEmailRequired,
|
||||||
type AddressFlagsDraft,
|
type AddressType,
|
||||||
} from '~/modules/commercial/utils/clientFormRules'
|
} from '~/modules/commercial/utils/clientFormRules'
|
||||||
import { useAddressAutocomplete, type AddressSuggestion } from '~/shared/composables/useAddressAutocomplete'
|
import { useAddressAutocomplete, type AddressSuggestion } from '~/shared/composables/useAddressAutocomplete'
|
||||||
import type { CategoryOption, RefOption } from '~/modules/commercial/composables/useClientReferentials'
|
import type { CategoryOption, RefOption } from '~/modules/commercial/composables/useClientReferentials'
|
||||||
@@ -213,6 +200,23 @@ const autocomplete = useAddressAutocomplete()
|
|||||||
|
|
||||||
const model = computed(() => props.modelValue)
|
const model = computed(() => props.modelValue)
|
||||||
|
|
||||||
|
// Type d'adresse (Select unique) derive des drapeaux back. null tant qu'aucun
|
||||||
|
// drapeau n'est pose -> champ vide + bouton « Valider » bloque (cf. parent).
|
||||||
|
const addressType = computed<AddressType | null>(() => addressTypeFromFlags(model.value))
|
||||||
|
|
||||||
|
const addressTypeOptions = computed<RefOption[]>(() => [
|
||||||
|
{ value: 'prospect', label: t('commercial.clients.form.address.addressTypeProspect') },
|
||||||
|
{ value: 'delivery', label: t('commercial.clients.form.address.addressTypeDelivery') },
|
||||||
|
{ value: 'billing', label: t('commercial.clients.form.address.addressTypeBilling') },
|
||||||
|
{ value: 'delivery_billing', label: t('commercial.clients.form.address.addressTypeDeliveryBilling') },
|
||||||
|
])
|
||||||
|
|
||||||
|
/** Applique le type choisi en repercutant les 3 drapeaux back (immutabilite). */
|
||||||
|
function onAddressTypeChange(value: string | number | null): void {
|
||||||
|
if (value === null) return
|
||||||
|
emit('update:modelValue', { ...props.modelValue, ...addressFlagsFromType(value as AddressType) })
|
||||||
|
}
|
||||||
|
|
||||||
// Mode degrade : service BAN indisponible → Ville/Adresse en saisie libre.
|
// Mode degrade : service BAN indisponible → Ville/Adresse en saisie libre.
|
||||||
const degraded = ref(false)
|
const degraded = ref(false)
|
||||||
// Villes proposees par la BAN (alimentees a la saisie du code postal).
|
// Villes proposees par la BAN (alimentees a la saisie du code postal).
|
||||||
@@ -254,25 +258,6 @@ function update<K extends keyof AddressFormDraft>(field: K, value: AddressFormDr
|
|||||||
emit('update:modelValue', { ...props.modelValue, [field]: value })
|
emit('update:modelValue', { ...props.modelValue, [field]: value })
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Coche/decoche un site Starseed rattache a l'adresse (M2M par IRI, RG-1.10). */
|
|
||||||
function toggleSite(siteIri: string, selected: boolean): void {
|
|
||||||
const current = props.modelValue.siteIris
|
|
||||||
const next = selected
|
|
||||||
? [...current, siteIri]
|
|
||||||
: current.filter(iri => iri !== siteIri)
|
|
||||||
update('siteIris', next)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Applique l'exclusivite Prospect / (Livraison|Facturation) au changement. */
|
|
||||||
function toggleFlag(field: keyof AddressFlagsDraft, value: boolean): void {
|
|
||||||
const flags = applyProspectExclusivity(
|
|
||||||
{ isProspect: model.value.isProspect, isDelivery: model.value.isDelivery, isBilling: model.value.isBilling },
|
|
||||||
field,
|
|
||||||
value,
|
|
||||||
)
|
|
||||||
emit('update:modelValue', { ...props.modelValue, ...flags })
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Bascule définitivement en mode degrade et previent le parent (toast unique). */
|
/** Bascule définitivement en mode degrade et previent le parent (toast unique). */
|
||||||
function enterDegraded(): void {
|
function enterDegraded(): void {
|
||||||
if (!degraded.value) {
|
if (!degraded.value) {
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
:model-value="model.email"
|
:model-value="model.email"
|
||||||
:label="t('commercial.clients.form.contact.email')"
|
:label="t('commercial.clients.form.contact.email')"
|
||||||
:readonly="readonly"
|
:readonly="readonly"
|
||||||
|
:lowercase="true"
|
||||||
:error="errors?.email"
|
:error="errors?.email"
|
||||||
@update:model-value="(v: string) => update('email', v)"
|
@update:model-value="(v: string) => update('email', v)"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -52,3 +52,71 @@ describe('useClientFormErrors', () => {
|
|||||||
expect(f.addressErrors.value[0]).toBeUndefined()
|
expect(f.addressErrors.value[0]).toBeUndefined()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Construit une erreur facon useApi : 422 avec violations Hydra.
|
||||||
|
function http422(path: string, message: string) {
|
||||||
|
return { response: { status: 422, _data: { violations: [{ propertyPath: path, message }] } } }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `submitRows` factorise la soumission d'une collection de blocs (contacts /
|
||||||
|
* adresses / RIB) : on tente TOUS les blocs et on collecte les erreurs par index
|
||||||
|
* sans stopper au premier echec (ERP-110 / ERP-101).
|
||||||
|
*/
|
||||||
|
describe('useClientFormErrors.submitRows', () => {
|
||||||
|
it('tente TOUS les blocs et mappe les erreurs par index, sans stopper au premier echec', async () => {
|
||||||
|
const { contactErrors, submitRows } = useClientFormErrors()
|
||||||
|
const seen: number[] = []
|
||||||
|
const onUnmapped = vi.fn()
|
||||||
|
|
||||||
|
const saveRow = async (_row: unknown, index: number) => {
|
||||||
|
seen.push(index)
|
||||||
|
if (index === 1) throw http422('email', 'Email invalide')
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasError = await submitRows(
|
||||||
|
[{ a: 0 }, { a: 1 }, { a: 2 }],
|
||||||
|
contactErrors,
|
||||||
|
saveRow,
|
||||||
|
onUnmapped,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(seen).toEqual([0, 1, 2]) // tous les blocs tentes
|
||||||
|
expect(hasError).toBe(true)
|
||||||
|
expect(contactErrors.value[1]).toEqual({ email: 'Email invalide' })
|
||||||
|
expect(contactErrors.value[0]).toBeUndefined()
|
||||||
|
expect(onUnmapped).not.toHaveBeenCalled() // 422 mappee, pas de fallback
|
||||||
|
})
|
||||||
|
|
||||||
|
it('delegue le fallback onUnmappedError pour une erreur non mappable et marque hasError', async () => {
|
||||||
|
const { ribErrors, submitRows } = useClientFormErrors()
|
||||||
|
const onUnmapped = vi.fn()
|
||||||
|
|
||||||
|
const hasError = await submitRows(
|
||||||
|
[{ a: 0 }],
|
||||||
|
ribErrors,
|
||||||
|
async () => { throw { response: { status: 500, _data: {} } } },
|
||||||
|
onUnmapped,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(hasError).toBe(true)
|
||||||
|
expect(onUnmapped).toHaveBeenCalledTimes(1)
|
||||||
|
expect(ribErrors.value[0]).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('saute les lignes filtrees par shouldSkip et renvoie false si tout passe', async () => {
|
||||||
|
const { contactErrors, submitRows } = useClientFormErrors()
|
||||||
|
const saved: number[] = []
|
||||||
|
|
||||||
|
const hasError = await submitRows(
|
||||||
|
[{ skip: true }, { skip: false }],
|
||||||
|
contactErrors,
|
||||||
|
async (_row, index) => { saved.push(index) },
|
||||||
|
vi.fn(),
|
||||||
|
(row: { skip: boolean }) => row.skip,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(saved).toEqual([1])
|
||||||
|
expect(hasError).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -43,6 +43,44 @@ export function useClientFormErrors() {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Soumet TOUS les blocs d'une collection (contacts / adresses / RIB) en
|
||||||
|
* collectant les erreurs par index : on n'arrete PAS au premier bloc en echec
|
||||||
|
* (decision ERP-110 / ERP-101). Reinitialise le tableau d'erreurs cible, tente
|
||||||
|
* chaque ligne via `saveRow`, mappe les 422 inline (mapRowError) ou delegue le
|
||||||
|
* fallback a `onUnmappedError`. `shouldSkip` permet d'ignorer les blocs vides
|
||||||
|
* (non remplis). Retourne true si au moins un bloc a echoue (le caller ne valide
|
||||||
|
* alors pas l'onglet et n'affiche pas de toast succes).
|
||||||
|
*/
|
||||||
|
async function submitRows<T>(
|
||||||
|
rows: T[],
|
||||||
|
target: Ref<Record<string, string>[]>,
|
||||||
|
saveRow: (row: T, index: number) => Promise<void>,
|
||||||
|
onUnmappedError: (error: unknown, index: number) => void,
|
||||||
|
shouldSkip?: (row: T, index: number) => boolean,
|
||||||
|
): Promise<boolean> {
|
||||||
|
target.value = []
|
||||||
|
let hasError = false
|
||||||
|
for (let index = 0; index < rows.length; index++) {
|
||||||
|
// L'index reste borne par rows.length : la ligne existe forcement.
|
||||||
|
const row = rows[index] as T
|
||||||
|
if (shouldSkip?.(row, index)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await saveRow(row, index)
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (!mapRowError(error, target, index)) {
|
||||||
|
onUnmappedError(error, index)
|
||||||
|
}
|
||||||
|
hasError = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasError
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mainErrors,
|
mainErrors,
|
||||||
informationErrors,
|
informationErrors,
|
||||||
@@ -51,5 +89,6 @@ export function useClientFormErrors() {
|
|||||||
addressErrors,
|
addressErrors,
|
||||||
ribErrors,
|
ribErrors,
|
||||||
mapRowError,
|
mapRowError,
|
||||||
|
submitRows,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -410,11 +410,15 @@ import {
|
|||||||
type MainFormDraft,
|
type MainFormDraft,
|
||||||
} from '~/modules/commercial/utils/clientEdit'
|
} from '~/modules/commercial/utils/clientEdit'
|
||||||
import {
|
import {
|
||||||
|
addressTypeFromFlags,
|
||||||
buildClientFormTabKeys,
|
buildClientFormTabKeys,
|
||||||
|
hasAllRequiredAccountingFields,
|
||||||
hasAtLeastOneValidContact,
|
hasAtLeastOneValidContact,
|
||||||
isBankRequiredForPaymentType,
|
isBankRequiredForPaymentType,
|
||||||
isBillingEmailRequired,
|
isBillingEmailRequired,
|
||||||
|
isContactBlank,
|
||||||
isContactNamed,
|
isContactNamed,
|
||||||
|
isRibBlank,
|
||||||
isRibRequiredForPaymentType,
|
isRibRequiredForPaymentType,
|
||||||
} from '~/modules/commercial/utils/clientFormRules'
|
} from '~/modules/commercial/utils/clientFormRules'
|
||||||
import {
|
import {
|
||||||
@@ -628,7 +632,7 @@ const {
|
|||||||
contactErrors,
|
contactErrors,
|
||||||
addressErrors,
|
addressErrors,
|
||||||
ribErrors,
|
ribErrors,
|
||||||
mapRowError,
|
submitRows,
|
||||||
} = useClientFormErrors()
|
} = useClientFormErrors()
|
||||||
|
|
||||||
// ── Bloc principal ───────────────────────────────────────────────────────────
|
// ── Bloc principal ───────────────────────────────────────────────────────────
|
||||||
@@ -742,11 +746,14 @@ async function submitContacts(): Promise<void> {
|
|||||||
}
|
}
|
||||||
removedContactIds.value = []
|
removedContactIds.value = []
|
||||||
|
|
||||||
for (let index = 0; index < contacts.value.length; index++) {
|
// On tente TOUS les blocs (collecte des erreurs par index, ERP-110). Seuls
|
||||||
const contact = contacts.value[index]
|
// les blocs TOTALEMENT vides sont ignores : un bloc partiellement rempli
|
||||||
if (!isContactNamed(contact)) continue
|
// sans nom (email seul) est soumis -> 422 RG-1.05 inline sous le bloc.
|
||||||
|
const hasError = await submitRows(
|
||||||
|
contacts.value,
|
||||||
|
contactErrors,
|
||||||
|
async (contact) => {
|
||||||
const body = buildContactPayload(contact)
|
const body = buildContactPayload(contact)
|
||||||
try {
|
|
||||||
if (contact.id === null) {
|
if (contact.id === null) {
|
||||||
const created = await api.post<{ '@id'?: string, id: number }>(
|
const created = await api.post<{ '@id'?: string, id: number }>(
|
||||||
`/clients/${clientId}/contacts`,
|
`/clients/${clientId}/contacts`,
|
||||||
@@ -759,15 +766,15 @@ async function submitContacts(): Promise<void> {
|
|||||||
else {
|
else {
|
||||||
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
catch (error) {
|
error => showError(error),
|
||||||
// 422 → erreurs inline sous les champs de CETTE ligne ; on stoppe.
|
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||||
if (!mapRowError(error, contactErrors, index)) {
|
// bloc existant vide est soumis -> 422 RG-1.05 inline (sinon la modif
|
||||||
showError(error)
|
// serait perdue en silence avec un faux toast de succes).
|
||||||
}
|
contact => contact.id === null && isContactBlank(contact),
|
||||||
return
|
)
|
||||||
}
|
// Tant qu'un bloc reste en erreur : pas de toast succes.
|
||||||
}
|
if (hasError) return
|
||||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
@@ -783,7 +790,8 @@ const canValidateAddresses = computed(() =>
|
|||||||
addresses.value.length > 0
|
addresses.value.length > 0
|
||||||
&& addresses.value.every((a) => {
|
&& addresses.value.every((a) => {
|
||||||
const filledBillingEmail = a.billingEmail !== null && a.billingEmail.trim() !== ''
|
const filledBillingEmail = a.billingEmail !== null && a.billingEmail.trim() !== ''
|
||||||
return a.siteIris.length >= 1
|
return addressTypeFromFlags(a) !== null
|
||||||
|
&& a.siteIris.length >= 1
|
||||||
&& a.categoryIris.length >= 1
|
&& a.categoryIris.length >= 1
|
||||||
&& (!isBillingEmailRequired(a) || filledBillingEmail)
|
&& (!isBillingEmailRequired(a) || filledBillingEmail)
|
||||||
}),
|
}),
|
||||||
@@ -824,10 +832,12 @@ async function submitAddresses(): Promise<void> {
|
|||||||
}
|
}
|
||||||
removedAddressIds.value = []
|
removedAddressIds.value = []
|
||||||
|
|
||||||
for (let index = 0; index < addresses.value.length; index++) {
|
// On tente TOUS les blocs d'adresse (collecte des erreurs par index, ERP-110).
|
||||||
const address = addresses.value[index]
|
const hasError = await submitRows(
|
||||||
|
addresses.value,
|
||||||
|
addressErrors,
|
||||||
|
async (address) => {
|
||||||
const body = buildAddressPayload(address, isBillingEmailRequired(address))
|
const body = buildAddressPayload(address, isBillingEmailRequired(address))
|
||||||
try {
|
|
||||||
if (address.id === null) {
|
if (address.id === null) {
|
||||||
const created = await api.post<{ id: number }>(
|
const created = await api.post<{ id: number }>(
|
||||||
`/clients/${clientId}/addresses`,
|
`/clients/${clientId}/addresses`,
|
||||||
@@ -839,14 +849,10 @@ async function submitAddresses(): Promise<void> {
|
|||||||
else {
|
else {
|
||||||
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
catch (error) {
|
error => showError(error),
|
||||||
if (!mapRowError(error, addressErrors, index)) {
|
)
|
||||||
showError(error)
|
if (hasError) return
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
@@ -875,6 +881,7 @@ function ribIsComplete(rib: { label: string | null, bic: string | null, iban: st
|
|||||||
}
|
}
|
||||||
|
|
||||||
const canValidateAccounting = computed(() => {
|
const canValidateAccounting = computed(() => {
|
||||||
|
if (!hasAllRequiredAccountingFields(accounting)) return false
|
||||||
if (isBankRequired.value && accounting.bankIri === null) return false
|
if (isBankRequired.value && accounting.bankIri === null) return false
|
||||||
if (isRibRequired.value && !ribs.value.some(ribIsComplete)) return false
|
if (isRibRequired.value && !ribs.value.some(ribIsComplete)) return false
|
||||||
return true
|
return true
|
||||||
@@ -905,6 +912,9 @@ async function submitAccounting(): Promise<void> {
|
|||||||
if (accountingReadonly.value || !canValidateAccounting.value || tabSubmitting.value) return
|
if (accountingReadonly.value || !canValidateAccounting.value || tabSubmitting.value) return
|
||||||
tabSubmitting.value = true
|
tabSubmitting.value = true
|
||||||
accountingErrors.clearErrors()
|
accountingErrors.clearErrors()
|
||||||
|
// Reset des erreurs RIB des le debut : l'etape 1 (PATCH scalaires) peut
|
||||||
|
// echouer et `return` avant submitRows (qui porte sinon le reset), laissant
|
||||||
|
// des erreurs de RIB obsoletes affichees sous les blocs.
|
||||||
ribErrors.value = []
|
ribErrors.value = []
|
||||||
try {
|
try {
|
||||||
// 1) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
// 1) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
||||||
@@ -921,12 +931,14 @@ async function submitAccounting(): Promise<void> {
|
|||||||
}
|
}
|
||||||
removedRibIds.value = []
|
removedRibIds.value = []
|
||||||
|
|
||||||
// 2) POST/PATCH des RIB (erreurs inline par ligne).
|
// 2) POST/PATCH des RIB (erreurs inline par ligne, tous les blocs tentes).
|
||||||
for (let index = 0; index < ribs.value.length; index++) {
|
// Seuls les blocs RIB TOTALEMENT vides sont ignores : un RIB partiel (ex.
|
||||||
const rib = ribs.value[index]
|
// IBAN seul) est soumis -> 422 NotBlank (label / bic / iban) inline.
|
||||||
if (!ribIsComplete(rib)) continue
|
const ribHasError = await submitRows(
|
||||||
|
ribs.value,
|
||||||
|
ribErrors,
|
||||||
|
async (rib) => {
|
||||||
const body = buildRibPayload(rib)
|
const body = buildRibPayload(rib)
|
||||||
try {
|
|
||||||
if (rib.id === null) {
|
if (rib.id === null) {
|
||||||
const created = await api.post<{ id: number }>(
|
const created = await api.post<{ id: number }>(
|
||||||
`/clients/${clientId}/ribs`,
|
`/clients/${clientId}/ribs`,
|
||||||
@@ -938,14 +950,14 @@ async function submitAccounting(): Promise<void> {
|
|||||||
else {
|
else {
|
||||||
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
catch (error) {
|
error => showError(error),
|
||||||
if (!mapRowError(error, ribErrors, index)) {
|
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||||
showError(error)
|
// RIB existant vide est soumis -> 422 NotBlank inline (sinon la modif
|
||||||
}
|
// serait perdue en silence avec un faux toast de succes).
|
||||||
return
|
rib => rib.id === null && isRibBlank(rib),
|
||||||
}
|
)
|
||||||
}
|
if (ribHasError) return
|
||||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
|||||||
@@ -380,12 +380,16 @@ import { computed, onMounted, reactive, ref, watch } from 'vue'
|
|||||||
import { useClientReferentials, type RefOption } from '~/modules/commercial/composables/useClientReferentials'
|
import { useClientReferentials, type RefOption } from '~/modules/commercial/composables/useClientReferentials'
|
||||||
import { useClientFormErrors } from '~/modules/commercial/composables/useClientFormErrors'
|
import { useClientFormErrors } from '~/modules/commercial/composables/useClientFormErrors'
|
||||||
import {
|
import {
|
||||||
|
addressTypeFromFlags,
|
||||||
buildClientFormTabKeys,
|
buildClientFormTabKeys,
|
||||||
CLIENT_FORM_PLACEHOLDER_TABS,
|
CLIENT_FORM_PLACEHOLDER_TABS,
|
||||||
|
hasAllRequiredAccountingFields,
|
||||||
hasAtLeastOneValidContact,
|
hasAtLeastOneValidContact,
|
||||||
isBankRequiredForPaymentType,
|
isBankRequiredForPaymentType,
|
||||||
isBillingEmailRequired,
|
isBillingEmailRequired,
|
||||||
|
isContactBlank,
|
||||||
isContactNamed,
|
isContactNamed,
|
||||||
|
isRibBlank,
|
||||||
isRibRequiredForPaymentType,
|
isRibRequiredForPaymentType,
|
||||||
} from '~/modules/commercial/utils/clientFormRules'
|
} from '~/modules/commercial/utils/clientFormRules'
|
||||||
import {
|
import {
|
||||||
@@ -441,7 +445,7 @@ const {
|
|||||||
contactErrors,
|
contactErrors,
|
||||||
addressErrors,
|
addressErrors,
|
||||||
ribErrors,
|
ribErrors,
|
||||||
mapRowError,
|
submitRows,
|
||||||
} = useClientFormErrors()
|
} = useClientFormErrors()
|
||||||
|
|
||||||
useHead({ title: t('commercial.clients.form.title') })
|
useHead({ title: t('commercial.clients.form.title') })
|
||||||
@@ -676,13 +680,14 @@ function askRemoveContact(index: number): void {
|
|||||||
async function submitContacts(): Promise<void> {
|
async function submitContacts(): Promise<void> {
|
||||||
if (clientId.value === null || !canValidateContacts.value || tabSubmitting.value) return
|
if (clientId.value === null || !canValidateContacts.value || tabSubmitting.value) return
|
||||||
tabSubmitting.value = true
|
tabSubmitting.value = true
|
||||||
contactErrors.value = []
|
|
||||||
try {
|
try {
|
||||||
for (let index = 0; index < contacts.value.length; index++) {
|
// On tente TOUS les blocs (collecte des erreurs par index, ERP-110). Seuls
|
||||||
const contact = contacts.value[index]
|
// les blocs TOTALEMENT vides sont ignores : un bloc partiellement rempli
|
||||||
// On ignore les blocs totalement vides (ni nom ni prenom).
|
// sans nom (email seul) est soumis -> 422 RG-1.05 inline sous le bloc.
|
||||||
if (!isContactNamed(contact)) continue
|
const hasError = await submitRows(
|
||||||
|
contacts.value,
|
||||||
|
contactErrors,
|
||||||
|
async (contact) => {
|
||||||
const body = {
|
const body = {
|
||||||
firstName: contact.firstName || null,
|
firstName: contact.firstName || null,
|
||||||
lastName: contact.lastName || null,
|
lastName: contact.lastName || null,
|
||||||
@@ -691,8 +696,6 @@ async function submitContacts(): Promise<void> {
|
|||||||
phoneSecondary: contact.hasSecondaryPhone ? (contact.phoneSecondary || null) : null,
|
phoneSecondary: contact.hasSecondaryPhone ? (contact.phoneSecondary || null) : null,
|
||||||
email: contact.email || null,
|
email: contact.email || null,
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
if (contact.id === null) {
|
if (contact.id === null) {
|
||||||
const created = await api.post<ContactResponse>(
|
const created = await api.post<ContactResponse>(
|
||||||
`/clients/${clientId.value}/contacts`,
|
`/clients/${clientId.value}/contacts`,
|
||||||
@@ -705,16 +708,15 @@ async function submitContacts(): Promise<void> {
|
|||||||
else {
|
else {
|
||||||
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
catch (error) {
|
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
// 422 → erreurs inline sous les champs de CETTE ligne ; on stoppe
|
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||||
// a la premiere ligne en echec (les suivantes ne sont pas tentees).
|
// bloc existant vide est soumis -> 422 RG-1.05 inline (sinon la modif
|
||||||
if (!mapRowError(error, contactErrors, index)) {
|
// serait perdue en silence avec un faux toast de succes).
|
||||||
toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) })
|
contact => contact.id === null && isContactBlank(contact),
|
||||||
}
|
)
|
||||||
return
|
// Tant qu'un bloc reste en erreur : pas de validation d'onglet ni de toast succes.
|
||||||
}
|
if (hasError) return
|
||||||
}
|
|
||||||
completeTab('contact')
|
completeTab('contact')
|
||||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
}
|
}
|
||||||
@@ -748,12 +750,14 @@ const countryOptions: RefOption[] = [
|
|||||||
{ value: 'Espagne', label: 'Espagne' },
|
{ value: 'Espagne', label: 'Espagne' },
|
||||||
]
|
]
|
||||||
|
|
||||||
// RG-1.10 (>= 1 site) + RG-1.11 (email facturation si Facturation) sur chaque adresse.
|
// Type d'adresse (Select) obligatoire + RG-1.10 (>= 1 site) + RG-1.11 (email
|
||||||
|
// facturation si Facturation) sur chaque adresse.
|
||||||
const canValidateAddresses = computed(() =>
|
const canValidateAddresses = computed(() =>
|
||||||
addresses.value.length > 0
|
addresses.value.length > 0
|
||||||
&& addresses.value.every((a) => {
|
&& addresses.value.every((a) => {
|
||||||
const filledBillingEmail = a.billingEmail !== null && a.billingEmail.trim() !== ''
|
const filledBillingEmail = a.billingEmail !== null && a.billingEmail.trim() !== ''
|
||||||
return a.siteIris.length >= 1
|
return addressTypeFromFlags(a) !== null
|
||||||
|
&& a.siteIris.length >= 1
|
||||||
&& a.categoryIris.length >= 1
|
&& a.categoryIris.length >= 1
|
||||||
&& (!isBillingEmailRequired(a) || filledBillingEmail)
|
&& (!isBillingEmailRequired(a) || filledBillingEmail)
|
||||||
}),
|
}),
|
||||||
@@ -784,10 +788,12 @@ function onAddressDegraded(): void {
|
|||||||
async function submitAddresses(): Promise<void> {
|
async function submitAddresses(): Promise<void> {
|
||||||
if (clientId.value === null || !canValidateAddresses.value || tabSubmitting.value) return
|
if (clientId.value === null || !canValidateAddresses.value || tabSubmitting.value) return
|
||||||
tabSubmitting.value = true
|
tabSubmitting.value = true
|
||||||
addressErrors.value = []
|
|
||||||
try {
|
try {
|
||||||
for (let index = 0; index < addresses.value.length; index++) {
|
// On tente TOUS les blocs d'adresse (collecte des erreurs par index, ERP-110).
|
||||||
const address = addresses.value[index]
|
const hasError = await submitRows(
|
||||||
|
addresses.value,
|
||||||
|
addressErrors,
|
||||||
|
async (address) => {
|
||||||
const body = {
|
const body = {
|
||||||
isProspect: address.isProspect,
|
isProspect: address.isProspect,
|
||||||
isDelivery: address.isDelivery,
|
isDelivery: address.isDelivery,
|
||||||
@@ -802,8 +808,6 @@ async function submitAddresses(): Promise<void> {
|
|||||||
contacts: address.contactIris,
|
contacts: address.contactIris,
|
||||||
billingEmail: isBillingEmailRequired(address) ? (address.billingEmail || null) : null,
|
billingEmail: isBillingEmailRequired(address) ? (address.billingEmail || null) : null,
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
if (address.id === null) {
|
if (address.id === null) {
|
||||||
const created = await api.post<{ id: number }>(
|
const created = await api.post<{ id: number }>(
|
||||||
`/clients/${clientId.value}/addresses`,
|
`/clients/${clientId.value}/addresses`,
|
||||||
@@ -815,14 +819,10 @@ async function submitAddresses(): Promise<void> {
|
|||||||
else {
|
else {
|
||||||
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
catch (error) {
|
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
if (!mapRowError(error, addressErrors, index)) {
|
)
|
||||||
toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) })
|
if (hasError) return
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
completeTab('address')
|
completeTab('address')
|
||||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
}
|
}
|
||||||
@@ -864,8 +864,11 @@ function ribIsComplete(rib: RibFormDraft): boolean {
|
|||||||
return filled(rib.label) && filled(rib.bic) && filled(rib.iban)
|
return filled(rib.label) && filled(rib.bic) && filled(rib.iban)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RG-1.30 : les 6 champs scalaires obligatoires (comme les onglets Contact /
|
||||||
|
// Adresse, le bouton reste desactive tant que l'onglet n'est pas complet).
|
||||||
// RG-1.12 : banque requise si VIREMENT. RG-1.13 : >= 1 RIB complet si LCR.
|
// RG-1.12 : banque requise si VIREMENT. RG-1.13 : >= 1 RIB complet si LCR.
|
||||||
const canValidateAccounting = computed(() => {
|
const canValidateAccounting = computed(() => {
|
||||||
|
if (!hasAllRequiredAccountingFields(accounting)) return false
|
||||||
if (isBankRequired.value && (accounting.bankIri === null)) return false
|
if (isBankRequired.value && (accounting.bankIri === null)) return false
|
||||||
if (isRibRequired.value && !ribs.value.some(ribIsComplete)) return false
|
if (isRibRequired.value && !ribs.value.some(ribIsComplete)) return false
|
||||||
return true
|
return true
|
||||||
@@ -893,6 +896,9 @@ async function submitAccounting(): Promise<void> {
|
|||||||
if (clientId.value === null || !canValidateAccounting.value || tabSubmitting.value) return
|
if (clientId.value === null || !canValidateAccounting.value || tabSubmitting.value) return
|
||||||
tabSubmitting.value = true
|
tabSubmitting.value = true
|
||||||
accountingErrors.clearErrors()
|
accountingErrors.clearErrors()
|
||||||
|
// Reset des erreurs RIB des le debut : l'etape 1 (PATCH scalaires) peut
|
||||||
|
// echouer et `return` avant submitRows (qui porte sinon le reset), laissant
|
||||||
|
// des erreurs de RIB obsoletes affichees sous les blocs.
|
||||||
ribErrors.value = []
|
ribErrors.value = []
|
||||||
try {
|
try {
|
||||||
// 1) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
// 1) PATCH des scalaires comptables (erreurs inline sur leurs champs).
|
||||||
@@ -912,30 +918,33 @@ async function submitAccounting(): Promise<void> {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) POST/PATCH des RIB (erreurs inline par ligne).
|
// 2) POST/PATCH des RIB (erreurs inline par ligne, tous les blocs tentes).
|
||||||
for (let index = 0; index < ribs.value.length; index++) {
|
// Seuls les blocs RIB TOTALEMENT vides sont ignores : un RIB partiel (ex.
|
||||||
const rib = ribs.value[index]
|
// IBAN seul) est soumis -> 422 NotBlank (label / bic / iban) inline.
|
||||||
if (!ribIsComplete(rib)) continue
|
const ribHasError = await submitRows(
|
||||||
try {
|
ribs.value,
|
||||||
|
ribErrors,
|
||||||
|
async (rib) => {
|
||||||
|
const body = { label: rib.label, bic: rib.bic, iban: rib.iban }
|
||||||
if (rib.id === null) {
|
if (rib.id === null) {
|
||||||
const created = await api.post<{ id: number }>(
|
const created = await api.post<{ id: number }>(
|
||||||
`/clients/${clientId.value}/ribs`,
|
`/clients/${clientId.value}/ribs`,
|
||||||
{ label: rib.label, bic: rib.bic, iban: rib.iban },
|
body,
|
||||||
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||||
)
|
)
|
||||||
rib.id = created.id
|
rib.id = created.id
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await api.patch(`/client_ribs/${rib.id}`, { label: rib.label, bic: rib.bic, iban: rib.iban }, { toast: false })
|
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
if (!mapRowError(error, ribErrors, index)) {
|
|
||||||
toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) })
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
error => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||||
|
// On ne saute QUE les amorces neuves (id null) totalement vides. Un
|
||||||
|
// RIB existant vide est soumis -> 422 NotBlank inline (sinon la modif
|
||||||
|
// serait perdue en silence avec un faux toast de succes).
|
||||||
|
rib => rib.id === null && isRibBlank(rib),
|
||||||
|
)
|
||||||
|
if (ribHasError) return
|
||||||
|
|
||||||
completeTab('accounting')
|
completeTab('accounting')
|
||||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||||
|
|||||||
@@ -1,17 +1,36 @@
|
|||||||
import { describe, it, expect } from 'vitest'
|
import { describe, it, expect } from 'vitest'
|
||||||
import {
|
import {
|
||||||
|
addressFlagsFromType,
|
||||||
|
addressTypeFromFlags,
|
||||||
applyProspectExclusivity,
|
applyProspectExclusivity,
|
||||||
buildClientFormTabKeys,
|
buildClientFormTabKeys,
|
||||||
canSelectDeliveryOrBilling,
|
canSelectDeliveryOrBilling,
|
||||||
canSelectProspect,
|
canSelectProspect,
|
||||||
|
hasAllRequiredAccountingFields,
|
||||||
hasAtLeastOneValidContact,
|
hasAtLeastOneValidContact,
|
||||||
isBankRequiredForPaymentType,
|
isBankRequiredForPaymentType,
|
||||||
isBillingEmailRequired,
|
isBillingEmailRequired,
|
||||||
|
isBlankRow,
|
||||||
|
isContactBlank,
|
||||||
isContactNamed,
|
isContactNamed,
|
||||||
|
isRibBlank,
|
||||||
isRibRequiredForPaymentType,
|
isRibRequiredForPaymentType,
|
||||||
type ContactDraft,
|
type ContactDraft,
|
||||||
|
type ContactFillableDraft,
|
||||||
} from '../clientFormRules'
|
} from '../clientFormRules'
|
||||||
|
|
||||||
|
/** Bloc contact totalement vide (amorce par defaut). */
|
||||||
|
function blankContact(): ContactFillableDraft {
|
||||||
|
return {
|
||||||
|
firstName: null,
|
||||||
|
lastName: null,
|
||||||
|
jobTitle: null,
|
||||||
|
phonePrimary: null,
|
||||||
|
phoneSecondary: null,
|
||||||
|
email: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('buildClientFormTabKeys (gating onglet Comptabilite + onglets edit-only)', () => {
|
describe('buildClientFormTabKeys (gating onglet Comptabilite + onglets edit-only)', () => {
|
||||||
it('inclut l onglet accounting si l utilisateur a accounting.view', () => {
|
it('inclut l onglet accounting si l utilisateur a accounting.view', () => {
|
||||||
expect(buildClientFormTabKeys(true)).toContain('accounting')
|
expect(buildClientFormTabKeys(true)).toContain('accounting')
|
||||||
@@ -59,6 +78,49 @@ describe('isContactNamed (RG-1.05)', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('isBlankRow (primitive : toutes les valeurs vides)', () => {
|
||||||
|
it('vrai si toutes les valeurs sont nulles / vides / espaces', () => {
|
||||||
|
expect(isBlankRow([null, undefined, '', ' '])).toBe(true)
|
||||||
|
expect(isBlankRow([])).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux des qu une valeur porte un caractere non-espace', () => {
|
||||||
|
expect(isBlankRow([null, 'x', ''])).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isRibBlank (bloc RIB totalement vide vs partiellement rempli)', () => {
|
||||||
|
it('vrai si label / bic / iban sont tous vides', () => {
|
||||||
|
expect(isRibBlank({ label: null, bic: null, iban: null })).toBe(true)
|
||||||
|
expect(isRibBlank({ label: ' ', bic: '', iban: null })).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si un IBAN seul est saisi (bloc a soumettre -> 422 NotBlank inline)', () => {
|
||||||
|
expect(isRibBlank({ label: null, bic: null, iban: 'FR1420041010050500013M02606' })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si seul le libelle est saisi', () => {
|
||||||
|
expect(isRibBlank({ label: 'Compte courant', bic: null, iban: null })).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isContactBlank (bloc totalement vide vs partiellement rempli)', () => {
|
||||||
|
it('vrai si aucun champ saisissable n est rempli', () => {
|
||||||
|
expect(isContactBlank(blankContact())).toBe(true)
|
||||||
|
expect(isContactBlank({ ...blankContact(), firstName: ' ', email: '' })).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si un email seul est saisi (bloc a soumettre -> 422 RG-1.05 inline)', () => {
|
||||||
|
expect(isContactBlank({ ...blankContact(), email: 'jean@acme.fr' })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si seul un telephone, une fonction ou un nom est saisi', () => {
|
||||||
|
expect(isContactBlank({ ...blankContact(), phonePrimary: '0612345678' })).toBe(false)
|
||||||
|
expect(isContactBlank({ ...blankContact(), jobTitle: 'Directeur' })).toBe(false)
|
||||||
|
expect(isContactBlank({ ...blankContact(), firstName: 'Alice' })).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('hasAtLeastOneValidContact (RG-1.14)', () => {
|
describe('hasAtLeastOneValidContact (RG-1.14)', () => {
|
||||||
it('faux sur une liste vide', () => {
|
it('faux sur une liste vide', () => {
|
||||||
expect(hasAtLeastOneValidContact([])).toBe(false)
|
expect(hasAtLeastOneValidContact([])).toBe(false)
|
||||||
@@ -137,6 +199,32 @@ describe('isBillingEmailRequired (RG-1.11)', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('type d\'adresse (Select front) <-> drapeaux back', () => {
|
||||||
|
it('addressFlagsFromType mappe chaque type vers les bons drapeaux', () => {
|
||||||
|
expect(addressFlagsFromType('prospect')).toEqual({ isProspect: true, isDelivery: false, isBilling: false })
|
||||||
|
expect(addressFlagsFromType('delivery')).toEqual({ isProspect: false, isDelivery: true, isBilling: false })
|
||||||
|
expect(addressFlagsFromType('billing')).toEqual({ isProspect: false, isDelivery: false, isBilling: true })
|
||||||
|
expect(addressFlagsFromType('delivery_billing')).toEqual({ isProspect: false, isDelivery: true, isBilling: true })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('addressTypeFromFlags reconstruit le type (Prospect prioritaire, livraison+facturation groupes)', () => {
|
||||||
|
expect(addressTypeFromFlags({ isProspect: true, isDelivery: false, isBilling: false })).toBe('prospect')
|
||||||
|
expect(addressTypeFromFlags({ isProspect: false, isDelivery: true, isBilling: false })).toBe('delivery')
|
||||||
|
expect(addressTypeFromFlags({ isProspect: false, isDelivery: false, isBilling: true })).toBe('billing')
|
||||||
|
expect(addressTypeFromFlags({ isProspect: false, isDelivery: true, isBilling: true })).toBe('delivery_billing')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('addressTypeFromFlags retourne null quand aucun drapeau (amorce vierge -> bouton bloque)', () => {
|
||||||
|
expect(addressTypeFromFlags({ isProspect: false, isDelivery: false, isBilling: false })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('aller-retour type -> drapeaux -> type stable pour les 4 types', () => {
|
||||||
|
for (const type of ['prospect', 'delivery', 'billing', 'delivery_billing'] as const) {
|
||||||
|
expect(addressTypeFromFlags(addressFlagsFromType(type))).toBe(type)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('regles type de reglement (RG-1.12 / RG-1.13)', () => {
|
describe('regles type de reglement (RG-1.12 / RG-1.13)', () => {
|
||||||
it('banque obligatoire si VIREMENT', () => {
|
it('banque obligatoire si VIREMENT', () => {
|
||||||
expect(isBankRequiredForPaymentType('VIREMENT')).toBe(true)
|
expect(isBankRequiredForPaymentType('VIREMENT')).toBe(true)
|
||||||
@@ -150,3 +238,36 @@ describe('regles type de reglement (RG-1.12 / RG-1.13)', () => {
|
|||||||
expect(isRibRequiredForPaymentType(null)).toBe(false)
|
expect(isRibRequiredForPaymentType(null)).toBe(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('hasAllRequiredAccountingFields (RG-1.30)', () => {
|
||||||
|
const complete = {
|
||||||
|
siren: '123456789',
|
||||||
|
accountNumber: '00012345678',
|
||||||
|
nTva: 'FR12345678901',
|
||||||
|
tvaModeIri: '/api/tva_modes/1',
|
||||||
|
paymentDelayIri: '/api/payment_delays/1',
|
||||||
|
paymentTypeIri: '/api/payment_types/1',
|
||||||
|
}
|
||||||
|
|
||||||
|
it('vrai quand les six champs obligatoires sont remplis', () => {
|
||||||
|
expect(hasAllRequiredAccountingFields(complete)).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux si un champ est manquant (null ou vide apres trim)', () => {
|
||||||
|
expect(hasAllRequiredAccountingFields({ ...complete, siren: null })).toBe(false)
|
||||||
|
expect(hasAllRequiredAccountingFields({ ...complete, accountNumber: ' ' })).toBe(false)
|
||||||
|
expect(hasAllRequiredAccountingFields({ ...complete, tvaModeIri: null })).toBe(false)
|
||||||
|
expect(hasAllRequiredAccountingFields({ ...complete, paymentTypeIri: null })).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('faux quand tout est vide (onglet non rempli)', () => {
|
||||||
|
expect(hasAllRequiredAccountingFields({
|
||||||
|
siren: null,
|
||||||
|
accountNumber: null,
|
||||||
|
nTva: null,
|
||||||
|
tvaModeIri: null,
|
||||||
|
paymentDelayIri: null,
|
||||||
|
paymentTypeIri: null,
|
||||||
|
})).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -86,6 +86,58 @@ export function hasAtLeastOneValidContact(contacts: ContactDraft[]): boolean {
|
|||||||
return contacts.some(isContactNamed)
|
return contacts.some(isContactNamed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Primitive reutilisable : vrai si TOUTES les valeurs fournies sont vides (null /
|
||||||
|
* undefined / espaces uniquement). Sert a detecter un bloc de collection
|
||||||
|
* totalement vide (amorce non remplie). Un bloc qui porte la moindre donnee
|
||||||
|
* n'est PAS « blank » : il doit etre soumis pour declencher sa 422 inline plutot
|
||||||
|
* que d'etre saute silencieusement.
|
||||||
|
*/
|
||||||
|
export function isBlankRow(values: (string | null | undefined)[]): boolean {
|
||||||
|
return values.every(value => !isFilled(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Champs saisissables d'un bloc contact (pour detecter un bloc totalement vide). */
|
||||||
|
export interface ContactFillableDraft extends ContactDraft {
|
||||||
|
jobTitle: string | null
|
||||||
|
phonePrimary: string | null
|
||||||
|
phoneSecondary: string | null
|
||||||
|
email: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vrai si AUCUN champ saisissable du bloc contact n'est rempli. Distingue un bloc
|
||||||
|
* d'amorce vide (a ignorer au submit) d'un bloc partiellement rempli sans nom
|
||||||
|
* (email / telephone / fonction seul) : ce dernier doit etre soumis pour
|
||||||
|
* declencher la 422 RG-1.05 (« prenom ou nom obligatoire ») affichee inline.
|
||||||
|
*/
|
||||||
|
export function isContactBlank(contact: ContactFillableDraft): boolean {
|
||||||
|
return isBlankRow([
|
||||||
|
contact.firstName,
|
||||||
|
contact.lastName,
|
||||||
|
contact.jobTitle,
|
||||||
|
contact.phonePrimary,
|
||||||
|
contact.phoneSecondary,
|
||||||
|
contact.email,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Champs saisissables d'un bloc RIB (pour detecter un bloc totalement vide). */
|
||||||
|
export interface RibFillableDraft {
|
||||||
|
label: string | null
|
||||||
|
bic: string | null
|
||||||
|
iban: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vrai si AUCUN champ du bloc RIB n'est rempli. Un RIB partiellement rempli (ex.
|
||||||
|
* IBAN seul) n'est PAS « blank » : il doit etre soumis pour declencher les 422
|
||||||
|
* NotBlank (label / bic / iban) inline plutot que d'etre saute silencieusement.
|
||||||
|
*/
|
||||||
|
export function isRibBlank(rib: RibFillableDraft): boolean {
|
||||||
|
return isBlankRow([rib.label, rib.bic, rib.iban])
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RG-1.06/07/08 : une adresse de prospection est exclusive d'une adresse de
|
* RG-1.06/07/08 : une adresse de prospection est exclusive d'une adresse de
|
||||||
* livraison/facturation. Prospect n'est selectionnable que si ni Livraison ni
|
* livraison/facturation. Prospect n'est selectionnable que si ni Livraison ni
|
||||||
@@ -135,6 +187,45 @@ export function isBillingEmailRequired(flags: AddressFlagsDraft): boolean {
|
|||||||
return flags.isBilling
|
return flags.isBilling
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type d'adresse expose a l'utilisateur (Select unique remplacant les trois
|
||||||
|
* cases a cocher). Sucre purement front : le back continue de recevoir les
|
||||||
|
* drapeaux isProspect / isDelivery / isBilling (aucune RG modifiee). Les seules
|
||||||
|
* combinaisons proposees respectent l'exclusivite Prospect (RG-1.06/07/08).
|
||||||
|
*/
|
||||||
|
export type AddressType = 'prospect' | 'delivery' | 'billing' | 'delivery_billing'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mappe le type d'adresse choisi vers les trois drapeaux back.
|
||||||
|
* « Adresse + Facturation » = livraison ET facturation sur la meme adresse.
|
||||||
|
*/
|
||||||
|
export function addressFlagsFromType(type: AddressType): AddressFlagsDraft {
|
||||||
|
switch (type) {
|
||||||
|
case 'prospect':
|
||||||
|
return { isProspect: true, isDelivery: false, isBilling: false }
|
||||||
|
case 'delivery':
|
||||||
|
return { isProspect: false, isDelivery: true, isBilling: false }
|
||||||
|
case 'billing':
|
||||||
|
return { isProspect: false, isDelivery: false, isBilling: true }
|
||||||
|
case 'delivery_billing':
|
||||||
|
return { isProspect: false, isDelivery: true, isBilling: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reconstruit le type d'adresse a partir des drapeaux (consultation / edition
|
||||||
|
* d'une adresse persistee, ou amorce vierge). Retourne null si aucun drapeau
|
||||||
|
* n'est positionne — le Select reste alors a saisir (et bloque la validation).
|
||||||
|
*/
|
||||||
|
export function addressTypeFromFlags(flags: AddressFlagsDraft): AddressType | null {
|
||||||
|
if (flags.isProspect) return 'prospect'
|
||||||
|
if (flags.isDelivery && flags.isBilling) return 'delivery_billing'
|
||||||
|
if (flags.isDelivery) return 'delivery'
|
||||||
|
if (flags.isBilling) return 'billing'
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
/** Code stable du type de reglement « virement » (cf. PaymentType.code, RG-1.12). */
|
/** Code stable du type de reglement « virement » (cf. PaymentType.code, RG-1.12). */
|
||||||
const PAYMENT_TYPE_TRANSFER = 'VIREMENT'
|
const PAYMENT_TYPE_TRANSFER = 'VIREMENT'
|
||||||
|
|
||||||
@@ -156,3 +247,32 @@ export function isBankRequiredForPaymentType(code: string | null | undefined): b
|
|||||||
export function isRibRequiredForPaymentType(code: string | null | undefined): boolean {
|
export function isRibRequiredForPaymentType(code: string | null | undefined): boolean {
|
||||||
return code === PAYMENT_TYPE_LCR
|
return code === PAYMENT_TYPE_LCR
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sous-ensemble du brouillon comptable portant les six champs obligatoires. */
|
||||||
|
export interface AccountingRequiredDraft {
|
||||||
|
siren: string | null
|
||||||
|
accountNumber: string | null
|
||||||
|
nTva: string | null
|
||||||
|
tvaModeIri: string | null
|
||||||
|
paymentDelayIri: string | null
|
||||||
|
paymentTypeIri: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RG-1.30 : les six champs scalaires de l'onglet Comptabilite sont obligatoires
|
||||||
|
* pour valider l'onglet (SIREN, N de compte, Mode de TVA, N de TVA, Delai de
|
||||||
|
* reglement, Type de reglement). bank / RIB restent conditionnels (RG-1.12 /
|
||||||
|
* RG-1.13) et sont evalues a part. Miroir front du
|
||||||
|
* ClientAccountingCompletenessValidator : meme gate que les onglets Contact /
|
||||||
|
* Adresse (bouton « Valider » desactive tant que l'onglet n'est pas complet).
|
||||||
|
*/
|
||||||
|
export function hasAllRequiredAccountingFields(accounting: AccountingRequiredDraft): boolean {
|
||||||
|
const filled = (v: string | null): boolean => v !== null && v.trim() !== ''
|
||||||
|
|
||||||
|
return filled(accounting.siren)
|
||||||
|
&& filled(accounting.accountNumber)
|
||||||
|
&& filled(accounting.nTva)
|
||||||
|
&& filled(accounting.tvaModeIri)
|
||||||
|
&& filled(accounting.paymentDelayIri)
|
||||||
|
&& filled(accounting.paymentTypeIri)
|
||||||
|
}
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ class Category implements TimestampableInterface, BlamableInterface, CategoryInt
|
|||||||
// persiste, sans contradiction entre l'ordre Validate / Process.
|
// persiste, sans contradiction entre l'ordre Validate / Process.
|
||||||
#[ORM\Column(length: 120)]
|
#[ORM\Column(length: 120)]
|
||||||
#[Assert\NotBlank(message: 'Le nom est obligatoire.', normalizer: 'trim')]
|
#[Assert\NotBlank(message: 'Le nom est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Length(min: 2, max: 120, normalizer: 'trim')]
|
#[Assert\Length(min: 2, max: 120, minMessage: 'Le nom doit comporter au moins {{ limit }} caractères.', maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['category:read', 'category:write'])]
|
#[Groups(['category:read', 'category:write'])]
|
||||||
private ?string $name = null;
|
private ?string $name = null;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Module\Commercial\Application\Validator;
|
||||||
|
|
||||||
|
use ApiPlatform\Validator\Exception\ValidationException;
|
||||||
|
use App\Module\Commercial\Domain\Entity\Client;
|
||||||
|
use Symfony\Component\Validator\ConstraintViolation;
|
||||||
|
use Symfony\Component\Validator\ConstraintViolationList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validator metier (spec-front § Onglet Comptabilite) : a la soumission complete
|
||||||
|
* de l'onglet Comptabilite, les six champs scalaires obligatoires doivent etre
|
||||||
|
* renseignes (SIREN, Numero de compte, Mode de TVA, N de TVA, Delai de reglement,
|
||||||
|
* Type de reglement). La banque reste conditionnelle (RG-1.12) et les RIB aussi
|
||||||
|
* (RG-1.13) : ils ne sont pas couverts ici.
|
||||||
|
*
|
||||||
|
* Calque sur ClientInformationCompletenessValidator (RG-1.04) : colonnes nullable
|
||||||
|
* en base + validateur contextuel, plutot qu'un Assert\NotBlank sur l'entite (qui
|
||||||
|
* casserait le POST de l'onglet principal, lequel n'envoie aucun champ comptable).
|
||||||
|
*
|
||||||
|
* Invoque par le ClientProcessor uniquement quand le payload porte les six champs
|
||||||
|
* (= une validation d'onglet), jamais sur un PATCH ciblant un seul champ comptable.
|
||||||
|
*
|
||||||
|
* Leve une ValidationException (HTTP 422) listant chaque champ manquant, par
|
||||||
|
* coherence avec les violations Symfony rendues par API Platform (mapping inline
|
||||||
|
* front via useFormErrors, ERP-101).
|
||||||
|
*/
|
||||||
|
final class ClientAccountingCompletenessValidator
|
||||||
|
{
|
||||||
|
public function validate(Client $client): void
|
||||||
|
{
|
||||||
|
// Map champ -> valeur courante des champs obligatoires de l'onglet.
|
||||||
|
$fields = [
|
||||||
|
'siren' => $client->getSiren(),
|
||||||
|
'accountNumber' => $client->getAccountNumber(),
|
||||||
|
'tvaMode' => $client->getTvaMode(),
|
||||||
|
'nTva' => $client->getNTva(),
|
||||||
|
'paymentDelay' => $client->getPaymentDelay(),
|
||||||
|
'paymentType' => $client->getPaymentType(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$violations = new ConstraintViolationList();
|
||||||
|
|
||||||
|
foreach ($fields as $property => $value) {
|
||||||
|
if ($this->isMissing($value)) {
|
||||||
|
$violations->add(new ConstraintViolation(
|
||||||
|
'Ce champ est obligatoire.',
|
||||||
|
null,
|
||||||
|
[],
|
||||||
|
$client,
|
||||||
|
$property,
|
||||||
|
$value,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($violations) > 0) {
|
||||||
|
throw new ValidationException($violations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Une valeur est manquante si null ou, pour une chaine, vide apres trim. Les
|
||||||
|
* references (TvaMode / PaymentDelay / PaymentType) ne sont manquantes que
|
||||||
|
* lorsqu'elles valent null.
|
||||||
|
*/
|
||||||
|
private function isMissing(mixed $value): bool
|
||||||
|
{
|
||||||
|
if (null === $value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return is_string($value) && '' === trim($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -147,7 +147,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
|||||||
// === Formulaire principal ===
|
// === Formulaire principal ===
|
||||||
#[ORM\Column(length: 180)]
|
#[ORM\Column(length: 180)]
|
||||||
#[Assert\NotBlank(message: 'Le nom de l\'entreprise est obligatoire.', normalizer: 'trim')]
|
#[Assert\NotBlank(message: 'Le nom de l\'entreprise est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Length(min: 2, max: 180, normalizer: 'trim')]
|
#[Assert\Length(min: 2, max: 180, minMessage: 'Le nom de l\'entreprise doit comporter au moins {{ limit }} caractères.', maxMessage: 'Le nom de l\'entreprise ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client:read', 'client:write:main'])]
|
#[Groups(['client:read', 'client:write:main'])]
|
||||||
private ?string $companyName = null;
|
private ?string $companyName = null;
|
||||||
|
|
||||||
@@ -188,6 +188,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
|||||||
private ?string $description = null;
|
private ?string $description = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 255, nullable: true)]
|
#[ORM\Column(length: 255, nullable: true)]
|
||||||
|
#[Assert\Length(max: 255, maxMessage: 'Ce champ ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client:read', 'client:write:information'])]
|
#[Groups(['client:read', 'client:write:information'])]
|
||||||
private ?string $competitors = null;
|
private ?string $competitors = null;
|
||||||
|
|
||||||
@@ -196,7 +197,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
|||||||
private ?DateTimeImmutable $foundedAt = null;
|
private ?DateTimeImmutable $foundedAt = null;
|
||||||
|
|
||||||
#[ORM\Column(nullable: true)]
|
#[ORM\Column(nullable: true)]
|
||||||
#[Assert\PositiveOrZero]
|
#[Assert\PositiveOrZero(message: 'L\'effectif doit être un nombre positif ou nul.')]
|
||||||
#[Groups(['client:read', 'client:write:information'])]
|
#[Groups(['client:read', 'client:write:information'])]
|
||||||
private ?int $employeesCount = null;
|
private ?int $employeesCount = null;
|
||||||
|
|
||||||
@@ -205,6 +206,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
|||||||
private ?string $revenueAmount = null;
|
private ?string $revenueAmount = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 120, nullable: true)]
|
#[ORM\Column(length: 120, nullable: true)]
|
||||||
|
#[Assert\Length(max: 120, maxMessage: 'Le nom du dirigeant ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client:read', 'client:write:information'])]
|
#[Groups(['client:read', 'client:write:information'])]
|
||||||
private ?string $directorName = null;
|
private ?string $directorName = null;
|
||||||
|
|
||||||
@@ -217,10 +219,12 @@ class Client implements TimestampableInterface, BlamableInterface
|
|||||||
// futur Provider si l'user a la permission accounting.view). Ecriture via
|
// futur Provider si l'user a la permission accounting.view). Ecriture via
|
||||||
// `client:write:accounting` (le futur Processor exige accounting.manage).
|
// `client:write:accounting` (le futur Processor exige accounting.manage).
|
||||||
#[ORM\Column(length: 20, nullable: true)]
|
#[ORM\Column(length: 20, nullable: true)]
|
||||||
|
#[Assert\Length(max: 20, maxMessage: 'Le SIREN ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
||||||
private ?string $siren = null;
|
private ?string $siren = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 40, nullable: true)]
|
#[ORM\Column(length: 40, nullable: true)]
|
||||||
|
#[Assert\Length(max: 40, maxMessage: 'Le numéro de compte ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
||||||
private ?string $accountNumber = null;
|
private ?string $accountNumber = null;
|
||||||
|
|
||||||
@@ -230,6 +234,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
|||||||
private ?TvaMode $tvaMode = null;
|
private ?TvaMode $tvaMode = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 40, nullable: true)]
|
#[ORM\Column(length: 40, nullable: true)]
|
||||||
|
#[Assert\Length(max: 40, maxMessage: 'Le numéro de TVA ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
||||||
private ?string $nTva = null;
|
private ?string $nTva = null;
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,11 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
|||||||
uriVariables: [
|
uriVariables: [
|
||||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||||
],
|
],
|
||||||
|
// read:false : pas de stade lecture du parent. Le Link toProperty
|
||||||
|
// resoudrait l'enfant (SELECT ClientAddress ... WHERE client = :id) et
|
||||||
|
// casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
||||||
|
// manuellement par ClientAddressProcessor::linkParent (404 si absent).
|
||||||
|
read: false,
|
||||||
security: "is_granted('commercial.clients.manage')",
|
security: "is_granted('commercial.clients.manage')",
|
||||||
normalizationContext: ['groups' => ['client_address:read']],
|
normalizationContext: ['groups' => ['client_address:read']],
|
||||||
denormalizationContext: ['groups' => ['client_address:write']],
|
denormalizationContext: ['groups' => ['client_address:write']],
|
||||||
@@ -125,33 +130,39 @@ class ClientAddress implements TimestampableInterface, BlamableInterface
|
|||||||
private bool $isBilling = false;
|
private bool $isBilling = false;
|
||||||
|
|
||||||
#[ORM\Column(length: 80, options: ['default' => 'France'])]
|
#[ORM\Column(length: 80, options: ['default' => 'France'])]
|
||||||
|
#[Assert\Length(max: 80, maxMessage: 'Le pays ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_address:read', 'client_address:write'])]
|
#[Groups(['client_address:read', 'client_address:write'])]
|
||||||
private string $country = 'France';
|
private string $country = 'France';
|
||||||
|
|
||||||
// RG-1.09 : code postal a 4 ou 5 chiffres (pas de controle CP/ville serveur).
|
// RG-1.09 : code postal a 4 ou 5 chiffres (pas de controle CP/ville serveur).
|
||||||
|
// Le Regex borne deja la longueur (≤ 5) : pas de Length redondant.
|
||||||
#[ORM\Column(length: 20)]
|
#[ORM\Column(length: 20)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'Le code postal est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Regex(pattern: '/^[0-9]{4,5}$/', message: 'Le code postal doit comporter 4 ou 5 chiffres.')]
|
#[Assert\Regex(pattern: '/^[0-9]{4,5}$/', message: 'Le code postal doit comporter 4 ou 5 chiffres.')]
|
||||||
#[Groups(['client_address:read', 'client_address:write'])]
|
#[Groups(['client_address:read', 'client_address:write'])]
|
||||||
private ?string $postalCode = null;
|
private ?string $postalCode = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 120)]
|
#[ORM\Column(length: 120)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'La ville est obligatoire.', normalizer: 'trim')]
|
||||||
|
#[Assert\Length(max: 120, maxMessage: 'La ville ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_address:read', 'client_address:write'])]
|
#[Groups(['client_address:read', 'client_address:write'])]
|
||||||
private ?string $city = null;
|
private ?string $city = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 255)]
|
#[ORM\Column(length: 255)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'La rue est obligatoire.', normalizer: 'trim')]
|
||||||
|
#[Assert\Length(max: 255, maxMessage: 'La rue ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_address:read', 'client_address:write'])]
|
#[Groups(['client_address:read', 'client_address:write'])]
|
||||||
private ?string $street = null;
|
private ?string $street = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 255, nullable: true)]
|
#[ORM\Column(length: 255, nullable: true)]
|
||||||
|
#[Assert\Length(max: 255, maxMessage: 'Le complément d\'adresse ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_address:read', 'client_address:write'])]
|
#[Groups(['client_address:read', 'client_address:write'])]
|
||||||
private ?string $streetComplement = null;
|
private ?string $streetComplement = null;
|
||||||
|
|
||||||
// RG-1.11 : obligatoire ssi isBilling (validateBillingEmailPresence + CHECK BDD).
|
// RG-1.11 : obligatoire ssi isBilling (validateBillingEmailPresence + CHECK BDD).
|
||||||
#[ORM\Column(length: 180, nullable: true)]
|
#[ORM\Column(length: 180, nullable: true)]
|
||||||
#[Assert\Email]
|
#[Assert\Email(message: 'L\'email de facturation n\'est pas valide.')]
|
||||||
|
#[Assert\Length(max: 180, maxMessage: 'L\'email de facturation ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_address:read', 'client_address:write'])]
|
#[Groups(['client_address:read', 'client_address:write'])]
|
||||||
private ?string $billingEmail = null;
|
private ?string $billingEmail = null;
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
uriVariables: [
|
uriVariables: [
|
||||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||||
],
|
],
|
||||||
|
// read:false : pas de stade lecture du parent. Le Link toProperty
|
||||||
|
// resoudrait l'enfant (SELECT ClientContact ... WHERE client = :id) et
|
||||||
|
// casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
||||||
|
// manuellement par ClientContactProcessor::linkParent (404 si absent).
|
||||||
|
read: false,
|
||||||
security: "is_granted('commercial.clients.manage')",
|
security: "is_granted('commercial.clients.manage')",
|
||||||
normalizationContext: ['groups' => ['client_contact:read']],
|
normalizationContext: ['groups' => ['client_contact:read']],
|
||||||
denormalizationContext: ['groups' => ['client_contact:write']],
|
denormalizationContext: ['groups' => ['client_contact:write']],
|
||||||
@@ -88,30 +93,36 @@ class ClientContact implements TimestampableInterface, BlamableInterface
|
|||||||
// RG-1.05 : firstName OU lastName obligatoire (CHECK BDD + Processor). Les
|
// RG-1.05 : firstName OU lastName obligatoire (CHECK BDD + Processor). Les
|
||||||
// deux restent nullable au niveau ORM.
|
// deux restent nullable au niveau ORM.
|
||||||
#[ORM\Column(length: 120, nullable: true)]
|
#[ORM\Column(length: 120, nullable: true)]
|
||||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
#[Assert\Length(max: 120, maxMessage: 'Le prénom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||||
private ?string $firstName = null;
|
private ?string $firstName = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 120, nullable: true)]
|
#[ORM\Column(length: 120, nullable: true)]
|
||||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
#[Assert\Length(max: 120, maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||||
private ?string $lastName = null;
|
private ?string $lastName = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 120, nullable: true)]
|
#[ORM\Column(length: 120, nullable: true)]
|
||||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
#[Assert\Length(max: 120, maxMessage: 'La fonction ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||||
private ?string $jobTitle = null;
|
private ?string $jobTitle = null;
|
||||||
|
|
||||||
|
// RG : pas de validation de format telephone (saisie libre), mais une
|
||||||
|
// Assert\Length calee sur la colonne VARCHAR(20) evite l'erreur Postgres
|
||||||
|
// (500 non rattachee au champ) au profit d'une 422 propre (ERP-107).
|
||||||
#[ORM\Column(length: 20, nullable: true)]
|
#[ORM\Column(length: 20, nullable: true)]
|
||||||
|
#[Assert\Length(max: 20, maxMessage: 'Le téléphone ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||||
private ?string $phonePrimary = null;
|
private ?string $phonePrimary = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 20, nullable: true)]
|
#[ORM\Column(length: 20, nullable: true)]
|
||||||
|
#[Assert\Length(max: 20, maxMessage: 'Le téléphone secondaire ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||||
private ?string $phoneSecondary = null;
|
private ?string $phoneSecondary = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 180, nullable: true)]
|
#[ORM\Column(length: 180, nullable: true)]
|
||||||
#[Assert\Email]
|
#[Assert\Email(message: 'L\'adresse email n\'est pas valide.')]
|
||||||
|
#[Assert\Length(max: 180, maxMessage: 'L\'email ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||||
private ?string $email = null;
|
private ?string $email = null;
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,11 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||||||
uriVariables: [
|
uriVariables: [
|
||||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||||
],
|
],
|
||||||
|
// read:false : pas de stade lecture du parent. Le Link toProperty
|
||||||
|
// resoudrait l'enfant (SELECT ClientRib ... WHERE client = :id) et
|
||||||
|
// casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
||||||
|
// manuellement par ClientRibProcessor::linkParent (404 si absent).
|
||||||
|
read: false,
|
||||||
security: "is_granted('commercial.clients.accounting.manage')",
|
security: "is_granted('commercial.clients.accounting.manage')",
|
||||||
normalizationContext: ['groups' => ['client_rib:read']],
|
normalizationContext: ['groups' => ['client_rib:read']],
|
||||||
denormalizationContext: ['groups' => ['client_rib:write']],
|
denormalizationContext: ['groups' => ['client_rib:write']],
|
||||||
@@ -97,20 +102,22 @@ class ClientRib implements TimestampableInterface, BlamableInterface
|
|||||||
private ?Client $client = null;
|
private ?Client $client = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 120)]
|
#[ORM\Column(length: 120)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'Le libellé du RIB est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
#[Assert\Length(max: 120, maxMessage: 'Le libellé ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
||||||
private ?string $label = null;
|
private ?string $label = null;
|
||||||
|
|
||||||
|
// Bic/Iban bornent deja le format (et donc la longueur) : pas de Length
|
||||||
|
// redondant calee sur la colonne (whitelist du garde-fou ERP-107).
|
||||||
#[ORM\Column(length: 20)]
|
#[ORM\Column(length: 20)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'Le BIC est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Bic]
|
#[Assert\Bic(message: 'Le BIC n\'est pas valide.')]
|
||||||
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
||||||
private ?string $bic = null;
|
private ?string $bic = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 34)]
|
#[ORM\Column(length: 34)]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'L\'IBAN est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Iban]
|
#[Assert\Iban(message: 'L\'IBAN n\'est pas valide.')]
|
||||||
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
||||||
private ?string $iban = null;
|
private ?string $iban = null;
|
||||||
|
|
||||||
|
|||||||
+8
-2
@@ -12,6 +12,7 @@ use App\Module\Commercial\Domain\Entity\Client;
|
|||||||
use App\Module\Commercial\Domain\Entity\ClientAddress;
|
use App\Module\Commercial\Domain\Entity\ClientAddress;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processor d'ecriture de la sous-ressource Adresse d'un client (M1, § 4.5).
|
* Processor d'ecriture de la sous-ressource Adresse d'un client (M1, § 4.5).
|
||||||
@@ -75,9 +76,14 @@ final class ClientAddressProcessor implements ProcessorInterface
|
|||||||
? $clientId
|
? $clientId
|
||||||
: $this->em->getRepository(Client::class)->find($clientId);
|
: $this->em->getRepository(Client::class)->find($clientId);
|
||||||
|
|
||||||
if ($client instanceof Client) {
|
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||||
$address->setClient($client);
|
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||||
|
// contrainte client_id NOT NULL).
|
||||||
|
if (!$client instanceof Client) {
|
||||||
|
throw new NotFoundHttpException('Client introuvable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$address->setClient($client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+8
-2
@@ -14,6 +14,7 @@ use App\Module\Commercial\Domain\Entity\ClientContact;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Symfony\Component\Validator\ConstraintViolation;
|
use Symfony\Component\Validator\ConstraintViolation;
|
||||||
use Symfony\Component\Validator\ConstraintViolationList;
|
use Symfony\Component\Validator\ConstraintViolationList;
|
||||||
|
|
||||||
@@ -88,9 +89,14 @@ final class ClientContactProcessor implements ProcessorInterface
|
|||||||
? $clientId
|
? $clientId
|
||||||
: $this->em->getRepository(Client::class)->find($clientId);
|
: $this->em->getRepository(Client::class)->find($clientId);
|
||||||
|
|
||||||
if ($client instanceof Client) {
|
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||||
$contact->setClient($client);
|
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||||
|
// contrainte client_id NOT NULL).
|
||||||
|
if (!$client instanceof Client) {
|
||||||
|
throw new NotFoundHttpException('Client introuvable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$contact->setClient($client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use ApiPlatform\Metadata\Operation;
|
|||||||
use ApiPlatform\State\ProcessorInterface;
|
use ApiPlatform\State\ProcessorInterface;
|
||||||
use ApiPlatform\Validator\Exception\ValidationException;
|
use ApiPlatform\Validator\Exception\ValidationException;
|
||||||
use App\Module\Commercial\Application\Service\ClientFieldNormalizer;
|
use App\Module\Commercial\Application\Service\ClientFieldNormalizer;
|
||||||
|
use App\Module\Commercial\Application\Validator\ClientAccountingCompletenessValidator;
|
||||||
use App\Module\Commercial\Application\Validator\ClientInformationCompletenessValidator;
|
use App\Module\Commercial\Application\Validator\ClientInformationCompletenessValidator;
|
||||||
use App\Module\Commercial\Domain\Entity\Client;
|
use App\Module\Commercial\Domain\Entity\Client;
|
||||||
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
||||||
@@ -75,6 +76,14 @@ final class ClientProcessor implements ProcessorInterface
|
|||||||
'paymentType', 'bank',
|
'paymentType', 'bank',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Champs comptables obligatoires a la validation complete de l'onglet
|
||||||
|
* (spec-front § Onglet Comptabilite). bank est exclu : conditionnel (RG-1.12).
|
||||||
|
*/
|
||||||
|
private const array ACCOUNTING_REQUIRED_FIELDS = [
|
||||||
|
'siren', 'accountNumber', 'tvaMode', 'nTva', 'paymentDelay', 'paymentType',
|
||||||
|
];
|
||||||
|
|
||||||
/** Champ d'archivage (groupe client:write:archive). */
|
/** Champ d'archivage (groupe client:write:archive). */
|
||||||
private const string ARCHIVE_FIELD = 'isArchived';
|
private const string ARCHIVE_FIELD = 'isArchived';
|
||||||
|
|
||||||
@@ -100,6 +109,7 @@ final class ClientProcessor implements ProcessorInterface
|
|||||||
private readonly ProcessorInterface $persistProcessor,
|
private readonly ProcessorInterface $persistProcessor,
|
||||||
private readonly ClientFieldNormalizer $normalizer,
|
private readonly ClientFieldNormalizer $normalizer,
|
||||||
private readonly ClientInformationCompletenessValidator $informationValidator,
|
private readonly ClientInformationCompletenessValidator $informationValidator,
|
||||||
|
private readonly ClientAccountingCompletenessValidator $accountingValidator,
|
||||||
private readonly Security $security,
|
private readonly Security $security,
|
||||||
private readonly RequestStack $requestStack,
|
private readonly RequestStack $requestStack,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
@@ -125,6 +135,7 @@ final class ClientProcessor implements ProcessorInterface
|
|||||||
|
|
||||||
$this->validateDistributorBroker($data);
|
$this->validateDistributorBroker($data);
|
||||||
$this->validateAccountingConsistency($data);
|
$this->validateAccountingConsistency($data);
|
||||||
|
$this->validateAccountingCompleteness($data);
|
||||||
$this->validateInformationCompleteness($data);
|
$this->validateInformationCompleteness($data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -486,6 +497,29 @@ final class ClientProcessor implements ProcessorInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spec-front § Onglet Comptabilite : a la validation COMPLETE de l'onglet
|
||||||
|
* (les six champs obligatoires presents dans le payload — le front les envoie
|
||||||
|
* toujours ensemble), chacun doit etre renseigne, sinon 422 par champ. On ne
|
||||||
|
* declenche pas sur un PATCH ciblant un sous-ensemble de champs comptables :
|
||||||
|
* ce n'est pas une validation d'onglet (edition ponctuelle preservee). bank /
|
||||||
|
* RIB restent geres par validateAccountingConsistency (RG-1.12 / RG-1.13).
|
||||||
|
*
|
||||||
|
* Colonnes nullable en base + validateur contextuel (meme parti que RG-1.04) :
|
||||||
|
* un Assert\NotBlank sur l'entite casserait le POST de l'onglet principal, qui
|
||||||
|
* n'envoie aucun champ comptable.
|
||||||
|
*/
|
||||||
|
private function validateAccountingCompleteness(Client $data): void
|
||||||
|
{
|
||||||
|
// Declenche uniquement si TOUS les champs requis sont presents dans le
|
||||||
|
// payload (= soumission d'onglet, pas un PATCH partiel cible).
|
||||||
|
if ([] !== array_diff(self::ACCOUNTING_REQUIRED_FIELDS, $this->payloadKeys())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->accountingValidator->validate($data);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RG-1.04 (durcie ERP-74) : si l'utilisateur porte le role metier
|
* RG-1.04 (durcie ERP-74) : si l'utilisateur porte le role metier
|
||||||
* Commerciale, TOUS les champs de l'onglet Information sont obligatoires sur
|
* Commerciale, TOUS les champs de l'onglet Information sont obligatoires sur
|
||||||
|
|||||||
+8
-2
@@ -12,6 +12,7 @@ use App\Module\Commercial\Domain\Entity\ClientRib;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processor d'ecriture de la sous-ressource RIB d'un client (M1, § 4.5).
|
* Processor d'ecriture de la sous-ressource RIB d'un client (M1, § 4.5).
|
||||||
@@ -77,9 +78,14 @@ final class ClientRibProcessor implements ProcessorInterface
|
|||||||
? $clientId
|
? $clientId
|
||||||
: $this->em->getRepository(Client::class)->find($clientId);
|
: $this->em->getRepository(Client::class)->find($clientId);
|
||||||
|
|
||||||
if ($client instanceof Client) {
|
// read:false sur le POST : sans stade lecture, un parent introuvable n'est
|
||||||
$rib->setClient($client);
|
// plus intercepte en amont -> 404 explicite (sinon 500 au persist sur la
|
||||||
|
// contrainte client_id NOT NULL).
|
||||||
|
if (!$client instanceof Client) {
|
||||||
|
throw new NotFoundHttpException('Client introuvable.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$rib->setClient($client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -79,13 +79,15 @@ class Role
|
|||||||
|
|
||||||
#[ORM\Column(length: 100)]
|
#[ORM\Column(length: 100)]
|
||||||
#[Groups(['role:read', 'role:write'])]
|
#[Groups(['role:read', 'role:write'])]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'Le code du rôle est obligatoire.', normalizer: 'trim')]
|
||||||
#[Assert\Regex(pattern: '/^[a-z][a-z0-9_]*$/', message: 'Le code doit etre en snake_case et commencer par une lettre minuscule.')]
|
#[Assert\Regex(pattern: '/^[a-z][a-z0-9_]*$/', message: 'Le code doit être en snake_case et commencer par une lettre minuscule.')]
|
||||||
|
#[Assert\Length(max: 100, maxMessage: 'Le code ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
private string $code;
|
private string $code;
|
||||||
|
|
||||||
#[ORM\Column(length: 255)]
|
#[ORM\Column(length: 255)]
|
||||||
#[Groups(['role:read', 'role:write'])]
|
#[Groups(['role:read', 'role:write'])]
|
||||||
#[Assert\NotBlank]
|
#[Assert\NotBlank(message: 'Le libellé du rôle est obligatoire.', normalizer: 'trim')]
|
||||||
|
#[Assert\Length(max: 255, maxMessage: 'Le libellé ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
private string $label;
|
private string $label;
|
||||||
|
|
||||||
#[ORM\Column(type: Types::TEXT, nullable: true)]
|
#[ORM\Column(type: Types::TEXT, nullable: true)]
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
|||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
use Symfony\Component\Serializer\Attribute\Groups;
|
use Symfony\Component\Serializer\Attribute\Groups;
|
||||||
use Symfony\Component\Serializer\Attribute\SerializedName;
|
use Symfony\Component\Serializer\Attribute\SerializedName;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
#[ApiResource(
|
#[ApiResource(
|
||||||
operations: [
|
operations: [
|
||||||
@@ -85,6 +86,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Busines
|
|||||||
private ?int $id = null;
|
private ?int $id = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 180, unique: true)]
|
#[ORM\Column(length: 180, unique: true)]
|
||||||
|
#[Assert\NotBlank(message: 'Le nom d\'utilisateur est obligatoire.', normalizer: 'trim')]
|
||||||
|
#[Assert\Length(max: 180, maxMessage: 'Le nom d\'utilisateur ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||||
#[Groups(['me:read', 'user:list', 'user:write'])]
|
#[Groups(['me:read', 'user:list', 'user:write'])]
|
||||||
private ?string $username = null;
|
private ?string $username = null;
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,19 @@
|
|||||||
"config/routes/security.yaml"
|
"config/routes/security.yaml"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"symfony/translation": {
|
||||||
|
"version": "8.0",
|
||||||
|
"recipe": {
|
||||||
|
"repo": "github.com/symfony/recipes",
|
||||||
|
"branch": "main",
|
||||||
|
"version": "6.3",
|
||||||
|
"ref": "620a1b84865ceb2ba304c8f8bf2a185fbf32a843"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"config/packages/translation.yaml",
|
||||||
|
"translations/.gitignore"
|
||||||
|
]
|
||||||
|
},
|
||||||
"symfony/twig-bundle": {
|
"symfony/twig-bundle": {
|
||||||
"version": "8.0",
|
"version": "8.0",
|
||||||
"recipe": {
|
"recipe": {
|
||||||
|
|||||||
@@ -0,0 +1,363 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Tests\Architecture;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping\Column;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionProperty;
|
||||||
|
use Symfony\Component\Finder\Finder;
|
||||||
|
use Symfony\Component\Serializer\Attribute\Groups;
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
|
use function in_array;
|
||||||
|
use function is_string;
|
||||||
|
use function sprintf;
|
||||||
|
use function str_contains;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Garde-fou architecture ERP-107 : toute contrainte `#[Assert\*]` portee par une
|
||||||
|
* entite metier doit avoir un message FR EXPLICITE (et non le defaut anglais de
|
||||||
|
* Symfony), et toute colonne string bornee writable doit avoir une `Assert\Length`
|
||||||
|
* calee sur le `length` de la colonne ORM.
|
||||||
|
*
|
||||||
|
* Pourquoi (lien ERP-101) : le front (useFormErrors / mapViolationsToRecord)
|
||||||
|
* affiche sous chaque champ le `message` renvoye par le back. Un message absent
|
||||||
|
* = defaut anglais ; une colonne bornee sans Assert\Length = erreur Postgres
|
||||||
|
* (500) au lieu d'une 422 propre rattachee au champ.
|
||||||
|
*
|
||||||
|
* Deux verifications, sur le modele de AuditableEntitiesHaveI18nLabelTest :
|
||||||
|
* 1. MESSAGE EXPLICITE : pour chaque contrainte connue, la (ou les) propriete(s)
|
||||||
|
* de message pertinente(s) doivent differer du defaut Symfony. La comparaison
|
||||||
|
* au defaut (instance « nue » de la meme contrainte) evite de valider un
|
||||||
|
* message anglais natif laisse tel quel.
|
||||||
|
* 2. LENGTH == ORM length : toute propriete string writable avec `ORM\Column(length:)`
|
||||||
|
* doit porter `Assert\Length(max:)` egal a ce length — sauf si le format est
|
||||||
|
* deja borne par Bic/Iban, ou whitelistee dans EXCLUDED_LENGTH_MIRROR.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class EntityConstraintsHaveFrenchMessageTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Proprietes writable exemptees du miroir Assert\Length == ORM length, avec
|
||||||
|
* justification. Toute entree doit citer la raison (format deja borne par une
|
||||||
|
* autre contrainte). Cle : "<ClasseCourte>::<propriete>".
|
||||||
|
*
|
||||||
|
* @var array<string, string>
|
||||||
|
*/
|
||||||
|
private const array EXCLUDED_LENGTH_MIRROR = [
|
||||||
|
// Le Regex /^[0-9]{4,5}$/ borne deja la longueur a 5 caracteres (< 20).
|
||||||
|
'ClientAddress::postalCode' => 'Regex {4,5} borne deja la longueur.',
|
||||||
|
// Le Regex /^#[0-9A-Fa-f]{6}$/ borne la longueur a exactement 7 caracteres.
|
||||||
|
'Site::color' => 'Regex code hex #RRGGBB borne deja la longueur.',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapping contrainte -> proprietes de message a verifier. Une contrainte
|
||||||
|
* absente de ce mapping (hors Callback) fait ECHOUER le test : il faut
|
||||||
|
* l'ajouter explicitement (anti faux positif vert sur une contrainte inconnue).
|
||||||
|
*
|
||||||
|
* Pour Length / Count, la liste est calculee dynamiquement (minMessage si
|
||||||
|
* `min` est pose, maxMessage si `max` est pose).
|
||||||
|
*
|
||||||
|
* @var list<class-string<Constraint>>
|
||||||
|
*/
|
||||||
|
private const array SIMPLE_MESSAGE_CONSTRAINTS = [
|
||||||
|
Assert\NotBlank::class,
|
||||||
|
Assert\NotNull::class,
|
||||||
|
Assert\Email::class,
|
||||||
|
Assert\Regex::class,
|
||||||
|
Assert\Bic::class,
|
||||||
|
Assert\Iban::class,
|
||||||
|
Assert\PositiveOrZero::class,
|
||||||
|
Assert\Positive::class,
|
||||||
|
Assert\NegativeOrZero::class,
|
||||||
|
Assert\Negative::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
public function testEveryConstraintHasAnExplicitFrenchMessage(): void
|
||||||
|
{
|
||||||
|
$checked = 0;
|
||||||
|
|
||||||
|
foreach ($this->entityProperties() as [$shortClass, $property]) {
|
||||||
|
foreach ($property->getAttributes() as $attribute) {
|
||||||
|
$name = $attribute->getName();
|
||||||
|
if (!is_subclass_of($name, Constraint::class)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Les Callback portent leur message dans la closure : hors scope.
|
||||||
|
if (Assert\Callback::class === $name) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var Constraint $constraint */
|
||||||
|
$constraint = $attribute->newInstance();
|
||||||
|
$messageProps = $this->messagePropertiesFor($constraint);
|
||||||
|
|
||||||
|
self::assertNotNull(
|
||||||
|
$messageProps,
|
||||||
|
sprintf(
|
||||||
|
'Contrainte non geree par le garde-fou : %s sur %s::$%s. '
|
||||||
|
.'Ajouter sa classe au mapping de EntityConstraintsHaveFrenchMessageTest.',
|
||||||
|
$name,
|
||||||
|
$shortClass,
|
||||||
|
$property->getName(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($messageProps as $prop) {
|
||||||
|
$actual = $constraint->{$prop} ?? null;
|
||||||
|
$default = $this->defaultMessageFor($name, $prop);
|
||||||
|
|
||||||
|
self::assertTrue(
|
||||||
|
is_string($actual) && '' !== $actual && $actual !== $default,
|
||||||
|
sprintf(
|
||||||
|
'La contrainte %s sur %s::$%s n\'a pas de %s FR explicite '
|
||||||
|
.'(message absent ou laisse au defaut anglais). Cf. ERP-107.',
|
||||||
|
$name,
|
||||||
|
$shortClass,
|
||||||
|
$property->getName(),
|
||||||
|
$prop,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
++$checked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self::assertGreaterThan(0, $checked, 'Aucune contrainte verifiee : detection d\'attributs cassee ?');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBoundedStringColumnsHaveMatchingLength(): void
|
||||||
|
{
|
||||||
|
$checked = 0;
|
||||||
|
|
||||||
|
foreach ($this->entityProperties() as [$shortClass, $property]) {
|
||||||
|
$column = $this->ormColumn($property);
|
||||||
|
if (null === $column || null === $column->length) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Colonnes non-string (text, decimal, date...) : pas de length scalaire a calquer.
|
||||||
|
if (null !== $column->type && 'string' !== $column->type) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Le miroir ne protege que la saisie utilisateur (champs writable).
|
||||||
|
if (!$this->isPropertyWritable($property)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$constraints = $this->constraintsOf($property);
|
||||||
|
|
||||||
|
// Format deja borne par Bic/Iban : longueur garantie cote contrainte.
|
||||||
|
if ($this->hasAnyConstraint($constraints, [Assert\Bic::class, Assert\Iban::class])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$excludeKey = $shortClass.'::'.$property->getName();
|
||||||
|
if (isset(self::EXCLUDED_LENGTH_MIRROR[$excludeKey])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$length = null;
|
||||||
|
foreach ($constraints as $c) {
|
||||||
|
if ($c instanceof Assert\Length) {
|
||||||
|
$length = $c->max;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self::assertNotNull(
|
||||||
|
$length,
|
||||||
|
sprintf(
|
||||||
|
'%s::$%s est une colonne string bornee (length=%d) writable sans Assert\Length : '
|
||||||
|
.'risque d\'erreur Postgres 500. Ajouter Assert\Length(max: %d) ou whitelister. Cf. ERP-107.',
|
||||||
|
$shortClass,
|
||||||
|
$property->getName(),
|
||||||
|
$column->length,
|
||||||
|
$column->length,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
self::assertSame(
|
||||||
|
$column->length,
|
||||||
|
$length,
|
||||||
|
sprintf(
|
||||||
|
'Derive Assert\Length.max (%s) != ORM length (%d) sur %s::$%s. '
|
||||||
|
.'Le max doit refleter le length de la colonne (anti-derive ERP-107).',
|
||||||
|
(string) $length,
|
||||||
|
$column->length,
|
||||||
|
$shortClass,
|
||||||
|
$property->getName(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
++$checked;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::assertGreaterThan(0, $checked, 'Aucune colonne string bornee verifiee : scan casse ?');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Itere (classe courte, ReflectionProperty) sur toutes les entites metier
|
||||||
|
* sous src/Module/<m>/Domain/Entity/.
|
||||||
|
*
|
||||||
|
* @return iterable<array{0: string, 1: ReflectionProperty}>
|
||||||
|
*/
|
||||||
|
private function entityProperties(): iterable
|
||||||
|
{
|
||||||
|
$finder = new Finder()
|
||||||
|
->files()
|
||||||
|
->in(__DIR__.'/../../src/Module')
|
||||||
|
->path('Domain/Entity')
|
||||||
|
->name('*.php')
|
||||||
|
;
|
||||||
|
|
||||||
|
self::assertNotEmpty(iterator_to_array($finder), 'Aucune entite scannee : chemin src/Module invalide ?');
|
||||||
|
|
||||||
|
foreach ($finder as $file) {
|
||||||
|
$fqcn = $this->extractFqcn($file->getRealPath());
|
||||||
|
if (null === $fqcn) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$reflection = new ReflectionClass($fqcn);
|
||||||
|
if ($reflection->isAbstract()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($reflection->getProperties() as $property) {
|
||||||
|
yield [$reflection->getShortName(), $property];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liste des proprietes de message a verifier pour une contrainte donnee, ou
|
||||||
|
* null si la contrainte n'est pas geree (le test echoue alors explicitement).
|
||||||
|
*
|
||||||
|
* @return list<string>|null
|
||||||
|
*/
|
||||||
|
private function messagePropertiesFor(Constraint $constraint): ?array
|
||||||
|
{
|
||||||
|
if ($constraint instanceof Assert\Length) {
|
||||||
|
$props = [];
|
||||||
|
if (null !== $constraint->min) {
|
||||||
|
$props[] = 'minMessage';
|
||||||
|
}
|
||||||
|
if (null !== $constraint->max) {
|
||||||
|
$props[] = 'maxMessage';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $props;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($constraint instanceof Assert\Count) {
|
||||||
|
$props = [];
|
||||||
|
if (null !== $constraint->min) {
|
||||||
|
$props[] = 'minMessage';
|
||||||
|
}
|
||||||
|
if (null !== $constraint->max) {
|
||||||
|
$props[] = 'maxMessage';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $props;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($constraint::class, self::SIMPLE_MESSAGE_CONSTRAINTS, true)) {
|
||||||
|
return ['message'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message par defaut d'une contrainte (instance « nue ») pour la propriete
|
||||||
|
* demandee. Sert de reference pour detecter un message laisse au defaut.
|
||||||
|
*/
|
||||||
|
private function defaultMessageFor(string $class, string $prop): ?string
|
||||||
|
{
|
||||||
|
$bare = match ($class) {
|
||||||
|
Assert\Length::class => new Assert\Length(max: 1),
|
||||||
|
Assert\Count::class => new Assert\Count(min: 1),
|
||||||
|
Assert\Regex::class => new Assert\Regex(pattern: '/^x$/'),
|
||||||
|
default => new $class(),
|
||||||
|
};
|
||||||
|
|
||||||
|
$value = $bare->{$prop} ?? null;
|
||||||
|
|
||||||
|
return is_string($value) ? $value : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ormColumn(ReflectionProperty $property): ?Column
|
||||||
|
{
|
||||||
|
$attrs = $property->getAttributes(Column::class);
|
||||||
|
|
||||||
|
return [] === $attrs ? null : $attrs[0]->newInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return list<Constraint> */
|
||||||
|
private function constraintsOf(ReflectionProperty $property): array
|
||||||
|
{
|
||||||
|
$out = [];
|
||||||
|
foreach ($property->getAttributes() as $attribute) {
|
||||||
|
if (is_subclass_of($attribute->getName(), Constraint::class)) {
|
||||||
|
$out[] = $attribute->newInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<Constraint> $constraints
|
||||||
|
* @param list<class-string<Constraint>> $classes
|
||||||
|
*/
|
||||||
|
private function hasAnyConstraint(array $constraints, array $classes): bool
|
||||||
|
{
|
||||||
|
foreach ($constraints as $c) {
|
||||||
|
if (in_array($c::class, $classes, true)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isPropertyWritable(ReflectionProperty $property): bool
|
||||||
|
{
|
||||||
|
$attrs = $property->getAttributes(Groups::class);
|
||||||
|
if ([] === $attrs) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var Groups $groups */
|
||||||
|
$groups = $attrs[0]->newInstance();
|
||||||
|
foreach ($groups->groups as $group) {
|
||||||
|
if (is_string($group) && str_contains($group, 'write')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function extractFqcn(string $path): ?string
|
||||||
|
{
|
||||||
|
$source = file_get_contents($path);
|
||||||
|
if (false === $source) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
1 !== preg_match('/^namespace\s+([^;]+);/m', $source, $nsMatch)
|
||||||
|
|| 1 !== preg_match('/^(?:final\s+|abstract\s+|readonly\s+)*class\s+(\w+)/m', $source, $classMatch)
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return trim($nsMatch[1]).'\\'.$classMatch[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Tests\Module\Commercial\Api;
|
namespace App\Tests\Module\Commercial\Api;
|
||||||
|
|
||||||
use App\Module\Commercial\Domain\Entity\Client as ClientEntity;
|
use App\Module\Commercial\Domain\Entity\Client as ClientEntity;
|
||||||
|
use App\Module\Commercial\Domain\Entity\ClientAddress;
|
||||||
use App\Module\Commercial\Domain\Entity\ClientContact;
|
use App\Module\Commercial\Domain\Entity\ClientContact;
|
||||||
use App\Module\Commercial\Domain\Entity\ClientRib;
|
use App\Module\Commercial\Domain\Entity\ClientRib;
|
||||||
use App\Module\Commercial\Domain\Entity\PaymentType;
|
use App\Module\Commercial\Domain\Entity\PaymentType;
|
||||||
@@ -66,6 +67,98 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
|||||||
self::assertResponseStatusCodeSame(422);
|
self::assertResponseStatusCodeSame(422);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ERP-107 : une violation de contrainte sort avec un message FR explicite ET
|
||||||
|
* un `propertyPath` rattache au champ (consommable par useFormErrors /
|
||||||
|
* mapViolationsToRecord cote front, ERP-101). On verifie le JSON 422 reel.
|
||||||
|
*/
|
||||||
|
public function testPostContactInvalidEmailReturns422WithFrenchMessageOnField(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Contact Bad Email');
|
||||||
|
|
||||||
|
$response = $client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => [
|
||||||
|
'firstName' => 'Jean',
|
||||||
|
'email' => 'pas-un-email',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(422);
|
||||||
|
$byPath = [];
|
||||||
|
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||||
|
$byPath[$v['propertyPath']] = $v['message'];
|
||||||
|
}
|
||||||
|
|
||||||
|
self::assertArrayHasKey('email', $byPath, 'La violation email doit porter propertyPath=email (mapping front).');
|
||||||
|
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regression ERP-110 (bug subresource Link toProperty) : POST d'un contact sur
|
||||||
|
* un client qui en a DEJA >= 2 ne doit pas exploser en 500
|
||||||
|
* (NonUniqueResultException sur la resolution du parent), mais creer (201).
|
||||||
|
*/
|
||||||
|
public function testPostContactOnClientWithTwoExistingContactsReturns201(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Contact Multi');
|
||||||
|
$this->seedContact($seed, 'Alpha');
|
||||||
|
$this->seedContact($seed, 'Beta');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['firstName' => 'Gamma'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meme contexte (>= 2 contacts existants) : un email invalide doit produire
|
||||||
|
* une 422 par champ (la validation est bien atteinte), pas une 500.
|
||||||
|
*/
|
||||||
|
public function testPostInvalidContactOnPopulatedClientReturns422OnField(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Contact Multi Bad');
|
||||||
|
$this->seedContact($seed, 'Alpha');
|
||||||
|
$this->seedContact($seed, 'Beta');
|
||||||
|
|
||||||
|
$response = $client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['firstName' => 'Gamma', 'email' => 'pas-un-email'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(422);
|
||||||
|
$byPath = [];
|
||||||
|
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||||
|
$byPath[$v['propertyPath']] = $v['message'];
|
||||||
|
}
|
||||||
|
self::assertArrayHasKey('email', $byPath);
|
||||||
|
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ERP-110 : avec read:false sur le POST, un parent introuvable n'est plus
|
||||||
|
* intercepte au stade lecture. Le 404 est desormais porte par
|
||||||
|
* ClientContactProcessor::linkParent (sinon 500 au persist sur client_id
|
||||||
|
* NOT NULL). Le payload est valide pour atteindre le processor (apres la
|
||||||
|
* validation).
|
||||||
|
*/
|
||||||
|
public function testPostContactOnMissingClientReturns404(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/999999/contacts', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['firstName' => 'Orphan'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(404);
|
||||||
|
}
|
||||||
|
|
||||||
public function testPatchContactNormalizes(): void
|
public function testPatchContactNormalizes(): void
|
||||||
{
|
{
|
||||||
$client = $this->createAdminClient();
|
$client = $this->createAdminClient();
|
||||||
@@ -173,6 +266,61 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
|||||||
self::assertResponseStatusCodeSame(422);
|
self::assertResponseStatusCodeSame(422);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regression ERP-110 : POST d'une adresse sur un client qui en a DEJA >= 2 ne
|
||||||
|
* doit pas exploser en 500 (NonUniqueResult sur la resolution du parent). Le
|
||||||
|
* POST porte un site + une categorie (RG-1.10 / RG-1.29) pour etre valide.
|
||||||
|
*/
|
||||||
|
public function testPostAddressOnClientWithTwoExistingAddressesReturns201(): void
|
||||||
|
{
|
||||||
|
$this->skipIfSitesModuleDisabled();
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Addr Multi');
|
||||||
|
$siteIri = $this->firstSiteIri();
|
||||||
|
$category = $this->createCategory('SECTEUR');
|
||||||
|
$this->seedAddress($seed, 'Bordeaux');
|
||||||
|
$this->seedAddress($seed, 'Lyon');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/'.$seed->getId().'/addresses', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => [
|
||||||
|
'postalCode' => '75001',
|
||||||
|
'city' => 'Paris',
|
||||||
|
'street' => '2 rue Neuve',
|
||||||
|
'sites' => [$siteIri],
|
||||||
|
'categories' => ['/api/categories/'.$category->getId()],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ERP-110 : POST adresse sur un client inexistant -> 404 porte par
|
||||||
|
* ClientAddressProcessor::linkParent (read:false). Payload valide (site +
|
||||||
|
* categorie, RG-1.10 / RG-1.29) pour atteindre le processor.
|
||||||
|
*/
|
||||||
|
public function testPostAddressOnMissingClientReturns404(): void
|
||||||
|
{
|
||||||
|
$this->skipIfSitesModuleDisabled();
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$siteIri = $this->firstSiteIri();
|
||||||
|
$category = $this->createCategory('SECTEUR');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/999999/addresses', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => [
|
||||||
|
'postalCode' => '75001',
|
||||||
|
'city' => 'Paris',
|
||||||
|
'street' => '2 rue Neuve',
|
||||||
|
'sites' => [$siteIri],
|
||||||
|
'categories' => ['/api/categories/'.$category->getId()],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(404);
|
||||||
|
}
|
||||||
|
|
||||||
// === RIBs ===
|
// === RIBs ===
|
||||||
|
|
||||||
public function testPostRibByAdminReturns201(): void
|
public function testPostRibByAdminReturns201(): void
|
||||||
@@ -211,6 +359,43 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
|||||||
self::assertResponseStatusCodeSame(422);
|
self::assertResponseStatusCodeSame(422);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regression ERP-110 : POST d'un RIB sur un client qui en a DEJA >= 2 ne doit
|
||||||
|
* pas exploser en 500 (NonUniqueResult sur la resolution du parent). L'admin
|
||||||
|
* porte commercial.clients.accounting.manage requis par le POST.
|
||||||
|
*/
|
||||||
|
public function testPostRibOnClientWithTwoExistingRibsReturns201(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
$seed = $this->seedClient('Rib Multi');
|
||||||
|
$this->seedRib($seed, 'Compte 1');
|
||||||
|
$this->seedRib($seed, 'Compte 2');
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/'.$seed->getId().'/ribs', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['label' => 'Compte 3', 'bic' => self::VALID_BIC, 'iban' => self::VALID_IBAN],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ERP-110 : POST RIB sur un client inexistant -> 404 porte par
|
||||||
|
* ClientRibProcessor::linkParent (read:false). L'admin porte
|
||||||
|
* commercial.clients.accounting.manage ; payload valide (BIC / IBAN).
|
||||||
|
*/
|
||||||
|
public function testPostRibOnMissingClientReturns404(): void
|
||||||
|
{
|
||||||
|
$client = $this->createAdminClient();
|
||||||
|
|
||||||
|
$client->request('POST', '/api/clients/999999/ribs', [
|
||||||
|
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||||
|
'json' => ['label' => 'Orphan', 'bic' => self::VALID_BIC, 'iban' => self::VALID_IBAN],
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertResponseStatusCodeSame(404);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDeleteRibNonLcrReturns204(): void
|
public function testDeleteRibNonLcrReturns204(): void
|
||||||
{
|
{
|
||||||
$client = $this->createAdminClient();
|
$client = $this->createAdminClient();
|
||||||
@@ -278,13 +463,34 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seede un ClientRib valide rattache a un client (sans passer par l'API).
|
* Seede une adresse minimale valide en base (sans passer par l'API) : seules
|
||||||
|
* les colonnes NOT NULL sont posees (CP / ville / rue). Les M2M sites /
|
||||||
|
* categories restent vides — non contraints en base, suffisant pour peupler
|
||||||
|
* un client de plusieurs adresses.
|
||||||
*/
|
*/
|
||||||
private function seedRib(ClientEntity $client): ClientRib
|
private function seedAddress(ClientEntity $client, string $city): ClientAddress
|
||||||
|
{
|
||||||
|
$em = $this->getEm();
|
||||||
|
$address = new ClientAddress();
|
||||||
|
$address->setClient($client);
|
||||||
|
$address->setPostalCode('33000');
|
||||||
|
$address->setCity($city);
|
||||||
|
$address->setStreet('1 rue du Test');
|
||||||
|
$em->persist($address);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
return $address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seede un ClientRib valide rattache a un client (sans passer par l'API). Le
|
||||||
|
* libelle est parametrable pour seeder plusieurs RIB distincts.
|
||||||
|
*/
|
||||||
|
private function seedRib(ClientEntity $client, string $label = 'Seed RIB'): ClientRib
|
||||||
{
|
{
|
||||||
$em = $this->getEm();
|
$em = $this->getEm();
|
||||||
$rib = new ClientRib();
|
$rib = new ClientRib();
|
||||||
$rib->setLabel('Seed RIB');
|
$rib->setLabel($label);
|
||||||
$rib->setBic(self::VALID_BIC);
|
$rib->setBic(self::VALID_BIC);
|
||||||
$rib->setIban(self::VALID_IBAN);
|
$rib->setIban(self::VALID_IBAN);
|
||||||
$rib->setClient($client);
|
$rib->setClient($client);
|
||||||
|
|||||||
@@ -8,11 +8,14 @@ use ApiPlatform\Metadata\Operation;
|
|||||||
use ApiPlatform\State\ProcessorInterface;
|
use ApiPlatform\State\ProcessorInterface;
|
||||||
use ApiPlatform\Validator\Exception\ValidationException;
|
use ApiPlatform\Validator\Exception\ValidationException;
|
||||||
use App\Module\Commercial\Application\Service\ClientFieldNormalizer;
|
use App\Module\Commercial\Application\Service\ClientFieldNormalizer;
|
||||||
|
use App\Module\Commercial\Application\Validator\ClientAccountingCompletenessValidator;
|
||||||
use App\Module\Commercial\Application\Validator\ClientInformationCompletenessValidator;
|
use App\Module\Commercial\Application\Validator\ClientInformationCompletenessValidator;
|
||||||
use App\Module\Commercial\Domain\Entity\Bank;
|
use App\Module\Commercial\Domain\Entity\Bank;
|
||||||
use App\Module\Commercial\Domain\Entity\Client;
|
use App\Module\Commercial\Domain\Entity\Client;
|
||||||
use App\Module\Commercial\Domain\Entity\ClientRib;
|
use App\Module\Commercial\Domain\Entity\ClientRib;
|
||||||
|
use App\Module\Commercial\Domain\Entity\PaymentDelay;
|
||||||
use App\Module\Commercial\Domain\Entity\PaymentType;
|
use App\Module\Commercial\Domain\Entity\PaymentType;
|
||||||
|
use App\Module\Commercial\Domain\Entity\TvaMode;
|
||||||
use App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor\ClientProcessor;
|
use App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor\ClientProcessor;
|
||||||
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
use App\Shared\Domain\Contract\BusinessRoleAwareInterface;
|
||||||
use App\Shared\Domain\Security\BusinessRoles;
|
use App\Shared\Domain\Security\BusinessRoles;
|
||||||
@@ -280,6 +283,65 @@ final class ClientProcessorTest extends TestCase
|
|||||||
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFullAccountingSubmitWithEmptyFieldsIsUnprocessable(): void
|
||||||
|
{
|
||||||
|
// spec-front § Onglet Comptabilite : une validation complete de l'onglet
|
||||||
|
// (les 6 champs presents dans le payload) avec des valeurs vides -> 422.
|
||||||
|
// C'est le bug corrige : avant, le back acceptait un onglet tout vide.
|
||||||
|
$client = $this->minimalClient(); // aucun champ comptable renseigne
|
||||||
|
|
||||||
|
$processor = $this->makeProcessor(
|
||||||
|
granted: ['commercial.clients.accounting.manage'],
|
||||||
|
payload: $this->emptyAccountingPayload(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->expectException(ValidationException::class);
|
||||||
|
$processor->process($client, $this->operation());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFullAccountingSubmitWithAllFieldsPasses(): void
|
||||||
|
{
|
||||||
|
// Les 6 champs obligatoires renseignes + type de reglement neutre
|
||||||
|
// (ni VIREMENT ni LCR -> ni banque ni RIB requis) -> 200.
|
||||||
|
$client = $this->minimalClient();
|
||||||
|
$client->setSiren('123456789');
|
||||||
|
$client->setAccountNumber('00012345678');
|
||||||
|
$client->setTvaMode(new TvaMode());
|
||||||
|
$client->setNTva('FR12345678901');
|
||||||
|
$client->setPaymentDelay(new PaymentDelay());
|
||||||
|
$client->setPaymentType($this->paymentType('CHEQUE'));
|
||||||
|
|
||||||
|
$processor = $this->makeProcessor(
|
||||||
|
granted: ['commercial.clients.accounting.manage'],
|
||||||
|
payload: $this->emptyAccountingPayload(),
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPartialAccountingPatchSkipsCompleteness(): void
|
||||||
|
{
|
||||||
|
// Un PATCH ciblant un seul champ comptable n'est pas une validation
|
||||||
|
// d'onglet : la completude n'est pas exigee (les autres champs restent
|
||||||
|
// vides) -> 200. Preserve l'edition ponctuelle (ex. Compta corrige le SIREN).
|
||||||
|
$client = $this->minimalClient();
|
||||||
|
$client->setSiren('999999999');
|
||||||
|
|
||||||
|
$processor = $this->makeProcessor(
|
||||||
|
granted: ['commercial.clients.accounting.manage'],
|
||||||
|
payload: ['siren' => '999999999'],
|
||||||
|
managed: true,
|
||||||
|
originalData: [
|
||||||
|
'siren' => '111111111',
|
||||||
|
'companyName' => 'TEST CO',
|
||||||
|
'triageService' => false,
|
||||||
|
'isArchived' => false,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertInstanceOf(Client::class, $processor->process($client, $this->operation()));
|
||||||
|
}
|
||||||
|
|
||||||
public function testCommercialeIncompleteInformationIsUnprocessable(): void
|
public function testCommercialeIncompleteInformationIsUnprocessable(): void
|
||||||
{
|
{
|
||||||
// RG-1.04 : role Commerciale + onglet Information incomplet -> 422.
|
// RG-1.04 : role Commerciale + onglet Information incomplet -> 422.
|
||||||
@@ -379,6 +441,7 @@ final class ClientProcessorTest extends TestCase
|
|||||||
$persist,
|
$persist,
|
||||||
new ClientFieldNormalizer(),
|
new ClientFieldNormalizer(),
|
||||||
new ClientInformationCompletenessValidator(),
|
new ClientInformationCompletenessValidator(),
|
||||||
|
new ClientAccountingCompletenessValidator(),
|
||||||
$security,
|
$security,
|
||||||
$requestStack,
|
$requestStack,
|
||||||
$em,
|
$em,
|
||||||
@@ -398,6 +461,25 @@ final class ClientProcessorTest extends TestCase
|
|||||||
return $client;
|
return $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload simulant une validation complete de l'onglet Comptabilite : les 6
|
||||||
|
* champs obligatoires presents (le front les envoie toujours ensemble). Les
|
||||||
|
* valeurs importent peu — la completude est evaluee sur l'etat de l'entite.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function emptyAccountingPayload(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'siren' => null,
|
||||||
|
'accountNumber' => null,
|
||||||
|
'tvaMode' => null,
|
||||||
|
'nTva' => null,
|
||||||
|
'paymentDelay' => null,
|
||||||
|
'paymentType' => null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
private function paymentType(string $code): PaymentType
|
private function paymentType(string $code): PaymentType
|
||||||
{
|
{
|
||||||
$type = new PaymentType();
|
$type = new PaymentType();
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# Les traductions natives FR viennent du vendor (validators.fr.xlf).
|
||||||
|
# Ce dossier accueille les overrides applicatifs eventuels.
|
||||||
Reference in New Issue
Block a user