A free open source IT asset/license management system
Improve mail contents
Marcus Mooreb3610b4d6c97
diff --git a/app/Console/Commands/RegenerateAcceptances.php b/app/Console/Commands/RegenerateAcceptances.phpindex 7ff6f18dd6..3c10f713fb 100644--- a/app/Console/Commands/RegenerateAcceptances.php+++ b/app/Console/Commands/RegenerateAcceptances.php@@ -70,11 +70,15 @@ class RegenerateAcceptances extends Commandprivate bool $notify = false;/**- * The holders this run created rows for, keyed by user id, each with how many rows- * they got. Keying by holder is what keeps a holder re-requested for three items to- * one email rather than three.+ * The holders this run created rows for, keyed by user id, each with the items they+ * were re-requested for. Keying by holder is what keeps a holder re-requested for+ * three items to one email rather than three.*- * @var array<int, array{user: User, count: int}>+ * The items are scalars, never the models: holding a checkoutable per created row+ * would pin every item this run touches in memory and undo the chunking that bounds+ * the command.+ *+ * @var array<int, array{user: User, items: array<int, array{name: string, type: class-string, qty: int|null}>}>*/private array $holdersToNotify = [];@@ -436,9 +440,11 @@ class RegenerateAcceptances extends Command$this->created++;$holder = $pair['user'];- $this->holdersToNotify[$holder->id] = [- 'user' => $holder,- 'count' => ($this->holdersToNotify[$holder->id]['count'] ?? 0) + 1,+ $this->holdersToNotify[$holder->id]['user'] = $holder;+ $this->holdersToNotify[$holder->id]['items'][] = [+ 'name' => $pair['item']->present()->name,+ 'type' => $pair['item']::class,+ 'qty' => $this->creationQty($pair['item'], $pair['qty']),];}@@ -465,7 +471,7 @@ class RegenerateAcceptances extends Commandcontinue;}- $mail = new AcceptanceReRequestMail($user, $holder['count']);+ $mail = new AcceptanceReRequestMail($user, $holder['items']);Mail::to($user->email)->send($user->locale ? $mail->locale($user->locale) : $mail);diff --git a/app/Mail/AcceptanceReRequestMail.php b/app/Mail/AcceptanceReRequestMail.phpindex cde41f3d80..4358f5a5aa 100644--- a/app/Mail/AcceptanceReRequestMail.php+++ b/app/Mail/AcceptanceReRequestMail.php@@ -2,6 +2,10 @@namespace App\Mail;+use App\Models\Accessory;+use App\Models\Asset;+use App\Models\Consumable;+use App\Models\LicenseSeat;use App\Models\User;use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailables\Address;@@ -10,20 +14,21 @@ use Illuminate\Mail\Mailables\Content;use Illuminate\Mail\Mailables\Envelope;use Illuminate\Queue\SerializesModels;-/**- * Asks a holder to accept the terms again for items they already hold.- *- * Deliberately not the unaccepted-items reminder: a holder who accepted a year ago- * and is being asked again reads "Reminder: You have Unaccepted Items" as a bug, so- * this is framed as a fresh request rather than a nudge about something they ignored.- */class AcceptanceReRequestMail extends BaseMailable{use Queueable, SerializesModels;+ /**+ * How many items the message names before it stops listing and counts the rest.+ */+ private const ITEM_LIST_LIMIT = 10;++ /**+ * @param array<int, array{name: string, type: class-string, qty: int|null}> $items+ */public function __construct(public readonly User $holder,- public readonly int $itemCount,+ public readonly array $items,) {}/**@@ -46,12 +51,41 @@ class AcceptanceReRequestMail extends BaseMailablemarkdown: 'notifications.markdown.acceptance-re-request',with: ['assigned_to' => $this->holder->present()->fullName,- 'count' => $this->itemCount,+ 'count' => count($this->items),+ 'shown_items' => array_map(+ fn (array $item) => [+ 'name' => $item['name'],+ 'type' => $this->typeLabel($item['type']),+ 'qty' => $item['qty'],+ ],+ array_slice($this->items, 0, self::ITEM_LIST_LIMIT),+ ),+ 'remaining' => max(count($this->items) - self::ITEM_LIST_LIMIT, 0),'accept_url' => route('account.accept'),],);}+ /**+ * What a holder calls the kind of thing they hold.+ *+ * `class_basename()` is what the operator's report table prints, but "LicenseSeat"+ * is not a word anyone outside this codebase uses. This runs while the mailable is+ * being rendered, so it is already inside the holder's locale.+ *+ * @param class-string $type+ */+ private function typeLabel(string $type): string+ {+ return match ($type) {+ Asset::class => trans('general.asset'),+ LicenseSeat::class => trans('general.license'),+ Accessory::class => trans('general.accessory'),+ Consumable::class => trans('general.consumable'),+ default => trans('general.component'),+ };+ }+/*** Get the attachments for the message.*diff --git a/resources/lang/en-US/mail.php b/resources/lang/en-US/mail.phpindex e471ecece8..0aef3f3e29 100644--- a/resources/lang/en-US/mail.php+++ b/resources/lang/en-US/mail.php@@ -40,6 +40,7 @@ return ['acceptance_declined_greeting' => ':user has declined an item','acceptance_re_request' => 'Action Required: Please Re-Accept Your Assigned Items','acceptance_re_request_intro' => 'Please re-accept the terms for the :count item currently assigned to you. Click the link below to accept or decline it.|Please re-accept the terms for the :count items currently assigned to you. Click the link below to accept or decline them.',+ 'acceptance_re_request_more_items' => '...and :count more item.|...and :count more items.','send_pdf_copy' => 'Send a copy of this acceptance to my email address','accessory_name' => 'Accessory Name','additional_notes' => 'Additional Notes',diff --git a/resources/views/notifications/markdown/acceptance-re-request.blade.php b/resources/views/notifications/markdown/acceptance-re-request.blade.phpindex b18b9ddd43..8ff31c6f50 100644--- a/resources/views/notifications/markdown/acceptance-re-request.blade.php+++ b/resources/views/notifications/markdown/acceptance-re-request.blade.php@@ -2,6 +2,15 @@# {{ trans('mail.hello') }} {{ $assigned_to }},{{ trans_choice('mail.acceptance_re_request_intro', $count, ['count' => $count]) }}++@foreach ($shown_items as $item)+- {{ $item['name'] }} ({{ $item['type'] }}){{ $item['qty'] > 1 ? ' × '.$item['qty'] : '' }}+@endforeach++@if ($remaining > 0)+{{ trans_choice('mail.acceptance_re_request_more_items', $remaining, ['count' => $remaining]) }}+@endif+[{{ trans('general.click_here') }}]({{ $accept_url }}){{ trans('mail.best_regards') }}diff --git a/tests/Feature/Console/RegenerateAcceptancesTest.php b/tests/Feature/Console/RegenerateAcceptancesTest.phpindex 8ec5680922..1aa0484da5 100644--- a/tests/Feature/Console/RegenerateAcceptancesTest.php+++ b/tests/Feature/Console/RegenerateAcceptancesTest.php@@ -725,17 +725,64 @@ class RegenerateAcceptancesTest extends TestCaseMail::assertSent(AcceptanceReRequestMail::class, function (AcceptanceReRequestMail $mail) {return $mail->hasTo('[email protected]')- && $mail->itemCount === 2+ && count($mail->items) === 2&& $mail->hasSubject(trans('mail.acceptance_re_request'))&& str_contains($mail->render(), trans_choice('mail.acceptance_re_request_intro', 2, ['count' => 2]));});Mail::assertSent(AcceptanceReRequestMail::class,- fn (AcceptanceReRequestMail $mail) => $mail->hasTo('[email protected]') && $mail->itemCount === 1,+ fn (AcceptanceReRequestMail $mail) => $mail->hasTo('[email protected]') && count($mail->items) === 1,);Mail::assertSent(AcceptanceReRequestMail::class, 2);}+ public function test_the_email_names_every_item_the_holder_was_re_requested_for(): void+ {+ Mail::fake();+ $company = Company::factory()->create();+ $holder = User::factory()->create(['email' => '[email protected]']);+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Alice laptop');+ $mouse = $this->heldAccessory($holder, $this->acceptanceCategory('accessory'), $company, 'Mouse');+ $mouse->checkouts()->create(['assigned_to' => $holder->id, 'assigned_type' => User::class]);++ $this->artisan('snipeit:regenerate-acceptances', ['--notify' => true])->assertExitCode(0);++ Mail::assertSent(AcceptanceReRequestMail::class, function (AcceptanceReRequestMail $mail) {+ $body = $mail->render();++ return str_contains($body, 'Alice laptop')+ && str_contains($body, trans('general.asset'))+ && str_contains($body, 'Mouse ('.trans('general.accessory').') × 2')+ && ! str_contains($body, trans_choice('mail.acceptance_re_request_more_items', 1, ['count' => 1]));+ });+ }++ public function test_the_email_lists_ten_items_then_counts_the_rest(): void+ {+ Mail::fake();+ $company = Company::factory()->create();+ $category = $this->acceptanceCategory('asset');+ $holder = User::factory()->create(['email' => '[email protected]']);++ foreach (range(1, 12) as $number) {+ $this->heldAsset($holder, $category, $company, 'Laptop '.$number);+ }++ $this->artisan('snipeit:regenerate-acceptances', ['--notify' => true])->assertExitCode(0);++ Mail::assertSent(AcceptanceReRequestMail::class, function (AcceptanceReRequestMail $mail) {+ $body = $mail->render();+ $named = array_filter(+ range(1, 12),+ fn (int $number) => str_contains($body, 'Laptop '.$number.' #'),+ );++ return count($mail->items) === 12+ && count($named) === 10+ && str_contains($body, trans_choice('mail.acceptance_re_request_more_items', 2, ['count' => 2]));+ });+ }+public function test_nothing_is_emailed_without_the_notify_flag(): void{Mail::fake();