Add mail for registration

This commit is contained in:
philipp lang 2024-03-15 01:52:27 +01:00
parent c3a2417cac
commit 9144ae1028
7 changed files with 111 additions and 15 deletions

View File

@ -40,17 +40,23 @@ class FieldCollection extends Collection
*/
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
return $this->getFullname() && $email
? (object) [
'name' => "$firstname $lastname",
'name' => $this->getFullname(),
"email" => $email,
] : null;
}
public function getFullname(): ?string
{
$firstname = $this->findBySpecialType(SpecialType::FIRSTNAME)?->value;
$lastname = $this->findBySpecialType(SpecialType::LASTNAME)?->value;
return $firstname && $lastname ? "$firstname $lastname" : null;
}
/**
* @param array<string, mixed> $input
*/
@ -59,6 +65,11 @@ class FieldCollection extends Collection
return $form->getFields()->each(fn ($field) => $field->value = array_key_exists($field->key, $input) ? $input[$field->key] : $field->default());
}
public function find(Field $givenField): ?Field
{
return $this->first(fn ($field) => $field->key === $givenField->key);
}
/**
* @return array<string, mixed>
*/

View File

@ -93,10 +93,18 @@ abstract class Field extends Data
{
return [
$this->key => $this->value,
$this->getDisplayAttribute() => $this->getPresenter()->present($this->value),
$this->getDisplayAttribute() => $this->presentRaw(),
];
}
/**
* @return mixed
*/
public function presentRaw()
{
return $this->getPresenter()->present($this->value);
}
/**
* @return array<string, string>
*/

View File

@ -2,6 +2,8 @@
namespace App\Form\Mails;
use App\Form\Data\FormConfigData;
use App\Form\Models\Participant;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
@ -13,6 +15,9 @@ class ConfirmRegistrationMail extends Mailable
{
use Queueable, SerializesModels;
public string $fullname;
public FormConfigData $config;
/**
* Create a new message instance.
*
@ -20,7 +25,8 @@ class ConfirmRegistrationMail extends Mailable
*/
public function __construct(public Participant $participant)
{
//
$this->fullname = $participant->getFields()->getFullname();
$this->config = $participant->getConfig();
}
/**
@ -43,7 +49,7 @@ class ConfirmRegistrationMail extends Mailable
public function content()
{
return new Content(
view: 'view.name',
markdown: 'mail.form.confirm-registration',
);
}

View File

@ -3,6 +3,7 @@
namespace App\Form\Models;
use App\Form\Data\FieldCollection;
use App\Form\Data\FormConfigData;
use App\Form\Mails\ConfirmRegistrationMail;
use App\Form\Scopes\ParticipantFilterScope;
use Illuminate\Database\Eloquent\Builder;
@ -52,6 +53,17 @@ class Participant extends Model
return FieldCollection::fromRequest($this->form, $this->data);
}
public function getConfig(): FormConfigData
{
return tap($this->form->config, function ($config) {
$config->sections->each(function ($section) {
$section->fields->each(function ($field) {
$field->value = $this->getFields()->find($field)->value;
});
});
});
}
public function sendConfirmationMail(): void
{
if (!$this->getFields()->getMailRecipient()) {

View File

@ -30,14 +30,6 @@ class ParticipantFactory extends Factory
];
}
/**
* @param array<int, FormtemplateSectionRequest> $sections
*/
public function sections(array $sections): self
{
return $this->state(['config' => ['sections' => array_map(fn ($section) => $section->create(), $sections)]]);
}
/**
* @param array<string, mixed> $data
*/

View File

@ -0,0 +1,23 @@
@component('mail::message')
# Hallo {{$fullname}},
{{ $participant->form->mail_top }}
{{ $participant->form->mail_bottom }}
@foreach($config->sections as $section)
## {{$section->name}}
@foreach ($section->fields as $field)
* {{$field->name}}: {{$field->presentRaw()}}
@endforeach
@endforeach
Im Anhang findet ihr die aktuelle Rechnung des Stammes Silva für das laufende Jahr. Bitte begleicht diese bis zum angegebenen Datum.
@component('mail::subcopy')
Herzliche Grüße und gut Pfad
Der Stammesvorstand
@endcomponent
@endcomponent

View File

@ -0,0 +1,44 @@
<?php
namespace Tests\Feature\Form;
use App\Form\Enums\SpecialType;
use App\Form\Mails\ConfirmRegistrationMail;
use App\Form\Models\Form;
use App\Form\Models\Participant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class FormRegisterMailTest extends FormTestCase
{
use DatabaseTransactions;
public function testItShowsFormContent(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$participant = Participant::factory()
->for(Form::factory()->mailTop('mail top')->mailBottom('mail bottom'))->create();
$mail = new ConfirmRegistrationMail($participant);
$mail->assertSeeInText('mail top');
$mail->assertSeeInText('mail bottom');
}
public function testItShowsParticipantGreeting(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$participant = Participant::factory()->for(Form::factory()->sections([
FormtemplateSectionRequest::new()->name('Persönliches')->fields([
$this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME),
$this->textField('nachname')->specialType(SpecialType::LASTNAME),
])
]))
->data(['vorname' => 'Max', 'nachname' => 'Muster'])
->create();
$mail = new ConfirmRegistrationMail($participant);
$mail->assertSeeInText('# Hallo Max Muster');
$mail->assertSeeInText('## Persönliches');
$mail->assertSeeInText('* Vorname: Max');
}
}