diff options
Diffstat (limited to 'app/services/alert_management/http_integrations/create_service.rb')
-rw-r--r-- | app/services/alert_management/http_integrations/create_service.rb | 64 |
1 files changed, 15 insertions, 49 deletions
diff --git a/app/services/alert_management/http_integrations/create_service.rb b/app/services/alert_management/http_integrations/create_service.rb index 1abe0548c45..17e39577c29 100644 --- a/app/services/alert_management/http_integrations/create_service.rb +++ b/app/services/alert_management/http_integrations/create_service.rb @@ -2,68 +2,34 @@ module AlertManagement module HttpIntegrations - class CreateService - # @param project [Project] - # @param current_user [User] - # @param params [Hash] - def initialize(project, current_user, params) - @project = project - @current_user = current_user - @params = params.with_indifferent_access - end - + class CreateService < BaseService def execute return error_no_permissions unless allowed? - return error_multiple_integrations unless creation_allowed? - - integration = project.alert_management_http_integrations.create(permitted_params) - return error_in_create(integration) unless integration.valid? - - success(integration) - end - private + ::AlertManagement::HttpIntegration.transaction do + integration = project.alert_management_http_integrations.build(permitted_params) - attr_reader :project, :current_user, :params + if integration.save + @response = success(integration) - def allowed? - current_user&.can?(:admin_operations, project) - end + if too_many_integrations?(integration) + @response = error_multiple_integrations - def creation_allowed? - project.alert_management_http_integrations.empty? - end - - def permitted_params - params.slice(*permitted_params_keys) - end + raise ActiveRecord::Rollback + end + else + @response = error_on_save(integration) + end + end - # overriden in EE - def permitted_params_keys - %i[name active] + @response end - def error(message) - ServiceResponse.error(message: message) - end - - def success(integration) - ServiceResponse.success(payload: { integration: integration }) - end + private def error_no_permissions error(_('You have insufficient permissions to create an HTTP integration for this project')) end - - def error_multiple_integrations - error(_('Multiple HTTP integrations are not supported for this project')) - end - - def error_in_create(integration) - error(integration.errors.full_messages.to_sentence) - end end end end - -::AlertManagement::HttpIntegrations::CreateService.prepend_mod_with('AlertManagement::HttpIntegrations::CreateService') |