-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Mutations | ||
class UpsertWorkWeeks < BaseMutation | ||
description "Create or update a work week record for a StaffPlan user." | ||
|
||
# arguments passed to the `resolve` method | ||
argument :assignment_id, ID, required: true, description: "The ID of the assignment this work week is being created for." | ||
argument :work_weeks, [Types::StaffPlan::WorkWeeksInputObject], required: true, description: "Attributes for creating or updating a work week record for a StaffPlan user." | ||
|
||
# return type from the mutation | ||
type Types::StaffPlan::AssignmentType | ||
|
||
def resolve(assignment_id:, work_weeks:) | ||
current_company = context[:current_company] | ||
|
||
# try and find the assignment | ||
assignment = current_company.assignments.find(assignment_id) | ||
|
||
unless assignment.user.memberships.active.exists?(company: current_company) | ||
# if the assignment isn't for an active user, raise an error | ||
raise GraphQL::ExecutionError, "User is not an active member of the company" | ||
end | ||
|
||
work_weeks.each do |ww| | ||
work_week = assignment.work_weeks.find_or_initialize_by(cweek: ww.cweek, year: ww.year) | ||
work_week.update!(ww.to_h.slice(:estimated_hours, :actual_hours)) | ||
end | ||
|
||
assignment | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
class Types::StaffPlan::WorkWeeksInputObject < Types::BaseInputObject | ||
description "Attributes for creating or updating a work week record for a StaffPlan user." | ||
argument :cweek, Int, required: true, description: "The calendar week number of the work week." | ||
argument :year, Int, required: true, description: "The calendar year of the work week." | ||
argument :estimated_hours, Int, required: false, description: "The estimated hours for this work week." | ||
argument :actual_hours, Int, required: false, description: "The actual hours for this work week." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Mutations::UpsertWorkWeeks do | ||
|
||
context "when updating work weeks" do | ||
it "updates the work weeks with valid params" do | ||
query_string = <<-GRAPHQL | ||
mutation($assignmentId: ID!, $workWeeks: [WorkWeeksInputObject!]!) { | ||
upsertWorkWeeks(assignmentId: $assignmentId, workWeeks: $workWeeks) { | ||
workWeeks { | ||
cweek | ||
year | ||
actualHours | ||
estimatedHours | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
user = create(:user) | ||
assignment = assignment_for_user(user:) | ||
work_weeks = 5.times.map do |i| | ||
date = Date.today - i.weeks | ||
create(:work_week, :blank, assignment:, cweek: date.cweek, year: date.year) | ||
end | ||
|
||
updated_work_weeks = work_weeks.map.with_index do |week, i| | ||
{ | ||
cweek: week.cweek, | ||
year: week.year, | ||
actualHours: i * 5, | ||
estimatedHours: i * 6 | ||
} | ||
end | ||
|
||
result = StaffplanReduxSchema.execute( | ||
query_string, | ||
context: { | ||
current_user: user, | ||
current_company: assignment.company | ||
}, | ||
variables: { | ||
assignmentId: assignment.id, | ||
workWeeks: updated_work_weeks | ||
} | ||
) | ||
|
||
post_result = result["data"]["upsertWorkWeeks"]["workWeeks"] | ||
|
||
post_result.each do |result| | ||
work_week = updated_work_weeks.detect do |uww| | ||
uww[:cweek] == result["cweek"] && uww[:year] == result["year"] | ||
end | ||
|
||
expect(work_week).to be_present | ||
expect(work_week[:actualHours]).to eq(result["actualHours"]) | ||
expect(work_week[:estimatedHours]).to eq(result["estimatedHours"]) | ||
end | ||
end | ||
|
||
it "fails if the assignment is not found" do | ||
query_string = <<-GRAPHQL | ||
mutation($assignmentId: ID!, $workWeeks: [WorkWeeksInputObject!]!) { | ||
upsertWorkWeeks(assignmentId: $assignmentId, workWeeks: $workWeeks) { | ||
workWeeks { | ||
cweek | ||
year | ||
actualHours | ||
estimatedHours | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
user = create(:user) | ||
work_weeks = Array(create(:work_week, :blank)).map do |ww| | ||
{ | ||
cweek: ww.cweek, | ||
year: ww.year, | ||
actualHours: 5, | ||
estimatedHours: 6 | ||
} | ||
end | ||
|
||
result = StaffplanReduxSchema.execute( | ||
query_string, | ||
context: { | ||
current_user: user, | ||
current_company: user.current_company | ||
}, | ||
variables: { | ||
assignmentId: -1, | ||
workWeeks: work_weeks | ||
} | ||
) | ||
|
||
post_result = result["errors"] | ||
expect(post_result.length).to eq(1) | ||
expect(post_result.first["message"]).to eq("Assignment not found") | ||
end | ||
|
||
it "fails if the current_user is not a member of the company that the assignment belongs to" do | ||
query_string = <<-GRAPHQL | ||
mutation($assignmentId: ID!, $workWeeks: [WorkWeeksInputObject!]!) { | ||
upsertWorkWeeks(assignmentId: $assignmentId, workWeeks: $workWeeks) { | ||
workWeeks { | ||
actualHours | ||
estimatedHours | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
user = create(:user) | ||
assignment = assignment_for_user(user:) | ||
random_user = create(:user) | ||
|
||
result = StaffplanReduxSchema.execute( | ||
query_string, | ||
context: { | ||
current_user: random_user, | ||
current_company: random_user.current_company | ||
}, | ||
variables: { | ||
assignmentId: assignment.id, | ||
workWeeks: [{ | ||
cweek: 15, | ||
year: 2024, | ||
actualHours: 5, | ||
estimatedHours: 5 | ||
}] | ||
} | ||
) | ||
|
||
post_result = result["errors"] | ||
expect(post_result.length).to eq(1) | ||
expect(post_result.first["message"]).to eq("Assignment not found") | ||
end | ||
|
||
it "fails if the user is not an active member of the company" do | ||
query_string = <<-GRAPHQL | ||
mutation($assignmentId: ID!, $workWeeks: [WorkWeeksInputObject!]!) { | ||
upsertWorkWeeks(assignmentId: $assignmentId, workWeeks: $workWeeks) { | ||
workWeeks { | ||
actualHours | ||
estimatedHours | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
work_week = create(:work_week, :blank) | ||
work_week.user.memberships.update_all(status: "inactive") | ||
|
||
result = StaffplanReduxSchema.execute( | ||
query_string, | ||
context: { | ||
current_user: work_week.user, | ||
current_company: work_week.company | ||
}, | ||
variables: { | ||
assignmentId: work_week.assignment_id, | ||
workWeeks: [{ | ||
cweek: 14, | ||
year: 2023, | ||
actualHours: 5, | ||
estimatedHours: 12 | ||
}] | ||
} | ||
) | ||
|
||
post_result = result["errors"] | ||
expect(post_result.length).to eq(1) | ||
expect(post_result.first["message"]).to eq("User is not an active member of the company") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters