diff options
Diffstat (limited to 'app/services/incident_management/timeline_events/create_service.rb')
-rw-r--r-- | app/services/incident_management/timeline_events/create_service.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/app/services/incident_management/timeline_events/create_service.rb b/app/services/incident_management/timeline_events/create_service.rb new file mode 100644 index 00000000000..7d287e1bd82 --- /dev/null +++ b/app/services/incident_management/timeline_events/create_service.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +module IncidentManagement + module TimelineEvents + DEFAULT_ACTION = 'comment' + + class CreateService < TimelineEvents::BaseService + def initialize(incident, user, params) + @project = incident.project + @incident = incident + @user = user + @params = params + end + + def execute + return error_no_permissions unless allowed? + + timeline_event_params = { + project: project, + incident: incident, + author: user, + note: params[:note], + action: params.fetch(:action, DEFAULT_ACTION), + note_html: params[:note_html].presence || params[:note], + occurred_at: params[:occurred_at], + promoted_from_note: params[:promoted_from_note] + } + + timeline_event = IncidentManagement::TimelineEvent.new(timeline_event_params) + + if timeline_event.save + add_system_note(timeline_event) + + success(timeline_event) + else + error_in_save(timeline_event) + end + end + + private + + attr_reader :project, :user, :incident, :params + + def add_system_note(timeline_event) + return unless Feature.enabled?(:incident_timeline, project) + + SystemNoteService.add_timeline_event(timeline_event) + end + end + end +end |