2022-11-25 23:54:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Packages
|
|
|
|
class BulkDestroy < ::Mutations::BaseMutation
|
|
|
|
graphql_name 'DestroyPackages'
|
|
|
|
|
|
|
|
MAX_PACKAGES = 20
|
|
|
|
TOO_MANY_IDS_ERROR = "Cannot delete more than #{MAX_PACKAGES} packages"
|
|
|
|
|
|
|
|
argument :ids,
|
|
|
|
[::Types::GlobalIDType[::Packages::Package]],
|
|
|
|
required: true,
|
|
|
|
description: "Global IDs of the Packages. Max #{MAX_PACKAGES}"
|
|
|
|
|
|
|
|
def resolve(ids:)
|
|
|
|
raise_resource_not_available_error!(TOO_MANY_IDS_ERROR) if ids.size > MAX_PACKAGES
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
model_ids = ids.map(&:model_id)
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
service = ::Packages::MarkPackagesForDestructionService.new(
|
2023-03-17 16:20:25 +05:30
|
|
|
packages: packages_from(model_ids),
|
2022-11-25 23:54:43 +05:30
|
|
|
current_user: current_user
|
|
|
|
)
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
raise_resource_not_available_error! if result.reason == :unauthorized
|
|
|
|
|
|
|
|
errors = result.error? ? Array.wrap(result[:message]) : []
|
|
|
|
|
|
|
|
{ errors: errors }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def packages_from(ids)
|
|
|
|
::Packages::Package.displayable
|
|
|
|
.id_in(ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|