Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: comparisons for discharge/abrogation dates #1938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions module/Decision/src/Mapper/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Decision\Mapper;

use Application\Mapper\BaseMapper;
use DateTime;
use Decision\Model\Member as MemberModel;
use Decision\Model\Organ as OrganModel;
use Decision\Model\OrganMember as OrganMemberModel;
Expand Down Expand Up @@ -128,9 +129,13 @@ public function findOrgans(MemberModel $member): array
->join('o.members', 'om')
->join('om.member', 'm')
->where('m.lidnr = :lidnr')
->andWhere('om.dischargeDate IS NULL');
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('om.dischargeDate'),
$qb->expr()->gt('om.dischargeDate', ':now'),
));

$qb->setParameter('lidnr', $member->getLidnr());
$qb->setParameter('lidnr', $member->getLidnr())
->setParameter('now', new DateTime());

return $qb->getQuery()->getResult();
}
Expand All @@ -147,10 +152,14 @@ public function findCurrentInstallations(MemberModel $member): array
->from(OrganMemberModel::class, 'om')
->leftJoin('om.organ', 'o')
->where('om.member = :member')
->andWhere('om.installDate <= CURRENT_TIMESTAMP()')
->andWhere('om.dischargeDate IS NULL OR om.dischargeDate > CURRENT_TIMESTAMP()');
->andWhere('om.installDate <= :now')
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('om.dischargeDate'),
$qb->expr()->gt('om.dischargeDate', ':now'),
));

$qb->setParameter('member', $member);
$qb->setParameter('member', $member)
->setParameter('now', new DateTime());

return $qb->getQuery()->getResult();
}
Expand All @@ -167,10 +176,13 @@ public function findHistoricalInstallations(MemberModel $member): array
->from(OrganMemberModel::class, 'om')
->leftJoin('om.organ', 'o')
->where('om.member = :member')
->andWhere('om.dischargeDate IS NOT NULL')
->andWhere('om.dischargeDate <= CURRENT_TIMESTAMP()');
->andWhere($qb->expr()->andX(
$qb->expr()->isNotNull('om.dischargeDate'),
$qb->expr()->lte('om.dischargeDate', ':now'),
));

$qb->setParameter('member', $member);
$qb->setParameter('member', $member)
->setParameter('now', new DateTime());

return $qb->getQuery()->getResult();
}
Expand Down
38 changes: 26 additions & 12 deletions module/Decision/src/Mapper/Organ.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Decision\Mapper;

use Application\Mapper\BaseMapper;
use DateTime;
use Decision\Model\Enums\OrganTypes;
use Decision\Model\Organ as OrganModel;
use Doctrine\ORM\NonUniqueResultException;
Expand All @@ -26,28 +27,37 @@ class Organ extends BaseMapper
*/
public function findActive(?OrganTypes $type = null): array
{
$criteria = [
'abrogationDate' => null,
];
$qb = $this->getRepository()->createQueryBuilder('o');
$qb->where($qb->expr()->orX(
$qb->expr()->isNull('o.abrogationDate'),
$qb->expr()->gt('o.abrogationDate', ':now'),
))
->setParameter('now', new DateTime());

if (null !== $type) {
$criteria['type'] = $type;
$qb->andWhere('o.type = :type')
->setParameter('type', $type);
}

return $this->getRepository()->findBy($criteria);
return $qb->getQuery()->getResult();
}

/**
* Check if an organ with id `$id` is not abrogated.
*/
public function findActiveById(int $id): ?OrganModel
{
return $this->getRepository()->findOneBy(
[
'id' => $id,
'abrogationDate' => null,
],
);
$qb = $this->getRepository()->createQueryBuilder('o');
$qb->where('o.id = :id')
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('o.abrogationDate'),
$qb->expr()->gt('o.abrogationDate', ':now'),
));

$qb->setParameter('id', $id)
->setParameter('now', new DateTime());

return $qb->getQuery()->getOneOrNullResult();
}

/**
Expand All @@ -58,7 +68,11 @@ public function findActiveById(int $id): ?OrganModel
public function findAbrogated(?OrganTypes $type = null): array
{
$qb = $this->getRepository()->createQueryBuilder('o');
$qb->where('o.abrogationDate IS NOT NULL')
$qb->where($qb->expr()->andX(
$qb->expr()->isNotNull('o.abrogationDate'),
$qb->expr()->lte('o.abrogationDate', ':now'),
))
->setParameter('now', new DateTime())
->orderBy('o.abrogationDate', 'DESC');

if (null !== $type) {
Expand Down
6 changes: 3 additions & 3 deletions module/Decision/src/Model/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ static function (BoardMember $boardMember) use ($today) {
$dischargeDate = $boardMember->getDischargeDate();

// Keep installation if not discharged or discharged in the future
return null === $dischargeDate || $dischargeDate >= $today;
return null === $dischargeDate || $dischargeDate > $today;
},
);

Expand Down Expand Up @@ -855,12 +855,12 @@ protected function isCurrentBoard(BoardMember $boardMember): bool
// Installation was (before) today.
if (
null === $releaseDate
|| $releaseDate >= $now
|| $releaseDate > $now
) {
// Not yet released or the release is the in the future.
if (
null === $dischargeDate
|| $dischargeDate >= $now
|| $dischargeDate > $now
) {
// Not yet discharged or the discharge is in the future.
return true;
Expand Down
2 changes: 1 addition & 1 deletion module/User/src/Permissions/Assertion/IsOrganMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ protected function isCurrentMember(OrganMember $organMember): bool
$now = new DateTime();

return $organMember->getInstallDate() <= $now &&
(null === $organMember->getDischargeDate() || $organMember->getDischargeDate() >= $now);
(null === $organMember->getDischargeDate() || $organMember->getDischargeDate() > $now);
}
}
Loading