person: add validation to relationship to avoid duplicate

This commit is contained in:
nobohan 2022-04-22 16:37:21 +02:00
parent 83dd8f810c
commit 07ea2b771c
4 changed files with 89 additions and 0 deletions

View File

@ -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
{

View File

@ -0,0 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\Relationship;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class RelationshipNoDuplicate extends Constraint
{
public $message = 'relationship.duplicate';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}

View File

@ -0,0 +1,56 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\Relationship;
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class RelationshipNoDuplicateValidator extends ConstraintValidator
{
private RelationshipRepository $relationshipRepository;
public function __construct(RelationshipRepository $relationshipRepository)
{
$this->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();
}
}
}
}

View File

@ -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