Skip to content

Commit a3459ad

Browse files
committed
fix: review feedback
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent 04376d4 commit a3459ad

4 files changed

Lines changed: 37 additions & 49 deletions

File tree

app/Models/Foundation/Main/Member.php

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Doctrine\DBAL\Exception;
2323
use Doctrine\ORM\Query\ResultSetMappingBuilder;
2424
use Illuminate\Support\Facades\Config;
25-
use Doctrine\DBAL\ParameterType;
2625
use LaravelDoctrine\ORM\Facades\EntityManager;
2726
use models\summit\Presentation;
2827
use models\summit\SummitMetric;
@@ -1861,14 +1860,38 @@ public function getActiveSummitsSponsorMemberships()
18611860
return [];
18621861
}
18631862

1864-
// Step 2 — load all sponsors in a single IN query. findBy() uses PK-based hydration
1865-
// which avoids the ORM 3 assertion failure triggered by the OneToOne inverse associations
1866-
// on Sponsor (lead_report_setting, sponsorservices_statistics) that DQL/native-query
1867-
// hydration hits. The result set is then re-sorted to match the SQL ORDER BY.
1868-
$position = array_flip($ids);
1869-
$sponsors = EntityManager::getRepository(Sponsor::class)->findBy(['id' => $ids]);
1870-
usort($sponsors, fn($a, $b) => $position[$a->getId()] <=> $position[$b->getId()]);
1871-
return $sponsors;
1863+
return $this->loadSponsorsByIds($ids);
1864+
}
1865+
1866+
/**
1867+
* @param Summit $summit
1868+
* @return ArrayCollection
1869+
* @throws Exception
1870+
*/
1871+
public function getAccessibleSponsorsBySummit(Summit $summit): ArrayCollection
1872+
{
1873+
$ids = $this->getSponsorMembershipIds($summit);
1874+
1875+
return new ArrayCollection($this->loadSponsorsByIds($ids));
1876+
}
1877+
1878+
/**
1879+
* Loads Sponsor entities by PK using DQL rather than native-query hydration.
1880+
* findBy()-style PK hydration avoids the ORM 3 assertion failure triggered by
1881+
* the OneToOne inverse associations on Sponsor (lead_report_setting,
1882+
* sponsorservices_statistics) that DQL/native-query hydration hits.
1883+
*
1884+
* @param int[] $ids
1885+
* @return Sponsor[]
1886+
*/
1887+
private function loadSponsorsByIds(array $ids): array
1888+
{
1889+
if (empty($ids)) return [];
1890+
1891+
return $this->getEM()
1892+
->createQuery('SELECT s FROM ' . Sponsor::class . ' s WHERE s.id IN (:ids) ORDER BY s.id ASC')
1893+
->setParameter('ids', $ids)
1894+
->getResult();
18721895
}
18731896

18741897
/**
@@ -1991,41 +2014,6 @@ public function addSummitRegistrationOrder(SummitOrder $summit_order)
19912014
$summit_order->setOwner($this);
19922015
}
19932016

1994-
/**
1995-
* @param Summit $summit
1996-
* @return ArrayCollection
1997-
* @throws Exception
1998-
*/
1999-
public function getAllowedSponsorsBySummit(Summit $summit): ArrayCollection
2000-
{
2001-
$sql = <<<SQL
2002-
SELECT su.SponsorID
2003-
FROM Sponsor_Users su
2004-
INNER JOIN Sponsor s ON s.ID = su.SponsorID
2005-
WHERE su.MemberID = :member_id
2006-
AND s.SummitID = :summit_id
2007-
AND (
2008-
JSON_CONTAINS(COALESCE(su.Permissions, '[]'), JSON_QUOTE(:slug_sponsors))
2009-
OR JSON_CONTAINS(COALESCE(su.Permissions, '[]'), JSON_QUOTE(:slug_external))
2010-
)
2011-
SQL;
2012-
$ids = $this->prepareRawSQL($sql, [
2013-
'member_id' => $this->getId(),
2014-
'summit_id' => $summit->getId(),
2015-
'slug_sponsors' => IGroup::Sponsors,
2016-
'slug_external' => IGroup::SponsorExternalUsers,
2017-
])->executeQuery()->fetchFirstColumn();
2018-
2019-
if (empty($ids)) {
2020-
return new ArrayCollection();
2021-
}
2022-
2023-
$position = array_flip($ids);
2024-
$sponsors = $this->getEM()->getRepository(Sponsor::class)->findBy(['id' => $ids]);
2025-
usort($sponsors, fn($a, $b) => $position[$a->getId()] <=> $position[$b->getId()]);
2026-
return new ArrayCollection($sponsors);
2027-
}
2028-
20292017
/**
20302018
* @param Summit $summit
20312019
* @param int $sponsor_id

app/Services/Model/Imp/SponsorUserInfoGrantService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function addBadgeScan(Summit $summit, Member $current_member, array $data
178178
if(is_null($badge))
179179
throw new EntityNotFoundException("badge not found.");
180180

181-
$member_sponsors = $current_member->getAllowedSponsorsBySummit($summit);
181+
$member_sponsors = $current_member->getAccessibleSponsorsBySummit($summit);
182182

183183
if ($member_sponsors->isEmpty())
184184
throw new ValidationException("Current member does not have badge scan permissions for any sponsor of this summit.");

database/migrations/model/Version20260408102410.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function up(Schema $schema): void
4343
SELECT JSON_ARRAYAGG(g.Code)
4444
FROM Group_Members gm
4545
INNER JOIN `Group` g ON g.ID = gm.GroupID
46-
WHERE gm.MemberID = su.MemberID
46+
WHERE gm.MemberID = su.MemberID AND g.Code IN ('sponsors', 'sponsor-external-users')
4747
)
4848
WHERE su.Permissions IS NULL
4949
SQL

tests/oauth2/OAuth2SummitBadgeScanApiControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function testAddEncryptedBadgeScan(){
9191
self::$em->flush();
9292

9393
$this->assertTrue($sponsor->hasUser(self::$member));
94-
$this->assertGreaterThan(0, self::$member->getAllowedSponsorsBySummit(self::$summit)->count());
94+
$this->assertGreaterThan(0, self::$member->getAccessibleSponsorsBySummit(self::$summit)->count());
9595

9696
$badge = $attendee->getFirstTicket()->getBadge();
9797
$badge_qr_code = $badge->generateQRCode();
@@ -374,7 +374,7 @@ public function testAddBadgeScanWithMultipleSponsorsWithoutSponsorId()
374374
self::$member->addSponsorPermission($sponsor1->getId(), IGroup::Sponsors);
375375
self::$member->addSponsorPermission($sponsor2->getId(), IGroup::Sponsors);
376376

377-
$this->assertGreaterThan(1, self::$member->getAllowedSponsorsBySummit(self::$summit)->count());
377+
$this->assertGreaterThan(1, self::$member->getAccessibleSponsorsBySummit(self::$summit)->count());
378378

379379
$params = [
380380
'id' => self::$summit->getId(),
@@ -422,7 +422,7 @@ public function testAddBadgeScanWithMultipleSponsorsWithSponsorId()
422422
self::$member->addSponsorPermission($sponsor1->getId(), IGroup::Sponsors);
423423
self::$member->addSponsorPermission($sponsor2->getId(), IGroup::Sponsors);
424424

425-
$this->assertGreaterThan(1, self::$member->getAllowedSponsorsBySummit(self::$summit)->count());
425+
$this->assertGreaterThan(1, self::$member->getAccessibleSponsorsBySummit(self::$summit)->count());
426426

427427
$params = [
428428
'id' => self::$summit->getId(),

0 commit comments

Comments
 (0)