diff --git a/app/Form/Actions/PreventionRememberAction.php b/app/Form/Actions/PreventionRememberAction.php index 3ca7bb78..d181b579 100644 --- a/app/Form/Actions/PreventionRememberAction.php +++ b/app/Form/Actions/PreventionRememberAction.php @@ -41,7 +41,7 @@ class PreventionRememberAction ->placeholder('formname', $participant->form->name) ->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()]); } diff --git a/app/Lib/Editor/EditorData.php b/app/Lib/Editor/EditorData.php index 0bac67e5..8c4bcf5a 100644 --- a/app/Lib/Editor/EditorData.php +++ b/app/Lib/Editor/EditorData.php @@ -14,8 +14,7 @@ class EditorData extends Data implements Editorable public string $version, public array $blocks, public int $time - ) { - } + ) {} public function placeholder(string $search, string $replacement): self { @@ -30,7 +29,15 @@ class EditorData extends Data implements Editorable */ 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 $should + */ + public function hasNot(string $should): bool + { + return !str(json_encode($this->blocks))->contains($should); } public static function default(): self @@ -65,7 +72,7 @@ class EditorData extends Data implements Editorable 'type' => 'list', 'data' => [ 'style' => 'unordered', - 'items' => collect($replacements)->map(fn ($replacement) => [ + 'items' => collect($replacements)->map(fn($replacement) => [ 'content' => $replacement, 'items' => [], ]), diff --git a/app/Prevention/Actions/YearlyRememberAction.php b/app/Prevention/Actions/YearlyRememberAction.php index 5cf694dc..2cb79d35 100644 --- a/app/Prevention/Actions/YearlyRememberAction.php +++ b/app/Prevention/Actions/YearlyRememberAction.php @@ -49,7 +49,7 @@ class YearlyRememberAction */ 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); } } diff --git a/app/Prevention/Data/PreventionData.php b/app/Prevention/Data/PreventionData.php index c9474550..4fa6a957 100644 --- a/app/Prevention/Data/PreventionData.php +++ b/app/Prevention/Data/PreventionData.php @@ -14,4 +14,12 @@ class PreventionData extends Data { 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') . ')') + ); + } } diff --git a/app/Prevention/Mails/PreventionRememberMail.php b/app/Prevention/Mails/PreventionRememberMail.php index 4d572fd4..e6eb5f26 100644 --- a/app/Prevention/Mails/PreventionRememberMail.php +++ b/app/Prevention/Mails/PreventionRememberMail.php @@ -5,12 +5,14 @@ namespace App\Prevention\Mails; use App\Invoice\InvoiceSettings; use App\Lib\Editor\EditorData; use App\Prevention\Contracts\Preventable; +use App\Prevention\Data\PreventionData; use Illuminate\Bus\Queueable; use Illuminate\Mail\Attachment; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Collection; class PreventionRememberMail extends Mailable { @@ -20,12 +22,13 @@ class PreventionRememberMail extends Mailable /** * Create a new message instance. + * @param Collection $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->bodyText = $this->bodyText - ->replaceWithList('wanted', collect($preventable->preventions())->map(fn($prevention) => $prevention->type->text())->toArray()); + ->replaceWithList('wanted', $preventions->map(fn($prevention) => $prevention->text())->toArray()); } /** diff --git a/app/Prevention/Mails/YearlyMail.php b/app/Prevention/Mails/YearlyMail.php index 04bc954c..94b8f84f 100644 --- a/app/Prevention/Mails/YearlyMail.php +++ b/app/Prevention/Mails/YearlyMail.php @@ -22,14 +22,13 @@ class YearlyMail extends Mailable /** * Create a new message instance. - * * @param Collection $preventions */ public function __construct(public Preventable $preventable, public EditorData $bodyText, public Collection $preventions) { $this->settings = app(InvoiceSettings::class); $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()); } /** diff --git a/tests/Feature/Member/PreventionTest.php b/tests/Feature/Member/PreventionTest.php index 29b3216a..25ac27e1 100644 --- a/tests/Feature/Member/PreventionTest.php +++ b/tests/Feature/Member/PreventionTest.php @@ -279,7 +279,7 @@ it('testItRendersSetttingMail', function () { Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll([ 'lorem lala ' . $form->name, 'erweitertes' - ])); + ]) && $mail->bodyText->hasNot(now()->format('d.m.Y'))); }); it('testItAppendsTextOfForm', function () { @@ -320,7 +320,7 @@ it('displays body text in prevention remember mail', function () { $form = createForm(); $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'); }); @@ -342,3 +342,35 @@ it('renders yearly mail', function () { ->assertSeeInText('ggtt') ->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'))); +});