-
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.
Merge branch 'main' into dependabot/bundler/rails-html-sanitizer-1.6.1
- Loading branch information
Showing
9 changed files
with
206 additions
and
4 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,33 @@ | ||
module Mutations | ||
class DeleteProject < BaseMutation | ||
description "Delete a project." | ||
|
||
# arguments passed to the `resolve` method | ||
argument :project_id, ID, required: true, | ||
description: "The ID of the project to delete. The project must meet the delete-ability requirements: no assignments, or all assignments having no actual hours recorded." | ||
|
||
# return type from the mutation | ||
type Types::StaffPlan::ProjectType | ||
|
||
def resolve(project_id:) | ||
project = context[:current_company].projects.find(project_id) | ||
|
||
project.destroy | ||
|
||
if project.errors.any? | ||
project.errors.each do |error| | ||
context.add_error( | ||
GraphQL::ExecutionError.new( | ||
error.full_message, | ||
extensions: { | ||
attribute: error.attribute.to_s, | ||
} | ||
) | ||
) | ||
end | ||
end | ||
|
||
project | ||
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
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
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,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Mutations::DeleteProject do | ||
|
||
context "resolve" do | ||
it 'resolves to an error when a project cannot be destroyed' do | ||
query_string = <<-GRAPHQL | ||
mutation($projectId: ID!) { | ||
deleteProject(projectId: $projectId) { | ||
id | ||
status | ||
} | ||
} | ||
GRAPHQL | ||
|
||
project = create(:project) | ||
company = project.client.company | ||
user = create(:membership, company:).user | ||
assignment = create(:assignment, project: project, user: user) | ||
create(:work_week, assignment: assignment, actual_hours: 5) | ||
|
||
result = StaffplanReduxSchema.execute( | ||
query_string, | ||
context: { | ||
current_user: user, | ||
current_company: company | ||
}, | ||
variables: { | ||
projectId: project.id | ||
} | ||
) | ||
|
||
expect(result["errors"].first["message"]).to eq("Cannot delete a project that has assignments with hours recorded. Try archiving the project instead.") | ||
expect(project.reload.persisted?).to eq(true) | ||
post_result = result["data"]["deleteProject"] | ||
expect(post_result["id"]).to eq(project.id.to_s) | ||
expect(post_result["status"]).to eq(Project::CONFIRMED) | ||
end | ||
|
||
it "destroys a project that can be destroyed" do | ||
query_string = <<-GRAPHQL | ||
mutation($projectId: ID!) { | ||
deleteProject(projectId: $projectId) { | ||
id | ||
} | ||
} | ||
GRAPHQL | ||
|
||
project = create(:project) | ||
company = project.client.company | ||
user = create(:membership, company:).user | ||
assignment = create(:assignment, project: project, user: user) | ||
create(:work_week, assignment: assignment, actual_hours: 0) | ||
|
||
result = StaffplanReduxSchema.execute( | ||
query_string, | ||
context: { | ||
current_user: user, | ||
current_company: company | ||
}, | ||
variables: { | ||
projectId: project.id | ||
} | ||
) | ||
|
||
post_result = result["data"]["deleteProject"] | ||
expect(result["errors"]).to be_nil | ||
expect(post_result["id"]).to eq(project.id.to_s) | ||
expect { project.reload }.to raise_error(ActiveRecord::RecordNotFound) | ||
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