summaryrefslogtreecommitdiff
path: root/app/services/alert_management/sync_alert_service_data_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/alert_management/sync_alert_service_data_service.rb')
-rw-r--r--app/services/alert_management/sync_alert_service_data_service.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/app/services/alert_management/sync_alert_service_data_service.rb b/app/services/alert_management/sync_alert_service_data_service.rb
new file mode 100644
index 00000000000..1ba197065c5
--- /dev/null
+++ b/app/services/alert_management/sync_alert_service_data_service.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+module AlertManagement
+ class SyncAlertServiceDataService
+ # @param alert_service [AlertsService]
+ def initialize(alert_service)
+ @alert_service = alert_service
+ end
+
+ def execute
+ http_integration = find_http_integration
+
+ result = if http_integration
+ update_integration_data(http_integration)
+ else
+ create_integration
+ end
+
+ result ? ServiceResponse.success : ServiceResponse.error(message: 'Update failed')
+ end
+
+ private
+
+ attr_reader :alert_service
+
+ def find_http_integration
+ AlertManagement::HttpIntegrationsFinder.new(
+ alert_service.project,
+ endpoint_identifier: ::AlertManagement::HttpIntegration::LEGACY_IDENTIFIER
+ )
+ .execute
+ .first
+ end
+
+ def create_integration
+ new_integration = AlertManagement::HttpIntegration.create(
+ project_id: alert_service.project_id,
+ name: 'HTTP endpoint',
+ endpoint_identifier: AlertManagement::HttpIntegration::LEGACY_IDENTIFIER,
+ active: alert_service.active,
+ encrypted_token: alert_service.data.encrypted_token,
+ encrypted_token_iv: alert_service.data.encrypted_token_iv
+ )
+
+ new_integration.persisted?
+ end
+
+ def update_integration_data(http_integration)
+ http_integration.update(
+ active: alert_service.active,
+ encrypted_token: alert_service.data.encrypted_token,
+ encrypted_token_iv: alert_service.data.encrypted_token_iv
+ )
+ end
+ end
+end