Skip to content

Commit

Permalink
improve assignments controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Mar 5, 2024
1 parent fe23968 commit 663468f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
18 changes: 13 additions & 5 deletions app/controllers/assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ class AssignmentsController < ApplicationController
before_action :set_assignment, 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 assignments_filter[:project_id].present?
project = Project.find(assignments_filter[:project_id])
statement_of_works_ids = project.statement_of_works.map(&:id)
elsif assignments_filter[:statement_of_work_id].present?
statement_of_works_ids = [assignments_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]
assignments_filter[:start_date], assignments_filter[:end_date]
)

@assignments = Assignment.where(requirement: requirements)
Expand Down Expand Up @@ -47,7 +55,7 @@ def assignment_params
params.require(:assignment).permit(:coverage, :requirement_id, :start_date, :end_date, :user_id)
end

def requirements_filter
params.permit(:project_id, :start_date, :end_date)
def assignments_filter
params.permit(:project_id, :statement_of_work_id, :start_date, :end_date)
end
end
31 changes: 25 additions & 6 deletions spec/controllers/assignments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@
end

describe 'GET #index' do
it 'returns a success response' do
requirement = create(:requirement)
project = requirement.statement_of_work.project
let(:project) { create(:project) }
let(:statement_of_work) { create(:statement_of_work, :with_maintenance, project:) }
let(:secondary_statement_of_work) { create(:statement_of_work, project:) }
let(:requirement) { create(:requirement, statement_of_work:) }
let(:secondary_requirement) { create(:requirement, statement_of_work: secondary_statement_of_work) }

before do
create_list(:assignment, 2, coverage: 0.5, requirement:)
get :index,
params: { start_date: Time.zone.today - 6.days, end_date: Time.zone.today + 6.days, project_id: project.id }
create_list(:assignment, 5, coverage: 0.5, requirement: secondary_requirement)
end

context 'when given the project id' do
it 'returns all the assignments 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(7)
end
end

expect(response.parsed_body.size).to eq(2)
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 663468f

Please sign in to comment.