diff options
Diffstat (limited to 'app/services/alert_management/http_integrations/update_service.rb')
-rw-r--r-- | app/services/alert_management/http_integrations/update_service.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/app/services/alert_management/http_integrations/update_service.rb b/app/services/alert_management/http_integrations/update_service.rb new file mode 100644 index 00000000000..220c4e759f0 --- /dev/null +++ b/app/services/alert_management/http_integrations/update_service.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module AlertManagement + module HttpIntegrations + class UpdateService + # @param integration [AlertManagement::HttpIntegration] + # @param current_user [User] + # @param params [Hash] + def initialize(integration, current_user, params) + @integration = integration + @current_user = current_user + @params = params + end + + def execute + return error_no_permissions unless allowed? + + params[:token] = nil if params.delete(:regenerate_token) + + if integration.update(params) + success + else + error(integration.errors.full_messages.to_sentence) + end + end + + private + + attr_reader :integration, :current_user, :params + + def allowed? + current_user&.can?(:admin_operations, integration) + end + + def error(message) + ServiceResponse.error(message: message) + end + + def success + ServiceResponse.success(payload: { integration: integration.reset }) + end + + def error_no_permissions + error(_('You have insufficient permissions to update this HTTP integration')) + end + end + end +end |