Skip to content

Commit

Permalink
feat: add mutation for creating demand effort
Browse files Browse the repository at this point in the history
  • Loading branch information
lhguerra committed Dec 18, 2024
1 parent 5e4d25c commit c968b3c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/demand_efforts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'csv'

class DemandEffortsController < AuthenticatedController
before_action :assign_demand
before_action :assign_demand, except: [:new]

def index
demand_efforts = DemandEffort.where(demand_id: @demand.id).order(start_time_to_computation: :desc)
Expand Down
28 changes: 28 additions & 0 deletions app/graphql/mutations/create_demand_effort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Mutations
class CreateDemandEffort < BaseMutation
argument :demand_external_id, ID, required: true
argument :demand_transition_id, ID, required: true
argument :end_date, GraphQL::Types::ISO8601DateTime, required: true
argument :item_assignment_id, ID, required: true
argument :start_date, GraphQL::Types::ISO8601DateTime, required: true

field :demand_effort, Types::DemandEffortType, null: true
field :status_message, Types::CreateResponses, null: false

def resolve(demand_external_id:, start_date:, end_date:, demand_transition_id:, item_assignment_id:)
demand = Demand.find_by(external_id: demand_external_id)
item_assignment = ItemAssignment.find_by(id: item_assignment_id)
demand_transition = DemandTransition.find_by(id: demand_transition_id)

return { status_message: 'NOT_FOUND' } if demand.blank? || item_assignment.blank? || demand_transition.blank?

demand_effort = DemandEffort.create(demand: demand, demand_transition: demand_transition, item_assignment: item_assignment, start_time_to_computation: start_date, finish_time_to_computation: end_date, automatic_update: false)

return { status_message: 'FAIL' } unless demand_effort.valid?

{ status_message: 'SUCCESS', demand_effort: demand_effort }
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/create_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module Types
class CreateResponses < Types::BaseEnum
value 'SUCCESS', 'answered when the creation was successful.'
value 'FAIL', 'answered when the creation was not successful.'
value 'NOT_FOUND', 'answered when relationship arguments are invalid.'
end
end
1 change: 1 addition & 0 deletions app/graphql/types/demand_effort_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class DemandEffortType < Types::BaseObject
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :demand_external_id, String, null: false
field :demand_id, Integer, null: false
field :demand_transition, Types::DemandTransitionType
field :effort_money, String, null: true
field :effort_value, Float, null: false
field :finish_time_to_computation, GraphQL::Types::ISO8601DateTime, null: false
Expand Down
18 changes: 18 additions & 0 deletions app/graphql/types/demand_transition_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Types
class DemandTransitionType < Types::BaseObject
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :demand, Types::DemandType, null: false
field :discarded_at, GraphQL::Types::ISO8601DateTime
field :id, ID, null: false
field :last_time_in, GraphQL::Types::ISO8601DateTime, null: false
field :last_time_out, GraphQL::Types::ISO8601DateTime
field :lock_version, Integer
field :stage, Types::StageType, null: false
field :team_member, Types::Teams::TeamMemberType
field :transition_notified, Boolean, null: false
field :transition_time_in_sec, Integer
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Types
class MutationType < Types::BaseObject
field :create_demand_effort, mutation: Mutations::CreateDemandEffort
field :create_portfolio_unit, mutation: Mutations::CreatePortfolioUnitMutation
field :create_product_risk_review, mutation: Mutations::CreateProductRiskReview
field :create_project_additional_hours, mutation: Mutations::CreateProjectAdditionalHoursMutation
Expand Down
26 changes: 26 additions & 0 deletions spec/controllers/demand_efforts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
it { expect(response).to redirect_to new_user_session_path }
end

describe 'GET #new' do
before { get :new, params: { company_id: 'foo', demand_id: 'bar', id: 'xpto' } }

it { expect(response).to redirect_to new_user_session_path }
end

describe 'PATCH #update' do
before { patch :update, params: { company_id: 'foo', demand_id: 'bar', id: 'xpto' } }

Expand Down Expand Up @@ -202,4 +208,24 @@
end
end
end

context 'authenticated as gold' do
before { travel_to Time.zone.local(2024, 12, 18, 10, 0, 0) }

let(:plan) { Fabricate :plan, plan_type: :gold }
let(:user) { Fabricate :user, first_name: 'zzz', user_role: :manager }
let!(:user_plan) { Fabricate :user_plan, user: user, plan: plan, active: true, paid: true, finish_at: 1.week.from_now }

before { sign_in user }

describe 'GET #new' do
it 'renders the spa template' do
company = Fabricate :company, users: [user]
demand = Fabricate :demand, company: company

get :new, params: { company_id: company, demand_id: demand }
expect(response).to render_template 'spa-build/index'
end
end
end
end
75 changes: 75 additions & 0 deletions spec/graphql/types/mutation_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -954,4 +954,79 @@
end
end
end

describe '#create_demand_effort' do
context 'with invalid demand external id' do
it 'returns not found error' do
demand_transition = Fabricate :demand_transition
item_assignment = Fabricate :item_assignment

mutation = %(mutation {
createDemandEffort(demandExternalId: "foo", demandTransitionId: #{demand_transition.id}, startDate: "#{11.hours.ago.iso8601}", endDate: "#{2.hours.ago.iso8601}", itemAssignmentId: #{item_assignment.id}) {
statusMessage
}
})

result = FlowClimateSchema.execute(mutation).as_json

expect(result.dig('data', 'createDemandEffort', 'statusMessage')).to eq 'NOT_FOUND'
end
end

context 'with invalid item assignment id' do
it 'returns not found error' do
demand = Fabricate :demand
demand_transition = Fabricate :demand_transition

mutation = %(mutation {
createDemandEffort(demandExternalId: #{demand.external_id}, startDate: "#{11.hours.ago.iso8601}", endDate: "#{2.hours.ago.iso8601}", demandTransitionId: #{demand_transition.id}, itemAssignmentId: "foo") {
statusMessage
}
})

result = FlowClimateSchema.execute(mutation).as_json

expect(result.dig('data', 'createDemandEffort', 'statusMessage')).to eq 'NOT_FOUND'
end
end

context 'with invalid demand transition id' do
it 'returns not found error' do
demand = Fabricate :demand
item_assignment = Fabricate :item_assignment

mutation = %(mutation {
createDemandEffort(demandExternalId: #{demand.external_id}, startDate: "#{11.hours.ago.iso8601}", endDate: "#{2.hours.ago.iso8601}", demandTransitionId: "foo", itemAssignmentId: #{item_assignment.id}) {
statusMessage
}
})

result = FlowClimateSchema.execute(mutation).as_json

expect(result.dig('data', 'createDemandEffort', 'statusMessage')).to eq 'NOT_FOUND'
end
end

context 'with valid arguments' do
it 'creates the effort and assigns it to de demand' do
demand = Fabricate :demand
demand_transition = Fabricate :demand_transition
item_assignment = Fabricate :item_assignment

mutation = %(mutation {
createDemandEffort(demandExternalId: #{demand.external_id}, startDate: "#{11.hours.ago.iso8601}", endDate: "#{2.hours.ago.iso8601}", demandTransitionId: #{demand_transition.id}, itemAssignmentId: #{item_assignment.id}) {
statusMessage
demandEffort {
automaticUpdate
}
}
})

result = FlowClimateSchema.execute(mutation).as_json

expect(result.dig('data', 'createDemandEffort', 'statusMessage')).to eq 'SUCCESS'
expect(result.dig('data', 'createDemandEffort', 'demandEffort', 'automaticUpdate')).to be false
end
end
end
end

0 comments on commit c968b3c

Please sign in to comment.