Skip to content

Commit

Permalink
allow to return requirements from a specific sow
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Mar 5, 2024
1 parent eba09fc commit fe23968
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 11 additions & 3 deletions app/controllers/requirements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
Expand Down Expand Up @@ -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
24 changes: 20 additions & 4 deletions spec/controllers/requirements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe23968

Please sign in to comment.