From 1aeb5c2b6c14e029b602b164789cc58d40df8444 Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Tue, 29 Aug 2023 12:59:30 +0200 Subject: [PATCH] Create a space title based on the related model (#15) * Create a space title based on the related model * fix --- composer.json | 2 +- src/Jobs/CreatePencilSpaceJob.php | 3 +- .../Strategies/DefaultSpaceTitleStrategy.php | 26 ++++ .../SpaceTitleRelatedToCourseStrategy.php | 11 ++ .../SpaceTitleRelatedToLessonStrategy.php | 11 ++ .../SpaceTitleRelatedToTopicStrategy.php | 11 ++ src/Jobs/Strategies/SpaceTitleStrategy.php | 8 ++ .../Strategies/SpaceTitleStrategyFactory.php | 26 ++++ src/Models/ConsultationAccessEnquiry.php | 1 + tests/Feature/CreatePencilSpaceJobTest.php | 11 ++ tests/Feature/SpaceTitleStrategyTest.php | 112 ++++++++++++++++++ 11 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 src/Jobs/Strategies/DefaultSpaceTitleStrategy.php create mode 100644 src/Jobs/Strategies/SpaceTitleRelatedToCourseStrategy.php create mode 100644 src/Jobs/Strategies/SpaceTitleRelatedToLessonStrategy.php create mode 100644 src/Jobs/Strategies/SpaceTitleRelatedToTopicStrategy.php create mode 100644 src/Jobs/Strategies/SpaceTitleStrategy.php create mode 100644 src/Jobs/Strategies/SpaceTitleStrategyFactory.php create mode 100644 tests/Feature/SpaceTitleStrategyTest.php diff --git a/composer.json b/composer.json index 148ab44..81a2625 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "require-dev": { "phpunit/phpunit": "^9.0", "orchestra/testbench": ">=5.0", - "darkaonline/l5-swagger": "8.1.0" + "escolalms/courses": "^0" }, "license": "MIT", "authors": [ diff --git a/src/Jobs/CreatePencilSpaceJob.php b/src/Jobs/CreatePencilSpaceJob.php index 5d5edb0..b5adffb 100644 --- a/src/Jobs/CreatePencilSpaceJob.php +++ b/src/Jobs/CreatePencilSpaceJob.php @@ -4,6 +4,7 @@ use EscolaLms\ConsultationAccess\Enum\MeetingLinkTypeEnum; use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent; +use EscolaLms\ConsultationAccess\Jobs\Strategies\SpaceTitleStrategyFactory; use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry; use EscolaLms\ConsultationAccess\Repositories\Contracts\ConsultationAccessEnquiryRepositoryContract; use EscolaLms\PencilSpaces\Facades\PencilSpace; @@ -39,7 +40,7 @@ public function handle(ConsultationAccessEnquiryRepositoryContract $consultation } $resource = new CreatePencilSpaceResource( - $enquiry->title ?? 'Consultation X ' . $enquiry->user->name, + SpaceTitleStrategyFactory::create($enquiry)->getTitle(), collect($enquiry->consultation->author_id), collect($enquiry->user_id) ); diff --git a/src/Jobs/Strategies/DefaultSpaceTitleStrategy.php b/src/Jobs/Strategies/DefaultSpaceTitleStrategy.php new file mode 100644 index 0000000..aa6ba9d --- /dev/null +++ b/src/Jobs/Strategies/DefaultSpaceTitleStrategy.php @@ -0,0 +1,26 @@ +enquiry = $enquiry; + } + + public function getTitle(): string + { + return sprintf( + '%s (%d) %s', + $this->enquiry->user->name, + $this->enquiry->user->getKey(), + Carbon::make($this->enquiry->consultationUser->executed_at)->format('d-m-Y'), + ); + } +} diff --git a/src/Jobs/Strategies/SpaceTitleRelatedToCourseStrategy.php b/src/Jobs/Strategies/SpaceTitleRelatedToCourseStrategy.php new file mode 100644 index 0000000..859526e --- /dev/null +++ b/src/Jobs/Strategies/SpaceTitleRelatedToCourseStrategy.php @@ -0,0 +1,11 @@ +enquiry->related->title . ' ' . parent::getTitle(); + } +} diff --git a/src/Jobs/Strategies/SpaceTitleRelatedToLessonStrategy.php b/src/Jobs/Strategies/SpaceTitleRelatedToLessonStrategy.php new file mode 100644 index 0000000..fd4a314 --- /dev/null +++ b/src/Jobs/Strategies/SpaceTitleRelatedToLessonStrategy.php @@ -0,0 +1,11 @@ +enquiry->related->course->title . ' ' . parent::getTitle(); + } +} diff --git a/src/Jobs/Strategies/SpaceTitleRelatedToTopicStrategy.php b/src/Jobs/Strategies/SpaceTitleRelatedToTopicStrategy.php new file mode 100644 index 0000000..ae4b60b --- /dev/null +++ b/src/Jobs/Strategies/SpaceTitleRelatedToTopicStrategy.php @@ -0,0 +1,11 @@ +enquiry->related->lesson->course->title . ' ' . parent::getTitle(); + } +} diff --git a/src/Jobs/Strategies/SpaceTitleStrategy.php b/src/Jobs/Strategies/SpaceTitleStrategy.php new file mode 100644 index 0000000..f4a00da --- /dev/null +++ b/src/Jobs/Strategies/SpaceTitleStrategy.php @@ -0,0 +1,8 @@ +related_type || !class_exists($enquiry->related_type) || !$enquiry->related) { + return new DefaultSpaceTitleStrategy($enquiry); + } + + switch ($enquiry->related_type){ + case \EscolaLms\Courses\Models\Topic::class: + return new SpaceTitleRelatedToTopicStrategy($enquiry); + case \EscolaLms\Courses\Models\Lesson::class: + return new SpaceTitleRelatedToLessonStrategy($enquiry); + case \EscolaLms\Courses\Models\Course::class: + return new SpaceTitleRelatedToCourseStrategy($enquiry); + default: + return new DefaultSpaceTitleStrategy($enquiry); + } + } +} diff --git a/src/Models/ConsultationAccessEnquiry.php b/src/Models/ConsultationAccessEnquiry.php index 347296f..d7e228e 100644 --- a/src/Models/ConsultationAccessEnquiry.php +++ b/src/Models/ConsultationAccessEnquiry.php @@ -31,6 +31,7 @@ * @property-read Consultation $consultation * @property-read Collection|ConsultationAccessEnquiryProposedTerm[] $consultationAccessEnquiryProposedTerms * @property-read ?ConsultationUserPivot $consultationUser + * @property-read ?Model $related */ class ConsultationAccessEnquiry extends Model { diff --git a/tests/Feature/CreatePencilSpaceJobTest.php b/tests/Feature/CreatePencilSpaceJobTest.php index 6195cf1..09ce9c8 100644 --- a/tests/Feature/CreatePencilSpaceJobTest.php +++ b/tests/Feature/CreatePencilSpaceJobTest.php @@ -2,12 +2,15 @@ namespace EscolaLms\ConsultationAccess\Tests\Feature; +use EscolaLms\Auth\Models\User; use EscolaLms\ConsultationAccess\Enum\EnquiryStatusEnum; use EscolaLms\ConsultationAccess\Enum\MeetingLinkTypeEnum; use EscolaLms\ConsultationAccess\Events\ConsultationAccessEnquiryApprovedEvent; use EscolaLms\ConsultationAccess\Jobs\CreatePencilSpaceJob; use EscolaLms\ConsultationAccess\Models\ConsultationAccessEnquiry; use EscolaLms\ConsultationAccess\Tests\TestCase; +use EscolaLms\Consultations\Models\Consultation; +use EscolaLms\Consultations\Models\ConsultationUserPivot; use EscolaLms\Core\Tests\CreatesUsers; use EscolaLms\PencilSpaces\Facades\PencilSpace; use Illuminate\Support\Facades\Event; @@ -28,12 +31,20 @@ public function testCreatePencilSpace(): void PencilSpace::fake(); Event::fake([ConsultationAccessEnquiryApprovedEvent::class]); + $consultationUser = ConsultationUserPivot::factory() + ->state([ + 'user_id' => User::factory(), + 'consultation_id' => Consultation::factory(), + ]) + ->create(); + /** @var ConsultationAccessEnquiry $enquiry */ $enquiry = ConsultationAccessEnquiry::factory() ->state([ 'status' => EnquiryStatusEnum::APPROVED, 'meeting_link' => null, 'meeting_link_type' => null, + 'consultation_user_id' => $consultationUser->getKey(), ]) ->create(); diff --git a/tests/Feature/SpaceTitleStrategyTest.php b/tests/Feature/SpaceTitleStrategyTest.php new file mode 100644 index 0000000..2066ba9 --- /dev/null +++ b/tests/Feature/SpaceTitleStrategyTest.php @@ -0,0 +1,112 @@ +consultationUser = ConsultationUserPivot::factory() + ->state([ + 'user_id' => User::factory(), + 'consultation_id' => Consultation::factory(), + ]) + ->create(); + + /** @var ConsultationAccessEnquiry $enquiry */ + $this->enquiry = ConsultationAccessEnquiry::factory() + ->state([ + 'status' => EnquiryStatusEnum::APPROVED, + 'meeting_link' => null, + 'meeting_link_type' => null, + 'consultation_user_id' => $this->consultationUser->getKey(), + ]) + ->create(); + } + + public function testDefaultSpaceTitle(): void + { + $title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); + $this->assertEquals($this->getDefaultTitle(), $title); + } + + public function testSpaceTitleWhenTopicIsRelated(): void + { + $course = Course::factory()->create(); + $lesson = Lesson::factory()->state(['course_id' => $course->getKey()])->create(); + $topic = Topic::factory()->state(['lesson_id' => $lesson->getKey()])->create(); + + $this->enquiry->update([ + 'related_type' => Topic::class, + 'related_id' => $topic->getKey(), + ]); + $title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); + $this->assertEquals($course->title . ' ' . $this->getDefaultTitle(), $title); + } + + public function testSpaceTitleWhenLessonIsRelated(): void + { + $course = Course::factory()->create(); + $lesson = Lesson::factory()->state(['course_id' => $course->getKey()])->create(); + + $this->enquiry->update([ + 'related_type' => Lesson::class, + 'related_id' => $lesson->getKey(), + ]); + $title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); + $this->assertEquals($course->title . ' ' . $this->getDefaultTitle(), $title); + } + + public function testSpaceTitleWhenCourseIsRelated(): void + { + $course = Course::factory()->create(); + + $this->enquiry->update([ + 'related_type' => Course::class, + 'related_id' => $course->getKey(), + ]); + $title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); + $this->assertEquals($course->title . ' ' . $this->getDefaultTitle(), $title); + } + + public function testSpaceTitleWhenRelatedModelNotExist(): void + { + $course = Course::factory()->create(); + + $this->enquiry->update([ + 'related_type' => Course::class, + 'related_id' => $course->getKey(), + ]); + + $course->delete(); + $title = SpaceTitleStrategyFactory::create($this->enquiry)->getTitle(); + $this->assertEquals($this->getDefaultTitle(), $title); + } + + private function getDefaultTitle(): string + { + return sprintf( + '%s (%d) %s', + $this->enquiry->user->name, + $this->enquiry->user->getKey(), + Carbon::make($this->consultationUser->executed_at)->format('d-m-Y') + ); + } +}