diff --git a/src/ConsultationAccess/ConsultationAccessEnquiryApprovedVariables.php b/src/ConsultationAccess/ConsultationAccessEnquiryApprovedVariables.php new file mode 100644 index 0000000..1ea336c --- /dev/null +++ b/src/ConsultationAccess/ConsultationAccessEnquiryApprovedVariables.php @@ -0,0 +1,63 @@ + $faker->dateTime->format('Y-m-d H:i'), + self::VAR_MEETING_LINK => $faker->url, + ]); + } + + public static function requiredVariables(): array + { + return array_merge(parent::requiredVariables(), [ + self::VAR_APPROVED_TERM, + self::VAR_MEETING_LINK, + ]); + } + + public static function requiredVariablesInSection(string $sectionKey): array + { + if ($sectionKey === 'content') { + return array_merge(parent::requiredVariablesInSection($sectionKey), [ + self::VAR_APPROVED_TERM, + self::VAR_MEETING_LINK, + ]); + } + return []; + } + + public static function variablesFromEvent(EventWrapper $event): array + { + return array_merge(parent::variablesFromEvent($event), [ + self::VAR_APPROVED_TERM => $event->getConsultationAccessEnquiry()->consultationUser->executed_at, + self::VAR_MEETING_LINK => $event->getConsultationAccessEnquiry()->meeting_link, + ]); + } + + public static function defaultSectionsContent(): array + { + return [ + 'title' => __('Approved term ":consultation"', [ + 'consultation' => self::VAR_CONSULTATION_NAME, + ]), + 'content' => self::wrapWithMjml(__('

Hello :user_name!

Reported term :approved_term for consultation ":consultation" was approved. Meeting link: :link

', [ + 'user_name' => self::VAR_USER_NAME, + 'consultation' => self::VAR_CONSULTATION_NAME, + 'approved_term' => self::VAR_APPROVED_TERM, + 'link' => self::VAR_MEETING_LINK, + ])), + ]; + } +} diff --git a/src/Providers/ConsultationAccessTemplatesServiceProvider.php b/src/Providers/ConsultationAccessTemplatesServiceProvider.php index 680dadc..9883e90 100644 --- a/src/Providers/ConsultationAccessTemplatesServiceProvider.php +++ b/src/Providers/ConsultationAccessTemplatesServiceProvider.php @@ -3,8 +3,10 @@ namespace EscolaLms\TemplatesEmail\Providers; use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryAdminCreatedEvent; +use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent; use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryDisapprovedEvent; use EscolaLms\TemplatesEmail\ConsultationAccess\ConsultationAccessEnquiryAdminCreatedVariables; +use EscolaLms\TemplatesEmail\ConsultationAccess\ConsultationAccessEnquiryApprovedVariables; use EscolaLms\TemplatesEmail\ConsultationAccess\ConsultationAccessEnquiryDisapprovedVariables; use EscolaLms\TemplatesEmail\Core\EmailChannel; use Illuminate\Support\ServiceProvider; @@ -16,5 +18,6 @@ public function boot() { Template::register(ConsultationAccessEnquiryAdminCreatedEvent::class, EmailChannel::class, ConsultationAccessEnquiryAdminCreatedVariables::class); Template::register(ConsultationAccessEnquiryDisapprovedEvent::class, EmailChannel::class, ConsultationAccessEnquiryDisapprovedVariables::class); + Template::register(ConsultationAccessEnquiryApprovedEvent::class, EmailChannel::class, ConsultationAccessEnquiryApprovedVariables::class); } } diff --git a/tests/Api/ConsultationAccessTest.php b/tests/Api/ConsultationAccessTest.php index 68537eb..823c51d 100644 --- a/tests/Api/ConsultationAccessTest.php +++ b/tests/Api/ConsultationAccessTest.php @@ -4,14 +4,17 @@ use EscolaLms\ConsultationAccess\Database\Seeders\ConsultationAccessPermissionSeeder; use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryAdminCreatedEvent; +use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent; use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryDisapprovedEvent; use EscolaLms\ConsultationAccess\Models\Consultation; use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry; +use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiryProposedTerm; use EscolaLms\Core\Tests\CreatesUsers; use EscolaLms\Templates\Listeners\TemplateEventListener; use EscolaLms\TemplatesEmail\Core\EmailMailable; use EscolaLms\TemplatesEmail\Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Mail; @@ -19,7 +22,7 @@ class ConsultationAccessTest extends TestCase { - use CreatesUsers, DatabaseTransactions; + use CreatesUsers, DatabaseTransactions, WithFaker; public function setUp(): void { @@ -100,4 +103,36 @@ public function testNotificationOnConsultationEnquiryDisapproved(): void return true; }); } + + public function testNotificationOnConsultationEnquiryApproved(): void + { + Notification::fake(); + Event::fake(); + Mail::fake(); + + /** @var ConsultationAccessEnquiryProposedTerm $proposedTerm */ + $proposedTerm = ConsultationAccessEnquiryProposedTerm::factory()->create(); + $meetingLink = $this->faker->url; + $this->actingAs($this->makeAdmin(), 'api') + ->postJson('api/admin/consultation-access-enquiries/approve/' . $proposedTerm->getKey(), [ + 'meeting_link' => $meetingLink, + ]) + ->assertOk(); + + $enquiry = $proposedTerm->consultationAccessEnquiry; + + Event::assertDispatched(function (ConsultationAccessEnquiryApprovedEvent $event) use ($enquiry) { + $this->assertEquals($event->getConsultationAccessEnquiry()->getKey(), $enquiry->getKey()); + return true; + }); + + $listener = app(TemplateEventListener::class); + $listener->handle(new ConsultationAccessEnquiryApprovedEvent($enquiry->user, $enquiry)); + + Mail::assertSent(EmailMailable::class, function (EmailMailable $mailable) use ($enquiry) { + $this->assertEquals(__('Approved term ":consultation"', ['consultation' => $enquiry->consultation->name]), $mailable->subject); + $this->assertTrue($mailable->hasTo($enquiry->user->email)); + return true; + }); + } }