From fe23968f2491afacfdabbdae3df17a49af912946 Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Tue, 5 Mar 2024 08:48:51 -0300 Subject: [PATCH] allow to return requirements from a specific sow --- app/controllers/requirements_controller.rb | 14 ++++++++--- .../requirements_controller_spec.rb | 24 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/controllers/requirements_controller.rb b/app/controllers/requirements_controller.rb index 6217150..7d9ce7a 100644 --- a/app/controllers/requirements_controller.rb +++ b/app/controllers/requirements_controller.rb @@ -4,8 +4,16 @@ class RequirementsController < ApplicationController before_action :set_requirement, only: %i[show update destroy] def index - project = Project.find(requirements_filter[:project_id]) - statement_of_works_ids = project.statement_of_works.map(&:id) + if requirements_filter[:project_id].present? + project = Project.find(requirements_filter[:project_id]) + statement_of_works_ids = project.statement_of_works.map(&:id) + elsif requirements_filter[:statement_of_work_id].present? + statement_of_works_ids = [requirements_filter[:statement_of_work_id]] + else + render json: { error: 'project_id or statement_of_work_id is required' }, status: :bad_request + return + end + @requirements = Requirement.where(statement_of_work_id: statement_of_works_ids).active_in_period( requirements_filter[:start_date], requirements_filter[:end_date] ) @@ -46,6 +54,6 @@ def requirement_params end def requirements_filter - params.permit(:project_id, :start_date, :end_date) + params.permit(:project_id, :statement_of_work_id, :start_date, :end_date) end end diff --git a/spec/controllers/requirements_controller_spec.rb b/spec/controllers/requirements_controller_spec.rb index e26dd54..82371a2 100644 --- a/spec/controllers/requirements_controller_spec.rb +++ b/spec/controllers/requirements_controller_spec.rb @@ -17,13 +17,29 @@ end describe 'GET #index' do - context 'when there are requirements in the period' do - it 'returns the requirements' do - create_list(:requirement, 2, statement_of_work:) + let(:secondary_statement_of_work) { create(:statement_of_work, project:) } + + before do + create_list(:requirement, 2, statement_of_work:) + create_list(:requirement, 5, statement_of_work: secondary_statement_of_work) + end + + context 'when given the project id' do + it 'returns all the requirements of that project' do get :index, params: { start_date: Time.zone.today - 6.days, end_date: Time.zone.today + 6.days, project_id: project.id } - expect(response.parsed_body.size).to eq(2) + expect(response.parsed_body.size).to eq(7) + end + end + + context 'when given the statement of work id' do + it 'returns all the requirements of that statement of work' do + get :index, + params: { start_date: Time.zone.today - 6.days, end_date: Time.zone.today + 6.days, + statement_of_work_id: secondary_statement_of_work.id } + + expect(response.parsed_body.size).to eq(5) end end end