Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Fix draft-event emails failing in the queue (#1407)

Dave Earley35778b1ace39
diff --git a/backend/app/DomainObjects/AccountUserDomainObject.php b/backend/app/DomainObjects/AccountUserDomainObject.php
index 1f1b95a18..b5aa44a76 100644
--- a/backend/app/DomainObjects/AccountUserDomainObject.php
+++ b/backend/app/DomainObjects/AccountUserDomainObject.php
@@ -6,6 +6,8 @@ class AccountUserDomainObject extends Generated\AccountUserDomainObjectAbstract
{
public ?AccountDomainObject $account = null;
+ public ?UserDomainObject $user = null;
+
public function getAccount(): ?AccountDomainObject
{
return $this->account;
@@ -17,4 +19,16 @@ class AccountUserDomainObject extends Generated\AccountUserDomainObjectAbstract
return $this;
}
+
+ public function getUser(): ?UserDomainObject
+ {
+ return $this->user;
+ }
+
+ public function setUser(?UserDomainObject $user): static
+ {
+ $this->user = $user;
+
+ return $this;
+ }
}
diff --git a/backend/app/Services/Domain/Mail/UnpublishedEventMailPolicy.php b/backend/app/Services/Domain/Mail/UnpublishedEventMailPolicy.php
index d5b535d92..74cb56bef 100644
--- a/backend/app/Services/Domain/Mail/UnpublishedEventMailPolicy.php
+++ b/backend/app/Services/Domain/Mail/UnpublishedEventMailPolicy.php
@@ -4,13 +4,14 @@ namespace HiEvents\Services\Domain\Mail;
use HiEvents\DomainObjects\AccountUserDomainObject;
use HiEvents\DomainObjects\EventDomainObject;
+use HiEvents\DomainObjects\Generated\AccountUserDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\EventDomainObjectAbstract;
use HiEvents\DomainObjects\Status\EventStatus;
use HiEvents\DomainObjects\Status\UserStatus;
use HiEvents\DomainObjects\UserDomainObject;
use HiEvents\Repository\Eloquent\Value\Relationship;
+use HiEvents\Repository\Interfaces\AccountUserRepositoryInterface;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
-use HiEvents\Repository\Interfaces\UserRepositoryInterface;
class UnpublishedEventMailPolicy
{
@@ -21,7 +22,7 @@ class UnpublishedEventMailPolicy
public function __construct(
private readonly EventRepositoryInterface $eventRepository,
- private readonly UserRepositoryInterface $userRepository,
+ private readonly AccountUserRepositoryInterface $accountUserRepository,
) {}
/**
@@ -41,11 +42,14 @@ class UnpublishedEventMailPolicy
return false;
}
- $verifiedMemberEmails = ($this->userRepository
- ->loadRelation(new Relationship(domainObject: AccountUserDomainObject::class, name: 'currentAccountUser'))
- ->findUsersByAccountId($event->getAccountId()) ?? collect())
- ->filter(fn (UserDomainObject $user) => $user->getEmailVerifiedAt() !== null
- && $user->getCurrentAccountUser()?->getStatus() === UserStatus::ACTIVE->name)
+ $verifiedMemberEmails = $this->accountUserRepository
+ ->loadRelation(new Relationship(domainObject: UserDomainObject::class, name: 'user'))
+ ->findWhere([
+ AccountUserDomainObjectAbstract::ACCOUNT_ID => $event->getAccountId(),
+ AccountUserDomainObjectAbstract::STATUS => UserStatus::ACTIVE->name,
+ ])
+ ->map(fn (AccountUserDomainObject $accountUser) => $accountUser->getUser())
+ ->filter(fn (?UserDomainObject $user) => $user?->getEmailVerifiedAt() !== null)
->map(fn (UserDomainObject $user) => mb_strtolower(trim($user->getEmail())))
->all();
diff --git a/backend/tests/Feature/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php b/backend/tests/Feature/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php
new file mode 100644
index 000000000..3d0f6b34b
--- /dev/null
+++ b/backend/tests/Feature/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php
@@ -0,0 +1,152 @@
+<?php
+
+namespace Tests\Feature\Services\Domain\Mail;
+
+use HiEvents\DomainObjects\EventDomainObject;
+use HiEvents\DomainObjects\Status\EventStatus;
+use HiEvents\Models\Account;
+use HiEvents\Models\AccountUser;
+use HiEvents\Models\User;
+use HiEvents\Services\Domain\Mail\UnpublishedEventMailPolicy;
+use Illuminate\Foundation\Testing\DatabaseTransactions;
+use Illuminate\Support\Facades\DB;
+use ReflectionProperty;
+use Tests\TestCase;
+
+class UnpublishedEventMailPolicyTest extends TestCase
+{
+ use DatabaseTransactions;
+
+ private ?int $previousCurrentAccountId = null;
+
+ private bool $hadCurrentAccountId = false;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $property = new ReflectionProperty(User::class, 'currentAccountId');
+ $this->hadCurrentAccountId = $property->isInitialized();
+ $this->previousCurrentAccountId = $this->hadCurrentAccountId ? $property->getValue() : null;
+ }
+
+ protected function tearDown(): void
+ {
+ if ($this->hadCurrentAccountId) {
+ User::setCurrentAccountId($this->previousCurrentAccountId);
+ }
+
+ parent::tearDown();
+ }
+
+ public function test_recognises_verified_members_when_no_account_is_current(): void
+ {
+ $account = $this->seedAccount();
+ $owner = $this->seedMember($account, 'owner');
+ $event = $this->draftEvent($account);
+
+ User::setCurrentAccountId(null);
+
+ $this->assertFalse($this->policy()->shouldWithholdEventContent($event, [strtoupper($owner)]));
+ $this->assertTrue($this->policy()->shouldWithholdEventContent($event, ['[email protected]']));
+ }
+
+ public function test_checks_the_events_account_rather_than_the_current_one(): void
+ {
+ $account = $this->seedAccount();
+ $otherAccount = $this->seedAccount();
+ $owner = $this->seedMember($account, 'owner');
+ $otherOwner = $this->seedMember($otherAccount, 'other-owner');
+ $event = $this->draftEvent($account);
+
+ User::setCurrentAccountId($otherAccount->id);
+
+ $this->assertFalse($this->policy()->shouldWithholdEventContent($event, [$owner]));
+ $this->assertTrue($this->policy()->shouldWithholdEventContent($event, [$otherOwner]));
+ }
+
+ public function test_withholds_content_from_invited_unverified_and_removed_members(): void
+ {
+ $account = $this->seedAccount();
+ $owner = $this->seedMember($account, 'owner');
+ $invited = $this->seedMember($account, 'invited', status: 'INVITED');
+ $unverified = $this->seedMember($account, 'unverified', verified: false);
+ $removed = $this->seedMember($account, 'removed', removed: true);
+ $event = $this->draftEvent($account);
+
+ User::setCurrentAccountId(null);
+
+ $this->assertFalse($this->policy()->shouldWithholdEventContent($event, [$owner]));
+ $this->assertTrue($this->policy()->shouldWithholdEventContent($event, [$invited]));
+ $this->assertTrue($this->policy()->shouldWithholdEventContent($event, [$unverified]));
+ $this->assertTrue($this->policy()->shouldWithholdEventContent($event, [$removed]));
+ }
+
+ private function policy(): UnpublishedEventMailPolicy
+ {
+ return $this->app->make(UnpublishedEventMailPolicy::class);
+ }
+
+ private function seedAccount(): Account
+ {
+ return Account::forceCreate([
+ 'name' => 'Draft Mail Account',
+ 'email' => 'account-'.uniqid().'@example.com',
+ 'timezone' => 'UTC',
+ 'currency_code' => 'USD',
+ 'short_id' => 'acc_'.uniqid(),
+ 'account_verified_at' => now(),
+ ]);
+ }
+
+ private function seedMember(
+ Account $account,
+ string $name,
+ string $status = 'ACTIVE',
+ bool $verified = true,
+ bool $removed = false,
+ ): string {
+ $email = $name.'+'.uniqid().'@example.com';
+
+ $user = User::forceCreate([
+ 'email' => $email,
+ 'first_name' => 'Team',
+ 'last_name' => 'Member',
+ 'password' => 'hash',
+ 'timezone' => 'UTC',
+ 'locale' => 'en',
+ 'email_verified_at' => $verified ? now() : null,
+ ]);
+
+ AccountUser::forceCreate([
+ 'account_id' => $account->id,
+ 'user_id' => $user->id,
+ 'role' => 'ADMIN',
+ 'status' => $status,
+ 'is_account_owner' => false,
+ 'deleted_at' => $removed ? now() : null,
+ ]);
+
+ return $email;
+ }
+
+ private function draftEvent(Account $account): EventDomainObject
+ {
+ $eventId = DB::table('events')->insertGetId([
+ 'title' => 'Draft Event',
+ 'account_id' => $account->id,
+ 'user_id' => $account->users()->first()->id,
+ 'short_id' => 'evt_'.uniqid(),
+ 'status' => EventStatus::DRAFT->name,
+ 'timezone' => 'UTC',
+ 'currency' => 'USD',
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ return (new EventDomainObject)
+ ->setId($eventId)
+ ->setAccountId($account->id)
+ ->setStatus(EventStatus::DRAFT->name);
+ }
+}
diff --git a/backend/tests/Unit/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php b/backend/tests/Unit/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php
index 27199579d..4051a52b9 100644
--- a/backend/tests/Unit/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php
+++ b/backend/tests/Unit/Services/Domain/Mail/UnpublishedEventMailPolicyTest.php
@@ -7,8 +7,8 @@ use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\Status\EventStatus;
use HiEvents\DomainObjects\Status\UserStatus;
use HiEvents\DomainObjects\UserDomainObject;
+use HiEvents\Repository\Interfaces\AccountUserRepositoryInterface;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
-use HiEvents\Repository\Interfaces\UserRepositoryInterface;
use HiEvents\Services\Domain\Mail\UnpublishedEventMailPolicy;
use Mockery;
use Mockery\MockInterface;
@@ -19,7 +19,7 @@ class UnpublishedEventMailPolicyTest extends TestCase
{
private EventRepositoryInterface|MockInterface $eventRepository;
- private UserRepositoryInterface|MockInterface $userRepository;
+ private AccountUserRepositoryInterface|MockInterface $accountUserRepository;
private UnpublishedEventMailPolicy $policy;
@@ -28,16 +28,16 @@ class UnpublishedEventMailPolicyTest extends TestCase
parent::setUp();
$this->eventRepository = Mockery::mock(EventRepositoryInterface::class);
- $this->userRepository = Mockery::mock(UserRepositoryInterface::class);
- $this->policy = new UnpublishedEventMailPolicy($this->eventRepository, $this->userRepository);
-
- $this->userRepository->shouldReceive('loadRelation')->andReturnSelf();
- $this->userRepository->shouldReceive('findUsersByAccountId')->with(9)->andReturn(collect([
- $this->member('[email protected]', '2026-01-01 00:00:00', UserStatus::ACTIVE),
- $this->member('[email protected]', null, UserStatus::ACTIVE),
- $this->member('[email protected]', '2026-01-01 00:00:00', UserStatus::INVITED),
- $this->member('[email protected]', '2026-01-01 00:00:00', UserStatus::INACTIVE),
- ]));
+ $this->accountUserRepository = Mockery::mock(AccountUserRepositoryInterface::class);
+ $this->policy = new UnpublishedEventMailPolicy($this->eventRepository, $this->accountUserRepository);
+
+ $this->accountUserRepository->shouldReceive('loadRelation')->andReturnSelf();
+ $this->accountUserRepository->shouldReceive('findWhere')
+ ->with(['account_id' => 9, 'status' => UserStatus::ACTIVE->name])
+ ->andReturn(collect([
+ $this->member('[email protected]', '2026-01-01 00:00:00'),
+ $this->member('[email protected]', null),
+ ]));
}
protected function tearDown(): void
@@ -133,11 +133,10 @@ class UnpublishedEventMailPolicyTest extends TestCase
return (new EventDomainObject)->setId(5)->setAccountId(9)->setStatus(EventStatus::DRAFT->name);
}
- private function member(string $email, ?string $verifiedAt, UserStatus $status): UserDomainObject
+ private function member(string $email, ?string $verifiedAt): AccountUserDomainObject
{
- return (new UserDomainObject)
- ->setEmail($email)
- ->setEmailVerifiedAt($verifiedAt)
- ->setCurrentAccountUser((new AccountUserDomainObject)->setStatus($status->name));
+ return (new AccountUserDomainObject)
+ ->setStatus(UserStatus::ACTIVE->name)
+ ->setUser((new UserDomainObject)->setEmail($email)->setEmailVerifiedAt($verifiedAt));
}
}