diff --git a/app/Event.php b/app/Event.php index 2c4060212..0857e36e1 100644 --- a/app/Event.php +++ b/app/Event.php @@ -416,6 +416,11 @@ public function approve() { $data = ['status' => 'APPROVED', 'approved_by' => auth()->user()->id]; + // Imported/Nova-created events can reach approval without a code, and the email prints it verbatim. + if (empty($this->codeweek_for_all_participation_code)) { + $data['codeweek_for_all_participation_code'] = 'cw'.now()->format('y').'-'.Str::random(5); + } + $this->update($data); if (! empty($this->user_email)) { diff --git a/resources/views/event/show.blade.php b/resources/views/event/show.blade.php index eace7636a..472a4d8a9 100644 --- a/resources/views/event/show.blade.php +++ b/resources/views/event/show.blade.php @@ -75,7 +75,7 @@ @endif
- +
@canany(['edit', 'delete'], $event) diff --git a/tests/Feature/ApproveEventTest.php b/tests/Feature/ApproveEventTest.php index ff6501eb4..bbb036125 100644 --- a/tests/Feature/ApproveEventTest.php +++ b/tests/Feature/ApproveEventTest.php @@ -194,4 +194,31 @@ public function ambassadors_of_right_country_can_see_the_approve_banner(): void ->assertSee('moderate-event'); } + + #[Test] + public function approving_event_without_participation_code_generates_one_for_the_email(): void + { + $this->withExceptionHandling(); + + Mail::fake(); + + $superadmin = \App\User::factory()->create(); + $superadmin->assignRole('super admin'); + + $this->signIn($superadmin); + + $event = \App\Event::factory()->create([ + 'status' => 'PENDING', + 'user_email' => 'foo@bar.com', + 'codeweek_for_all_participation_code' => null, + ]); + + $event->approve(); + + $event = $event->fresh(); + $code = (string) $event->codeweek_for_all_participation_code; + + $this->assertMatchesRegularExpression('/^cw\d{2}-[A-Za-z0-9]+$/', $code); + $this->assertStringContainsString($code, (new EventApproved($event, $event->owner))->render()); + } }