From 1f621d35efbae09f33f6e13f44d19375d570a8e3 Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Wed, 23 Aug 2023 12:02:53 +0200 Subject: [PATCH] Add filter by consultation terms (#14) --- src/Dtos/CriteriaDto.php | 4 ++++ .../Swagger/ConsultationAccessEnquiryApiSwagger.php | 13 +++++++++++++ .../ListConsultationAccessEnquiryRequest.php | 2 ++ .../Resources/ConsultationAccessEnquiryResource.php | 6 ++++++ tests/Api/ConsultationAccessEnquiryListApiTest.php | 8 +++++++- tests/Api/ConsultationAccessEnquiryReadApiTest.php | 1 + 6 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Dtos/CriteriaDto.php b/src/Dtos/CriteriaDto.php index 420fd08..ca117d4 100644 --- a/src/Dtos/CriteriaDto.php +++ b/src/Dtos/CriteriaDto.php @@ -9,6 +9,7 @@ use EscolaLms\Core\Dtos\CriteriaDto as BaseCriteriaDto; use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion; use EscolaLms\Core\Repositories\Criteria\Primitives\HasCriterion; +use EscolaLms\Core\Repositories\Criteria\Primitives\InCriterion; use EscolaLms\Core\Repositories\Criteria\Primitives\WhereCriterion; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; @@ -46,6 +47,9 @@ public static function instantiateFromRequest(Request $request): self $query->whereDate('proposed_at', '<=', Carbon::make($request->get('proposed_at_to'))); })); } + if ($request->get('consultation_term_ids')) { + $criteria->push(new InCriterion('consultation_user_id', $request->get('consultation_term_ids'))); + } return new static($criteria); } diff --git a/src/Http/Controllers/Swagger/ConsultationAccessEnquiryApiSwagger.php b/src/Http/Controllers/Swagger/ConsultationAccessEnquiryApiSwagger.php index 99c2d73..2655552 100644 --- a/src/Http/Controllers/Swagger/ConsultationAccessEnquiryApiSwagger.php +++ b/src/Http/Controllers/Swagger/ConsultationAccessEnquiryApiSwagger.php @@ -82,6 +82,19 @@ interface ConsultationAccessEnquiryApiSwagger * enum={"true", "false"} * ), * ), + * @OA\Parameter( + * name="consultation_term_ids[]", + * required=false, + * in="query", + * @OA\Schema( + * type="array", + * @OA\Items( + * @OA\Schema( + * type="integer" + * ), + * ), + * ), + * ), * @OA\Response( * response=200, * description="Successfull operation", diff --git a/src/Http/Requests/ListConsultationAccessEnquiryRequest.php b/src/Http/Requests/ListConsultationAccessEnquiryRequest.php index 0d603a7..7451919 100644 --- a/src/Http/Requests/ListConsultationAccessEnquiryRequest.php +++ b/src/Http/Requests/ListConsultationAccessEnquiryRequest.php @@ -22,6 +22,8 @@ public function rules(): array return [ 'order' => ['sometimes', 'string', 'in:ASC,DESC'], 'order_by' => ['sometimes', 'string', 'in:id,consultation_id,status,description,user_id,meeting_link,created_at,term_date'], + 'consultation_term_ids' => ['sometimes', 'array'], + 'consultation_term_ids.*' => ['integer'], ]; } diff --git a/src/Http/Resources/ConsultationAccessEnquiryResource.php b/src/Http/Resources/ConsultationAccessEnquiryResource.php index b9c8bb1..1aba346 100644 --- a/src/Http/Resources/ConsultationAccessEnquiryResource.php +++ b/src/Http/Resources/ConsultationAccessEnquiryResource.php @@ -25,6 +25,11 @@ * ref="#/components/schemas/ConsultationShortResource" * ), * @OA\Property( + * property="consultation_term_id", + * description="consultation_term_id", + * type="integer" + * ), + * @OA\Property( * property="user", * ref="#/components/schemas/ConsultationUserShortResource" * ), @@ -82,6 +87,7 @@ public function toArray($request): array 'created_at' => $this->created_at, 'status' => $this->status, 'consultation' => ConsultationShortResource::make($this->consultation), + 'consultation_term_id' => $this->consultation_user_id, 'user' => UserShortResource::make($this->user), 'proposed_terms' => ConsultationAccessEnquiryProposedTermsResource::collection($this->consultationAccessEnquiryProposedTerms), 'description' => $this->description, diff --git a/tests/Api/ConsultationAccessEnquiryListApiTest.php b/tests/Api/ConsultationAccessEnquiryListApiTest.php index 8ac865e..3b22129 100644 --- a/tests/Api/ConsultationAccessEnquiryListApiTest.php +++ b/tests/Api/ConsultationAccessEnquiryListApiTest.php @@ -50,6 +50,7 @@ public function testConsultationAccessEnquiryList(): void 'id', 'name', ], + 'consultation_term_id', 'user' => [ 'id', 'name', @@ -76,7 +77,8 @@ public function testConsultationAccessEnquiryListFiltering(): void ->has(ConsultationAccessEnquiryProposedTerm::factory()->state(['proposed_at' => Carbon::now()])) ->create(); - ConsultationAccessEnquiry::factory() + /** @var ConsultationAccessEnquiry $approved */ + $approved = ConsultationAccessEnquiry::factory() ->state(['user_id' => $student->getKey()]) ->approved() ->has(ConsultationAccessEnquiryProposedTerm::factory()->state(['proposed_at' => Carbon::now()->addDays(3)])) @@ -93,6 +95,10 @@ public function testConsultationAccessEnquiryListFiltering(): void $this->actingAs($student, 'api')->getJson('api/consultation-access-enquiries?is_coming=1') ->assertOk() ->assertJsonCount(1, 'data'); + + $this->actingAs($student, 'api')->getJson('api/consultation-access-enquiries?consultation_term_ids[]=' . $approved->consultation_user_id) + ->assertOk() + ->assertJsonCount(1, 'data'); } public function testConsultationAccessEnquiryListPagination(): void diff --git a/tests/Api/ConsultationAccessEnquiryReadApiTest.php b/tests/Api/ConsultationAccessEnquiryReadApiTest.php index 8414cb0..9bd3565 100644 --- a/tests/Api/ConsultationAccessEnquiryReadApiTest.php +++ b/tests/Api/ConsultationAccessEnquiryReadApiTest.php @@ -53,6 +53,7 @@ public function testConsultationAccessEnquiryRead(): void 'id', 'name', ], + 'consultation_term_id', 'user' => [ 'id', 'name',