From 5616e13886e5915653c73216a1465bc87f0c96af Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Sun, 1 Dec 2024 14:54:15 +0100 Subject: [PATCH] REK-69 Add filter by updated_at to QuestionAnswer (#58) --- src/Dtos/QuestionAnswerFilterCriteriaDto.php | 23 ++++++++- .../QuestionAnswerAdminApiContract.php | 20 ++++++++ .../Requests/QuestionAnswerListingRequest.php | 8 ++++ tests/Api/QuestionAnswerListTest.php | 48 +++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/Dtos/QuestionAnswerFilterCriteriaDto.php b/src/Dtos/QuestionAnswerFilterCriteriaDto.php index 6a9fbb7..f42677b 100644 --- a/src/Dtos/QuestionAnswerFilterCriteriaDto.php +++ b/src/Dtos/QuestionAnswerFilterCriteriaDto.php @@ -4,11 +4,12 @@ use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest; use EscolaLms\Core\Dtos\CriteriaDto; +use EscolaLms\Core\Repositories\Criteria\Primitives\DateCriterion; use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion; -use EscolaLms\Core\Repositories\Criteria\Primitives\LikeCriterion; use EscolaLms\Questionnaire\Enums\QuestionnairePermissionsEnum; use EscolaLms\Questionnaire\Repository\Criteria\AuthoredModelsQuestionnaireCriterion; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; @@ -30,6 +31,26 @@ public static function instantiateFromRequest(Request $request): self $criteria->push(new AuthoredModelsQuestionnaireCriterion('questionnaireModel')); } + if ($request->has('updated_at_from')) { + $criteria->push( + new DateCriterion( + 'question_answers.updated_at', + Carbon::make($request->get('updated_at_from')), + '>=' + ) + ); + } + + if ($request->has('updated_at_to')) { + $criteria->push( + new DateCriterion( + 'question_answers.updated_at', + Carbon::make($request->get('updated_at_to')), + '<=' + ) + ); + } + return new self($criteria); } } diff --git a/src/Http/Controllers/Contracts/QuestionAnswerAdminApiContract.php b/src/Http/Controllers/Contracts/QuestionAnswerAdminApiContract.php index b929d27..3ef2a49 100644 --- a/src/Http/Controllers/Contracts/QuestionAnswerAdminApiContract.php +++ b/src/Http/Controllers/Contracts/QuestionAnswerAdminApiContract.php @@ -70,6 +70,26 @@ interface QuestionAnswerAdminApiContract * in="query", * required=false * ), + * @OA\Parameter( + * name="updated_at_from", + * description="From date filter", + * required=false, + * in="query", + * @OA\Schema( + * type="string", + * format="date-time" + * ), + * ), + * @OA\Parameter( + * name="updated_at_to", + * description="To date filter", + * required=false, + * in="query", + * @OA\Schema( + * type="string", + * format="date-time" + * ), + * ), * @OA\Response( * response=200, * description="Lists questions answers for questionaire", diff --git a/src/Http/Requests/QuestionAnswerListingRequest.php b/src/Http/Requests/QuestionAnswerListingRequest.php index 76e4516..12fcade 100644 --- a/src/Http/Requests/QuestionAnswerListingRequest.php +++ b/src/Http/Requests/QuestionAnswerListingRequest.php @@ -54,6 +54,14 @@ public function rules(): array 'nullable', Rule::exists('users', 'id'), ], + 'updated_at_from' => [ + 'sometimes', + 'date', + ], + 'updated_at_to' => [ + 'sometimes', + 'date', + ], ]; } } diff --git a/tests/Api/QuestionAnswerListTest.php b/tests/Api/QuestionAnswerListTest.php index 158ad7f..f5a5dc2 100644 --- a/tests/Api/QuestionAnswerListTest.php +++ b/tests/Api/QuestionAnswerListTest.php @@ -225,6 +225,54 @@ public function testAnswersListWithFiltersAndSorts(): void $this->assertTrue($response->json('data.0.id') === $questionAnswer1->getKey()); } + public function testAnswersListWithDateFilters(): void + { + $questionnaireModel = QuestionnaireModel::factory()->createOne(); + $questionnaire = Questionnaire::factory()->create(); + $question1 = Question::factory()->create([ + 'questionnaire_id' => $questionnaire->getKey(), + 'title' => 'aaaa', + ]); + + QuestionAnswer::factory() + ->count(5) + ->sequence( + ['updated_at' => now()->subDays(2)], + ['updated_at' => now()->subDays(15)], + ['updated_at' => now()->subDays(30)], + ['updated_at' => now()->subDays(40)], + ['updated_at' => now()->subDays(50)], + ) + ->state([ + 'question_id' => $question1->getKey(), + 'questionnaire_model_id' => $questionnaireModel->getKey(), + ]) + ->create(); + + $this->actingAs($this->user, 'api')->json( + 'get', + '/api/admin/question-answers/' . $questionnaire->getKey(), + [ + 'questionnaire_model_id' => $questionnaireModel->getKey(), + 'updated_at_from' => now()->subDays(35), + ] + ) + ->assertOk() + ->assertJsonCount(3, 'data'); + + $this->actingAs($this->user, 'api')->json( + 'get', + '/api/admin/question-answers/' . $questionnaire->getKey(), + [ + 'questionnaire_model_id' => $questionnaireModel->getKey(), + 'updated_at_from' => now()->subDays(35), + 'updated_at_to' => now()->subDays(10), + ] + ) + ->assertOk() + ->assertJsonCount(2, 'data'); + } + public function testListAnswersOnlyAuthoredModels(): void { $tutor = $this->makeInstructor();