Skip to content

Commit

Permalink
Add the delete mutation for projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Dec 4, 2023
1 parent 4451c1f commit 07db18a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
25 changes: 23 additions & 2 deletions lib/decidim/api/budget_mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class BudgetMutationType < GraphQL::Schema::Object
argument :attributes, ProjectAttributes, required: true
end

field :delete_project, Decidim::Budgets::ProjectType, null: false do
description "A mutation to delete a project within a budget."

argument :id, GraphQL::Types::ID, required: true
end

def create_project(attributes:)
enforce_permission_to :create, :project

Expand All @@ -47,10 +53,10 @@ def create_project(attributes:)
end

def update_project(id:, attributes:)
enforce_permission_to :update, :project, project: object
project = object.projects.find(id)
enforce_permission_to :update, :project, project: project

form = project_form_from(attributes)
project = object.projects.find(id)
Decidim::Budgets::Admin::UpdateProject.call(form, project) do
on(:ok) do
return project
Expand All @@ -67,6 +73,21 @@ def update_project(id:, attributes:)
)
end

def delete_project(id:)
project = object.projects.find(id)
enforce_permission_to :destroy, :project, project: project

Decidim::Budgets::Admin::DestroyProject.call(project, current_user) do
on(:ok) do
return project
end
end

GraphQL::ExecutionError.new(
I18n.t("decidim.budgets.admin.projects.destroy.invalid")
)
end

private

def project_form_from(attributes, project = nil)
Expand Down
37 changes: 35 additions & 2 deletions spec/types/decidim/budgeting_pipeline/budget_mutation_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@
context "with no user" do
let!(:current_user) { nil }

it "does not allow creating the project" do
it "does not allow updating the project" do
expect { response }.to raise_error(Decidim::BudgetingPipeline::ActionForbidden)
end
end

context "with a participant user" do
it "does not allow creating the project" do
it "does not allow updating the project" do
expect { response }.to raise_error(Decidim::BudgetingPipeline::ActionForbidden)
end
end
Expand Down Expand Up @@ -205,6 +205,39 @@
end
end

describe "deleteProject" do
let!(:project) { create(:budgeting_pipeline_project, budget: model) }
let(:query) { "{ deleteProject(id: #{project.id}) { id } }" }

context "with no user" do
let!(:current_user) { nil }

it "does not allow deleting the project" do
expect do
expect { response }.to raise_error(Decidim::BudgetingPipeline::ActionForbidden)
end.not_to change(Decidim::Budgets::Project, :count)
end
end

context "with a participant user" do
it "does not allow deleting the project" do
expect do
expect { response }.to raise_error(Decidim::BudgetingPipeline::ActionForbidden)
end.not_to change(Decidim::Budgets::Project, :count)
end
end

context "with an admin user" do
let!(:current_user) { create(:user, :confirmed, :admin, organization: current_organization) }

it "deletes the project" do
expect { response }.to change(Decidim::Budgets::Project, :count).by(-1)

expect(response["deleteProject"]).to eq("id" => project.id.to_s)
end
end
end

def attributes_to_graphql(attributes)
payload = attributes.map do |key, value|
case value
Expand Down

0 comments on commit 07db18a

Please sign in to comment.