Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/services/organizations/delete_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def execute
end

transactional do |t|
namespace = organization.ensure_namespace

organization.delete

if organization.persisted?
Expand All @@ -25,12 +27,20 @@ def execute
details: organization.errors)
end

namespace.delete
Comment thread
raphael-goetz marked this conversation as resolved.

if namespace.persisted?
t.rollback_and_return! ServiceResponse.error(message: 'Failed to delete namespace',
error_code: :invalid_organization,
details: namespace.errors)
end

AuditService.audit(
:organization_deleted,
author_id: current_authentication.user.id,
entity: organization,
details: {},
target: organization.ensure_namespace
target: AuditEvent::GLOBAL_TARGET
)

ServiceResponse.success(message: 'Organization deleted', payload: organization)
Expand Down
12 changes: 12 additions & 0 deletions app/services/users/delete_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def execute
end

transactional do |t|
namespace = user.namespace

user.destroy

if user.persisted?
Expand All @@ -27,6 +29,16 @@ def execute
)
end

namespace&.delete
Comment thread
raphael-goetz marked this conversation as resolved.

if namespace.present? && namespace.persisted?
t.rollback_and_return! ServiceResponse.error(
message: 'Failed to delete user namespace',
error_code: :invalid_user,
details: namespace.errors
)
end

AuditService.audit(
:user_deleted,
author_id: current_authentication.user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
entity_id: organization.id,
entity_type: 'Organization',
details: {},
target_id: organization.namespace.id,
target_type: 'Namespace'
target_id: 0,
target_type: 'global'
)
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/services/organizations/delete_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@
it { expect(service_response.payload).to eq(organization) }
it { expect { service_response }.to change { Organization.count }.by(-1) }

it do
expect { service_response }.to change { Namespace.exists?(organization.namespace.id) }.from(true).to(false)
end

it do
expect { service_response }.to create_audit_event(
:organization_deleted,
author_id: current_user.id,
entity_type: 'Organization',
details: {},
target_id: organization.namespace.id,
target_type: 'Namespace'
target_id: 0,
target_type: 'global'
)
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/services/users/delete_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
let(:current_user) { create(:user, :admin) }

it 'deletes the user successfully' do
namespace_id = user.ensure_namespace.id

expect { service_response }.to change { User.exists?(user.id) }.from(true).to(false)
.and change {
Namespace.exists?(namespace_id)
}.from(true).to(false)
expect(service_response).to be_success
expect(service_response.payload).to eq(user)

Expand Down