Add expires year to mail

This commit is contained in:
philipp lang 2025-05-28 23:22:19 +02:00
parent 47b5abc0f1
commit 919041d2cf
7 changed files with 61 additions and 12 deletions

View File

@ -41,7 +41,7 @@ class PreventionRememberAction
->placeholder('formname', $participant->form->name) ->placeholder('formname', $participant->form->name)
->append($participant->form->prevention_text); ->append($participant->form->prevention_text);
Mail::send(new PreventionRememberMail($participant, $body)); Mail::send(new PreventionRememberMail($participant, $body, $participant->preventions()));
$participant->update(['last_remembered_at' => now()]); $participant->update(['last_remembered_at' => now()]);
} }

View File

@ -14,8 +14,7 @@ class EditorData extends Data implements Editorable
public string $version, public string $version,
public array $blocks, public array $blocks,
public int $time public int $time
) { ) {}
}
public function placeholder(string $search, string $replacement): self public function placeholder(string $search, string $replacement): self
{ {
@ -30,7 +29,15 @@ class EditorData extends Data implements Editorable
*/ */
public function hasAll(array $wanted): bool public function hasAll(array $wanted): bool
{ {
return collect($wanted)->first(fn ($search) => !str(json_encode($this->blocks))->contains($search)) === null; return collect($wanted)->doesntContain(fn($search) => !str(json_encode($this->blocks))->contains($search));
}
/**
* @param array<int, string> $should
*/
public function hasNot(string $should): bool
{
return !str(json_encode($this->blocks))->contains($should);
} }
public static function default(): self public static function default(): self
@ -65,7 +72,7 @@ class EditorData extends Data implements Editorable
'type' => 'list', 'type' => 'list',
'data' => [ 'data' => [
'style' => 'unordered', 'style' => 'unordered',
'items' => collect($replacements)->map(fn ($replacement) => [ 'items' => collect($replacements)->map(fn($replacement) => [
'content' => $replacement, 'content' => $replacement,
'items' => [], 'items' => [],
]), ]),

View File

@ -49,7 +49,7 @@ class YearlyRememberAction
*/ */
protected function createMail(Member $member, Collection $preventions): YearlyMail protected function createMail(Member $member, Collection $preventions): YearlyMail
{ {
$body = app(PreventionSettings::class)->refresh()->formmail; $body = app(PreventionSettings::class)->refresh()->yearlymail;
return new YearlyMail($member, $body, $preventions); return new YearlyMail($member, $body, $preventions);
} }
} }

View File

@ -14,4 +14,12 @@ class PreventionData extends Data
{ {
return $this->expires->isSameDay($date); return $this->expires->isSameDay($date);
} }
public function text(): string
{
return str($this->type->text())->when(
!$this->expiresAt(now()),
fn($str) => $str->append(' (fällig am ' . $this->expires->format('d.m.Y') . ')')
);
}
} }

View File

@ -5,12 +5,14 @@ namespace App\Prevention\Mails;
use App\Invoice\InvoiceSettings; use App\Invoice\InvoiceSettings;
use App\Lib\Editor\EditorData; use App\Lib\Editor\EditorData;
use App\Prevention\Contracts\Preventable; use App\Prevention\Contracts\Preventable;
use App\Prevention\Data\PreventionData;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Mail\Attachment; use Illuminate\Mail\Attachment;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope; use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
class PreventionRememberMail extends Mailable class PreventionRememberMail extends Mailable
{ {
@ -20,12 +22,13 @@ class PreventionRememberMail extends Mailable
/** /**
* Create a new message instance. * Create a new message instance.
* @param Collection<int, PreventionData> $preventions
*/ */
public function __construct(public Preventable $preventable, public EditorData $bodyText) public function __construct(public Preventable $preventable, public EditorData $bodyText, public Collection $preventions)
{ {
$this->settings = app(InvoiceSettings::class); $this->settings = app(InvoiceSettings::class);
$this->bodyText = $this->bodyText $this->bodyText = $this->bodyText
->replaceWithList('wanted', collect($preventable->preventions())->map(fn($prevention) => $prevention->type->text())->toArray()); ->replaceWithList('wanted', $preventions->map(fn($prevention) => $prevention->text())->toArray());
} }
/** /**

View File

@ -22,14 +22,13 @@ class YearlyMail extends Mailable
/** /**
* Create a new message instance. * Create a new message instance.
*
* @param Collection<int, PreventionData> $preventions * @param Collection<int, PreventionData> $preventions
*/ */
public function __construct(public Preventable $preventable, public EditorData $bodyText, public Collection $preventions) public function __construct(public Preventable $preventable, public EditorData $bodyText, public Collection $preventions)
{ {
$this->settings = app(InvoiceSettings::class); $this->settings = app(InvoiceSettings::class);
$this->bodyText = $this->bodyText $this->bodyText = $this->bodyText
->replaceWithList('wanted', collect($preventions)->pluck('type')->map(fn($prevention) => $prevention->text())->toArray()); ->replaceWithList('wanted', $preventions->map(fn($prevention) => $prevention->text())->toArray());
} }
/** /**

View File

@ -279,7 +279,7 @@ it('testItRendersSetttingMail', function () {
Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll([ Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll([
'lorem lala ' . $form->name, 'lorem lala ' . $form->name,
'erweitertes' 'erweitertes'
])); ]) && $mail->bodyText->hasNot(now()->format('d.m.Y')));
}); });
it('testItAppendsTextOfForm', function () { it('testItAppendsTextOfForm', function () {
@ -320,7 +320,7 @@ it('displays body text in prevention remember mail', function () {
$form = createForm(); $form = createForm();
$participant = createParticipant($form); $participant = createParticipant($form);
$mail = new PreventionRememberMail($participant, EditorRequestFactory::new()->paragraphs(['ggtt'])->toData()); $mail = new PreventionRememberMail($participant, EditorRequestFactory::new()->paragraphs(['ggtt'])->toData(), collect([]));
$mail->assertSeeInText('ggtt'); $mail->assertSeeInText('ggtt');
}); });
@ -342,3 +342,35 @@ it('renders yearly mail', function () {
->assertSeeInText('ggtt') ->assertSeeInText('ggtt')
->assertSeeInText('Stamm Beispiel'); ->assertSeeInText('Stamm Beispiel');
}); });
it('renders setting of yearly mail', function () {
Mail::fake();
app(PreventionSettings::class)->fill([
'yearlymail' => EditorRequestFactory::new()->paragraphs(["{wanted}", "bbb"])->toData()
])->save();
$member = createMember((['efz' => now()->subYears(5), 'ps_at' => now(), 'has_vk' => true]));
YearlyRememberAction::run();
Mail::assertSent(
YearlyMail::class,
fn($mail) => $mail->bodyText->hasAll(['erweitertes', 'bbb'])
&& $mail->bodyText->hasNot(now()->format('d.m.Y'))
);
});
it('renders expires at date for preventions', function () {
Mail::fake();
app(PreventionSettings::class)->fill([
'yearlymail' => EditorRequestFactory::new()->paragraphs(["{wanted}"])->toData(),
'weeks' => 4,
])->save();
createMember((['efz' => now()->subYears(5)->addWeeks(4), 'ps_at' => now(), 'has_vk' => true]));
YearlyRememberAction::run();
Mail::assertSent(YearlyMail::class, fn($mail) => $mail->bodyText->hasAll([
'erweitertes',
'am ' . now()->addWeeks(4)->format('d.m.Y'),
]) && $mail->bodyText->hasNot(now()->format('d.m.Y')));
});