docbrown/app/workers/organizations/delete_worker.rb
Anna Buianova ba1c13bbb6
Asynchronous organization delete (#13388)
* Asynchronous organization destroy

* Notification after an organization was deleted

* Send data to Datadog and Honeybadger when org deletion failed

* Delete unused code

* Fixed Organization#destroyable? and organization delete specs

* Reorganized organization deletion specs

* Removed redundant specs

* Improved org deleted email text template

* Don't peform the org delete when a user or an org were not found

* Renamed user_id in the org delete worker

* Audit logging when deleten an organization

* Removed specs for tracking pixel and UTM params

* Changed slug for audit log on org delete

* Fixed schema.rb
2021-04-19 18:30:35 +03:00

44 lines
1.3 KiB
Ruby

module Organizations
class DeleteWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority, retry: 10
# operator_id - the user who deletes the organization
def perform(organization_id, operator_id)
org = Organization.find_by(id: organization_id)
return unless org
user = User.find_by(id: operator_id)
return unless user
Organizations::Delete.call(org)
user.touch(:organization_info_updated_at)
EdgeCache::BustUser.call(user)
# notify user that the org was deleted
NotifyMailer.with(name: user.name, org_name: org.name, email: user.email).organization_deleted_email.deliver_now
audit_log(org, user)
rescue StandardError => e
ForemStatsClient.count("organizations.delete", 1,
tags: ["action:failed", "organization_id:#{org.id}", "user_id:#{user.id}"])
Honeybadger.context({ organization_id: org.id, user_id: user.id })
Honeybadger.notify(e)
end
def audit_log(org, user)
AuditLog.create(
user: user,
category: "user.organization.delete",
roles: user.roles_name,
slug: "organization_delete",
data: {
organization_id: org.id,
organization_slug: org.slug
},
)
end
end
end