fix validation groups and sequential validator msg

This commit is contained in:
Julien Fastré 2021-06-14 16:39:27 +02:00
parent b2c1d75fc5
commit 41617295c1
4 changed files with 32 additions and 7 deletions

View File

@ -1209,6 +1209,20 @@ class Person implements HasCenterInterface
return $this->householdParticipations;
}
public function getHouseholdParticipationsShareHousehold(): Collection
{
$criteria = new Criteria();
$expr = Criteria::expr();
$criteria->where(
$expr->eq('shareHousehold', true)
);
return $this->getHouseholdParticipations()
->matching($criteria)
;
}
public function getCurrentHousehold(?\DateTimeImmutable $at = null): ?Household
{
$criteria = new Criteria();

View File

@ -19,6 +19,8 @@ class MembersEditor
private array $persistables = [];
private array $membershipsAffected = [];
public const VALIDATION_GROUP = 'household_memberships';
public function __construct(ValidatorInterface $validator, ?Household $household)
{
$this->validator = $validator;
@ -41,12 +43,12 @@ class MembersEditor
$this->household->addMember($membership);
if ($position->getShareHousehold()) {
foreach ($person->getHouseholdParticipations() as $participation) {
if (FALSE === $participation->getShareHousehold()) {
foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) {
if ($participation === $membership) {
continue;
}
if ($participation === $membership) {
if ($participation->getStartDate() > $membership->getStartDate()) {
continue;
}
@ -92,7 +94,13 @@ class MembersEditor
public function validate(): ConstraintViolationListInterface
{
return $this->validator->validate($this->getHousehold(), null, [ "memberships" ]);
$list = $this->validator->validate($this->getHousehold(), null, [ self::VALIDATION_GROUP ]);
foreach ($this->membershipsAffected as $m) {
$list->addAll($this->validator->validate($m, null, [ self::VALIDATION_GROUP ]));
}
return $list;
}
public function getPersistable(): array

View File

@ -29,7 +29,7 @@ class HouseholdMembershipSequentialValidator extends ConstraintValidator
throw new UnexpectedTypeException($constraint, Person::class);
}
$participations = $person->getHouseholdParticipations();
$participations = $person->getHouseholdParticipationsShareHousehold();
if ($participations->count() === 0) {
return;
@ -47,6 +47,7 @@ class HouseholdMembershipSequentialValidator extends ConstraintValidator
if ($covers->hasIntersections()) {
foreach ($covers->getIntersections() as list($start, $end, $metadata)) {
$participation = $participations[$metadata[0]];
$nbHousehold = count($metadata);
$this->context
->buildViolation("household_membership.Person with membership covering")
@ -55,7 +56,9 @@ class HouseholdMembershipSequentialValidator extends ConstraintValidator
$participation->getPerson(), []
),
// TODO when date is correctly i18n, fix this
'%from%' => $start->format('d-m-Y')
'%from%' => $start->format('d-m-Y'),
'%nbHousehold%' => $nbHousehold,
])
->addViolation()
;

View File

@ -40,4 +40,4 @@ household:
max_holder_overflowed: Il ne peut y avoir plus de deux titulaires simultanément. Or, avec cette modification, ce nombre sera dépassé entre le {{ start }} et le {{ end }}.
household_membership:
The end date must be after start date: La date de la fin de l'appartenance doit être postérieure à la date de début.
Person with membership covering: Une personne ne peut pas appartenir à deux ménages simultanément. Or, avec cette modification, %person_name% appartiendrait à deux ménages à partir du %from%.
Person with membership covering: Une personne ne peut pas appartenir à deux ménages simultanément. Or, avec cette modification, %person_name% appartiendrait à %nbHousehold% ménages à partir du %from%.