Add mail to participant

This commit is contained in:
philipp lang 2024-03-15 00:28:57 +01:00
parent 2a979d932e
commit 5d576cda6a
5 changed files with 141 additions and 0 deletions

View File

@ -31,6 +31,8 @@ class RegisterAction
$form->getFields()->each(fn ($field) => $field->afterRegistration($form, $participant, $input));
$participant->sendConfirmationMail();
return $participant;
}

View File

@ -2,10 +2,12 @@
namespace App\Form\Data;
use App\Form\Enums\SpecialType;
use App\Form\Fields\Field;
use App\Form\Fields\NamiField;
use App\Form\Models\Form;
use Illuminate\Support\Collection;
use stdClass;
/**
* @extends Collection<int, Field>
@ -33,6 +35,22 @@ class FieldCollection extends Collection
return $this->filter(fn ($field) => !is_a($field, NamiField::class));
}
/**
* @return stdClass
*/
public function getMailRecipient(): ?stdClass
{
$firstname = $this->findBySpecialType(SpecialType::FIRSTNAME)?->value;
$lastname = $this->findBySpecialType(SpecialType::LASTNAME)?->value;
$email = $this->findBySpecialType(SpecialType::EMAIL)?->value;
return $firstname && $lastname && $email
? (object) [
'name' => "$firstname $lastname",
"email" => $email,
] : null;
}
/**
* @param array<string, mixed> $input
*/
@ -54,4 +72,9 @@ class FieldCollection extends Collection
return $attributes->toArray();
}
private function findBySpecialType(SpecialType $specialType): ?Field
{
return $this->first(fn ($field) => $field->specialType === $specialType);
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace App\Form\Mails;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class ConfirmRegistrationMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope()
{
return new Envelope(
subject: 'Confirm Registration Mail',
);
}
/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
return new Content(
view: 'view.name',
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
return [];
}
}

View File

@ -3,12 +3,14 @@
namespace App\Form\Models;
use App\Form\Data\FieldCollection;
use App\Form\Mails\ConfirmRegistrationMail;
use App\Form\Scopes\ParticipantFilterScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Mail;
class Participant extends Model
{
@ -49,4 +51,13 @@ class Participant extends Model
{
return FieldCollection::fromRequest($this->form, $this->data);
}
public function sendConfirmationMail(): void
{
if (!$this->getFields()->getMailRecipient()) {
return;
}
Mail::to($this->getFields()->getMailRecipient())->queue(new ConfirmRegistrationMail());
}
}

View File

@ -3,6 +3,8 @@
namespace Tests\Feature\Form;
use App\Form\Enums\NamiType;
use App\Form\Enums\SpecialType;
use App\Form\Mails\ConfirmRegistrationMail;
use App\Form\Models\Form;
use App\Group;
use App\Group\Enums\Level;
@ -10,6 +12,7 @@ use App\Member\Member;
use Carbon\Carbon;
use Generator;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Mail;
use Illuminate\Testing\TestResponse;
class FormRegisterActionTest extends FormTestCase
@ -17,6 +20,12 @@ class FormRegisterActionTest extends FormTestCase
use DatabaseTransactions;
public function setUp(): void
{
parent::setUp();
Mail::fake();
}
public function testItSavesParticipantAsModel(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
@ -42,6 +51,43 @@ class FormRegisterActionTest extends FormTestCase
$this->assertEquals('Abraham', $participants->first()->data['spitzname']);
}
public function testItSendsEmailToParticipant(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()
->sections([
FormtemplateSectionRequest::new()->fields([
$this->textField('vorname')->specialType(SpecialType::FIRSTNAME),
$this->textField('nachname')->specialType(SpecialType::LASTNAME),
$this->textField('email')->specialType(SpecialType::EMAIL),
]),
])
->create();
$this->register($form, ['vorname' => 'Lala', 'nachname' => 'GG', 'email' => 'example@test.test'])
->assertOk();
Mail::assertQueued(ConfirmRegistrationMail::class, fn ($message) => $message->hasTo('example@test.test', 'Lala GG'));
}
public function testItDoesntSendEmailWhenNoMailFieldGiven(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()
->sections([
FormtemplateSectionRequest::new()->fields([
$this->textField('vorname')->specialType(SpecialType::FIRSTNAME),
$this->textField('nachname')->specialType(SpecialType::LASTNAME),
]),
])
->create();
$this->register($form, ['vorname' => 'Lala', 'nachname' => 'GG'])
->assertOk();
Mail::assertNotQueued(ConfirmRegistrationMail::class);
}
/**
* @dataProvider validationDataProvider
* @param array<string, mixed> $payload