Skip to content

Commit

Permalink
REK-69 Add filter by updated_at to QuestionAnswer (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Dec 1, 2024
1 parent 2cdd149 commit 5616e13
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Dtos/QuestionAnswerFilterCriteriaDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/Http/Controllers/Contracts/QuestionAnswerAdminApiContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/Http/Requests/QuestionAnswerListingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public function rules(): array
'nullable',
Rule::exists('users', 'id'),
],
'updated_at_from' => [
'sometimes',
'date',
],
'updated_at_to' => [
'sometimes',
'date',
],
];
}
}
48 changes: 48 additions & 0 deletions tests/Api/QuestionAnswerListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 5616e13

Please sign in to comment.