From 07ea2b771c8581066e164ba0a0390c59fd38d744 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 22 Apr 2022 16:37:21 +0200 Subject: [PATCH] person: add validation to relationship to avoid duplicate --- .../Entity/Relationships/Relationship.php | 2 + .../Relationship/RelationshipNoDuplicate.php | 27 +++++++++ .../RelationshipNoDuplicateValidator.php | 56 +++++++++++++++++++ .../translations/validators.fr.yml | 4 ++ 4 files changed, 89 insertions(+) create mode 100644 src/Bundle/ChillPersonBundle/Validator/Constraints/Relationship/RelationshipNoDuplicate.php create mode 100644 src/Bundle/ChillPersonBundle/Validator/Constraints/Relationship/RelationshipNoDuplicateValidator.php diff --git a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php index d51881bb2..62fbb63a9 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php +++ b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php @@ -15,6 +15,7 @@ use Chill\MainBundle\Doctrine\Model\TrackCreationInterface; use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface; use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Validator\Constraints\Relationship\RelationshipNoDuplicate; use DateTimeImmutable; use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; @@ -31,6 +32,7 @@ use Symfony\Component\Validator\Constraints as Assert; * @DiscriminatorMap(typeProperty="type", mapping={ * "relationship": Relationship::class * }) + * @RelationshipNoDuplicate */ class Relationship implements TrackCreationInterface, TrackUpdateInterface { diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/Relationship/RelationshipNoDuplicate.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/Relationship/RelationshipNoDuplicate.php new file mode 100644 index 000000000..db4d7c0d6 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/Relationship/RelationshipNoDuplicate.php @@ -0,0 +1,27 @@ +relationshipRepository = $relationshipRepository; + } + + public function validate($relationship, Constraint $constraint) + { + if (!$constraint instanceof RelationshipNoDuplicate) { + throw new UnexpectedTypeException($constraint, RelationshipNoDuplicate::class); + } + + $fromPerson = $relationship->getFromPerson(); + $toPerson = $relationship->getToPerson(); + + $relationships = $this->relationshipRepository->findBy([ + 'fromPerson' => $fromPerson, + 'fromPerson' => $toPerson, + 'toPerson' => $fromPerson, + 'toPerson' => $fromPerson, + ]); + + foreach ($relationships as $r) { + if ( + $r->getFromPerson() === $fromPerson + || $r->getFromPerson() === $toPerson + || $r->getToPerson() === $fromPerson + || $r->getToPerson() === $toPerson + ) { + $this->context->buildViolation($constraint->message) + ->addViolation(); + } + } + } +} diff --git a/src/Bundle/ChillPersonBundle/translations/validators.fr.yml b/src/Bundle/ChillPersonBundle/translations/validators.fr.yml index 13fcb54fb..b2bc549d7 100644 --- a/src/Bundle/ChillPersonBundle/translations/validators.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/validators.fr.yml @@ -62,3 +62,7 @@ You cannot associate a resource with the same person: Vous ne pouvez pas ajouter #location The period must remain located: 'Un parcours doit être localisé' The person where the course is located must be associated to the course. Change course's location before removing the person.: "Le parcours est localisé auprès cet usager. Veuillez changer la localisation du parcours avant de suprimer l'usager" + +#relationship +relationship: + duplicate: Une relation de filiation existe déjà entre ces 2 personnes \ No newline at end of file