Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to return requirements from a specific sow #177

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading